我正在尝试将 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:full
。 demoapp
是您应用的启动方案,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/