如何在窗口栏中获得菜单? [英] How do i get a Menu in the Window bar?

查看:31
本文介绍了如何在窗口栏中获得菜单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

如果能够在左侧有 File、Edit 等,在右侧有标准的 Minimize、Maximize 和 Close 按钮​​,那就太好了.这可能吗?

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

这就是我目前所拥有的.

解决方案

您必须使用

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

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

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

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

WindowChrome.IsHitTestVisibleInChrome=True"

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

<Setter 属性 =SnapsToDevicePixels"值=真"/><Setter Property="WindowChrome.WindowChrome"><Setter.Value><WindowChrome NonClientFrameEdges="Right";ResizeBorderThickness="{x:Static SystemParameters.WindowResizeBorderThickness}";/></Setter.Value></Setter><Setter 属性 =模板"><Setter.Value><ControlTemplate TargetType="{x:Type Window}"><Border Background="{TemplateBinding Background}";BorderBrush="{TemplateBinding BorderBrush}";BorderThickness={TemplateBinding BorderThickness}"><网格背景=透明"><AdornerDecorator><边框背景=透明"边距 ={x:Static SystemParameters.WindowNonClientFrameThickness}"><ContentPresenter/></边框></AdornerDecorator><ResizeGrip x:Name=WindowResizeGrip"水平对齐=右"VerticalAlignment=底部"可见性=折叠"IsTabStop=假"/><网格高度={绑定源={x:Static SystemParameters.WindowNonClientFrameThickness}, Path=Top}";背景=#FF3F3F3F"VerticalAlignment=顶部"><Grid.ColumnDefinitions><列定义/><ColumnDefinition Width=自动";/></Grid.ColumnDefinitions><!-- 自定义窗口镶边 --><StackPanel Grid.Column=0"方向=水平"边距 ={x:Static SystemParameters.WindowResizeBorderThickness}"><Image Source="{TemplateBinding Icon}";/><TextBlock Text="{TemplateBinding Title}";TextTrimming="CharacterEllipsis"VerticalAlignment=中心"边距=16,0"/><Menu shell:WindowChrome.IsHitTestVisibleInChrome=True"><MenuItem Header="CustomChromeMenu"><MenuItem Header="Action";/></菜单项></菜单></StackPanel><StackPanel Grid.Column=1"方向=水平"水平对齐=右"><按钮宽度=45"高度={Binding Source={x:Static SystemParameters.WindowNonClientFrameThickness},Path=Top}"ToolTip="最小化窗口"ToolTipService.ShowDuration="5000";shell:WindowChrome.IsHitTestVisibleInChrome=True"><TextBlock Foreground="{Binding RelativeSource={RelativeSource AncestorType=Control}, Path=Foreground}";FontFamily=Segoe MDL2 资产"字体大小=11"文本=&#xE921;"/></按钮><按钮工具提示=最大化窗口"宽度=45"高度={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 资产"字体大小=11"文本=&#xE922;"/></按钮><按钮工具提示=关闭应用程序"ToolTipService.ShowDuration="5000";宽度=50"高度={Binding Source={x:Static SystemParameters.WindowNonClientFrameThickness},Path=Top}"shell:WindowChrome.IsHitTestVisibleInChrome=True"><TextBlock Foreground="{Binding RelativeSource={RelativeSource AncestorType=Control}, Path=Foreground}";FontFamily=Segoe MDL2 资产"字体大小=11"文本=&#xE8BB;"/></按钮></StackPanel></网格></网格></边框><ControlTemplate.Triggers><触发器属性=ResizeMode";值=CanResizeWithGrip"><Setter TargetName=WindowResizeGrip"属性=可见性"值=可见"/></触发器></ControlTemplate.Triggers></控制模板></Setter.Value></Setter></风格></ResourceDictionary>

I would like to know how to get a menu in the window bar, the same as Visual studio does.

It would be good to be able to have File, Edit, etc on the left and the standard Minimize, Maximize and Close buttons on the right. Is this at all possible?

I have tried setting Window WindowStyle="None" and adding my own icons in the bar but it doesnt seem right but is this the only way?

This is what i have at the moment.

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

解决方案

You must create your custom window chrome using the WindowChrome class:

The Window element in WPF is actually hosted in a non-WPF (non-client) host. This host includes the title bar (caption) and the standard buttons, an icon and the actual frame. This is known as window chrome.

Usually you can only modify the client area of a Window. But whith the help of the WindowChrome class, WPF allows the client area to extend into the non-client area.
The disadvantage is that you would have to basically replicate the original non-client area in order to preserve the original look and feel. But after all you still get some basic behavior like maximizing the window on double click ou of the box.

The following example is very basic, but should give you an idea how to achieve your task:

I highly recommend to follow the provided link to the WindowChrome class and read the remarks section, which contains very valuable information.
You can use the actual system values provided by the static SystemParameters class to get the current dimension values e.g. SystemParameters.WindowResizeBorderThickness which you should use in your custom chrome style.

Also note that to allow WPF to capture mouse events on you custom chrome's input elements like buttons or menus you must set the attached property WindowChrome.IsHitTestVisibleInChrome on each relevant element to true:

WindowChrome.IsHitTestVisibleInChrome="True"

The very basic style that creates the above visual is as followed:

<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>

这篇关于如何在窗口栏中获得菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