[英]How to move and minimize Maui windows application programmatically
I have a maui app with blazor and I'm creating a custom titlebar.我有一个带有 blazor 的 maui 应用程序,我正在创建一个自定义标题栏。
I about to close the maui app, using on blazor Application.Current.Quit();
我即将关闭 maui 应用程序,使用 blazor Application.Current.Quit();
Now how can i minimize and move maui app现在我如何最小化和移动 maui 应用程序
My code blazor我的代码 blazor
private void MoveWindow()
{
}
private void MinimizeWindow()
{
}
private void CloseWindow() {
Application.Current.Quit();
}
Maui already has a function to minimize the application. Maui 已经有一个 function 来最小化应用程序。
Use the following in your blazor:在 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
}
You need to call native code for that.您需要为此调用本机代码。 Add a reference to PInvoke.User32
package:添加对PInvoke.User32
package 的引用:
<PackageReference Include="PInvoke.User32" Version="0.7.104" Condition="$([MSBuild]::IsOSPlatform('windows'))"/>
As an example upon a button click we minimize the window:作为单击按钮的示例,我们最小化 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
}
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.可以使用MoveWindow()
调整 window 的大小,但由于问题的目的是仅移动 window,因此我们提供当前窗口的宽度和高度值作为参数。
Ps: Unfortunately I didn't find a simpler solution to get the window dimensions. Ps:不幸的是,我没有找到更简单的解决方案来获得 window 尺寸。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.