gpt4 book ai didi

c# - 我如何在窗口栏中获得菜单?

转载 作者:行者123 更新时间:2023-12-02 16:11:58 26 4
gpt4 key购买 nike

我想知道如何在窗口栏中获取菜单,就像 Visual Studio 一样。

Visual Studios Window

最好能在左边有文件、编辑等按钮,在右边有标准的最小化、最大化和关闭按钮。这有可能吗?

我已经尝试设置 Window WindowStyle="None" 并在栏中添加我自己的图标,但它似乎不正确,但这是唯一的方法吗?

这就是我目前拥有的。

My Current Window

<Window
Title="MainWindow"
Height="{x:Static SystemParameters.PrimaryScreenHeight}"
Width="{x:Static SystemParameters.PrimaryScreenWidth}"
Closing="Window_Closing"
WindowState="Maximized">

最佳答案

您必须使用 WindowChrome 创建自定义窗口镶边类:

WPF 中的 Window 元素实际上托管在非 WPF(非客户端)主机中。这个宿主包括标题栏(caption)和标准按钮、一个图标和实际框架。这称为窗口镶边。

enter image description here

通常你只能修改窗口的客户区。但是在 WindowChrome 类的帮助下,WPF 允许客户区扩展到非客户区。
缺点是您必须基本上复制原始的非客户区以保留原始的外观和感觉。但毕竟你仍然会得到一些基本的行为,比如双击框最大化窗口。

以下示例非常基础,但应该可以让您了解如何完成您的任务:

enter image description here

我强烈建议按照提供的链接访问 WindowChrome 类并阅读备注部分,其中包含非常有值(value)的信息。
您可以使用静态 SystemParameters 提供的实际系统值获取当前维度值的类,例如SystemParameters.WindowResizeBorderThickness,您应该在自定义 chrome 样式中使用它。

另请注意,要允许 WPF 在您自定义 chrome 的输入元素(如按钮或菜单)上捕获鼠标事件,您必须将每个相关元素上的附加属性 WindowChrome.IsHitTestVisibleInChrome 设置为 true:

WindowChrome.IsHitTestVisibleInChrome="True"

创建上述视觉效果的最基本样式如下:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:shell="clr-namespace:System.Windows.Shell;assembly=PresentationFramework">


<Style x:Key="WindowStyle" TargetType="{x:Type Window}">
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="WindowChrome.WindowChrome">
<Setter.Value>
<WindowChrome NonClientFrameEdges="Right"
ResizeBorderThickness="{x:Static SystemParameters.WindowResizeBorderThickness}" />
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Window}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<Grid Background="Transparent">

<AdornerDecorator>
<Border Background="Transparent" Margin="{x:Static SystemParameters.WindowNonClientFrameThickness}">
<ContentPresenter />
</Border>
</AdornerDecorator>
<ResizeGrip x:Name="WindowResizeGrip"
HorizontalAlignment="Right"
VerticalAlignment="Bottom"
Visibility="Collapsed"
IsTabStop="false" />
<Grid Height="{Binding Source={x:Static SystemParameters.WindowNonClientFrameThickness}, Path=Top}"
Background="#FF3F3F3F"
VerticalAlignment="Top">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>

<!-- Custom window chrome -->
<StackPanel Grid.Column="0" Orientation="Horizontal"
Margin="{x:Static SystemParameters.WindowResizeBorderThickness}">
<Image Source="{TemplateBinding Icon}" />
<TextBlock Text="{TemplateBinding Title}"
TextTrimming="CharacterEllipsis"
VerticalAlignment="Center"
Margin="16,0" />
<Menu shell:WindowChrome.IsHitTestVisibleInChrome="True">
<MenuItem Header="CustomChromeMenu">
<MenuItem Header="Action" />
</MenuItem>
</Menu>
</StackPanel>

<StackPanel Grid.Column="1"
Orientation="Horizontal"
HorizontalAlignment="Right">
<Button Width="45"
Height="{Binding Source={x:Static SystemParameters.WindowNonClientFrameThickness}, Path=Top}"
ToolTip="Minimize window"
ToolTipService.ShowDuration="5000"
shell:WindowChrome.IsHitTestVisibleInChrome="True">
<TextBlock Foreground="{Binding RelativeSource={RelativeSource AncestorType=Control}, Path=Foreground}"
FontFamily="Segoe MDL2 Assets"
FontSize="11"
Text="&#xE921;" />
</Button>
<Button ToolTip="Maximize window"
Width="45"
Height="{Binding Source={x:Static SystemParameters.WindowNonClientFrameThickness}, Path=Top}"
ToolTipService.ShowDuration="5000"
shell:WindowChrome.IsHitTestVisibleInChrome="True">
<TextBlock Foreground="{Binding RelativeSource={RelativeSource AncestorType=Control}, Path=Foreground}"
FontFamily="Segoe MDL2 Assets"
FontSize="11"
Text="&#xE922;" />
</Button>
<Button ToolTip="Close application"
ToolTipService.ShowDuration="5000"
Width="50"
Height="{Binding Source={x:Static SystemParameters.WindowNonClientFrameThickness}, Path=Top}"
shell:WindowChrome.IsHitTestVisibleInChrome="True">
<TextBlock Foreground="{Binding RelativeSource={RelativeSource AncestorType=Control}, Path=Foreground}"
FontFamily="Segoe MDL2 Assets"
FontSize="11"
Text="&#xE8BB;" />
</Button>
</StackPanel>
</Grid>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="ResizeMode"
Value="CanResizeWithGrip">
<Setter TargetName="WindowResizeGrip"
Property="Visibility"
Value="Visible" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>

关于c# - 我如何在窗口栏中获得菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67768902/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com