C++ – Does overriding OnNcPaint() affect the painting of the client area of a window

cmfconncpaintwindows

I want to change the appearance of a window's caption bar, so I decided to override the OnNcPaint() method of CMainFrame. But when I did this, I found a problem. If there is another window covering my window, and I drag the window quickly, the content of the client area of my window disappeared, which came to sight only when I stopped the dragging.

My overridden OnNcPaint() is like below:

void CMainFrame::OnNcPaint()
{
    CDC* pWinDC = GetWindowDC();
    //do some drawing
    ReleaseDC(pWinDC);
}

Is there something wrong with my approach?
Thank you!

Best Answer

Unless you use a clipping region set up to exclude the client area, you can paint over it from OnNcPaint(). So... if your drawing logic can't be modified to exclude the client in some other way, set up an appropriate clipping region first:

CRect rect;
GetWindowRect(&rect);
ScreenToClient(&rect);
CRect rectClient;
GetClientRect(&rectClient);
rectClient.OffsetRect(-rect.left, -rect.top);
rect.OffsetRect(-rect.left, -rect.top);
pWinDC->ExcludeClipRect(&rectClient);
// ...
// draw stuff here
// ...
pWinDC->SelectClipRgn(NULL);