Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Windows] How to completely hide the TitleBar? #15142

Closed
scriptBoris opened this issue May 17, 2023 · 1 comment
Closed

[Windows] How to completely hide the TitleBar? #15142

scriptBoris opened this issue May 17, 2023 · 1 comment

Comments

@scriptBoris
Copy link

scriptBoris commented May 17, 2023

          I think this has mostly to do with the fact that the lifecycle is a little different with .NET MAUI. You totally can do this, but not exactly the same as with WinUI.

Coincidentally I have written about it here: https://blog.verslu.is/maui/full-screen-disable-minimize-maximize-for-net-maui-windows-apps/ with the code being here: https://github.com/jfversluis/MauiWindowsFullscreenMinimizeMaximizeSample

What you want to do is hook into the lifecycle events we have setup for windows (note: a window, not Windows). You can do that in the MauiProgram when the app is setup:

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 =>
    {
        // Make sure to add "using Microsoft.Maui.LifecycleEvents;" in the top of the file 
        events.AddWindows(windowsLifecycleBuilder =>
        {
            windowsLifecycleBuilder.OnWindowCreated(window =>
            {
                window.ExtendsContentIntoTitleBar = false;
                var handle = WinRT.Interop.WindowNative.GetWindowHandle(window);
                var id = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(handle);
                var appWindow = Microsoft.UI.Windowing.AppWindow.GetFromWindowId(id);
                switch (appWindow.Presenter)
                {
                    case Microsoft.UI.Windowing.OverlappedPresenter overlappedPresenter:
                        overlappedPresenter.SetBorderAndTitleBar(false, false);
                        overlappedPresenter.Maximize();
                        break;
                }
            });
        });
    });
#endif

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

    return builder.Build();
}

The important part is in the #if WINDOWS block. First we set the ExtendsContentIntoTitleBar to false. I think that might be the cause of the small titlebar that you still see in your scenario. Maybe setting that for you is even enough. From there, I think we're doing pretty much the same thing to get the window handle, window, etc. and then set the right properties on the Presenter. Of course if you want to make this specific to one type of window or just one window you'll have to implement some logic to only do it for that window.

The above is for opening the window initially, you can also do it on some user interaction. That code with different scenarios can be found here: https://github.com/jfversluis/MauiWindowsFullscreenMinimizeMaximizeSample/blob/main/MauiWindowsFullscreenMinimizeMaximizeSample/MainPage.xaml.cs

If there is anything still missing from this feel free to reach out and/or open a new issue with that specific scenario.

Thanks!

Originally posted by @jfversluis in #10057 (comment)

image
After using this solution now appear white top line (artefact). How fix it?

Code:

internal static void Init(MauiAppBuilder builder)
    {
        builder.ConfigureLifecycleEvents(x =>
        {
            x.AddWindows(x =>
            {
                x.OnActivated((w, e) =>
                {
                });

                x.OnWindowCreated(w =>
                {
                    if (w is not MauiWinUIWindow window)
                        return;

                    window.ExtendsContentIntoTitleBar = false;
                    var handle = WinRT.Interop.WindowNative.GetWindowHandle(window);
                    var id = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(handle);
                    var appWindow = Microsoft.UI.Windowing.AppWindow.GetFromWindowId(id);
                    switch (appWindow.Presenter)
                    {
                        case Microsoft.UI.Windowing.OverlappedPresenter overlappedPresenter:
                            overlappedPresenter.SetBorderAndTitleBar(false, false);
                            break;
                    }
                });
            });
        });
    }
@scriptBoris scriptBoris changed the title [Windows] Full hide TitleBar [Windows] How to completely hide the TitleBar? May 17, 2023
@DoubleDBE
Copy link

OverlappedPresenter has some aditional properties you need to set to completely hide the title bar:

if (appWindow.Presenter is OverlappedPresenter pre)
{
    pre.IsMaximizable = false;
    pre.IsResizable = false;
    pre.IsMinimizable = false;
    pre.SetBorderAndTitleBar(false, false);
 }

@ghost ghost locked as resolved and limited conversation to collaborators Jun 17, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants