Code:
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <time.h>
//#define PORTALPATH "\\\\srv-mantis\\TechDocs\\Portal\\RDP\\password"
#define PORTALPATH "c:\\temp"
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HINSTANCE g_hinst;
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
HWND hwnd;
MSG msg ;
WNDCLASS wc = {0};
wc.lpszClassName = TEXT("Application");
wc.hInstance = hInstance ;
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
wc.lpfnWndProc = WndProc ;
wc.hCursor = LoadCursor(0,IDC_ARROW);
g_hinst = hInstance;
RegisterClass(&wc);
hwnd = CreateWindow(wc.lpszClassName, TEXT("Combo Box"),
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
300, 300, 270, 170, 0, 0, hInstance, 0);
while( GetMessage(&msg, NULL, 0, 0)) {
DispatchMessage(&msg);
}
return (int) msg.wParam;
}
LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
static HWND hwndCombo, hwndStatic;
const TCHAR *items[] = { TEXT("FreeBSD"), TEXT("OpenBSD"), TEXT("Ubuntu"), TEXT("Solaris"), TEXT("1"), TEXT("2"), TEXT("3") };
int i;
LRESULT sel = 0;
// get array of files
typedef char FILENAME[MAX_PATH + 1];
// get number of files and then use this size to create the array
FILENAME *arrFilename = (FILENAME *)malloc(10000 * sizeof *arrFilename);
int j,k;
char path[100] = PORTALPATH;
// directory var
DIR *dir;
struct dirent *ent;
// set directory of password files
dir = opendir(path);
// get filenames
if (dir != NULL){
j = 0;
while((ent = readdir (dir)) != NULL) {
//printf("j = %d fname: %s\n",j,ent->d_name) ;
//copy string to array
strcpy(arrFilename[j],ent->d_name);
j++;
}
closedir (dir);
} else {
//could not open directory
perror (path);
//return EXIT_FAILURE;
}
// get array of files END
switch(msg)
{
case WM_CREATE:
hwndCombo = CreateWindow(TEXT("combobox"), NULL,
WS_CHILD | WS_VISIBLE | CBS_DROPDOWN | WS_VSCROLL,
10, 10, 120, 510, hwnd, NULL, g_hinst, NULL);
CreateWindow(TEXT("button"), TEXT("Drop down"),
WS_CHILD | WS_VISIBLE,
150, 10, 90, 25, hwnd, (HMENU)1, g_hinst, NULL);
hwndStatic = CreateWindow(TEXT("static"), TEXT(""),
WS_CHILD | WS_VISIBLE,
150, 80, 90, 25, hwnd, NULL, g_hinst, NULL);
// enter filenames into combobox
for ( i = 0; i < 7; i++ ) {
SendMessage(hwndCombo, CB_ADDSTRING, 0, (LPARAM) items[i]);
}
break;
case WM_COMMAND:
if (HIWORD(wParam) == BN_CLICKED) {
SendMessage(hwndCombo, CB_SHOWDROPDOWN, (WPARAM) TRUE, 0);
}
if ( HIWORD(wParam) == CBN_SELCHANGE) {
sel = SendMessage(hwndCombo, CB_GETCURSEL, 0, 0);
SetWindowText(hwndStatic, items[sel]);
SetFocus(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}