更新控件后,Win32窗口不会重绘

时间:2020-07-24 00:31:05

标签: c++ winapi

我是Win32编程的新手,所以我事先表示歉意。我有一个窗口,其中有2个似乎相关的问题。

  1. 如果我调整窗口的大小,则会使背景的一部分变黑,或者如果我将其最小化则全部变黑。
  2. 我有一个SysLink,当单击它时会调用ShowWindow隐藏自身并显示隐藏的组合框控件。将显示组合框,但链接不会隐藏。它无法访问,但仍然可见。

enter image description here

以下是完整的测试代码。

#include <Windows.h>
#include <commctrl.h>

#pragma comment(linker,"\"/manifestdependency:type='win32' \
name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HWND link;
HWND combo;

int WINAPI wWinMain(HINSTANCE hInstance,
                    HINSTANCE hPrevInstance, 
                    PWSTR pCmdLine,
                    int CmdShow)
{
    WNDCLASSW wc = {0};
    wc.lpszClassName = L"MainWindow";
    wc.hInstance     = hInstance;
    wc.lpfnWndProc   = WndProc;
    wc.hCursor       = LoadCursor(0, IDC_ARROW);

    RegisterClassW(&wc);
    CreateWindowW(wc.lpszClassName, L"Test", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                  0, 0, 340, 280,
                  nullptr, nullptr, hInstance, nullptr);
    
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return (int)msg.wParam;
} // end wWinMain

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg)
    {
        case WM_CREATE:
            link = CreateWindowExW(0, L"SysLink", L"<a>Add filter</a>",
                                   WS_CHILD | WS_VISIBLE,
                                   20, 20, 100, 28, hwnd, nullptr, nullptr, nullptr);
            combo = CreateWindowExW(0, L"COMBOBOX", nullptr,
                                    WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST | CBS_HASSTRINGS,
                                    50, 20, 140, 28, hwnd, nullptr, nullptr, nullptr);
            ShowWindow(combo, SW_HIDE);
            break;

        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;

        case WM_NOTIFY:
            switch (((LPNMHDR)lParam)->code)
            {
                case NM_CLICK:
                case NM_RETURN:
                    if (((LPNMHDR)lParam)->hwndFrom == link)
                    {
                        ShowWindow(link, SW_HIDE);
                        ShowWindow(combo, SW_SHOW);
                    }
                    break;
            }
    }

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

1 个答案:

答案 0 :(得分:1)

在没有背景画笔的情况下注册了窗口类。要解决此问题并重新粉刷,请在RegisterClassW之前添加(例如)以下内容。

    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
相关问题