2

How can I remove the close button in WinUI 3?

Screenshot of WinUI 3 App

1

vimuth
  • 5,064
  • 33
  • 79
  • 116
  • 1
    Title Bar Customization https://learn.microsoft.com/en-us/windows/apps/develop/title-bar but only for Windows 11+ – Simon Mourier Aug 04 '22 at 08:42
  • 1
    AFAIK, you can't customize caption buttons (for the moment at least). That area is a reserved area. The closest way that I can think of is to HIDE caption buttons by making your window Full Screen mode. – Andrew KeepCoding Aug 05 '22 at 05:46

1 Answers1

0

In my limited experience with WinUI 3, I cannot only remove the close button, however, it is possible to hide the whole title bar area.

MainWindow.xaml.cs

using Microsoft.UI;
using Microsoft.UI.Xaml;
using Microsoft.UI.Windowing;

namespace WinUI3
{
    public sealed partial class MainWindow : Window
    {
        public MainWindow()
        {
            this.InitializeComponent();

            GetAppWindowAndPresenter();
            _apw.IsShownInSwitchers = false;
            _presenter.SetBorderAndTitleBar(false, false);
        }

        private void myButton_Click(object sender, RoutedEventArgs e)
        {
            myButton.Content = "Clicked";
            this.Close();
        }

        public void GetAppWindowAndPresenter()
        {
            var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
            WindowId myWndId = Win32Interop.GetWindowIdFromWindow(hWnd);
            _apw = AppWindow.GetFromWindowId(myWndId);
            _presenter = _apw.Presenter as OverlappedPresenter;
        }
        private AppWindow _apw;
        private OverlappedPresenter _presenter;
    }
}

This piece of code will create a window without title bar (minimize button, maximize button and close button).

GeorgeLin
  • 61
  • 5