Why make the initial call to ShowWindow()?

2.4k views Asked by At

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 the ShowWindow or ShowOwnedPopups 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 or WS_MINIMIZE style.

  • When the SW_SHOWNORMAL flag is specified in the call to the ShowWindow function.

The MSDN also states that:

The first time an application calls ShowWindow, it should use the WinMain function's nCmdShow parameter as its nCmdShow 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!

2

There are 2 answers

5
Paul Sanders On

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 to ShowWindow (hWnd, SW_HIDE) and ShowWindow (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 also SW_MINIMIZE, SW_MAXIMIZE and SW_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.

  1. 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.

  2. 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.

  3. 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.

4
Remy Lebeau On

why bother with the initial call to ShowWindow() in the body of WinMain() before the execution of the message loop?

The answer is in the ShowWindow() documentation:

nCmdShow

Controls how the window is to be shown. This parameter is ignored the first time an application calls ShowWindow, if the program that launched the application provides a STARTUPINFO structure. Otherwise, the first time ShowWindow is called, the value should be the value obtained by the WinMain function in its nCmdShow parameter.

If the app is started by the user, there is no STARTUPINFO, and nCmdShow from WinMain() should be used to determine how your main UI should be displayed (or not).

If the app is started by the system, or by another app, there is likely to be a STARTUPINFO, so you should ignore nCmdShow from WinMain() and use the nCmdShow from the STARTUPINFO instead.

Calling ShowWindow() handles both conditions for you. But if you force the window visible with VS_VISIBLE, you are not respecting how the caller wishes your app to appear (or not) at startup.