1

I am wanting to handle WM_NCPAINT messages to draw my own window frame. I wrote some simple code to draw just a rectangle, which should give a black border around it. Here was the code:

LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam){
    switch(msg){
        case WM_NCPAINT:
        {
            RECT rc;
            GetWindowRect(hWnd, &rc);
            HDC hDC = GetWindowDC(hWnd);
            Rectangle(hDC, 0, 0, rc.right - rc.left, rc.bottom - rc.top);
            ReleaseDC(hWnd, hDC);
            return TRUE;
        }
        case WM_DESTROY:
        {
            PostQuitMessage(0);
            break;
        }
    }
    return DefWindowProc(hWnd, msg, wParam, lparam);
}

However, there were a couple issues with this. When resizing the window on the left or top edge, it causes the window to flicker heavily on the bottom and right edges. The second problem is that on the upper corners of the window, there are rounded corners, where my painting does not seem to take place. (This is Windows 10). To my understanding, there should not be any flicker, as I am painting the window immediately after receiving a WM_NCPAINT message, which does not seem to be the case. Can someone tell me what I am doing wrong and how to avoid these problems?

By the way, this is what the rounded corners look like, on the top-left and top-right corners. Window rounded corners

Garhoogin
  • 91
  • 2
  • 9

1 Answers1

2

After some more searching, I found the solution. I was a bit skeptical at first. I added the following code to my WM_NCCREATE:

HMODULE uxtheme = LoadLibrary("uxtheme.dll");
HRESULT __stdcall (* SetWindowTheme) (HWND, LPWSTR, LPWSTR) = GetProcAddress("SetWindowTheme");
SetWindowTheme(uxtheme, L" ", L" ");
FreeLibrary(uxtheme);

This fixes both the flickering and the annoying rounded corners.

Garhoogin
  • 91
  • 2
  • 9
  • Hi, I have a problem with the rounded corners. I try this solution but the second and the third line can't compile. I don't know what the second line does, but the third line clearly needs HWND instead of HMODULE. Can you help me? – TonyG Jan 25 '22 at 09:39
  • This solution saves my life. @TonyG I came across this question. You need to define `SetWindowTheme` function pointer type first. Something like `using SetWindowTheme = HRESULT __stdcall(*)(HWND, LPWSTR, LPWSTR)` – sz ppeter May 07 '22 at 03:29