Question

I inherited an MFC app, and it has a window that has several owner-draw widgets that respond to OnPaint and do various drawing.

I noticed that in order to force the controls to redraw in response to various user actions, there was the following code:

CRect rect;
m_myControl.GetWindowRect(&rect);
ScreenToClient(&rect);
InvalidateRect(&rect, FALSE);

I thought this could be simplified like so:

m_myControl.Invalidate(FALSE);

But, in practice, when I do it this way, the control paints sometimes but not others. Specifically, when I'm interacting with controls in the window, sometimes myControl ends up just painting as solid gray. I changed the code back to the more-complicated InvalidateRect style and it's working great again.

Why would there be a difference here?

Was it helpful?

Solution

When you invalidate a window, you don't invalidate the window underneath it. If the parent window is responsible for drawing the control it won't get triggered because you didn't tell it that it needed updating. The original code does the right thing in that case.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top