First a caveat, I only very recently started learning about the WinAPI. I'm sure this question has been asked many times before, but for some reason I can't find it anywhere online. The question is simply this; why bother with the initial call to ShowWindow()
in the body of WinMain()
before the execution of the message loop? Why not simply set the window to be initially visible through use of the WS_VISIBLE
flag?
I also have some questions as to the mechanics of the ShowWindow()
function. Which messages does it actually send? In MSDN it states that:
If a window has the
WS_VISIBLE
style when it is created, the window receives this message[WM_SHOWWINDOW]
after it is created, but before it is displayed. A window also receives this message when its visibility state is changed by theShowWindow
orShowOwnedPopups
function.
Does this mean the primary means of communication between the ShowWindow()
function and Windows is through the WM_SHOWWINDOW
message? It also states that:
The
WM_SHOWWINDOW
message is not sent under the following circumstances:
When a top-level, overlapped window is created with the
WS_MAXIMIZE
orWS_MINIMIZE
style.When the
SW_SHOWNORMAL
flag is specified in the call to theShowWindow
function.
The MSDN also states that:
The first time an application calls
ShowWindow
, it should use theWinMain
function'snCmdShow
parameter as itsnCmdShow
parameter.
Petzold states that the argument passed to this nCmdShow
parameter will be either SW_SHOWNORMAL
, SW_SHOWMAXIMIZED
or SW_SHOWMINNOACTIVE
. Am I to take from this that the only time the ShowWindow()
function does not send a WM_SHOWWINDOW
message, is when we make that very first initial call to it in Winmain()
? If so, how does it then get the window to display? Also, how does all of this relate to the actual painting of the window?
I'm sorry if my question is bit of a jumbled mess, but the mechanics of showing a window kind of confuse me, and for some reason it's hard to find clear answers to these questions online (as opposed to just bits and pieces of information). Any help in clarifying all of this will be greatly appreciated!
The idea behind the
nCmdShow
parameter to WinMain is that it gives Windows a chance to let your application know how Windows would like it to show the window. That mechanism is probably no longer useful, but maybe there are edge cases. In any case, you should pass it on to whatever you consider your main window after you have created it. Creating it hidden allows you to create any child windows without flicker, so that's what most people do.I think the logic behind when
WM_SHOWWINDOW
is and isn't sent is to let you use it to catch calls toShowWindow (hWnd, SW_HIDE)
andShowWindow (hWnd, SW_SHOW)
in your window proc, since it's likely that you might want to take some action at that time (such as stop playing audio, for example). And maybe alsoSW_MINIMIZE
,SW_MAXIMIZE
andSW_RESTORE
, I guess it all depends.Does that help at all?
Edit
Well, quite a lot of information has been posted to this thread so I thought I would try to summarise it as best I understand it. Here goes.
The nCmdShow parameter to WinMain appears to be historical. Instead, the first call to ShowWindow acts as if you have passed it this value, whether you like it or not, so that call had better be to your main window. Still, that said, you might as well play the game and pass it on, you never know.
Read and understand Hans Passant's comment to this post. That will tell you where in the Windows UI this value most commonly comes from.
Just FYI, it's OK to create your child windows with WS_VISIBLE set. You won't see them until you show the main window.
OK, I'm done. Sometimes less is more.