Suppose your window procedure doesn’t paint when it gets a
WM_PAINT
message.
What happens?
It depends on how you don’t paint.
If you have an explicit handler for the
WM_PAINT
message
that does nothing but return without painting,
then the window manager will turn around and
put a new
WM_PAINT
message in your queue.
“And try harder this time.”
Remember that the rules for the
WM_PAINT
message are that the window manager will
generate a
WM_PAINT
message
for any window that has a dirty region.
If you fail to remove the dirty region in your
WM_PAINT
message handler,
well, then the rules state that you get another
WM_PAINT
message.
(The most common way of clearing the dirty region is to call
BeginPaint
,
but there are other less common ways,
like
ValidateRect
or
RedrawWindow
with the RDW_VALIDATE
flag.)
The other case is that you
simply don’t have a WM_PAINT
handler
and let the message fall through to
DefWindowProc
.
In that case,
DefWindowProc
will do a blank paint for you.
In other words,
DefWindowProc
contains
the logical equivalent of
case WM_PAINT: { PAINTSTRUCT ps; if (BeginPaint(hwnd, &ps)) EndPaint(hwnd, &ps); } return 0;
In the case where you pass the
WM_PAINT
to
DefWindowProc
,
the dirty region is cleared because
DefWindowProc
will call
BeginPaint
for you.
There are some quirks in the handling of the
WM_PAINT
message by the
DefWindowProc
function
to handle various application compatibility cases,
but the above is the basic idea.
To avoid tripping over the weird application compatibility
cases, decide up front how you want to deal with
WM_PAINT
messages delivered to your window
procedure.
- Handle them completely
by calling
BeginPaint
andEndPaint
, then returning 0. (Do not pass the message toDefWindowProc
.) - Pass them all to
DefWindowProc
, and let it do theBeginPaint
andEndPaint
.
Don’t try playing fancy games like
“Oh, I’m going to call
BeginPaint
and
EndPaint
,
but sometimes I’m also going to pass the message to
DefWindowProc
afterwards.”
Just pick one plan and stick to it.
It’s a lot simpler for everybody that way.
0 comments