default transparent tite bar, when create a new window

Eduardo Gomez 3,671 Reputation points
2023-06-28T11:04:16.9766667+00:00

I am using depency injection, to create the man window

  public partial class App : Application {

        private IHost _host;

        public App() {
            InitializeComponent();

        }

        protected override void OnLaunched(LaunchActivatedEventArgs args) {
            _host = Host.CreateDefaultBuilder()
                .ConfigureServices(ConfigureServices)
                .Build();

            // Start the host
            _host.Start();

            // Resolve and activate the main window using dependency injection
            var mainWindow = _host.Services.GetRequiredService<NavWindow>();
            mainWindow.Activate();

        }

        private void ConfigureServices(IServiceCollection services) {
            // Register your dependencies here
            services.AddSingleton<NavWindowViewModel>();
            services.AddSingleton(provider => new NavWindow(provider.GetRequiredService<NavWindowViewModel>()));

            services.AddSingleton<SubjectsPageViewModel>();

            // Add other services as needed
        }
    }
}


What I want to do is that every time I create a new Wndows, it will have a tranparent tiitle bar.

I found this

https://learn.microsoft.com/en-us/windows/apps/develop/title-bar?msclkid=e23d7633cf9311eca704e47ee540cf97&tabs=wasdk#simple-customization

and I implemented it but I get

User's image

I want something like

User's image

I dont want to do this in every window

     private AppWindow m_AppWindow;

        public NavWindowViewModel ViewModel { get; set; }

        public NavWindow(NavWindowViewModel viewModel) {
            InitializeComponent();
            ViewModel = viewModel;
            ExtendsContentIntoTitleBar = true;
            SetTitleBarColors();

            // Subscribe to the message
            WeakReferenceMessenger.Default.Register<NavigationViewItem>(this,
                HandleSelectedItemMessage);
        }

        private void HandleSelectedItemMessage(object recipient, NavigationViewItem message) {
            if (message != null) {

                var tag = message.Tag.ToString();
                NavFrame.Navigate(PageTypeMapper.PageTypeDictionary[tag]);
            }
        }

        private bool SetTitleBarColors() {
            // Check to see if customization is supported.
            // The method returns true on Windows 10 since Windows App SDK 1.2, and on all versions of
            // Windows App SDK on Windows 11.
            if (AppWindowTitleBar.IsCustomizationSupported()) {
                if (m_AppWindow is null) {
                    m_AppWindow = GetAppWindowForCurrentWindow();
                }
                var titleBar = m_AppWindow.TitleBar;

                // Set active window colors
                // Note: No effect when app is running on Windows 10 since color customization is not
                // supported.
                titleBar.ForegroundColor = Colors.Transparent;
                titleBar.ButtonForegroundColor = Colors.Transparent;
                titleBar.ButtonBackgroundColor = Colors.Transparent;
                titleBar.ButtonHoverForegroundColor = Colors.Transparent;
                titleBar.ButtonHoverBackgroundColor = Colors.DarkSeaGreen;
                titleBar.ButtonPressedForegroundColor = Colors.Gray;
                titleBar.ButtonPressedBackgroundColor = Colors.LightGreen;

                // Set inactive window colors
                // Note: No effect when app is running on Windows 10 since color customization is not
                // supported.
                titleBar.InactiveForegroundColor = Colors.Gainsboro;
                titleBar.InactiveBackgroundColor = Colors.SeaGreen;
                titleBar.ButtonInactiveForegroundColor = Colors.Gainsboro;
                titleBar.ButtonInactiveBackgroundColor = Colors.SeaGreen;
                return true;
            }
            return false;
        }

        private AppWindow GetAppWindowForCurrentWindow() {
            IntPtr hWnd = WindowNative.GetWindowHandle(this);
            WindowId wndId = Win32Interop.GetWindowIdFromWindow(hWnd);
            return AppWindow.GetFromWindowId(wndId);
        }
    }
}

The problem with this approach is that is for a specific Window, what happends if I create a new one?

Also it overlaps my navView

Code: https://github.com/eduardoagr/Demy-AI/tree/master/Demy

Windows development | Windows App SDK
{count} votes

1 answer

Sort by: Most helpful
  1. Jeanine Zhang-MSFT 11,356 Reputation points Microsoft External Staff
    2023-06-29T08:24:11.27+00:00

    Hello,

    Welcome to Microsoft Q&A!

    There is no way to make every window have a transparent title bar. You must set the transparent title bar separately for every window.

    Thank you.

    Jeanine


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.