标题栏自定义

您所在的位置:网站首页 按钮的定义 标题栏自定义

标题栏自定义

2023-10-14 18:25| 来源: 网络整理| 查看: 265

标题栏自定义 项目 06/02/2023

Windows 为每个窗口提供默认标题栏,并允许你自定义它以匹配应用的个性。 默认标题栏附带一些标准组件和核心功能,例如拖动窗口和调整窗口大小。

有关自定义应用的标题栏、可接受的标题 栏 区域内容和建议的 UI 模式的指导,请参阅标题栏设计文章。

请参阅标题栏Windows 11 Fluent 设计指南

标题栏组件

此列表描述标准标题栏的组件。

标题栏矩形 标题文本 UWP) 以外的系统图标 ( 系统菜单 - 通过单击应用图标或右键单击标题栏进行访问 字幕控件 “最小化”按钮 最大化/还原按钮 “关闭”按钮 平台选项

标题栏的确切功能和可用于自定义它的选项取决于你的 UI 平台和应用要求。 本文介绍如何为使用 Windows 应用 SDK、WinUI 3 或 UWP 和 WinUI 2 的应用自定义标题栏。

注意

有关Windows 应用 SDK和 UWP 使用的开窗模型的详细比较,请参阅开窗功能迁移。

Windows 应用 SDK WinUI 3 UWP/WinUI 2 适用范围:Windows 应用 SDK 重要 API: AppWindow.TitleBar 属性、 AppWindowTitleBar 类、 AppWindow 类

Windows 应用 SDK中的开窗功能是通过基于 Win32 HWND 模型的 Microsoft.UI.Windowing.AppWindow 类实现的。 AppWindow 与应用中的顶级 HWND 之间存在 1:1 映射。 AppWindow 及其相关类提供 API,让你能够管理应用的顶级窗口的许多方面,包括标题栏的自定义。 你可以修改 Windows 提供的默认标题栏,使其与 UI 的其余部分混合,或者将应用画布扩展到标题栏区域并提供你自己的标题栏内容。

重要

对标题栏自定义 API 的支持因不同版本的 Windows 和不同版本的Windows 应用 SDK而异。 下表介绍了详细信息。

自定义选项 Windows 10 Windows 11 简单自定义 部分由于 Windows 应用 SDK 1.2 (不支持颜色自定义) 是,所有版本的 Windows 应用 SDK 完全自定义 是,自 Windows 应用 SDK 1.2 起 是,所有版本的 Windows 应用 SDK

有关自 Windows 应用 SDK 1.2 以来Windows 10支持哪些 API 的信息,请参阅“窗口化”部分下的Windows 应用 SDK发行说明页了解详细信息。

建议在调用这些 API 之前,在代码中检查 AppWindowTitleBar.IsCustomizationSupported,以确保你的应用不会在其他版本的 Windows 上崩溃。

对于使用 WinUI 3 的 XAML 应用,XAML 窗口 API 提供了一种更简单的方式来自定义同样适用于Windows 10的标题栏。 这些 API 可与 Windows 应用 SDK API 结合使用, (请参阅 WinUI 3 选项卡) 。

如何使用 AppWindow

可以将 AppWindow API 与Windows 应用 SDK支持的任何 UI 框架(Win32、WPF、WinForms 或 WinUI 3)配合使用,并且可以仅使用所需的 API 以增量方式采用它们。 使用互操作 API 从现有窗口获取 AppWindow 对象。 使用此 AppWindow 对象,可以访问标题栏自定义 API。 有关互操作 API 的详细信息,请参阅 管理应用窗口 - UI 框架和 HWND 互操作 以及 窗口库示例。

适用于:WinUI 3 桌面应用 重要 API: Microsoft.UI.Xaml.Window 类、 Window.ExtendsContentIntoTitleBar 属性、 Window.SetTitleBar 方法

WinUI 3 中的开窗功能是通过基于 Win32 HWND 模型的 Microsoft.UI.Xaml.Window 类实现的。 Window 类包含 API,可用于将标准标题栏替换为自己的自定义内容。

WinUI 3 也是Windows 应用 SDK的一部分,因此 Window 类和 AppWindow 类都可用于自定义标题栏。 可以将 XAML 窗口的窗口句柄传递给 AppWindow 对象,并将 AppWindow 功能与窗口 API 结合使用 (查看Windows 应用 SDK选项卡) 。

下表描述了 Window 和 AppWindow 之间的差异。

功能 Window 类 AppWindow 类 Windows 10 支持 是 部分,由于 Windows 应用 SDK 1.2 () ,请参阅“Windows 应用 SDK”选项卡。 简单自定义 标题 标题、颜色、图标和系统菜单 替换系统标题栏 Window.ExtendsContentIntoTitleBar AppWindowTitleBar.ExtendsContentIntoTitleBar 设置标题栏内容 在 XAML UIElement 中定义标题栏,然后 (UIElement) 调用 SetTitleBar。 编写自定义代码来计算和设置拖动矩形,包括窗口大小更改的时间。 设置描述文字按钮颜色 否 是 标题按钮大小信息 不适用 RightInset 和 LeftInset 属性 适用于:UWP/WinUI 2 重要 API:ApplicationView.TitleBar 属性、ApplicationViewTitleBar 类、CoreApplicationViewTitleBar 类

在 UWP 应用程序中,可以使用 ApplicationView 和 CoreApplicationView 类的成员自定义标题栏。 有多个 API 可以根据所需的自定义级别逐步修改标题栏的外观。

注意

用于 UWP 应用中辅助窗口的 Windows.UI.WindowManagement.AppWindow 类不支持标题栏自定义。 若要自定义使用辅助窗口的 UWP 应用的标题栏,请使用 ApplicationView,如 使用 ApplicationView 显示多个视图中所述。

如果你正在考虑将 UWP 应用迁移到Windows 应用 SDK,请查看我们的开窗功能迁移指南。 有关详细信息 ,请参阅开窗功能迁移 。

标题栏的自定义程度

可以向标题栏应用两个级别的自定义:对默认标题栏应用次要修改,或将应用画布扩展到标题栏区域并提供完全自定义的内容。

简单

简单自定义仅适用于 Windows 应用 SDK 和 UWP/WinUI 2。

对于简单的自定义(例如更改标题栏颜色),可以在应用窗口的标题栏对象上设置属性,以指定要用于标题栏元素的颜色。 在这种情况下,系统仍负责标题栏的所有其他方面,例如绘制应用标题和定义拖动区域。

完整

另一个选项是隐藏默认标题栏并将其替换为你自己的自定义内容。 例如,可以在标题栏区域中放置文本、搜索框或自定义菜单。 还需要使用此选项将 材料 背景(如 Mica)扩展到标题栏区域。

当你选择完全自定义时,你负责将内容放入标题栏区域,并且可以定义自己的拖动区域。 描述文字控件 (系统“关闭”、“最小化”和“最大化”按钮) 仍可供系统使用和处理,但应用标题等元素则不可用。 你将需要根据应用的需要自行创建这些元素。

简单自定义

如果只想自定义标题栏颜色或图标,可以在应用窗口的标题栏对象上设置属性。

Windows 应用 SDK WinUI 3 UWP/WinUI 2

(自 Windows 应用 SDK 1.2 起在 Windows 10 上部分受支持,在 Windows 11 上完全受支持。有关详细信息,请参阅平台选项。)

这些示例演示如何获取 AppWindow 的实例并设置其属性。

标题

默认情况下,标题栏将应用类型显示为窗口标题 (例如,“WinUI Desktop”) 。 若要更改窗口标题,请将 AppWindow.Title 属性设置为单行文本值。

using Microsoft.UI; // Needed for WindowId. using Microsoft.UI.Windowing; // Needed for AppWindow. using WinRT.Interop; // Needed for XAML/HWND interop. private AppWindow m_AppWindow; public MainWindow() { this.InitializeComponent(); m_AppWindow = GetAppWindowForCurrentWindow(); m_AppWindow.Title = "App title"; } private AppWindow GetAppWindowForCurrentWindow() { IntPtr hWnd = WindowNative.GetWindowHandle(this); WindowId wndId = Win32Interop.GetWindowIdFromWindow(hWnd); return AppWindow.GetFromWindowId(wndId); } 颜色

此示例演示如何获取 AppWindowTitleBar 的实例并设置其颜色属性。

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.White; titleBar.BackgroundColor = Colors.Green; titleBar.ButtonForegroundColor = Colors.White; titleBar.ButtonBackgroundColor = Colors.SeaGreen; titleBar.ButtonHoverForegroundColor = Colors.Gainsboro; 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; } 图标和系统菜单

