如何在 wpf 中创建自定义窗口镶边? [英] How to create custom window chrome in wpf?
本文介绍了如何在 wpf 中创建自定义窗口镶边?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何为 WPF 窗口创建基本的自定义窗口镶边,其中不包括关闭按钮并且仍然是一个可移动和可调整大小的窗口?
How can I create a basic custom window chrome for a WPF window, that doesn't include the close button and still a moveable and resizeable window?
推荐答案
您设置窗口的 WindowStyle="None"
,然后构建您自己的窗口界面.您需要构建自己的 Min/Max/Close/Drag 事件处理程序,但仍会保留调整大小.
You set your Window's WindowStyle="None"
, then build your own window interface. You need to build in your own Min/Max/Close/Drag event handlers, but Resizing is still maintained.
例如:
<Window
WindowState="Maximized"
WindowStyle="None"
WindowStartupLocation="CenterScreen"
MaxWidth="{Binding Source={x:Static SystemParameters.WorkArea}, Path=Width}"
MaxHeight="{Binding Source={x:Static SystemParameters.WorkArea}, Path=Height}"
>
<DockPanel x:Name="RootWindow">
<DockPanel x:Name="TitleBar" DockPanel.Dock="Top">
<Button x:Name="CloseButton" Content="X"
Click="CloseButton_Click"
DockPanel.Dock="Right" />
<Button x:Name="MaxButton" Content="Restore"
Click="MaximizeButton_Click"
DockPanel.Dock="Right" />
<Button x:Name="MinButton" Content="Min"
Click="MinimizeButton_Click"
DockPanel.Dock="Right" />
<TextBlock HorizontalAlignment="Center">Application Name</TextBlock>
</DockPanel>
<ContentControl Content="{Binding CurrentPage}" />
</DockPanel>
</Window>
这里有一些常见窗口功能的示例代码隐藏
And here's some example code-behind for common window functionality
/// <summary>
/// TitleBar_MouseDown - Drag if single-click, resize if double-click
/// </summary>
private void TitleBar_MouseDown(object sender, MouseButtonEventArgs e)
{
if(e.ChangedButton == MouseButton.Left)
if (e.ClickCount == 2)
{
AdjustWindowSize();
}
else
{
Application.Current.MainWindow.DragMove();
}
}
/// <summary>
/// CloseButton_Clicked
/// </summary>
private void CloseButton_Click(object sender, RoutedEventArgs e)
{
Application.Current.Shutdown();
}
/// <summary>
/// MaximizedButton_Clicked
/// </summary>
private void MaximizeButton_Click(object sender, RoutedEventArgs e)
{
AdjustWindowSize();
}
/// <summary>
/// Minimized Button_Clicked
/// </summary>
private void MinimizeButton_Click(object sender, RoutedEventArgs e)
{
this.WindowState = WindowState.Minimized;
}
/// <summary>
/// Adjusts the WindowSize to correct parameters when Maximize button is clicked
/// </summary>
private void AdjustWindowSize()
{
if (this.WindowState == WindowState.Maximized)
{
this.WindowState = WindowState.Normal;
MaximizeButton.Content = "1";
}
else
{
this.WindowState = WindowState.Maximized;
MaximizeButton.Content = "2";
}
}
这篇关于如何在 wpf 中创建自定义窗口镶边?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文