如何将 WindowState 从桌面快捷方式传递到 WPF 应用程序?

发布于 2024-11-29 11:07:30 字数 260 浏览 1 评论 0原文

如何从桌面快捷方式控制 WPF 主窗口的初始 WindowState(正常、最小化、最大化)?

快捷方式属性对话框的“运行:”组合框让我可以在“正常窗口”、“最小化”和“最大化”之间进行选择。但 WPF 应用程序似乎完全忽略了此选项。使用 WinForms 可以自动支持此功能,无需额外代码。

有没有办法从启动的 WPF 进程访问此选项?我知道我可以在启动新进程时指定 ProcessStartInfo.WindowStyle 属性。但是我如何从正在启动的进程中访问此选项?

How can I control the initial WindowState (Normal, Minimized, Maximized) of a WPF main window from a desktop shortcut?

The "Run:" combobox of the shortcut's properties dialog let's me choose between "Normal window", "Minimized" and "Maximized". But this option seems to be completely ignored by WPF apps. With WinForms this was automatically supported with no additional code.

Is there a way to access this option from the launched WPF process? I know I can specify the ProcessStartInfo.WindowStyle property when launching new processes. But how can I access this option from the process being launched?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

毅然前行 2024-12-06 11:07:30
System.Diagnostics.Process.GetCurrentProcess().StartInfo.WindowStyle
System.Diagnostics.Process.GetCurrentProcess().StartInfo.WindowStyle
数理化全能战士 2024-12-06 11:07:30

使用 NativeMethods.StartupInfo.GetInitialWindowStyle() 仅获取初始窗口状态,或使用 NativeMethods.StartupInfo.FromCurrentProcess 访问全部信息。

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)] public readonly 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;
            }
        }
    } 
}

Use NativeMethods.StartupInfo.GetInitialWindowStyle() to get just the initial window state or NativeMethods.StartupInfo.FromCurrentProcess to access the entire information.

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)] public readonly 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;
            }
        }
    } 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文