Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does an overridden non-client area show the default when the window loses focus on win32?

Tags:

c++

winapi

I have a overridden the non-client area of my window, in the window's callback function, as follows:

    case WM_NCPAINT:
    {
        HDC hdc;
        RECT rect;
        HPEN pen;

        hdc=GetDCEx(hWnd,(HRGN)wParam,DCX_WINDOW|DCX_CACHE|DCX_INTERSECTRGN|DCX_LOCKWINDOWUPDATE);
        GetWindowRect(hWnd,&rect);
        pen=CreatePen(PS_SOLID, 10, RGB(255, 0, 0));//red pen 10 pixels in size
        SelectObject(hdc,pen);
        Rectangle(hdc,0,0,(rect.right-rect.left),(rect.bottom-rect.top));
        DeleteObject(pen);
        ReleaseDC(hWnd,hdc);
        RedrawWindow(hWnd,&rect,(HRGN)wParam,RDW_UPDATENOW)
    }break;

That does the trick and in the case above draws a red rectangle around my window. However, if the window loses focus, then the default non-client area is painted and my custom non-client area drawing disappear.

I have tried catching the message WM_KILLFOCUS in my window's callback function and do the same with it as I do with WM_NCPAINT but it didn't do anything (although I saw I receive this message when I press on another window and my window losses focus).

What am I missing here...?

like image 687
Gidi Avatar asked Sep 03 '14 09:09

Gidi


Video Answer


1 Answers

Add a handler for WM_NCACTIVATE:

case WM_NCACTIVATE:
    // Paint the non-client area now, otherwise Windows will paint its own
    RedrawWindow(hWnd, NULL, NULL, RDW_UPDATENOW);
    break;
like image 151
arx Avatar answered Oct 16 '22 15:10

arx