3

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!

Shog9
  • 156,901
  • 35
  • 231
  • 235
user26404
  • 1,371
  • 4
  • 27
  • 38

1 Answers1

5

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);
Shog9
  • 156,901
  • 35
  • 231
  • 235
  • The client rect does not include things like the scrollbars, so this will allow ncpaint to draw over them. Just FYI. – Dan Jun 19 '19 at 21:16
  • offsetting the rectangle rect after the rectClient is redundant. – TonyG Jul 12 '23 at 12:44