如何在win32 winapi中处理无效绘制的ComboBox控件?

时间:2018-04-02 00:25:11

标签: c++ windows winapi combobox

我在使用WinAPI绘制无效的ComboBox时出现问题。当您最小化应用程序并在未隐藏选择ComboBox控件后恢复它时,它看起来像这样:

enter image description here

正如您所看到的,OK按钮具有焦点,但ComboBox的选择仍未隐藏。当控件失去输入焦点时,ComboBox的正常行为会隐藏选择。

代码:

#define WIN32_MEAN_AND_LEAN

#include <SDKDDKVer.h>
#include <Windows.h>
#include <Windowsx.h>
#include <CommCtrl.h>
#include <assert.h>

struct window_context {
    HINSTANCE _instance;
    HWND _window;

    HWND _combo_box2;
    HWND _ok_button;

    window_context(HINSTANCE instance) noexcept : _instance{ instance }
    {
    }
};

static BOOL on_create(HWND hwnd, LPCREATESTRUCT lpCreateStruct) noexcept
{
    auto context = reinterpret_cast<window_context*>(lpCreateStruct->lpCreateParams);

    context->_combo_box2 = CreateWindowW(WC_COMBOBOXW,
                                        L"",
                                        CBS_DROPDOWN | CBS_AUTOHSCROLL | WS_TABSTOP | WS_VSCROLL | WS_VISIBLE | WS_CHILD,
                                        0, 0, 0, 0,
                                        hwnd,
                                        (HMENU)44,
                                        nullptr,
                                        nullptr);
    ComboBox_AddString(context->_combo_box2, L"select me");

    context->_ok_button = CreateWindowW(WC_BUTTONW,
                                        L"OK",
                                        BS_DEFPUSHBUTTON | BS_NOTIFY | WS_TABSTOP | WS_VISIBLE | WS_CHILD,
                                        0, 0, 0, 0,
                                        hwnd,
                                        nullptr,
                                        nullptr,
                                        nullptr);

    return true;
}

static void on_destroy(HWND hwnd) noexcept
{
    PostQuitMessage(0);
}

static void on_size(HWND hwnd, UINT state, int cx, int cy) noexcept
{
    auto context = reinterpret_cast<window_context*>(GetWindowLongPtrW(hwnd, GWLP_USERDATA));

    MoveWindow(context->_combo_box2,
            10,
            10,
            100,
            100,
            true); // causes the weird behaviour to occur
                   // using SetWindowPos doesn't help
                   // using SetWindowPos with SWP_NOSIZE prevents the bug from occuring
                   // but also locks it into size which is not acceptable
                   // since I use this to deal with DPI scaling changes

    MoveWindow(context->_ok_button,
            10,
            110,
            100,
            50,
            true);
}

static void on_activate(HWND hwnd, UINT state, HWND hwndActDeact, BOOL fMinimized) noexcept
{
    auto context = reinterpret_cast<window_context*>(GetWindowLongPtrW(hwnd, GWLP_USERDATA));

    if (state)
        SetFocus(context->_ok_button);
}

static BOOL on_nc_create(HWND hwnd, LPCREATESTRUCT lpCreateStruct) noexcept
{
    auto context = reinterpret_cast<window_context*>(lpCreateStruct->lpCreateParams);

    context->_window = hwnd;
    SetWindowLongPtrW(hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(context));

    return FORWARD_WM_NCCREATE(hwnd, lpCreateStruct, DefWindowProcW);
}

static LRESULT CALLBACK wnd_proc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) noexcept
{
    switch (uMsg) {
        HANDLE_MSG(hwnd, WM_CREATE, on_create);
        HANDLE_MSG(hwnd, WM_DESTROY, on_destroy);
        HANDLE_MSG(hwnd, WM_SIZE, on_size);
        HANDLE_MSG(hwnd, WM_ACTIVATE, on_activate);
        HANDLE_MSG(hwnd, WM_NCCREATE, on_nc_create);
    }

    return DefWindowProcW(hwnd, uMsg, wParam, lParam);
}

static bool register_class(HINSTANCE hInstance) noexcept
{
    WNDCLASSEXW wcex;
    wcex.cbSize = sizeof(wcex);
    wcex.style = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc = &wnd_proc;
    wcex.cbClsExtra = 0;
    wcex.cbWndExtra = 0;
    wcex.hInstance = hInstance;
    wcex.hIcon = nullptr;
    wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wcex.lpszMenuName = nullptr;
    wcex.lpszClassName = L"test";
    wcex.hIconSm = nullptr;

    return RegisterClassExW(&wcex) != 0;
}

static bool create_window(window_context& context) noexcept
{
    return CreateWindowExW(0,
                        L"test",
                        L"Win32 Program",
                        WS_OVERLAPPEDWINDOW,
                        CW_USEDEFAULT,
                        CW_USEDEFAULT,
                        800,
                        600,
                        nullptr,
                        nullptr,
                        context._instance,
                        &context);
}

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                    _In_opt_ HINSTANCE hPrevInstance,
                    _In_ LPWSTR    lpCmdLine,
                    _In_ int       nCmdShow)
{
    if (!register_class(hInstance))
        return false;

    window_context context{ hInstance };

    if (!create_window(context))
        return false;

    ShowWindow(context._window, nCmdShow);

    MSG msg;
    BOOL got_message;

    while ((got_message = GetMessageW(&msg, nullptr, 0, 0)) && got_message != -1) {
        if (IsDialogMessageW(context._window, &msg))
            continue;

        TranslateMessage(&msg);
        DispatchMessageW(&msg);
    }

    return static_cast<int>(msg.wParam);
}

重现问题的步骤:

  • 从ComboBox中选择select me
  • 最小化窗口
  • 恢复窗口

ComboBox现在应该显示为选中状态,即使它不是。

解决问题的步骤:

  • 使ComboBox的区域无效无效,重新绘制组合框仍然无法解决问题
  • 当您只是在指定位置创建ComboBox并且从不移动其位置或更改其大小时,不会出现此问题
  • 使用SetWindowPos
  • 移动 ComboBox的位置时,不会出现此问题
  • 只要更改ComboBox的大小,就会出现这种奇怪的行为。
  • 对话以某种方式正确处理
  • WinForms也正确地处理了这个问题,但我没有找到他们做的事情来阻止它
  • 如果您从下拉列表中选择了一个项目,当您输入自定义文本时,该错误就会消失

这肯定是一个极具异国情调的问题,我非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

您可以通过调用ComboBox_SetEditSel(context->_combo_box2, -1, -1);来解决此问题(请参阅CB_SETEDITSEL)或在调整大小之前在组合框的编辑控件中找到所选字符,然后在调整大小后恢复这些值。

static void on_size(HWND hwnd, UINT, int, int) noexcept
{
    auto context=reinterpret_cast<window_context*>(GetWindowLongPtr(hwnd,GWLP_USERDATA));

    DWORD range = ComboBox_GetEditSel(context->_combo_box2);
    DWORD start = LOWORD(range);
    DWORD end = HIWORD(range);

    MoveWindow(context->_combo_box2, 10, 10, 150, 100, true);
    MoveWindow(context->_ok_button, 10, 110, 100, 50, true);

    ComboBox_SetEditSel(context->_combo_box2, start, end);
}

但是,您可能会发现您将遇到与其他控件类似的问题,很难找到所有内容的修复程序。请考虑使用DialogBox代替。