add custom titlebar (WinUI3) newapi

Thu, 12 Oct 2023 13:52:18 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 12 Oct 2023 13:52:18 +0200
branch
newapi
changeset 211
5d71a36b833b
parent 210
83556205edad
child 212
ad5c74af14c4

add custom titlebar (WinUI3)

make/vs/testapp/app.manifest file | annotate | diff | comparison | revisions
make/vs/testapp/main.c file | annotate | diff | comparison | revisions
make/vs/testapp/testapp.vcxproj file | annotate | diff | comparison | revisions
make/vs/testapp/testapp.vcxproj.filters file | annotate | diff | comparison | revisions
ui/winui/App.xaml file | annotate | diff | comparison | revisions
ui/winui/MainWindow.xaml file | annotate | diff | comparison | revisions
ui/winui/MainWindow.xaml.cpp file | annotate | diff | comparison | revisions
ui/winui/window.cpp file | annotate | diff | comparison | revisions
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/make/vs/testapp/app.manifest	Thu Oct 12 13:52:18 2023 +0200
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="utf-8"?>
+<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
+  <assemblyIdentity version="1.0.0.0" name="test.app"/>
+
+  <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
+    <application>
+      <!--The ID below informs the system that this application is compatible with OS features first introduced in Windows 8. 
+      For more info see https://docs.microsoft.com/windows/win32/sysinfo/targeting-your-application-at-windows-8-1 
+      
+      It is also necessary to support features in unpackaged applications, for example the custom titlebar implementation.-->
+      <supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" />
+    </application>
+  </compatibility>
+  
+  <application xmlns="urn:schemas-microsoft-com:asm.v3">
+    <windowsSettings>
+      <dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2</dpiAwareness>
+    </windowsSettings>
+  </application>
+</assembly>
\ No newline at end of file
--- a/make/vs/testapp/main.c	Thu Oct 12 13:51:44 2023 +0200
+++ b/make/vs/testapp/main.c	Thu Oct 12 13:52:18 2023 +0200
@@ -283,6 +283,10 @@
     ui_toolbar_menu("Menu", .label = "Menu") {
         
         ui_menuitem(.label = "x", NULL, NULL);
+        ui_menuitem(.label = "x", NULL, NULL);
+        ui_menuitem(.label = "x", NULL, NULL);
+        ui_menuitem(.label = "x", NULL, NULL);
+        ui_menuitem(.label = "x", NULL, NULL);
         ui_menu("TB Sub") {
             ui_menuitem("TB subitem", NULL, NULL);
         }
--- a/make/vs/testapp/testapp.vcxproj	Thu Oct 12 13:51:44 2023 +0200
+++ b/make/vs/testapp/testapp.vcxproj	Thu Oct 12 13:52:18 2023 +0200
@@ -151,6 +151,9 @@
       <Project>{27da0164-3475-43e2-a1a4-a5d07d305749}</Project>
     </ProjectReference>
   </ItemGroup>
+  <ItemGroup>
+    <Manifest Include="app.manifest" />
+  </ItemGroup>
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
   <ImportGroup Label="ExtensionTargets">
     <Import Project="..\packages\Microsoft.Windows.CppWinRT.2.0.230225.1\build\native\Microsoft.Windows.CppWinRT.targets" Condition="Exists('..\packages\Microsoft.Windows.CppWinRT.2.0.230225.1\build\native\Microsoft.Windows.CppWinRT.targets')" />
--- a/make/vs/testapp/testapp.vcxproj.filters	Thu Oct 12 13:51:44 2023 +0200
+++ b/make/vs/testapp/testapp.vcxproj.filters	Thu Oct 12 13:52:18 2023 +0200
@@ -25,4 +25,7 @@
   <ItemGroup>
     <Natvis Include="$(MSBuildThisFileDirectory)..\..\natvis\wil.natvis" />
   </ItemGroup>
+  <ItemGroup>
+    <Manifest Include="app.manifest" />
+  </ItemGroup>
 </Project>
\ No newline at end of file
--- a/ui/winui/App.xaml	Thu Oct 12 13:51:44 2023 +0200
+++ b/ui/winui/App.xaml	Thu Oct 12 13:52:18 2023 +0200
@@ -11,6 +11,8 @@
                 <!-- Other merged dictionaries here -->
             </ResourceDictionary.MergedDictionaries>
             <!-- Other app resources here -->
+            <SolidColorBrush x:Key="WindowCaptionBackground">Transparent</SolidColorBrush>
+            <SolidColorBrush x:Key="WindowCaptionBackgroundDisabled">Transparent</SolidColorBrush>
         </ResourceDictionary>
     </Application.Resources>
 </Application>
--- a/ui/winui/MainWindow.xaml	Thu Oct 12 13:51:44 2023 +0200
+++ b/ui/winui/MainWindow.xaml	Thu Oct 12 13:52:18 2023 +0200
@@ -9,11 +9,12 @@
     mc:Ignorable="d">
 
     <Window.SystemBackdrop>
-        <MicaBackdrop />
-
+        <MicaBackdrop />
     </Window.SystemBackdrop>
 
-    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
-        <Button x:Name="myButton" Click="myButton_Click">Click Me !</Button>
-    </StackPanel>
+    <Grid RowDefinitions="Auto, Auto" ColumnDefinitions="*"  Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
+        <StackPanel x:Name="AppTitleBar" Orientation="Horizontal" Grid.Column="0" Grid.Row="0" Padding="10,5,5,10">
+            <TextBlock x:Name="AppTitleTextBlock" Text="Window Title"></TextBlock>
+        </StackPanel>
+    </Grid>
 </Window>
--- a/ui/winui/MainWindow.xaml.cpp	Thu Oct 12 13:51:44 2023 +0200
+++ b/ui/winui/MainWindow.xaml.cpp	Thu Oct 12 13:52:18 2023 +0200
@@ -18,6 +18,8 @@
     MainWindow::MainWindow()
     {
         InitializeComponent();
+        //ExtendsContentIntoTitleBar(true);
+        //SetTitleBar(AppTitleBar());
     }
 
     int32_t MainWindow::MyProperty()
@@ -32,6 +34,6 @@
 
     void MainWindow::myButton_Click(IInspectable const&, RoutedEventArgs const&)
     {
-        myButton().Content(box_value(L"Clicked"));
+        
     }
 }
