How to disable window maximize functionality in MAUI?

Viewed 102

I want to make my app only minimizable but not resizable and not maximizable. Ideally both on Windows and Mac, but I wanted to get it working on Windows first.

Based on examples, I tried the following approaches.

Platforms/Windows/App.xaml.cs:

public App()
{
    this.InitializeComponent();
    Microsoft.Maui.Handlers.WindowHandler.Mapper.AppendToMapping(nameof(IWindow), (handler, view) =>
    {
        var nativeWindow = handler.PlatformView;
        nativeWindow.Activate();
        IntPtr hWnd = WinRT.Interop.WindowNative.GetWindowHandle(nativeWindow);
        WindowId windowId = Win32Interop.GetWindowIdFromWindow(hWnd);
        AppWindow appWindow = AppWindow.GetFromWindowId(windowId);

        var presenter = appWindow.Presenter as OverlappedPresenter;
        presenter.IsResizable = false;
        presenter.IsMaximizable = false;
    });
}

IsResizable works, now borders cannot be resized. However, the Maximize button and control menu Maximize command is still present.

Tried the same code in an overridden method:

protected override void OnLaunched(LaunchActivatedEventArgs args)
{
    base.OnLaunched(args);

    var currentWindow = Application.Windows[0].Handler.PlatformView;
    IntPtr _windowHandle = WindowNative.GetWindowHandle(currentWindow);
    var windowId = Win32Interop.GetWindowIdFromWindow(_windowHandle);

    AppWindow appWindow = AppWindow.GetFromWindowId(windowId);
    var presenter = appWindow.Presenter as OverlappedPresenter;
    presenter.IsResizable = false;
    presenter.IsMaximizable = false;
    presenter.IsMinimizable = false;
}

The same result, only IsResizable = false; has an effect.

Then I tried to move the same code to MauiProgram.cs:

public static class MauiProgram
{
    public static MauiApp CreateMauiApp()
    {
        var builder = MauiApp.CreateBuilder();
        builder
            .UseMauiApp<App>()
            .ConfigureFonts(fonts =>
            {
                fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
            });

#if WINDOWS
        builder.ConfigureLifecycleEvents(events =>
        {
            events.AddWindows(wndLifeCycleBuilder =>
            {
                wndLifeCycleBuilder.OnWindowCreated(window =>
                {
                    IntPtr nativeWindowHandle = WinRT.Interop.WindowNative.GetWindowHandle(window);
                    WindowId win32WindowsId = Win32Interop.GetWindowIdFromWindow(nativeWindowHandle);
                    AppWindow winuiAppWindow = AppWindow.GetFromWindowId(win32WindowsId);

                    var p = winuiAppWindow.Presenter as OverlappedPresenter;
                    p.IsResizable = false;
                    p.IsMaximizable = false;
                });
            });
        });
#endif     

The same result, only IsResizable = false; has an effect.

I even tried messing with Interop and SetWindowLong:

flags &= ~(int)(WS.SizeFrame | WS.MaximizeBox);
SetWindowLongPtr(hWnd, GWL_STYLE, (IntPtr)flags);

which worked fine for WinForms apps (although not needed there because you can configure it in Properties). But MAUI window seems to ignoring all my attempts.

How do I make maximize go away?

0 Answers
Related