How to Maximize, Minimize, Restore WinUI window from code
Asked Answered
P

5

4

I'm creating a demo app using WinUI 3 prerelease 0.5 (project reunion).

I would like to maximize, minimize, and restore the window state from c# managed code. Are there any examples available?

Planet answered 24/3, 2021 at 16:44 Comment(1)
See following question: #72125953Normalize
P
3

Apparently this is not possible using managed code at this time. PInvoke is the only option currenlty. I used PInvoke.User32 nuget package.

Planet answered 31/3, 2021 at 6:28 Comment(3)
Can you give us the source that states that this basic function isn't possible? What I am really looking for is a way to set my app to fullscreen. No luck so far. :[Jurat
@LeBrown Jones - Nothing you can link to - I got the information through a side conversation with one of the PM's on the Project Reunion team. You can follow the relevant conversation here: github.com/microsoft/microsoft-ui-xaml/issues/4668Planet
Nowadays this can be done from managed code. See my answer using the AppWindow.Presenter to maximize/minimize the window.Luca
J
3

Use AppWindow class from Windows App SDK

// Retrieve the window handle (HWND) of the current (XAML) WinUI 3 window.
IntPtr hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);

// Retrieve the WindowId that corresponds to hWnd.
WindowId windowId = Win32Interop.GetWindowIdFromWindow(hWnd);

// Lastly, retrieve the AppWindow for the current (XAML) WinUI 3 window.
AppWindow appWindow = AppWindow.GetFromWindowId(windowId);
appWindow.Closing += AppWindow_Closing;
private void AppWindow_Closing(AppWindow sender, AppWindowClosingEventArgs args)
{
    args.Cancel = true;
}

https://learn.microsoft.com/en-us/windows/apps/windows-app-sdk/windowing/windowing-overview

Jobina answered 13/3, 2022 at 17:33 Comment(0)
L
2

This is now possible using the Microsoft.UI.Windowing.OverlappedPresenter class. Your app window can be of different kinds, by default it's usually this one. Here's how you can use it from your code:

// Assuming you have a reference to your window (currentWindow)
if (currentWindow.AppWindow.Presenter is OverlappedPresenter presenter)
{
    presenter.Restore();
    presenter.Minimize();
    presenter.Maximize();
}
Luca answered 14/2, 2024 at 9:36 Comment(0)
C
1

Here is my implementation based on the PInvoke.User32 nuget package.

Version using Window parameter

    public void MaximizeWindow(Window window)
    {
        var windowHandle = WinRT.Interop.WindowNative.GetWindowHandle(window);

        PInvoke.User32.ShowWindow(windowHandle, PInvoke.User32.WindowShowStyle.SW_MAXIMIZE);
    }

Version using extension method

This previous code can also be transformed into an extension method for the Window class, so I can just call this.Maximize() from within the Window:

public static class WindowExtensions
{
    public static void Maximize(this Window window)
    {
        var windowHandle = WinRT.Interop.WindowNative.GetWindowHandle(window);

        PInvoke.User32.ShowWindow(windowHandle, PInvoke.User32.WindowShowStyle.SW_MAXIMIZE);
    }
}

Usage in your Window derived class:

this.Maximize();


Minize and Restore

The "PInvoke.User32.WindowShowStyle" definition used in the examples above contains also constants for SW_MINIMIZE and SW_RESTORE, but I did not test this.

Caveman answered 9/2, 2022 at 13:12 Comment(1)
User32.ShowWindow(hwnd, ShowWindowCommand.SW_MAXIMIZE);Saragossa
S
0

If you just want to maximize a window you may use this minimal class without including PInvoke.User32

using System;
using System.Runtime.InteropServices;

namespace JimPrototypes2.Services
{
    class PInvoke
    {
        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool ShowWindow(IntPtr hWnd, WindowShowStyle nCmdShow);

        // Full enum def is at https://github.com/dotnet/pinvoke/blob/main/src/User32/User32+WindowShowStyle.cs
        public enum WindowShowStyle : uint
        {
            SW_MAXIMIZE = 3,     
        }

        public static void  MaximizeWindow(IntPtr hWnd)
        {
            ShowWindow(hWnd, WindowShowStyle.SW_MAXIMIZE);
        }
    }
}

Usage:

 IntPtr hWnd = (IntPtr)WinRT.Interop.WindowNative.GetWindowHandle(App.Window);
 PInvoke.MaximizeWindow(hWnd);
Sluggish answered 30/12, 2023 at 14:51 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.