What is nCmdShow?

12,454

Solution 1

It is basically a hint to the application how it should show its main window. Although it is legacy, it is not as legacy as the hPrevInstance parameter. But, I digress...

The value of the nCmdShow parameter will be one of the constants specified in ShowWindow's API reference. It can be set by another process or system launching your application via CreateProcess. The STARTUPINFO struct that can optionally be passed to CreateProcess contains a wShowWindow member variable that will get passed to WinMain through the nCmdShow parameter.

Another way the nCmdShow parameter is passed is via calls to ShellExecute.

Off the top of my head, I can't think of any scenario (in recent versions of Windows) in which the operating system will explicitly pass a value other than SW_SHOW when launching an application.

It's not uncommon nor bad for an application to ignore the nCmdShow flag passed to WinMain[?].

Solution 2

Note this section from the ShowWindow documentation:

nCmdShow: This parameter is ignored the first time an application calls ShowWindow, if the program that launched the application provides a STARTUPINFO structure.

Even though your program has no window when it starts, the specified value gets implicitly used the first time you eventually call ShowWindow. (It's not read directly from WinMain's local nCmdShow variable, though, so you can't change its value within WinMain and expect to get different results. In that sense, it's not particularly useful unless your program needs to do something special if it's started minimized or maximized.)

Solution 3

The "n" in nCmdShow means "Short int".

(This is what I wanted to know when I came to this stack overflow page)

Source: https://msdn.microsoft.com/en-us/library/windows/desktop/aa378932(v=vs.85).aspx

Solution 4

nCmdShow is integer type,this parameter specifies how the application windows should be display( to O.S.) If no value is specified by you than by default Windows O.S. say SW_NORMAL value of this param. You can specify values of this parameter , but those who passed to WinMain() only for Windows O.S

Share:
12,454

Related videos on Youtube

Dmytro
Author by

Dmytro

Updated on September 14, 2022

Comments

  • Dmytro
    Dmytro about 1 year

    I've always been curious on what nCmdShow means in WinMain of a C program using Windows API.

    I looked up the formal explanation: "Controls how the window is to be shown. This parameter can be one of the following values.".

    I do not understand what that means, as a Windows program can contain more than one window, or no windows at all. In addition, as program begins, there is no window to be shown to begin with, which makes me question this argument even more.

    Also from what I read, it always stays 10, which isn't even on the list of options in "http://msdn.microsoft.com/en-us/library/windows/desktop/ms633559%28v=vs.85%29.aspx"...

    Is it obsolete? Can somebody explain its purpose, or provide any references explaining its use? I tried googling but saw nothing.

    Thanks!

    REVISITED:

    When you right click a shortcut and go to properties, there is an option to start the window Minimized, Maximized, or Normal(ly).

    Windows provides an nCmdShow to your program in case it wants to act in a special way if it was launched in any of these three ways. For example, it may hide itself inside notification bar if it was requested to be started minimized.


    For exhaustiveness:

    enter image description here

    https://msdn.microsoft.com/en-us/library/windows/desktop/ms633548(v=vs.85).aspx describes all the different ways that may be passed.

  • Dmytro
    Dmytro over 10 years
    Thanks for the explanation :).
  • jamesdlin
    jamesdlin over 10 years
    You'll get values other than SW_SHOW if you ask the program to start maximized or minimized (via a shortcut or via start).
  • Dmytro
    Dmytro almost 7 years
    the most correct answer is "if you right click an executable you can see there are options to start minimized, etc, this option is encoded in nCmdShow in case you want to make your program respond to them".