I'm trying to bring an UWP APP window in the front and make the window is maximized.
I tried if the UWP APP window is restored, my code works fine. But if the window is minimized, the window won't be show and still keep in minimized status.
I'm using FindWindowByCaption(IntPtr.Zero, "Demo App") to get the window's handle. Demo App is the UWP APP's display name.
Simple code as below:
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);
}
}
}
Any good suggestions? Thank you very much.
How to programmatically bring the UWP app window to the front and maximize it while the window is minimized
For your requirement, We suggest launch the uwp app with protocol. For example: demoapp:full. the demoapp is your app's launch scheme and the full is parameter.
We could intercept the parameter in OnActivated method then make the app full screen with parameter.
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();
}
}
Update
You could use TryResizeView to resize your window
var view = ApplicationView.GetForCurrentView();
if (view.TryResizeView(new Size { Width = 600, Height = 600 }))
{
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With