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

标签 c# uwp

我正在尝试将 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);
            }
        }
}

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

最佳答案

How to programmatically bring the UWP app window to the front and maximize it while the window is minimized

根据您的要求,我们建议使用 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 }))
{

}

关于c# - 如何以编程方式将 UWP 应用程序窗口置于最前面并在窗口最小化时将其最大化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57950067/

相关文章:

C# - Window.Current.CoreWindow.GetKeyState 未按预期工作

xaml - UWP C++ 中的通用工具栏

c++ - 检测到 'vccorlib_lib_should_be_specified_before_msvcrt_lib_to_linker' : value '1' doesn't match value '0' in msvcrtd. 库不匹配

c# - 如何从内存中执行 WPF 程序集?

c# - MethodBase.GetCurrentMethod().Name 与 [CallerMemberName]

c# - ArraySegment - 返回实际段 C#

c# - 如何调试 Windows 应用商店应用程序崩溃?

c# - append 到文本文件(不覆盖!)

c# - Windows Phone 锁屏下导航

c# - WCF 客户端/Java Web 服务 - 反序列化对象为空