繁体   English   中英

C ++ Win32 API组合框在打开时冻结

[英]C++ Win32 API Combo Box freezes when opened

我正在尝试使用下拉组合框( CBS_DROPDOWNCBS_DROPDOWNLIST )来工作。 问题是,每当我打开组合框时,整个窗口都会“冻结”(它什么也没有响应,甚至无法关闭,它会忽略点击)。 “解冻”的唯一方法是按“ esc”键。

当窗口“冻结”时,组合框不会收到任何消息,也不会移动鼠标,单击等。

这是我用来创建组合框的代码:

case WM_CREATE:
    hComboBox = CreateWindow("COMBOBOX", nullptr,
        WS_CHILD | WS_VISIBLE | WS_OVERLAPPED | CBS_DROPDOWNLIST,
        25, 25, 150, 280, hwnd, (HMENU)12345, GetModuleHandle(nullptr), nullptr);
    populate_cb();
    break;

“ populate_cb()”函数仅向组合框添加一些字符串并设置初始元素:

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);
}

但是,简单的( CBS_SIMPLE )组合框可以工作。

这是完整的代码:

#include <Windows.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 WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
    switch (msg) {
    case WM_CREATE:
        hComboBox = CreateWindow("COMBOBOX", nullptr,
            WS_OVERLAPPED | WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST,
            25, 25, 150, 280, hwnd, (HMENU)12345, GetModuleHandle(nullptr), nullptr);
        populate_cb();
        break;
    case WM_CLOSE:
        DestroyWindow(hwnd);
        break;
    case WM_DESTROY:
        PostQuitMessage(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;
}
while (GetMessage(&msg, hwnd, 0, 0) > 0) 

仅泵送主窗口消息。 您需要为线程中的所有窗口提供排队的消息。 将此行更改为

while (GetMessage(&msg, NULL, 0, 0)) 

GetMessage文档中所述,为第二个参数传递NULL将为线程拥有的所有窗口检索消息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM