Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why we need to call UpdateWindow following ShowWindow?

Tags:

windows

winapi

ShowWindow(g_hWnd, 1);
UpdateWindow(g_hWnd);

I am wondering why we need to call UpdateWindow following the ShowWindow?

like image 600
Adam Lee Avatar asked Jun 20 '12 11:06

Adam Lee


3 Answers

It is entirely unnecessary, your window will paint just fine without it.

You'll see a minor benefit from it if your program goes off doing lots of stuff after creating the window but before entering the message loop. The user has something to look at. A splash screen is the more typical approach.

like image 181
Hans Passant Avatar answered Dec 20 '22 22:12

Hans Passant


ShowWindow does not repaint the window. The call to UpdateWindow sends WM_PAINT message to the window and thus repainting it.

like image 22
Jeeva Avatar answered Dec 20 '22 21:12

Jeeva


Normally, the system sends WM_PAINT only if the message queue is empty. Under normal circumstances this is good enough and it actually optimizes out a lot of unnecessary repaint. The messages in the queue often will change application state which can often result in invalidating part of the window, and hence result in yet another painting (so the user sees the new application state). So the repaint just happens after all such messages are handled and the system thinks the new window content will be valid for some time (until yet another message(s) come into the queue).

However if you need to force the WM_PAINT immediately and bypass the logic above, you may force sending WM_PAINT (if there is an invalid region) by calling UpdateWindow().

like image 44
mity Avatar answered Dec 20 '22 22:12

mity