【问题标题】:Is there a c# equivalent of m_nCmdShow?是否有与 m_nCmdShow 等效的 c#?
【发布时间】:2011-09-14 16:19:37
【问题描述】:

在 MFC 程序中,您可以通过检查 m_nCmdShow 的值来确定应用程序快捷方式的运行值是否设置为“最小化”。在 c# 中是否有等效的方法来执行此操作?

为了澄清,我不想设置特定表单的状态。如果您查看快捷方式的属性,则会出现“运行”选项。您可以将此值设置为 Normal Window、Minimized 或 Maximized。

在 C++ 中,您可以通过查看 m_nCmdShow 来读取该启动值的设置。我需要在 C# 中做同样的事情。

更新

这次尝试:

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

始终报告Normal,无论快捷方式设置为什么。

【问题讨论】:

    标签: c#


    【解决方案1】:

    在 WindowsForms 中,它是 Form 类的 WindowState 属性。在设计时在属性中检查它或从代码中设置它。

    编辑:当从快捷方式运行程序时,Windows 可能会使用 CreateProcess API 将 STARTUPINFO 结构传递给它。

    从您的 Windows 窗体应用程序中,您可以通过以下方式获得这样的结构:

    System.Diagnostics.Process.GetCurrentProcess().StartInfo
    

    其中包含属性:WindowStyle,它的可用值是枚举的值:

    System.Diagnostics.ProcessWindowStyle
    

    所以:

    Hidden;
    Minimized;
    Maximized;
    Normal;
    

    这就是 OP 正在寻找的到 m_nCmdShow 的映射。

    【讨论】:

    • 有趣!该成员如何在 MFC 中设置?它的值必须分配到某个地方...
    • @Ifalin:查看我的最后编辑 :) 希望这能完全回答您的问题。
    • 奇怪...实际上还有另一个问题:stackoverflow.com/questions/7026395/…
    • 启动对象是否设置为表单可能会有所不同。我的设置为一个类而不是一个表单,因为在启动时还有一些其他的事情需要发生。
    【解决方案2】:

    这使您可以通过在代码中访问 NativeMethods.StartupInfo.GetInitialWindowStyle() 来检索初始窗口状态。您可以通过访问NativeMethods.StartupInfo.FromCurrentProcess 使用更多信息。如果您从 cmd.exe 使用START "My Program Title" /MIN MyProgram.exe 启动程序,您会在NativeMethods.StartupInfo.FromCurrentProcess.lpTitle 中找到“我的程序标题”,而NativeMethods.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;
                }
            }
        } 
    }
    

    【讨论】:

      猜你喜欢
      • 2021-04-30
      • 2017-01-01
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-06
      • 2010-12-23
      • 2011-07-14
      相关资源
      最近更新 更多