在MFC程序中,您可以通过检查m_nCmdShow
的值来确定应用程序快捷方式是否将“运行”值设置为“最小化”。在c#中有相同的方法吗?
为了澄清,我不想设置特定表单的状态。如果查看快捷方式的属性,则会显示“运行”选项。您可以将此值设置为“正常窗口”,“最小化”或“最大化”。
在C ++中,您可以通过查看m_nCmdShow
来了解启动值的设置。我需要在C#中做同样的事情。
更新
这次尝试:
[STAThread]
static void Main(string[] args)
{
ProcessStartInfo processInfo = Process.GetCurrentProcess().StartInfo;
MessageBox.Show(processInfo.WindowStyle.ToString());
...
}
始终报告Normal
,无论快捷方式设置为什么。
答案 0 :(得分:3)
在WindowsForms中,它是Form类的WindowState属性。在设计时在属性中检查它或从代码中设置它。
编辑:从快捷方式运行程序时,Windows可能会使用CreateProcess API将STARTUPINFO结构传递给它。
从Windows窗体应用程序中,您可以通过这种方式获得此类结构:
System.Diagnostics.Process.GetCurrentProcess().StartInfo
包含属性:WindowStyle
,其可用值是枚举的值:
System.Diagnostics.ProcessWindowStyle
这样:
Hidden;
Minimized;
Maximized;
Normal;
这就是OP正在寻找m_nCmdShow
的映射。
答案 1 :(得分:0)
这使您可以通过访问代码中的NativeMethods.StartupInfo.GetInitialWindowStyle()
来检索初始窗口状态。您可以通过访问NativeMethods.StartupInfo.FromCurrentProcess
来使用更多信息。如果您使用START "My Program Title" /MIN MyProgram.exe
从cmd.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;
}
}
}
}