还可以隐藏系统图标,或将其替换为自定义图标。 右键单击或点击一次时,系统图标会显示系统菜单。 双击/点击时会关闭窗口。

若要显示或隐藏系统图标和关联行为,请设置标题栏 IconShowOptions 属性。

titleBar.IconShowOptions = IconShowOptions.HideIconAndSystemMenu;

注意

枚举 IconShowOptions 允许在将来的版本中添加其他选项。 如果对此感兴趣,可以向我们提供有关 GitHub 上Windows 应用 SDK存储库的反馈。

若要使用自定义窗口图标,请调用 AppWindow.SetIcon 方法之一来设置新图标。

SetIcon(String)

SetIcon (String) 方法目前仅适用于 .ico 文件。 传递给此方法的字符串是 .ico 文件的完全限定路径。

m_AppWindow.SetIcon("iconPath/iconName.ico");

SetIcon(IconId)

如果已有图标 (HICON 从某个 Icon 函数 (如 CreateIcon)) 的句柄,则可以使用 GetIconIdFromIcon 互操作 API 获取 IconId。 然后,可以将 传递给 IconIdSetIcon (IconId) 方法来设置窗口图标。

m_AppWindow.SetIcon(iconId)); 标题

默认情况下,标题栏将应用的显示名称显示为窗口标题。 显示名称在 Package.appxmanifest 文件中设置。

若要将标题替换为自定义文本,请将 Window.Title 属性设置为文本值,如下所示。

public MainWindow() { InitializeComponent(); Title = "App title"; } ...

注意

若要向默认标题栏添加颜色或更改 WinUI 3 窗口附带的窗口图标,需要使用 Windows 应用 SDK AppWindow API 或选择完全自定义标题栏。

标题

默认情况下,标题栏将应用的显示名称显示为窗口标题。 显示名称在 Package.appxmanifest 文件中设置。

若要向标题添加自定义文本,请将 ApplicationView.Title 属性设置为文本值,如下所示。

public MainPage() { this.InitializeComponent(); ApplicationView.GetForCurrentView().Title = "Custom text"; }

文本将追加到窗口标题前面,该标题将显示为“自定义文本 - 应用显示名称”。 若要显示不带应用显示名称的自定义标题,必须替换默认标题栏,如 完全自定义 部分所示。

颜色

此示例演示如何获取 ApplicationViewTitleBar 的实例并设置其颜色属性。

此代码可放在应用的 OnLaunched 方法 (App.xaml.cs) 中、对 Window.Activate 的调用的后面,或应用的第一页中。

// using Windows.UI; // using Windows.UI.ViewManagement; var titleBar = ApplicationView.GetForCurrentView().TitleBar; // Set active window colors titleBar.ForegroundColor = Colors.White; titleBar.BackgroundColor = Colors.Green; titleBar.ButtonForegroundColor = Colors.White; titleBar.ButtonBackgroundColor = Colors.SeaGreen; titleBar.ButtonHoverForegroundColor = Colors.White; titleBar.ButtonHoverBackgroundColor = Colors.DarkSeaGreen; titleBar.ButtonPressedForegroundColor = Colors.Gray; titleBar.ButtonPressedBackgroundColor = Colors.LightGreen; // Set inactive window colors titleBar.InactiveForegroundColor = Colors.Gainsboro; titleBar.InactiveBackgroundColor = Colors.SeaGreen; titleBar.ButtonInactiveForegroundColor = Colors.Gainsboro; titleBar.ButtonInactiveBackgroundColor = Colors.SeaGreen;

设置标题栏颜色时需要注意以下几点:

按钮背景色不适用于关闭按钮 悬停 和 按下 状态。 关闭按钮始终对这些状态使用系统定义的颜色。 将颜色属性设置为 null 可将其重置为默认系统颜色。 你无法设置透明色。 颜色的 alpha 通道会被忽略。

Windows 为用户提供了将选定的主题色应用于标题栏的选项。 如果你要设置任何标题栏颜色,那么我们建议你显式设置所有颜色。 这可以确保不存在因用户定义的颜色设置而出现的意外颜色组合。

完全自定义

当你选择进行标题栏完全自定义时,应用的客户端区域会进行扩展以覆盖整个窗口,包括标题栏区域。 你负责整个窗口的绘制和输入处理,描述文字按钮除外,这些按钮仍由窗口提供。

若要隐藏默认标题栏并将内容扩展到标题栏区域,请将将应用内容扩展到标题栏区域的 属性设置为 true。 在 XAML 应用中,可以在应用的 OnLaunched 方法 (App.xaml.cs) 或应用的第一页中设置此属性。

提示

请参阅 完全自定义示例 部分,一次查看所有代码。

