简体   繁体   English

如何以编程方式移动和最小化 Maui windows 应用程序

[英]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'))"/>

Minimize最小化

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
}

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.可以使用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.

相关问题 如何以编程方式挂起/最小化/退出Windows Phone应用程序? - How to suspend/minimize/exit Windows Phone application programmatically? 在 Windows 平台的情况下,如何为 MAUI 应用程序指定 window 大小? - How to specify window size for a MAUI application in case of Windows platform? 如何以编程方式移动Windows任务栏(取两个) - How to programmatically move windows taskbar (take two) 如何以编程方式卸载Windows中的应用程序? - How to programmatically uninstall an application in Windows? 像在Windows 7中一样最小化应用程序 - Minimize application just like in Windows 7 如何自动最小化Windows窗体应用程序(C#)? - How to automatically minimize my Windows Form Application (C#)? 如何在C#中最小化单个Windows应用程序窗体 - How to minimize a single Windows application form in C# 如何在 Windows 窗体应用程序中设计自定义关闭、最小化和最大化按钮? - how to design a custom close, minimize and maximize button in windows form application? 有没有办法以编程方式最大化/最小化 WinRT 应用程序? - Is there a way to programmatically maximize/minimize WinRT application? 将 .net MAUI 应用程序发布为 windows 可执行文件 - Publish .net MAUI Application as windows executable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM