1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
#include <Windows.h>
#include <iostream>
#include <CommCtrl.h>
HWND hComboBox;
void populate_cb() {
SendMessage(hComboBox, CB_ADDSTRING, 0, (LPARAM)"Item 1");
SendMessage(hComboBox, CB_ADDSTRING, 0, (LPARAM)"Item 2");
SendMessage(hComboBox, CB_ADDSTRING, 0, (LPARAM)"Item 3");
SendMessage(hComboBox, CB_ADDSTRING, 0, (LPARAM)"Item 4");
SendMessage(hComboBox, CB_ADDSTRING, 0, (LPARAM)"Item 5");
SendMessage(hComboBox, CB_SETCURSEL, (WPARAM)2, 0);
}
LRESULT CALLBACK CBProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam,
UINT_PTR uIdSubclass, DWORD_PTR dwRefData) {
switch (msg) {
default:
std::cout << "MESSAGE: " << msg << '\n';
}
return DefSubclassProc(hwnd, msg, wParam, lParam);
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_CREATE:
hComboBox = CreateWindow("COMBOBOX", nullptr,
WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_OVERLAPPED | CBS_DROPDOWNLIST,
25, 25, 150, 280, hwnd, (HMENU)1337, GetModuleHandle(nullptr), nullptr);
populate_cb();
SetWindowSubclass(hComboBox, CBProc, 0, 0);
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
RemoveWindowSubclass(hComboBox, CBProc, 0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
WNDCLASSEX wcex;
wcex.cbClsExtra = 0;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.cbWndExtra = 0;
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW);
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hIcon = LoadIcon(nullptr, IDI_APPLICATION);
wcex.hIconSm = LoadIcon(nullptr, IDI_APPLICATION);
wcex.hInstance = hInstance;
wcex.lpfnWndProc = WndProc;
wcex.lpszClassName = "WindowClass";
wcex.lpszMenuName = nullptr;
wcex.style = 0;
if (!RegisterClassEx(&wcex)) {
MessageBox(nullptr, "Window class registration failed!", "Error", MB_OK | MB_ICONERROR);
return 1;
}
HWND hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, "WindowClass", "Window Title",
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, nullptr,
nullptr, hInstance, nullptr);
if (!hwnd) {
MessageBox(nullptr, "Window creation failed!", "Error", MB_OK | MB_ICONERROR);
return 1;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
MSG msg;
while (GetMessage(&msg, hwnd, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
int main() {
return WinMain(GetModuleHandle(nullptr), nullptr, nullptr, SW_SHOW);
}
|