Windows 应用 SDK WinUI 3 UWP/WinUI 2

(自 Windows 应用 SDK 1.2 起在 Windows 10 上受支持,在 Windows 11 上完全受支持。有关详细信息,请参阅平台选项。)

此示例演示如何获取 AppWindowTitleBar 并将 ExtendsContentIntoTitleBar 属性设置为 true。

重要

应用可能运行的所有 Windows 版本都不支持标题栏自定义 API,因此,在调用这些 API 之前,请务必在代码中检查 AppWindowTitleBar.IsCustomizationSupported。 如果不支持标题栏自定义,则通常会通过将 设置为 VisibilityCollapsed来隐藏自定义标题栏 UI。

using Microsoft.UI; // Needed for WindowId using Microsoft.UI.Windowing; // Needed for AppWindow using WinRT.Interop; // Needed for XAML/HWND interop private AppWindow m_AppWindow; public MainWindow() { this.InitializeComponent(); m_AppWindow = GetAppWindowForCurrentWindow(); // 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()) { var titleBar = m_AppWindow.TitleBar; // Hide default title bar. titleBar.ExtendsContentIntoTitleBar = true; } else { // In the case that title bar customization is not supported, hide the custom title bar // element. AppTitleBar.Visibility = Visibility.Collapsed; } } private AppWindow GetAppWindowForCurrentWindow() { IntPtr hWnd = WindowNative.GetWindowHandle(this); WindowId wndId = Win32Interop.GetWindowIdFromWindow(hWnd); return AppWindow.GetFromWindowId(wndId); }

此示例演示如何将 Window.ExtendsContentIntoTitleBar 属性设置为 true。