--- a/ui/winui/window.cpp	Thu Oct 12 13:51:44 2023 +0200
+++ b/ui/winui/window.cpp	Thu Oct 12 13:52:18 2023 +0200
@@ -62,7 +62,7 @@
 
 	//Window window = Window();
 	Window window = make<winui::implementation::MainWindow>();
-	//window.ExtendsContentIntoTitleBar(true);
+	window.ExtendsContentIntoTitleBar(true);
 	if (title) {
 		wchar_t *wtitle = str2wstr(title, nullptr);
 		window.Title(wtitle);
@@ -76,6 +76,16 @@
 	Grid grid = Grid();
 	window.Content(grid);
 
+	StackPanel titleBar = StackPanel();
+	Thickness titleBarPadding = { 10, 5, 5, 10 };
+	titleBar.Padding(titleBarPadding);
+	titleBar.Orientation(Orientation::Horizontal);
+	TextBlock titleLabel = TextBlock();
+	titleLabel.Text(hstring(L"Window Title"));
+	titleBar.Children().Append(titleLabel);
+
+	window.SetTitleBar(titleBar);
+
 	obj->wobj = new UiWindow(window);
 	
 
@@ -86,6 +96,9 @@
 
 	obj->container = new UiBoxContainer(grid, UI_BOX_CONTAINER_VBOX, 0, 0);
 
+	titleBar.VerticalAlignment(VerticalAlignment::Top);
+	obj->container->Add(titleBar, false);
+
 	if (uic_get_menu_list()) {
 		// create/add menubar
 		MenuBar mb = ui_create_menubar(obj);

mercurial