1

While looking for ways to solve the problem of MAUI applications having a grey title bar on Windows, I managed to make the actual, accent-color-respecting title bar reappear by adding a Win32 subclass that intercepts WM_NCCALCIZE and passes it to DefWindowProc. Unfortunately, the grey fake title bar added by WinUI3 is still there, which means I have two title bars now (plus the Shell's navigation bar):

The Win32 title bar is back, but the WinUI3 "title bar" is still there.

I tried the proposed fix of setting ExtendsContentIntoTitleBar and calling SetTitleBar on the WinUI Window:

    var myTitleBar = new Microsoft.UI.Xaml.Controls.TextBlock() {
        Name="DummyTitleBar",
        Text="Dummy title bar"
    };
    winuiContentContainer.Children.Add(myTitleBar);
    winuiWindow.ExtendsContentIntoTitleBar = true;
    winuiWindow.SetTitleBar(myTitleBar);

Unfortunately, instead of replacing the grey title bar with the dummy UIElement (that I planned to later swap for something like a 1-pixel line), it merely draws the element over the grey fake title bar:

"Dummy title bar" overlaps the grey fake title bar but doesn't replace it

I need a way to make the grey fake title bar disappear for good (or at least significantly reduce its height) without harming the real title bar, or cover it with the Shell. Only on Windows, of course.

Edit: To reiterate from the other question, I'm on Windows 10, so I can't use AppWindow.TitleBar which works only on Windows 11.

Medinoc
  • 6,577
  • 20
  • 42

1 Answers1

1

I covered the default title bar by setting the appwindow in the maui project. You can put the following code in the MainPage.cs:

protected override void OnHandlerChanged()
      {
            base.OnHandlerChanged();
#if WINDOWS
        var uiSettings = new Windows.UI.ViewManagement.UISettings();
        var color = uiSettings.GetColorValue(Windows.UI.ViewManagement.UIColorType.AccentLight1);
        Microsoft.UI.Xaml.Window window = (Microsoft.UI.Xaml.Window)App.Current.Windows.First<Window>().Handler.PlatformView;
        IntPtr windowHandle = WinRT.Interop.WindowNative.GetWindowHandle(window);
        Microsoft.UI.WindowId windowId = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(windowHandle);
        Microsoft.UI.Windowing.AppWindow appWindow = Microsoft.UI.Windowing.AppWindow.GetFromWindowId(windowId);
        appWindow.Title = "this is title";
        Microsoft.UI.Windowing.AppWindowTitleBar titlebar = appWindow.TitleBar;
        titlebar.BackgroundColor = color;
        titlebar.ButtonBackgroundColor = color;
#endif
    }
Liyun Zhang - MSFT
  • 8,271
  • 1
  • 2
  • 14
  • I get a `NullReferenceException` because `appWindow.TitleBar` is null. Isn't that the Windows-11-only stuff? I can't use it. Your answer does bring up what may be a better place to put changes than my current one, though. Also, `"this is title"` affects the real title bar but not the fake one, interesting. – Medinoc Jan 04 '23 at 08:49
  • 1
    Sorry for my careless, my pc's os is Windows 11. @Medinoc – Liyun Zhang - MSFT Jan 04 '23 at 08:56
  • 1
    So the title bar should be a part of the appwindow. Even though the `appWindow.TitleBar` is null, but the `appWindow` has an instance. And according to the official document, `ExtendsContentIntoTitleBar = true` will hide the default title bar. But it didn't, so you can report it on the github. @Medinoc – Liyun Zhang - MSFT Jan 04 '23 at 09:05
  • In addition, I saw you added the `TextBlock` into the title bar successfully, you may can try to create a UI such as a Grid with a not grey backgroud color. And then make the UI cover the title bar by call the `winuiWindow.SetTitleBar(GridUI);`. @Medinoc – Liyun Zhang - MSFT Jan 04 '23 at 09:20
  • Thanks. Covering it with arbitrary content would be a step in the right direction, but ideally I want its screen real estate to be reclaimed by the MAUI `Shell`. – Medinoc Jan 04 '23 at 10:13
  • Unfortunately I don't have a GitHub account yet, and don't know the GitHub netiquette. But If I can ask WinUI devs to fix a problem, I'd rather ask them to fix the bug that caused me to jump through those hoops in the first place: That WinUI's forcible customization of window title bars ignores the **"Show accent color on title bars and window borders"** *Settings* checkbox and can't be disabled. – Medinoc Jan 04 '23 at 13:46
  • This may be the limit in the maui. According to the official document, custom tilte bar is easy in the native winui3. @Medinoc – Liyun Zhang - MSFT Jan 05 '23 at 06:00
  • From my reading of the document, customizing WinUI3's already-customized title bar is relatively easy if you replace it altogether, and merely setting its color is trivial if you have Windows 11. Having it NOT override the native colors is not (you always need to customize the customization --> Abstraction inversion). MAUI has the added hurdle that replacing the title bar altogether is harder because you don't directly control the outer window – Medinoc Jan 06 '23 at 17:35
  • So this seems can't be resolved now. – Liyun Zhang - MSFT Jan 09 '23 at 09:19