In Microsoft.Maui
, within Microsoft.Maui.LifecycleEvents
, there is an extension that manages the MAUI window lifecycle.
public static MauiAppBuilder ConfigureLifecycleEvents(
this MauiAppBuilder builder,
Action<ILifecycleBuilder>? configureDelegate);
The purpose of this article is to use WinUI to control the window interface on Windows.
References:
https://docs.microsoft.com/zh-cn/windows/win32/winmsg/about-windows
https://stackoverflow.com/questions/71806578/maui-how-to-remove-the-title-bar-and-fix-the-window-size
When the program starts, window lifecycle handling events are injected, allowing window management via code.
#if WINDOWS
builder.ConfigureLifecycleEvents(events =>
{
events.AddWindows(wndLifeCycleBuilder =>
{
wndLifeCycleBuilder.OnWindowCreated(window =>
{
// Whether to display the system's built-in title bar
window.ExtendsContentIntoTitleBar = true;
IntPtr nativeWindowHandle = WinRT.Interop.WindowNative.GetWindowHandle(window);
WindowId win32WindowsId = Win32Interop.GetWindowIdFromWindow(nativeWindowHandle);
AppWindow winuiAppWindow = AppWindow.GetFromWindowId(win32WindowsId);
if (winuiAppWindow.Presenter is OverlappedPresenter p)
{
p.SetBorderAndTitleBar(false, false);
}
const int width = 1200;
const int height = 800;
// Reset the default opening size
winuiAppWindow.MoveAndResize(new RectInt32(1920 / 2 - width / 2, 1080 / 2 - height / 2, width, height));
winuiAppWindow.SetPresenter(AppWindowPresenterKind.Overlapped);
winuiAppWindow.Changed += (w, e) =>
{
// Window size adjustment event
if (e.DidSizeChange)
{
}
};
})
// After the window is closed,
.OnClosed((window, args) =>
{
});
});
});
#endif
Among them, the two window objects AppWindow and Window (winuiAppWindow, window) are both necessary for managing the window subsequently. If the program only has one window, these two objects can be handled using the singleton pattern.
Their API usage is as follows:
private readonly AppWindow _appWindow;
private readonly Window _window;
private WindowService(AppWindow appWindow, Window window)
{
_appWindow = appWindow;
_window = window;
}
public bool FullScreenState { get => _appWindow.Presenter.Kind == AppWindowPresenterKind.FullScreen; }
public void FullScreen()
{
_appWindow.SetPresenter(AppWindowPresenterKind.FullScreen);
}
public void ExitFullScreen()
{
_appWindow.SetPresenter(AppWindowPresenterKind.Default);
}
public void Minmize()
{
#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
}
public void Exit()
{
_window.Close();
}
public void SetSize(int _X, int _Y, int _Width, int _Height)
{
_appWindow.MoveAndResize(new RectInt32(_X, _Y, _Width, _Height));
}
public (int X, int Y) GetPosition()
{
var p = _appWindow.Position;
return (p.X, p.Y);
}
public (int Width, int Height, int ClientWidth, int ClientHeight) GetSize()
{
var size = _appWindow.Size;
var clientSize = _appWindow.ClientSize;
return (size.Width, size.Height, clientSize.Width, clientSize.Height);
}
public (PointInt32 Position, SizeInt32 Size, SizeInt32 ClientSize) GetAppSize()
{
return (_appWindow.Position, _appWindow.Size, _appWindow.ClientSize);
}
文章评论