3

I have a maui app with blazor and I'm creating a custom titlebar.

I about to close the maui app, using on blazor Application.Current.Quit();

Now how can i minimize and move maui app

My code blazor

private void MoveWindow()
{
    
}

private void MinimizeWindow()
{
        
}


private void CloseWindow() {
    Application.Current.Quit();
}

2 Answers 2

8

Maui already has a function to minimize the application.

Use the following in your blazor:

private void MinimizeWindow()
{
#if WINDOWS
    var Window = App.Current.Windows.First();
    var nativeWindow = Window.Handler.PlatformView;
    IntPtr windowHandle = WinRT.Interop.WindowNative.GetWindowHandle(nativeWindow);
    WindowId WindowId = Win32Interop.GetWindowIdFromWindow(windowHandle);
    AppWindow appWindow = AppWindow.GetFromWindowId(WindowId);

    var p = appWindow.Presenter as OverlappedPresenter;

    p.Minimize();
#endif
}
Sign up to request clarification or add additional context in comments.

2 Comments

It works, is there any maui function to move window?
AppWindow can also be used to hide and show a window completely. Thank you so much.
1

You need to call native code for that. Add a reference to PInvoke.User32 package:

<PackageReference Include="PInvoke.User32" Version="0.7.104" Condition="$([MSBuild]::IsOSPlatform('windows'))"/>

Minimize

As an example upon a button click we minimize the window:

void MinimizeWindow(object sender, EventArgs e)
{
#if WINDOWS
            var mauiWindow = App.Current.Windows.First();
            var nativeWindow = mauiWindow.Handler.PlatformView;
            IntPtr windowHandle = WinRT.Interop.WindowNative.GetWindowHandle(nativeWindow);

            PInvoke.User32.ShowWindow(windowHandle, PInvoke.User32.WindowShowStyle.SW_MINIMIZE);
#endif
}

Move

void MoveWindow(object sender, EventArgs e)
{
#if WINDOWS
            var mauiWindow = App.Current.Windows.First();
            var nativeWindow = mauiWindow.Handler.PlatformView;
            IntPtr windowHandle = WinRT.Interop.WindowNative.GetWindowHandle(nativeWindow);
            PInvoke.RECT rect;

            PInvoke.User32.GetWindowRect(windowHandle, out rect);
            (var width, var height) = GetWinSize(rect);

            PInvoke.User32.MoveWindow(windowHandle, 50, 0, width, height, true);

#endif
}

(int width, int height) GetWinSize(PInvoke.RECT rect) =>
            (rect.right - rect.left, rect.bottom - rect.top);

It's possible to resize the window with MoveWindow(), but since the aim in the question is to move the window only, then we supply the values of the current window's width and height as parameters.

Ps: Unfortunately I didn't find a simpler solution to get the window dimensions.

EDIT

A better alternative is to use AppWindow.Move(PointInt32):

#if WINDOWS
WindowId WindowId = Win32Interop.GetWindowIdFromWindow(windowHandle);
AppWindow appWindow = AppWindow.GetFromWindowId(WindowId);
appWindow.Move(new Windows.Graphics.PointInt32(x,y))
#endif

x and y are the coordinate for the desired new position. The origin (0,0) is the left upper corner of the screen.

For information about the screen dimensions:

4 Comments

Maui does not have any function capable of doing this ? And about the question of moving the window, can you tell me about it?
This mode using appWindow.Move looks interesting, how can I get x and y value from my app ?
depending where you want to move your window (the new position), i have included more details
What I try to do is the same as wpf DragMove()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.