API | MFC | C++ | C |

Programming Windows API

Combobox Control Example

//combo box demo
#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HINSTANCE g_hinst;
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,PWSTR 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, 100, 100, 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("Paris"), TEXT("London"), TEXT("Berlin"), TEXT("Rome") };
switch(msg)
{
case WM_CREATE:
//create combo box
hwndCombo = CreateWindow(TEXT("combobox"), NULL, WS_CHILD | WS_VISIBLE | CBS_DROPDOWN,10, 10, 120, 110, hwnd, NULL, g_hinst, NULL);
//create static control
hwndStatic = CreateWindow(TEXT("static"), TEXT(""), WS_CHILD | WS_VISIBLE|WS_BORDER,150, 10, 90, 25, hwnd, NULL, g_hinst, NULL);
int i;
for (i = 0; i < 4; i++ )
{
//populate combo box
SendMessage(hwndCombo, CB_ADDSTRING, 0, (LPARAM) items[i]);
}
break;
case WM_COMMAND:
//respond to combo box selection
if ( HIWORD(wParam) == CBN_SELCHANGE) {
TCHAR strText[255] = TEXT("\0");
//get position of selected item
LRESULT sel = SendMessage(hwndCombo, CB_GETCURSEL, 0, 0);
//get selected item text
SendMessage(hwndCombo,CB_GETLBTEXT, sel,(LPARAM)strText);
//set value of static bex to value selected in combo box
SetWindowText(hwndStatic, strText);
SetFocus(hwnd);
} break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}

Creating a Simple Window | Common Elements | Data Types and Character Sets | The Device Context | Graphics Device Interface | Displaying Text | Displaying Graphics | Mapping Modes | Keyboard Input | Working with the Mouse | Menus | Child Windows | ScrollBar Control | The Dialog Box | Windows Message Box | Common Dialog Box | Bitmaps | Common Controls | Creating a Toolbar | Multiple Document Interface | Timers | DLL’s | Creating Custom Controls | Owner Drawn Controls | API Hooking and DLL Injection | File Management Functions | String Manipulation | System Information Functions |