MAUI 隐藏标题栏 .net core 7

526 阅读1分钟

参考:c# - MAUI how to remove the Title Bar and fix the window size? - Stack Overflow

MAUI Blazor 也是可以用

  • 效果 image.png

  • MauiProgram.cs 文件

using Microsoft.Extensions.Logging;
using Microsoft.Maui.LifecycleEvents;
#if WINDOWS
    using Microsoft.UI;
    using Microsoft.UI.Windowing;
    using Windows.Graphics;
#endif
namespace MauiApp1;

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(lifeCycleBuilder =>
            {
                lifeCycleBuilder.OnWindowCreated(w =>
                {
                    w.ExtendsContentIntoTitleBar = false;
                    IntPtr wHandle = WinRT.Interop.WindowNative.GetWindowHandle(w);
                    WindowId windowId = Win32Interop.GetWindowIdFromWindow(wHandle);
                    AppWindow mauiWindow = AppWindow.GetFromWindowId(windowId);
                    //mauiWindow.SetPresenter(AppWindowPresenterKind.FullScreen);  // TO SET THE APP INTO FULL SCREEN
                    if (mauiWindow.Presenter is OverlappedPresenter p)
                    {
                        p.SetBorderAndTitleBar(false, false);X
                    }
X
                    //OR USE THIS LINE FOR YOUR CUSTOM POSITION
                    //mauiWindow.MoveAndResize(x, y, w, h);
                    mauiWindow.MoveAndResize(new RectInt32(0, 0, 800, 600)); //设置窗口显示位置与宽高
                });
            });
        });
#endif

#if DEBUG
        builder.Logging.AddDebug();
#endif

        return builder.Build();
    }
}

  • 显示好像还是有点问题
    • 顶部好像还有几个像素是标题栏的,还没有测试有空测试看看
    • 设置了位置 0 ,0 左边还有几个像素没有到最左