Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

uwp

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.

like image 495
jerry Avatar asked Dec 08 '25 06:12

jerry


1 Answers

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 }))
{

}
like image 140
Nico Zhu - MSFT Avatar answered Dec 09 '25 20:12

Nico Zhu - MSFT



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!