public MainWindow() { this.InitializeComponent(); // Hide default title bar. ExtendsContentIntoTitleBar = true; }

注意

ExtendsContentIntoTitleBar 显示在 的 XAML IntelliSense 中 Window,但在 XAML 中设置它会导致错误。 改为在代码中设置此属性。

警告

如果在未指定拖动区域的情况下设置 ExtendsContentIntoTitleBar=true ,则应用程序SetTitleBarUIElement将创建仅包含最小拖动区域和描述文字控件的回退标题栏。

此示例演示如何获取 CoreApplicationViewTitleBar 并将 ExtendViewIntoTitleBar 属性设置为 true。

using Windows.ApplicationModel.Core; public MainPage() { this.InitializeComponent(); // Hide default title bar. var coreTitleBar = CoreApplication.GetCurrentView().TitleBar; coreTitleBar.ExtendViewIntoTitleBar = true; }

提示

当应用关闭并重启后,系统会保留此设置。 在 Visual Studio 中,如果设置为 ExtendViewIntoTitleBartrue,然后想要还原为默认值,则应将其显式设置为 false 并运行应用以覆盖持久化设置。

标题栏内容和拖动区域

当你的应用扩展到标题栏区域时,你负责定义和管理标题栏的 UI。 这通常至少包括指定标题文本和拖动区域。 标题栏的拖动区域定义用户可以单击和拖动以移动窗口的位置。 这也是用户右键单击以显示系统菜单的位置。

若要详细了解可接受的标题栏内容和建议的 UI 模式,请参阅 标题栏设计。

Windows 应用 SDK WinUI 3 UWP/WinUI 2

(自 Windows 应用 SDK 1.2 起在 Windows 10 上受支持,在 Windows 11 上完全受支持。有关详细信息,请参阅平台选项。)

将内容扩展到标题栏区域时,系统默认保留整个标题栏区域(描述文字按钮除外)作为拖动区域。 如果不在标题栏中放置交互式内容,则可以将此默认拖动区域保留原样。 如果将交互式内容放置在标题栏中,则需要指定拖动区域,我们将在下一部分进行介绍。

此示例演示没有交互式内容的自定义标题栏 UI 的 XAML。

重要

LeftPaddingColumn和 RightPaddingColumn 用于为描述文字按钮保留空间。 这些 Width 列的值在代码中设置,稍后会显示。 有关代码和说明,请参阅系统描述文字按钮部分。

可以通过调用 Window.SetTitleBar 方法并传入定义拖动区域的 UIElement 来指定拖动区域。 (通常是 UIElement 一个包含其他元素的面板。) ExtendsContentIntoTitleBar 必须将 属性设置为 true ,才能使 对 SetTitleBar 的调用生效。

下面介绍如何将内容的 设置为 Grid 可拖动的标题栏区域。 此代码会写入应用第一页的 XAML 和代码隐藏部分中。

public MainWindow() { this.InitializeComponent(); ExtendsContentIntoTitleBar = true; SetTitleBar(AppTitleBar); }

默认情况下,系统标题栏将应用的显示名称显示为窗口标题。 显示名称在 Package.appxmanifest 文件中设置。 可以获取此值,并在自定义标题栏中使用它,如下所示。

AppTitleTextBlock.Text = AppInfo.Current.DisplayInfo.DisplayName;

可以通过调用 Window.SetTitleBar 方法并传入定义拖动区域的 UIElement 来指定拖动区域。 (通常是 UIElement 包含其他元素的面板。) ExtendViewIntoTitleBar 必须将 属性设置为 true ,才能使对 的调用 SetTitleBar 产生任何效果。

下面介绍如何将内容设置为 Grid 可拖动的标题栏区域。 此代码会写入应用第一页的 XAML 和代码隐藏部分中。

public MainPage() { this.InitializeComponent(); var coreTitleBar = CoreApplication.GetCurrentView().TitleBar; coreTitleBar.ExtendViewIntoTitleBar = true; // Set XAML element as a drag region. Window.Current.SetTitleBar(AppTitleBar); }

默认情况下,系统标题栏将应用的显示名称显示为窗口标题。 显示名称在 Package.appxmanifest 文件中设置。 可以获取此值,并在自定义标题栏中使用它,如下所示。

AppTitleTextBlock.Text = AppInfo.Current.DisplayInfo.DisplayName;

重要

指定的拖动区域需要命中可测试。 默认情况下,某些 UI 元素(如 Grid)在未设置背景时不参与命中测试。 这意味着,对于某些元素,可能需要设置透明背景画笔。 有关详细信息,请参阅关于 VisualTreeHelper.FindElementsInHostCoordinates 的备注。

例如,如果将网格定义为拖动区域,请将其设置为 Background="Transparent" 可拖动。

此网格不是拖动 (但其中可见元素) : 。

此网格看上去相同,但是整个网格可以拖动:。

交互式内容

你可以在应用顶部放置交互式控件,如按钮、菜单或搜索框,以使它们显示在标题栏中。 但是,必须遵循一些规则来确保交互式元素接收用户输入,同时仍允许用户移动窗口。

Windows 应用 SDK WinUI 3 UWP/WinUI 2

(自 Windows 应用 SDK 1.2 起在 Windows 10 上受支持,Windows 11 完全支持。有关详细信息,请参阅平台选项。)

如果在标题栏区域中添加交互式内容,则应在该内容周围定义显式拖动区域,以便用户可以与之交互。 设置自定义拖动区域后,将删除默认拖动区域,并且系统不会保留任何必需的拖动区域。 你负责确保标题栏中有足够的空间供用户移动窗口。

若要设置拖动区域,请调用 AppWindowTitleBar.SetDragRectangles 方法。 此方法采用一个矩形数组,其中每个矩形定义一个拖动区域。 当标题栏的大小发生更改时,需要重新计算拖动区域以匹配新大小,并使用新值调用 SetDragRectangles 。

如果运行自定义标题栏的系统上不受支持,则不会显示该标题栏。 应该为放置在自定义标题栏中的任何功能提供替代 UI。

此示例演示带有搜索框的自定义标题栏 UI,并演示如何计算和设置搜索框两侧的拖动矩形。 下面是代码中要注意的一些要点。

将 AppTitleBar 网格高度设置为 48 以遵循交互式内容的 标题栏 设计指南。 若要更轻松地计算拖动矩形,请使用 Grid 具有多个命名列的 布局。 在 MainWindow 构造函数中设置为 ExtendsContentIntoTitleBartrue 。 如果在稍后调用的代码中设置它,则默认系统标题栏可能会先显示,然后隐藏。 在元素加载AppTitleBar_Loaded () 后AppTitleBar,进行初始调用以计算拖动区域。 否则,无法保证用于计算的元素具有其正确的值。 仅当元素 () 更改大小AppTitleBar_SizeChanged后AppTitleBar,才更新拖动矩形计算。 如果依赖于窗口 Changed 事件,则会出现窗口最大化/最小化) 等 (情况,即在调整大小之前 AppTitleBar 发生事件,并且计算将使用不正确的值。 仅在检查IsCustomizationSupported并ExtendsContentIntoTitleBar确认支持和使用自定义标题栏后调用 SetDragRectangles 。 using System.Runtime.InteropServices; private AppWindow m_AppWindow; public MainWindow() { this.InitializeComponent(); m_AppWindow = GetAppWindowForCurrentWindow(); // 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()) { var titleBar = m_AppWindow.TitleBar; titleBar.ExtendsContentIntoTitleBar = true; AppTitleBar.Loaded += AppTitleBar_Loaded; AppTitleBar.SizeChanged += AppTitleBar_SizeChanged; } else { // In the case that title bar customization is not supported, hide the custom title bar // element. AppTitleBar.Visibility = Visibility.Collapsed; // Show alternative UI for any functionality in // the title bar, such as search. } } private void AppTitleBar_Loaded(object sender, RoutedEventArgs e) { // 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()) { SetDragRegionForCustomTitleBar(m_AppWindow); } } private void AppTitleBar_SizeChanged(object sender, SizeChangedEventArgs e) { // 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() && m_AppWindow.TitleBar.ExtendsContentIntoTitleBar) { // Update drag region if the size of the title bar changes. SetDragRegionForCustomTitleBar(m_AppWindow); } } private AppWindow GetAppWindowForCurrentWindow() { IntPtr hWnd = WindowNative.GetWindowHandle(this); WindowId wndId = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(hWnd); return AppWindow.GetFromWindowId(wndId); } [DllImport("Shcore.dll", SetLastError = true)] internal static extern int GetDpiForMonitor(IntPtr hmonitor, Monitor_DPI_Type dpiType, out uint dpiX, out uint dpiY); internal enum Monitor_DPI_Type : int { MDT_Effective_DPI = 0, MDT_Angular_DPI = 1, MDT_Raw_DPI = 2, MDT_Default = MDT_Effective_DPI } private double GetScaleAdjustment() { IntPtr hWnd = WindowNative.GetWindowHandle(this); WindowId wndId = Win32Interop.GetWindowIdFromWindow(hWnd); DisplayArea displayArea = DisplayArea.GetFromWindowId(wndId, DisplayAreaFallback.Primary); IntPtr hMonitor = Win32Interop.GetMonitorFromDisplayId(displayArea.DisplayId); // Get DPI. int result = GetDpiForMonitor(hMonitor, Monitor_DPI_Type.MDT_Default, out uint dpiX, out uint _); if (result != 0) { throw new Exception("Could not get DPI for monitor."); } uint scaleFactorPercent = (uint)(((long)dpiX * 100 + (96 >> 1)) / 96); return scaleFactorPercent / 100.0; } private void SetDragRegionForCustomTitleBar(AppWindow appWindow) { // 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() && appWindow.TitleBar.ExtendsContentIntoTitleBar) { double scaleAdjustment = GetScaleAdjustment(); RightPaddingColumn.Width = new GridLength(appWindow.TitleBar.RightInset / scaleAdjustment); LeftPaddingColumn.Width = new GridLength(appWindow.TitleBar.LeftInset / scaleAdjustment); List dragRectsList = new(); Windows.Graphics.RectInt32 dragRectL; dragRectL.X = (int)((LeftPaddingColumn.ActualWidth) * scaleAdjustment); dragRectL.Y = 0; dragRectL.Height = (int)(AppTitleBar.ActualHeight * scaleAdjustment); dragRectL.Width = (int)((IconColumn.ActualWidth + TitleColumn.ActualWidth + LeftDragColumn.ActualWidth) * scaleAdjustment); dragRectsList.Add(dragRectL); Windows.Graphics.RectInt32 dragRectR; dragRectR.X = (int)((LeftPaddingColumn.ActualWidth + IconColumn.ActualWidth + TitleTextBlock.ActualWidth + LeftDragColumn.ActualWidth + SearchColumn.ActualWidth) * scaleAdjustment); dragRectR.Y = 0; dragRectR.Height = (int)(AppTitleBar.ActualHeight * scaleAdjustment); dragRectR.Width = (int)(RightDragColumn.ActualWidth * scaleAdjustment); dragRectsList.Add(dragRectR); Windows.Graphics.RectInt32[] dragRects = dragRectsList.ToArray(); appWindow.TitleBar.SetDragRectangles(dragRects); } }

警告

AppWindow 使用物理像素与不使用逻辑坐标的 UI 框架兼容。 如果使用 WPF 或 WinUI 3,RightInsetLeftInset则 如果显示比例不为 100%,则需要调整传递给 SetDragRectangles 的值。 在此示例中,我们计算一个 scaleAdjustment 值来解释显示比例设置。

对于 WPF,可以处理 Window.DpiChanged 事件以获取 NewDpi 值。

对于 WinUI 3,请使用 平台调用 (P/Invoke) 调用本机 GetDpiForMonitor 函数,如前面的示例所示。

提示

可以获取系统 TitleBar (int titleBarHeight = appWindow.TitleBar.Height;) 的高度,并使用它来设置自定义标题栏和拖动区域的高度。 但是,如果添加其他控件, 设计指南 建议将标题栏高度设置为 48px。 在这种情况下,系统标题栏的高度与内容不匹配,因此请改用标题栏元素的 ActualHeight 来设置拖动区域高度。

传递给 SetTitleBar 的元素支持与标准标题栏相同的系统交互,包括拖动、双击调整大小以及右键单击以显示窗口上下文菜单。 因此,所有指针输入 (鼠标、触摸、笔等) 都由系统处理。 它不再被标题栏元素及其子元素识别。 标题栏元素占用的矩形区域用作指针的标题栏,即使元素被另一个元素阻止,或者元素是透明的。 但是,键盘输入被识别,子元素可以接收键盘焦点。

这意味着不能与标题栏区域中的元素交互,除非通过键盘输入和焦点。 我们不建议这样做,因为它会带来可发现性和辅助功能问题。

必须调用 SetTitleBar 才能将区域定义为可拖动标题栏区域。 否则,系统会在页面顶部设置默认拖动区域。 然后,系统将处理此区域的所有用户输入,并阻止输入到达你的控件。 将交互式控件放置在由调用 SetTitleBar (定义的拖动区域的顶部,) 具有更高的 z 顺序。 不要使传递给 SetTitleBar的 UIElement 的交互式控件的子级。 将元素 SetTitleBar传递给 后,系统会将其视为系统标题栏,并处理指向该元素的所有指针输入。

此处, AutoSuggestBox 元素的 z 顺序高于 AppTitleBar,因此它接收用户输入。

系统标题按钮 Windows 应用 SDK WinUI 3 UWP/WinUI 2

(自 Windows 应用 SDK 1.2 起在 Windows 10 上受支持,Windows 11 完全支持。有关详细信息,请参阅平台选项。)

系统保留应用窗口左上角或右上角的系统描述文字按钮, (最小化、最大化/还原、关闭) 。 系统保留对描述文字按钮区域的控制,以确保为拖动、最小化、最大化和关闭窗口提供最少的功能。 系统在右上方为从左到右的语言绘制“关闭”按钮,在左上方为从右到左的语言绘制“关闭”按钮。

可以在描述文字控件区域下绘制内容,例如应用背景,但不应放置任何你希望用户能够与之交互的 UI。 它不会接收任何输入,因为标题控件的输入由系统进行处理。

上一示例中的这些行显示了 XAML 中定义标题栏的填充列。 使用填充列而不是边距可确保背景绘制描述文字控件按钮下的区域, (透明按钮) 。 使用右填充列和左填充列可确保标题栏在从右到左和从左到右布局中的行为正确。

描述文字控件区域的尺寸和位置由 AppWindowTitleBar 类传达,以便你可以在标题栏 UI 的布局中说明它。 每一侧保留区域的宽度由 LeftInset 或 RightInset 属性提供,其高度由 Height 属性提供。

下面介绍如何在计算和设置拖动区域时指定填充列的宽度。

// Get caption button occlusion information. int CaptionButtonOcclusionWidthRight = appWindow.TitleBar.RightInset; int CaptionButtonOcclusionWidthLeft = appWindow.TitleBar.LeftInset; // Set the width of padding columns in the UI. RightPaddingColumn.Width = new GridLength(CaptionButtonOcclusionWidthRight); LeftPaddingColumn.Width = new GridLength(CaptionButtonOcclusionWidthLeft);

重要

请参阅 交互式内容 部分中的重要信息,了解显示缩放如何影响这些值。

高标题栏支持自定义标题栏

在标题栏中添加搜索框或人物图片等交互式内容时,建议增加标题栏的高度,为这些元素提供更多空间。 较高的标题栏还可以简化触摸操作。 AppWindowTitleBar.PreferredHeightOption 属性提供将标题栏高度从默认标准高度增加到更高高度的选项。 选择Tall标题栏模式时,系统会在工作区中作为覆盖绘制的描述文字按钮呈现得更高,其最小/最大/关闭字形居中。 如果尚未指定拖动区域,系统将绘制一个拖动区域,该区域扩展窗口的宽度和高度由 PreferredHeightOption 你设置的值确定。

AppWindowTitleBar.ExtendsContentIntoTitleBar 属性必须使true属性PreferredHeightOption生效。 如果将 before 设置ExtendsContentIntoTitlebar设置为 PreferredHeightOptiontrue,则会以无提示方式忽略属性,直到设置为 ExtendsContentIntoTitlebartrue,此时它生效。

此示例演示如何设置 PreferredHeightOption 属性。

bool isTallTitleBar = true; // A taller title bar is only supported when drawing a fully custom title bar if (AppWindowTitleBar.IsCustomizationSupported() && m_AppWindow.TitleBar.ExtendsContentIntoTitleBar) { if (isTallTitleBar) { // Choose a tall title bar to provide more room for interactive elements // like search box or person picture controls. m_AppWindow.TitleBar.PreferredHeightOption = TitleBarHeightOption.Tall; } else { _mainAppWindow.TitleBar.PreferredHeightOption = TitleBarHeightOption.Standard; } // Recalculate the drag region for the custom title bar // if you explicitly defined new draggable areas. SetDragRegionForCustomTitleBar(_m_AppWindow); }

系统保留应用窗口左上角或右上角的系统描述文字按钮, (最小化、最大化/还原、关闭) 。 系统保留对描述文字按钮区域的控制,以确保为拖动、最小化、最大化和关闭窗口提供最少的功能。 系统在右上方为从左到右的语言绘制“关闭”按钮,在左上方为从右到左的语言绘制“关闭”按钮。

系统保留应用窗口左上角或右上角的系统描述文字按钮, (最小化、最大化/还原、关闭) 。 系统保留对描述文字按钮区域的控制,以确保为拖动、最小化、最大化和关闭窗口提供最少的功能。 系统在右上方为从左到右的语言绘制“关闭”按钮,在左上方为从右到左的语言绘制“关闭”按钮。

可以在描述文字控件区域下绘制内容,例如应用背景,但不应放置任何你希望用户能够与之交互的 UI。 它不会接收任何输入,因为标题控件的输入由系统进行处理。

上一示例中的这些行显示了 XAML 中定义标题栏的填充列。 使用填充列而不是边距可确保背景绘制描述文字控件按钮下的区域, (透明按钮) 。 使用右填充列和左填充列可确保标题栏在从右到左和从左到右布局中的行为正确。

描述文字控件区域的尺寸和位置由 CoreApplicationViewTitleBar 类传达,以便你可以在标题栏 UI 的布局中说明它。 每一侧上的保留区域的宽度由 SystemOverlayLeftInset 或 SystemOverlayRightInset 属性提供,其高度由 Height 属性提供。

你可以处理 LayoutMetricsChanged 事件,以响应标题按钮的大小变化。 例如,如果应用布局从左到右从右到右到左更改,则可能会发生这种情况。 处理此事件可验证并更新取决于标题栏大小的 UI 元素的定位。

此示例演示如何调整标题栏的布局,以考虑标题栏指标中的更改。 AppTitleBar、LeftPaddingColumn 和 RightPaddingColumn 在之前显示的 XAML 中声明。

private void CoreTitleBar_LayoutMetricsChanged(CoreApplicationViewTitleBar sender, object args) { // Get the size of the caption controls and set padding. LeftPaddingColumn.Width = new GridLength(coreTitleBar.SystemOverlayLeftInset); RightPaddingColumn.Width = new GridLength(coreTitleBar.SystemOverlayRightInset); } 描述文字按钮的颜色和透明度

将应用内容扩展到标题栏区域时,可以将描述文字按钮的背景设为透明,让应用背景显示。 通常将背景设置为 以 Colors.Transparent 完全透明。 对于部分透明度,请为 Color 将 属性设置为 的 设置 alpha 通道。

Windows 应用 SDK WinUI 3 UWP/WinUI 2

(自 Windows 应用 SDK 1.2 起在 Windows 10 上受支持,Windows 11 完全支持。有关详细信息,请参阅平台选项。)

这些标题栏属性可以是透明的:

ButtonBackgroundColor ButtonHoverBackgroundColor ButtonPressedBackgroundColor ButtonInactiveBackgroundColor

所有其他颜色属性都将继续忽略 alpha 通道。 如果 ExtendsContentIntoTitleBar 设置为 false,则始终忽略所有 AppWindowTitleBar 颜色属性的 alpha 通道。

参考: Colors.Transparent、 ColorHelper

使用自定义标题栏时,可以修改描述文字按钮的颜色以匹配应用。 为此,请使用) 默认值替代此处所示的以下资源 (:

此示例演示如何替代 App.xaml 中的默认值。

Green LightGreen Red Pink

和 WindowCaptionForegroundDisabled 画笔支持WindowCaptionBackground透明度。

这些标题栏属性可以是透明的:

ButtonBackgroundColor ButtonHoverBackgroundColor ButtonPressedBackgroundColor ButtonInactiveBackgroundColor

所有其他颜色属性都将继续忽略 alpha 通道。 如果 ExtendViewIntoTitleBar 设置为 false,则始终忽略所有 ApplicationViewTitleBar 颜色属性的 alpha 通道。

参考: Colors.Transparent、 Color

按钮背景色不适用于“关闭”按钮 悬停 和 已按下 状态。 关闭按钮始终对这些状态使用系统定义的颜色。

提示

Mica 是一种令人愉快 的材料 ,有助于区分焦点窗口。 建议将其用作Windows 11中长期窗口的背景。 如果在窗口的工作区中应用了 Mica,则可以将其扩展到标题栏区域,并使描述文字按钮透明,以便 Mica 显示。 有关详细信息,请参阅 Mica 材料 。

当窗口处于非活动状态时,调暗标题栏

当窗口处于活动状态或处于非活动状态时,应使它变得明显。 至少应更改标题栏中的文本、图标和按钮的颜色。

Windows 应用 SDK WinUI 3 UWP/WinUI 2

处理事件以确定窗口的激活状态,并根据需要更新标题栏 UI。 确定窗口状态的方式取决于用于应用的 UI 框架。

Win32:侦听和响应 WM_ACTIVATE 消息。 WPF:处理 Window.Activated、 Window.Deactivated。 WinForms:处理 Form.Activated、 Form.Deactivate。 带有Windows 应用 SDK标题栏 API 的 WinUI 3:处理 Window.Activated (请参阅 WinUI 3 选项卡) 。

处理 Window.Activated 事件以确定窗口的激活状态,并根据需要更新标题栏 UI。

public MainWindow() { ... Activated += MainWindow_Activated; } private void MainWindow_Activated(object sender, WindowActivatedEventArgs args) { if (args.WindowActivationState == WindowActivationState.Deactivated) { AppTitleTextBlock.Foreground = (SolidColorBrush)App.Current.Resources["WindowCaptionForegroundDisabled"]; } else { AppTitleTextBlock.Foreground = (SolidColorBrush)App.Current.Resources["WindowCaptionForeground"]; } }

处理 CoreWindow.Activated 事件以确定窗口的激活状态,并根据需要更新标题栏 UI。

public MainPage() { ... Window.Current.CoreWindow.Activated += CoreWindow_Activated; } private void CoreWindow_Activated(CoreWindow sender, WindowActivatedEventArgs args) { UISettings settings = new UISettings(); if (args.WindowActivationState == CoreWindowActivationState.Deactivated) { AppTitleTextBlock.Foreground = new SolidColorBrush(settings.UIElementColor(UIElementType.GrayText)); } else { AppTitleTextBlock.Foreground = new SolidColorBrush(settings.UIElementColor(UIElementType.WindowText)); } } 重置标题栏 Windows 应用 SDK WinUI 3 UWP/WinUI 2

(自 Windows 应用 SDK 1.2 起在 Windows 10 上受支持,Windows 11 完全支持。有关详细信息,请参阅平台选项。)

若要在应用运行时重置或切换到系统标题栏,可以调用 AppWindowTitleBar.ResetToDefault。

m_AppWindow.TitleBar.ResetToDefault();

在应用运行时,可以调用 SetTitleBar 切换到新的标题栏元素。 还可以将 作为参数SetTitleBar传递给 null ,并将 ExtendsContentIntoTitleBar 设置为 false 以还原默认系统标题栏。

在应用运行时,可以调用 SetTitleBar 切换到新的标题栏元素。 还可以将 作为参数传递给 null ,SetTitleBar并将 ExtendViewIntoTitleBar 设置为 false 以还原默认系统标题栏。

显示和隐藏标题栏

如果向应用添加对 全屏 或 紧凑覆盖 模式的支持,则当你的应用在这些模式之间切换时,可能需要对标题栏进行更改。

Windows 应用 SDK WinUI 3 UWP/WinUI 2

(自 Windows 应用 SDK 1.2 起在 Windows 10 上受支持,Windows 11 完全支持。有关详细信息,请参阅平台选项。)

当应用在全屏模式下运行时,系统会隐藏标题栏和描述文字控件按钮。 可以处理 AppWindow.Changed 事件并检查事件参数 DidPresenterChange 属性,以确定是否应显示、隐藏或更改标题栏以响应新窗口演示文稿。

此示例演示如何处理 事件, Changed 以在前面的示例中显示和隐藏 AppTitleBar 元素。 如果窗口处于 紧凑覆盖 模式,标题栏将重置为默认的系统标题栏 (,或者可以提供针对紧凑覆盖) 优化的自定义标题栏。

public MainWindow() { this.InitializeComponent(); m_AppWindow = GetAppWindowForCurrentWindow(); m_AppWindow.Changed += AppWindow_Changed; } private void AppWindow_Changed(AppWindow sender, AppWindowChangedEventArgs args) { // 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 (args.DidPresenterChange && AppWindowTitleBar.IsCustomizationSupported()) { switch (sender.Presenter.Kind) { case AppWindowPresenterKind.CompactOverlay: // Compact overlay - hide custom title bar // and use the default system title bar instead. AppTitleBar.Visibility = Visibility.Collapsed; sender.TitleBar.ResetToDefault(); break; case AppWindowPresenterKind.FullScreen: // Full screen - hide the custom title bar // and the default system title bar. AppTitleBar.Visibility = Visibility.Collapsed; sender.TitleBar.ExtendsContentIntoTitleBar = true; break; case AppWindowPresenterKind.Overlapped: // Normal - hide the system title bar // and use the custom title bar instead. AppTitleBar.Visibility = Visibility.Visible; sender.TitleBar.ExtendsContentIntoTitleBar = true; SetDragRegionForCustomTitleBar(sender); break; default: // Use the default system title bar. sender.TitleBar.ResetToDefault(); break; } } }

注意

仅当应用支持时,才能进入全屏和紧凑覆盖模式。 有关详细信息,请参阅 管理应用窗口、 FullScreenPresenter 和 CompactOverlayPresenter 。

WinUI 3 不提供任何 API 来支持全屏模式。 WinUI 3 应用可以使用Windows 应用 SDK API 来执行此操作,但在这种情况下,XAML 窗口不会隐藏标题栏。

当应用在全屏或平板模式(仅 Windows 10)下运行时,系统将隐藏标题栏和标题控制按钮。 但是,用户可以调用标题栏,以使其以覆盖形式显示在应用的 UI 顶部。

你可以处理隐藏或调用标题栏时将通知的 CoreApplicationViewTitleBar.IsVisibleChanged 事件,并根据需要显示或隐藏你的自定义标题栏内容。

此示例演示如何处理 事件, IsVisibleChanged 以在前面的示例中显示和隐藏 AppTitleBar 元素。

public MainPage() { this.InitializeComponent(); var coreTitleBar = CoreApplication.GetCurrentView().TitleBar; // Register a handler for when the title bar visibility changes. // For example, when the title bar is invoked in full screen mode. coreTitleBar.IsVisibleChanged += CoreTitleBar_IsVisibleChanged; } private void CoreTitleBar_IsVisibleChanged(CoreApplicationViewTitleBar sender, object args) { if (sender.IsVisible) { AppTitleBar.Visibility = Visibility.Visible; } else { AppTitleBar.Visibility = Visibility.Collapsed; } }

注意

仅在应用支持时才能进入全屏模式。 有关详细信息,请参阅 ApplicationView.IsFullScreenMode。 平板电脑模式 (Windows 10仅) 是支持的硬件上Windows 10的用户选项,因此用户可以选择在平板电脑模式下运行任何应用。

应做事项和禁止事项 务必清楚地显示窗口何时处于活动状态,何时处于非活动状态。 至少要更改标题栏中文本、图标和按钮的颜色。 在应用画布的上边缘定义一个拖动区域。 通过匹配系统标题栏的位置,用户可以更容易找到。 如果应用画布上有任何) ,请定义与视觉对象标题栏匹配的拖动区域 (。 完全自定义示例

此示例显示完全自定义部分中描述的所有代码。

Windows 应用 SDK WinUI 3 UWP/WinUI 2

(自 Windows 应用 SDK 1.2 起在 Windows 10 上受支持,在 Windows 11 上完全受支持。有关详细信息,请参阅平台选项。)

using Microsoft.UI; using Microsoft.UI.Windowing; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; using System; using System.Collections.Generic; using System.Runtime.InteropServices; using WinRT.Interop; namespace WASDK_ExtendedTitleBar { public sealed partial class MainWindow : Window { private AppWindow m_AppWindow; public MainWindow() { this.InitializeComponent(); m_AppWindow = GetAppWindowForCurrentWindow(); m_AppWindow.Changed += AppWindow_Changed; // 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()) { var titleBar = m_AppWindow.TitleBar; titleBar.ExtendsContentIntoTitleBar = true; AppTitleBar.Loaded += AppTitleBar_Loaded; AppTitleBar.SizeChanged += AppTitleBar_SizeChanged; } else { // In the case that title bar customization is not supported, hide the custom title // bar element. AppTitleBar.Visibility = Visibility.Collapsed; // Show alternative UI for any functionality in // the title bar, such as search. } } private void AppTitleBar_Loaded(object sender, RoutedEventArgs e) { if (AppWindowTitleBar.IsCustomizationSupported()) { SetDragRegionForCustomTitleBar(m_AppWindow); } } private void AppTitleBar_SizeChanged(object sender, SizeChangedEventArgs e) { if (AppWindowTitleBar.IsCustomizationSupported() && m_AppWindow.TitleBar.ExtendsContentIntoTitleBar) { // Update drag region if the size of the title bar changes. SetDragRegionForCustomTitleBar(m_AppWindow); } } private AppWindow GetAppWindowForCurrentWindow() { IntPtr hWnd = WindowNative.GetWindowHandle(this); WindowId wndId = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(hWnd); return AppWindow.GetFromWindowId(wndId); } [DllImport("Shcore.dll", SetLastError = true)] internal static extern int GetDpiForMonitor(IntPtr hmonitor, Monitor_DPI_Type dpiType, out uint dpiX, out uint dpiY); internal enum Monitor_DPI_Type : int { MDT_Effective_DPI = 0, MDT_Angular_DPI = 1, MDT_Raw_DPI = 2, MDT_Default = MDT_Effective_DPI } private double GetScaleAdjustment() { IntPtr hWnd = WindowNative.GetWindowHandle(this); WindowId wndId = Win32Interop.GetWindowIdFromWindow(hWnd); DisplayArea displayArea = DisplayArea.GetFromWindowId(wndId, DisplayAreaFallback.Primary); IntPtr hMonitor = Win32Interop.GetMonitorFromDisplayId(displayArea.DisplayId); // Get DPI. int result = GetDpiForMonitor(hMonitor, Monitor_DPI_Type.MDT_Default, out uint dpiX, out uint _); if (result != 0) { throw new Exception("Could not get DPI for monitor."); } uint scaleFactorPercent = (uint)(((long)dpiX * 100 + (96 >> 1)) / 96); return scaleFactorPercent / 100.0; } private void SetDragRegionForCustomTitleBar(AppWindow appWindow) { if (AppWindowTitleBar.IsCustomizationSupported() && appWindow.TitleBar.ExtendsContentIntoTitleBar) { double scaleAdjustment = GetScaleAdjustment(); RightPaddingColumn.Width = new GridLength(appWindow.TitleBar.RightInset / scaleAdjustment); LeftPaddingColumn.Width = new GridLength(appWindow.TitleBar.LeftInset / scaleAdjustment); List dragRectsList = new(); Windows.Graphics.RectInt32 dragRectL; dragRectL.X = (int)((LeftPaddingColumn.ActualWidth) * scaleAdjustment); dragRectL.Y = 0; dragRectL.Height = (int)(AppTitleBar.ActualHeight * scaleAdjustment); dragRectL.Width = (int)((IconColumn.ActualWidth + TitleColumn.ActualWidth + LeftDragColumn.ActualWidth) * scaleAdjustment); dragRectsList.Add(dragRectL); Windows.Graphics.RectInt32 dragRectR; dragRectR.X = (int)((LeftPaddingColumn.ActualWidth + IconColumn.ActualWidth + TitleTextBlock.ActualWidth + LeftDragColumn.ActualWidth + SearchColumn.ActualWidth) * scaleAdjustment); dragRectR.Y = 0; dragRectR.Height = (int)(AppTitleBar.ActualHeight * scaleAdjustment); dragRectR.Width = (int)(RightDragColumn.ActualWidth * scaleAdjustment); dragRectsList.Add(dragRectR); Windows.Graphics.RectInt32[] dragRects = dragRectsList.ToArray(); appWindow.TitleBar.SetDragRectangles(dragRects); } } private void AppWindow_Changed(AppWindow sender, AppWindowChangedEventArgs args) { if (args.DidPresenterChange && AppWindowTitleBar.IsCustomizationSupported()) { switch (sender.Presenter.Kind) { case AppWindowPresenterKind.CompactOverlay: // Compact overlay - hide custom title bar // and use the default system title bar instead. AppTitleBar.Visibility = Visibility.Collapsed; sender.TitleBar.ResetToDefault(); break; case AppWindowPresenterKind.FullScreen: // Full screen - hide the custom title bar // and the default system title bar. AppTitleBar.Visibility = Visibility.Collapsed; sender.TitleBar.ExtendsContentIntoTitleBar = true; break; case AppWindowPresenterKind.Overlapped: // Normal - hide the system title bar // and use the custom title bar instead. AppTitleBar.Visibility = Visibility.Visible; sender.TitleBar.ExtendsContentIntoTitleBar = true; SetDragRegionForCustomTitleBar(sender); break; default: // Use the default system title bar. sender.TitleBar.ResetToDefault(); break; } } } private void SwitchPresenter(object sender, RoutedEventArgs e) { if (m_AppWindow != null) { AppWindowPresenterKind newPresenterKind; switch ((sender as Button).Name) { case "CompactoverlaytBtn": newPresenterKind = AppWindowPresenterKind.CompactOverlay; break; case "FullscreenBtn": newPresenterKind = AppWindowPresenterKind.FullScreen; break; case "OverlappedBtn": newPresenterKind = AppWindowPresenterKind.Overlapped; break; default: newPresenterKind = AppWindowPresenterKind.Default; break; } // If the same presenter button was pressed as the // mode we're in, toggle the window back to Default. if (newPresenterKind == m_AppWindow.Presenter.Kind) { m_AppWindow.SetPresenter(AppWindowPresenterKind.Default); } else { // Else request a presenter of the selected kind // to be created and applied to the window. m_AppWindow.SetPresenter(newPresenterKind); } } } } } using Microsoft.UI.Xaml; namespace WinUI3_ExtendedTitleBar { /// /// An empty window that can be used on its own or navigated to within a Frame. /// public sealed partial class MainWindow : Window { public MainWindow() { this.InitializeComponent(); ExtendsContentIntoTitleBar = true; SetTitleBar(AppTitleBar); } } } public MainPage() { this.InitializeComponent(); // Hide default title bar. CoreApplicationViewTitleBar coreTitleBar = CoreApplication.GetCurrentView().TitleBar; coreTitleBar.ExtendViewIntoTitleBar = true; // Set caption buttons background to transparent. ApplicationViewTitleBar titleBar = ApplicationView.GetForCurrentView().TitleBar; titleBar.ButtonBackgroundColor = Colors.Transparent; // Set XAML element as a drag region. Window.Current.SetTitleBar(AppTitleBar); // Register a handler for when the size of the overlaid caption control changes. coreTitleBar.LayoutMetricsChanged += CoreTitleBar_LayoutMetricsChanged; // Register a handler for when the title bar visibility changes. // For example, when the title bar is invoked in full screen mode. coreTitleBar.IsVisibleChanged += CoreTitleBar_IsVisibleChanged; // Register a handler for when the window activation changes. Window.Current.CoreWindow.Activated += CoreWindow_Activated; } private void CoreTitleBar_LayoutMetricsChanged(CoreApplicationViewTitleBar sender, object args) { // Get the size of the caption controls and set padding. LeftPaddingColumn.Width = new GridLength(coreTitleBar.SystemOverlayLeftInset); RightPaddingColumn.Width = new GridLength(coreTitleBar.SystemOverlayRightInset); } private void CoreTitleBar_IsVisibleChanged(CoreApplicationViewTitleBar sender, object args) { if (sender.IsVisible) { AppTitleBar.Visibility = Visibility.Visible; } else { AppTitleBar.Visibility = Visibility.Collapsed; } } private void CoreWindow_Activated(CoreWindow sender, WindowActivatedEventArgs args) { UISettings settings = new UISettings(); if (args.WindowActivationState == CoreWindowActivationState.Deactivated) { AppTitleTextBlock.Foreground = new SolidColorBrush(settings.UIElementColor(UIElementType.GrayText)); } else { AppTitleTextBlock.Foreground = new SolidColorBrush(settings.UIElementColor(UIElementType.WindowText)); } } 相关文章 Acrylic Mica 颜色


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3