Smooth WM_NCPAINT in Win32

1.1k Views Asked by At

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

1

There are 1 best solutions below

2
On BEST ANSWER

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.