当使用WindowChrome(downloadable here)自定义窗口的非客户区域时,自然的起点是创建一个标题栏,该标题栏的外观和行为与标准标题栏相同。这需要增加一个“假”的应用程序图标和标题栏,因为很明显WindowChrome禁用这些功能(最小化,最大化和关闭按钮仍然有效。)用WindowChrome复制标准系统菜单?
这是我到目前为止有:
<Window x:Class="MyApp.MainWindow"
x:Name="MainWindowItself"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:shell="clr-namespace:Microsoft.Windows.Shell;assembly=Microsoft.Windows.Shell"
xmlns:local="clr-namespace:MyApp"
Title="My Application" Icon="App.ico" Height="350" Width="525">
<Window.Resources>
<Style TargetType="{x:Type local:MainWindow}">
<Setter Property="shell:WindowChrome.WindowChrome">
<Setter.Value>
<shell:WindowChrome />
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:MainWindow}">
<Grid>
<Border Background="White" Margin="{Binding Source={x:Static shell:SystemParameters2.Current}, Path=WindowNonClientFrameThickness}">
<ContentPresenter Content="{TemplateBinding Content}" />
</Border>
<TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Title}"
VerticalAlignment="Top" HorizontalAlignment="Left"
Margin="32,8,0,0"/>
<Image x:Name="SystemMenuIcon" Source="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Icon}"
VerticalAlignment="Top" HorizontalAlignment="Left"
Margin="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(shell:WindowChrome.WindowChrome).ResizeBorderThickness}"
Width="{Binding Source={x:Static shell:SystemParameters2.Current}, Path=SmallIconSize.Width}"
shell:WindowChrome.IsHitTestVisibleInChrome="True" MouseDown="SystemMenuIcon_MouseDown">
</Image>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<TextBlock Text="Client area content goes here"/>
</Grid>
</Window>
代码后面:
private void SystemMenuIcon_MouseDown(object sender, MouseButtonEventArgs e)
{
var offs = SystemParameters2.Current.WindowNonClientFrameThickness;
SystemCommands.ShowSystemMenu(this, new Point(Left + offs.Left, Top + offs.Top));
}
这非常接近工作。第一个问题是,单击应用程序图标并出现系统菜单后,如果再次单击该菜单,该菜单应该消失 - 而只是重新绘制菜单。此外,如果您双击然后该窗口应该关闭,但图像没有双击事件。你会如何建议添加这些功能?
有一件事失踪,使这个_the_完美的解决方案:设置'WindowChrome.IsHitTestVisibleInChrome =”在“StackPanel”中的True“'会使窗口双击并拖动。 (@Pushpraj在他对[C#| WPF - 自定义标题栏 - 最大化时从顶部拖动窗口不起作用]的答案](https://stackoverflow.com/questions/24654845/c-sharp-wpf-custom-title-棒拖窗口从机上时最大化-犯规-WO#24654932))。 – Informagic 2017-07-12 07:18:52