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!
WS_VISIBLE
style on the main window, then the system implicitly callsShowWindow
(the win32k function in the kernel, of course). It defaults toSW_SHOW
unlessx
isCW_USEDEFAULT
andy
is notCW_USEDEFAULT
, i.e.y
is somenCmdShow
value. – AlphabeticShowWindow
is called for the main window, either implicitly or explicitly, and only the first time, then, ifnCmdShow
isSW_SHOW
,SW_SHOWNORMAL
, orSW_SHOWDEFAULT
, then the system instead uses either theSTARTUPINFO
wShowWindow
value if theSTARTF_USESHOWWINDOW
flag is set or otherwiseSW_SHOWNORMAL
if the flag is not set. TheSTARTUPINFO
value is only used for the first call. Subsequent calls toShowWindow
that specifySW_SHOWDEFAULT
will actually useSW_SHOWNORMAL
. – Alphabetic[w]WinMain
-- of course its value ofnCmdShow
is just theSTARTUPINFO
wShowWindow
value if theSTARTF_USESHOWWINDOW
flag is set, and otherwiseSW_SHOWDEFAULT
. – AlphabeticSTARTUPINFO
. That makes no sense. WinAPISTARTUPINFO
is an amalgamation of NT'sRTL_USER_PROCESS_PARAMETERS
and creation attributes (i.e.STARTUPINFOEX
lpAttributeList
). A pointer to theProcessParameters
always exists in the Process Environment Block. This includes the WinAPISTARTUPINFO
record, includingWindowFlags
,ShowWindowFlags
,WindowTitle
,DesktopInfo
,ShellInfo
,StandardInput
, etc. It's what WinAPIGetStartupInfo
use to reconstruct theSTARTUPINFO
record. – Alphabetic