-
December 19th, 2016, 04:24 PM
#1
Combo box freezes
Hello,
I'm trying to get a drop-down combo box to work. The problem is, when I open the combo box, the entire window freezes until I press the Escape key. When the window freezes, the combo box doesn't receive any messages at all.
This is the code I use:
Code:
#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);
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|