【问题标题】:How to programmatically bring the UWP app window to the front and maximize it while the window is minimized如何以编程方式将 UWP 应用程序窗口置于最前面并在窗口最小化时最大化它
【发布时间】:2020-01-16 21:22:30
【问题描述】:

我正在尝试将一个 UWP APP 窗口放在前面,并使窗口最大化。

我试过UWP APP窗口是否恢复,我的代码工作正常。但是如果窗口被最小化了,窗口就不会显示,仍然保持最小化状态。

我正在使用FindWindowByCaption(IntPtr.Zero, "Demo App") 来获取窗口的句柄。 Demo App 是 UWP APP 的显示名称。

简单代码如下:

class Program
{
        [DllImport("user32.dll")]
        static extern bool SetForegroundWindow(IntPtr hWnd);

        [DllImport("user32.dll")]
        static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        /// Find window by Caption only. Note you must pass IntPtr.Zero as the first parameter.
        [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
        static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);

        static void Main(string[] args)
        {
            IntPtr handle = FindWindowByCaption(IntPtr.Zero, "Demo App");

            if (handle != IntPtr.Zero)
            {
                SetForegroundWindow(handle);
                ShowWindow(handle, 3);
            }
        }
}

有什么好的建议吗?非常感谢。

【问题讨论】:

    标签: c# uwp


    【解决方案1】:

    如何以编程方式将 UWP 应用窗口置于最前面并在窗口最小化时最大化它

    根据您的要求,我们建议使用protocol 启动 uwp 应用程序。例如:demoapp:fulldemoapp 是您应用的启动方案,full 是参数。

    我们可以截取OnActivated方法中的参数,然后带参数让应用全屏。

    protected override void OnActivated(IActivatedEventArgs e)
    {
        if (e.Kind == ActivationKind.Protocol)
        {
            Frame rootFrame = CreateRootFrame();
    
            if (rootFrame.Content == null)
            {
                if (!rootFrame.Navigate(typeof(MainPage)))
                {
                    throw new Exception("Failed to create initial page");
                }
            }
            var arg = e as ProtocolActivatedEventArgs;
            if (arg.Uri.LocalPath == "full")
            {
                 var view = ApplicationView.GetForCurrentView();
                 if (view.TryResizeView(new Size { Width = 600, Height = 600 }))
                 {
    
                 }
            }
    
            var p = rootFrame.Content as MainPage;
            p.NavigateToPageWithParameter(3, e);
    
            // Ensure the current window is active
            Window.Current.Activate();
        }
    }
    

    更新

    您可以使用TryResizeView 调整窗口大小

    var view = ApplicationView.GetForCurrentView();
    if (view.TryResizeView(new Size { Width = 600, Height = 600 }))
    {
    
    }
    

    【讨论】:

    • 是的,我同意你的看法。但由于某些特殊原因,我无法将应用程序设为全屏。就像我需要以编程方式将两个应用程序分成半屏一样。
    • 所以,你只是想让窗口更大,对吧?
    • 其实我已经按照你在win8.1上说的做了。对于win8.1,我可以模拟键盘发送“win + . + right”键将APP向右移动,然后将另一个应用程序向左移动。但是在win10下,UWP应用全屏的时候就不行了。
    • 是的,我只是想让 UWP 应用的窗口最大化而不是全屏。
    猜你喜欢
    • 1970-01-01
    • 2021-10-07
    • 1970-01-01
    • 2011-02-14
    • 2017-04-17
    • 2015-07-21
    • 1970-01-01
    • 2020-07-24
    • 2018-08-24
    相关资源
    最近更新 更多