Code:
#pragma comment( lib, "user32.lib" )
#include <windows.h>
#include <string.h>
#define ID_LIST 1
#define ID_TEXT 2
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
char szClassName[ ] = "ComboBox App";
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
HWND hwnd;
MSG messages;
WNDCLASSEX wincl;
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure;
wincl.style = CS_DBLCLKS;
wincl.cbSize = sizeof (WNDCLASSEX);
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL;
wincl.cbClsExtra = 0;
wincl.cbWndExtra = 0;
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
if (!RegisterClassEx (&wincl))
return 0;
hwnd = CreateWindowEx (
0,
szClassName,
"ComboBox App",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
544,
375,
HWND_DESKTOP,
NULL,
hThisInstance,
NULL
);
ShowWindow (hwnd, nCmdShow);
while (GetMessage (&messages, NULL, 0, 0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return messages.wParam;
}
void FillListBox(HWND hwndList, DWORD items)
{
TCHAR *pVarName1[] = {"Yes", "No"};
TCHAR *pVarName2[] = {"Yes_1","No_1" };
TCHAR *pVarName3[] = {"No_1_Second", "No_2_Second","No_3_second", "Go to Finished" };
TCHAR *pVarName4[] = {"Yes_1_Second", "Yes_2_Second","Yes_3_Second", "Go to Finished" };
TCHAR *pVarName5[] = {"Terminate App", "Go Back to Start"};
switch(items)
{
case 1:
SendMessage( hwndList, CB_RESETCONTENT, 0, 0 );
for(int i=0; i<2; i++)
SendMessage(hwndList, CB_ADDSTRING, 0, (LPARAM)pVarName1[i]);
SendMessage(hwndList, CB_SETCURSEL, 0, 0);
break;
case 2:
SendMessage( hwndList, CB_RESETCONTENT, 0, 0 );
for(int i=0; i<2; i++)
SendMessage(hwndList, CB_ADDSTRING, 0, (LPARAM)pVarName2[i]);
SendMessage(hwndList, CB_SETCURSEL, 0, 0);
break;
case 3:
SendMessage( hwndList, CB_RESETCONTENT, 0, 0 );
for(int i=0; i<4; i++)
SendMessage(hwndList, CB_ADDSTRING, 0, (LPARAM)pVarName3[i]);
SendMessage(hwndList, CB_SETCURSEL, 0, 0);
break;
case 4:
SendMessage( hwndList, CB_RESETCONTENT, 0, 0 );
for(int i=0; i<4; i++)
SendMessage(hwndList, CB_ADDSTRING, 0, (LPARAM)pVarName4[i]);
SendMessage(hwndList, CB_SETCURSEL, 0, 0);
break;
case 5:
SendMessage( hwndList, CB_RESETCONTENT, 0, 0 );
for(int i=0; i<2; i++)
SendMessage(hwndList, CB_ADDSTRING, 0, (LPARAM)pVarName5[i]);
SendMessage(hwndList, CB_SETCURSEL, 0, 0);
break;
default:
break;
}
}
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HWND hwndList, hwndText;
int iIndex, iLength, cxChar, cyChar;
TCHAR pVarName[30];
switch (message) /* handle the messages */
{
case WM_CREATE:
cxChar = LOWORD(GetDialogBaseUnits());
cyChar = HIWORD(GetDialogBaseUnits());
hwndText = CreateWindow(TEXT("static"),NULL, WS_CHILD | WS_VISIBLE |
SS_LEFT, cxChar, cyChar,
GetSystemMetrics(SM_CXSCREEN), cyChar,
hwnd, (HMENU)ID_TEXT,
(HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE),
NULL);
hwndList = CreateWindow(TEXT("Combobox"), NULL, WS_CHILD | WS_VISIBLE |
LBS_STANDARD, cxChar, cyChar*3,
cxChar*16 + GetSystemMetrics(SM_CXVSCROLL),
cyChar*5, hwnd, (HMENU)ID_LIST,
(HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE),
NULL);
FillListBox(hwndList, 1);
GetWindowText(hwndList, pVarName, 30);
SetWindowText(hwndText, pVarName);
return 0;
case WM_COMMAND:
if( LOWORD(wParam)==ID_LIST && HIWORD(wParam)==CBN_SELCHANGE)
{
GetWindowText(hwndList, pVarName, 30);
if( strcmp(pVarName,"Yes") == 0)
{
SetWindowText(hwndText, pVarName);
FillListBox(hwndList, 2);
}
else if( strcmp(pVarName,"No") == 0)
{
SetWindowText(hwndText, pVarName);
FillListBox(hwndList, 3);
}
else if( strcmp(pVarName,"Yes_1") == 0)
{
SetWindowText(hwndText, pVarName);
FillListBox(hwndList, 4);
}
else if( strcmp(pVarName,"Go to Finished") == 0)
{
SetWindowText(hwndText, pVarName);
FillListBox(hwndList, 5);
}
else if( strcmp(pVarName,"Go Back to Start") == 0)
{
SetWindowText(hwndText, pVarName);
FillListBox(hwndList, 1);
}
else if( strcmp(pVarName,"Terminate App") == 0)
{
SetWindowText(hwndText, "App to be terminated");
MessageBox(NULL, "We Will Terminate App", "TERMINATE", MB_OK);
PostQuitMessage (0);
}
else SetWindowText(hwndText, pVarName);
}
return 0;
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default:
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}