To answer Point 1: The Win32 API was designed for C (it is quite old) and to put the PAINTSTRUCT ps;
inside the case
statement would require putting the contents of the case
inside braces {}
as C doesn't allow for inline declarations. C++ may also have a problem declaring variables inside case
statements without the braces - calling destructors without calling the constructor. You will quickly find that the switch() case...
style will become awkward to maintain, the function will become quite large and unwieldy and it doesn't play well with things like Intellisense. Quite often, you'll see developers use a map to associate functions with specific messages:-
WindowProc (args)
{
func_ptr = some_map.GetValue (message_type)
if func_ptr not null
call func_ptr
else
DefWindowProc (args)
}
As for Point 2, the value of hdc
will be null
when there's an error and it's looks cleaner to use hdc
instead of ps.hdc
. Also, an hdc
is easier to pass to other functions than a ps
. But apart from that, the documentation doesn't call out any reason why ps.hdc
would be different to the result of BeginPaint
.