Win32 window not redrawing when controls are updated

Viewed 151

I am new to win32 programming, so I apologize in advance. I have a window with 2 problems that seem related.

  1. if I resize a window, it leaves a portion of the background black, or all of it if I minimize.
  2. I have a SysLink that when clicked calls ShowWindow to hide itself and shows a hidden combobox control. The combobox displays, but the link doesn't hide. It is inaccessible, but still visible.

enter image description here

The following is the full test code.

#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 Answers

The window class is registered without a background brush. To fix that, and the repaint along with it, add (for example) the following before RegisterClassW.

    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
Related