2011-09-14 68 views
8

在MFC程序中,您可以通過檢查m_nCmdShow的值來確定應用程序快捷方式是否將「運行」值設置爲「最小化」。有沒有一種等效的方式在c#中做到這一點?是否有一個C#等價於m_nCmdShow?

爲了澄清,我不想設置特定表單的狀態。如果您查看快捷方式的屬性,則會出現「運行」選項。您可以將此值設置爲普通窗口,最小化或最大化。

在C++中,您可以通過查看m_nCmdShow來閱讀設置的啓動值。我需要在C#中做同樣的事情。

更新

這種嘗試:

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

始終報告Normal,無論什麼快捷方式設置爲。

回答

3

在WindowsForms中它是Form類的WindowState屬性。在設計時檢查屬性或從代碼中設置它。

編輯:從快捷方式運行程序時,Windows可能使用CreateProcess API向其傳遞STARTUPINFO結構。

從您的Windows窗體應用程序以這種方式得到這樣的結構:其中包含屬性

System.Diagnostics.Process.GetCurrentProcess().StartInfo 

WindowStyle併爲它的可用值與枚舉:

System.Diagnostics.ProcessWindowStyle 

所以:

Hidden; 
Minimized; 
Maximized; 
Normal; 

這就是映射到m_nCmdShow OP正在尋找。

+0

有趣!該成員在MFC中如何設置?它的價值必須得到指定的地方... –

+0

@伊法林:看到我最後的編輯:)希望這完全回答你的問題。 –

+0

奇怪...實際上還有另一個關於此問題:http://stackoverflow.com/questions/7026395/how-to-pass-windowstate-from-desktop-shortcut-into-wpf-app –

0

這使您可以通過訪問代碼中的NativeMethods.StartupInfo.GetInitialWindowStyle()來檢索初始窗口狀態。您可以通過訪問NativeMethods.StartupInfo.FromCurrentProcess來使用更多信息。如果您使用START "My Program Title" /MIN MyProgram.exe從cmd.exe啓動程序,則會在NativeMethods.StartupInfo.FromCurrentProcess.lpTitleNativeMethods.StartupInfo.GetInitialWindowStyle()中找到「我的程序標題」,並返回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; 
      } 
     } 
    } 
} 
相關問題