Is there a C # m_nCmdShow equivalent?

In the MFC program, you can determine whether the Startup was set to Minimize in the application’s m_nCmdShow checking the m_nCmdShow value. Is there an equivalent way to do this in C #?

To clarify, I do not want to set the state of a particular form. If you look at the properties for the shortcut, there is a Run option. You can set this value to “Normal window”, “Minimize” or “Maximum”.

In C ++, you can read what the launch value was set for by looking at m_nCmdShow . I need to do the same in C #.

Update

This attempt:

 [STAThread] static void Main(string[] args) { ProcessStartInfo processInfo = Process.GetCurrentProcess().StartInfo; MessageBox.Show(processInfo.WindowStyle.ToString()); ... } 

always reports Normal , regardless of what the shortcut is set to.

+8
c #
source share
2 answers

WindowsForms has a WindowState property of the Form class. check it in properties during development or install it from code.

Edit: When starting a program from a shortcut, Windows can use the CreateProcess API, passing it the STARTUPINFO structure.

from your Windows Forms application, you get this structure as follows:

 System.Diagnostics.Process.GetCurrentProcess().StartInfo 

which contains the: WindowStyle property, and the values ​​available to it are enumeration values:

 System.Diagnostics.ProcessWindowStyle 

So:

 Hidden; Minimized; Maximized; Normal; 

and that mapping in m_nCmdShow looking for OP.

+3
source share

This allows you to get the initial state of the window by contacting NativeMethods.StartupInfo.GetInitialWindowStyle() in your code. You can use additional information by contacting NativeMethods.StartupInfo.FromCurrentProcess . If you run your program from cmd.exe using START "My Program Title" /MIN MyProgram.exe , you will find "My program name" in NativeMethods.StartupInfo.FromCurrentProcess.lpTitle and NativeMethods.StartupInfo.GetInitialWindowStyle() returns ProcessWindowStyle.Minimized .

 static partial class NativeMethods { public static class StartupInfo { [StructLayout(LayoutKind.Sequential)] public class STARTUPINFO { public readonly UInt32 cb; private IntPtr lpReserved; [MarshalAs(UnmanagedType.LPWStr)] public readonly string lpDesktop; [MarshalAs(UnmanagedType.LPWStr)] public readonly string lpTitle; public readonly UInt32 dwX; public readonly UInt32 dwY; public readonly UInt32 dwXSize; public readonly UInt32 dwYSize; public readonly UInt32 dwXCountChars; public readonly UInt32 dwYCountChars; public readonly UInt32 dwFillAttribute; public readonly UInt32 dwFlags; [MarshalAs(UnmanagedType.U2)] public readonly UInt16 wShowWindow; [MarshalAs(UnmanagedType.U2)] private UInt16 cbReserved2; private IntPtr lpReserved2; public readonly IntPtr hStdInput; public readonly IntPtr hStdOutput; public readonly IntPtr hStdError; } public readonly static STARTUPINFO FromCurrentProcess = null; const uint STARTF_USESHOWWINDOW = 0x00000001; const ushort SW_HIDE = 0; const ushort SW_SHOWNORMAL = 1; const ushort SW_SHOWMINIMIZED = 2; const ushort SW_SHOWMAXIMIZED = 3; const ushort SW_MINIMIZE = 6; const ushort SW_SHOWMINNOACTIVE = 7; const ushort SW_FORCEMINIMIZE = 11; [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] static extern void GetStartupInfoW(IntPtr startupInfoPtr); static StartupInfo() //Static constructor { FromCurrentProcess = new STARTUPINFO(); int length = Marshal.SizeOf(typeof(STARTUPINFO)); IntPtr ptr = Marshal.AllocHGlobal(length); Marshal.StructureToPtr(FromCurrentProcess, ptr, false); GetStartupInfoW(ptr); Marshal.PtrToStructure(ptr, FromCurrentProcess); Marshal.FreeHGlobal(ptr); } public static ProcessWindowStyle GetInitialWindowStyle() { if ((FromCurrentProcess.dwFlags & STARTF_USESHOWWINDOW) == 0) return ProcessWindowStyle.Normal; switch (FromCurrentProcess.wShowWindow) { case SW_HIDE: return ProcessWindowStyle.Hidden; case SW_SHOWNORMAL: return ProcessWindowStyle.Normal; case SW_MINIMIZE: case SW_FORCEMINIMIZE: case SW_SHOWMINNOACTIVE: case SW_SHOWMINIMIZED: return ProcessWindowStyle.Minimized; case SW_SHOWMAXIMIZED: return ProcessWindowStyle.Maximized; default: return ProcessWindowStyle.Normal; } } } } 
0
source share