如何在wpf中创build自定义的窗口镶边?

我怎样才能创build一个基本的自定义窗口铬为WPF窗口,不包括closuresbutton,仍然是一个可移动和可resize的窗口?

你设置你的Window的WindowStyle="None" ,然后build立你自己的窗口界面。 您需要构build您自己的最小/最大/closures/拖动事件处理程序,但resize仍然保留。

例如:

 <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"> <Button x:Name="CloseButton" DockPanel.Dock="Right" /> <Button x:Name="MaxButton" DockPanel.Dock="Right" /> <Button x:Name="MinButton" DockPanel.Dock="Right" /> <TextBlock HorizontalAlignment="Center">Application Name</TextBlock> </DockPanel> <ContentControl Content="{Binding CurrentPage}" /> </DockPanel> </Window> 

以下是一些常用窗口function的代码示例

 /// <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"; } } 

.NET 4.5增加了一个新的类,大大简化了这一点。

WindowChrome类使您能够将Windows Presentation Foundation(WPF)内容扩展到通常为操作系统的窗口pipe理器保留的窗口的非客户端区域。

你可以在这里find一个教程 。

这里有一个简短的例子用法 。

我刚刚使用下面的例子.net 4.5,它工作得很好。 有趣的是,它使用一个代码作为点击事件的资源字典。 你所要做的就是在你的app.xaml文件中引用资源字典,然后将窗口风格CustomWindowStyle 。 这是从http://www.eidias.com/blog/2014/1/27/restyle-your-window无耻地被盗。;

 <ResourceDictionary x:Class="WpfApp7.WindowStyle" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Style x:Key="CustomWindowStyle" TargetType="{x:Type Window}"> <Setter Property="WindowChrome.WindowChrome"> <Setter.Value> <WindowChrome CaptionHeight="30" CornerRadius="4" GlassFrameThickness="0" NonClientFrameEdges="None" ResizeBorderThickness="5" UseAeroCaptionButtons="False" /> </Setter.Value> </Setter> <Setter Property="BorderBrush" Value="Black" /> <Setter Property="Background" Value="Gray" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Window}"> <Grid> <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="5,30,5,5"> <AdornerDecorator> <ContentPresenter /> </AdornerDecorator> </Border> <Grid Height="30" VerticalAlignment="Top"> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="*"/> <ColumnDefinition Width="Auto"/> </Grid.ColumnDefinitions> <StackPanel Orientation="Horizontal" Margin="5,0"> <Button Content="A" Margin="0,0,5,0" VerticalAlignment="Center" Click="Button_Click" WindowChrome.IsHitTestVisibleInChrome="True"/> <Button Content="B" Margin="0,0,5,0" VerticalAlignment="Center" Click="Button_Click" WindowChrome.IsHitTestVisibleInChrome="True"/> <Button Content="C" Margin="0,0,5,0" VerticalAlignment="Center" Click="Button_Click" WindowChrome.IsHitTestVisibleInChrome="True"/> <Button Content="D" Margin="0,0,5,0" VerticalAlignment="Center" Click="Button_Click" WindowChrome.IsHitTestVisibleInChrome="True"/> </StackPanel> <TextBlock Margin="5,0,0,0" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="16" Foreground="White" Text="{TemplateBinding Title}" Grid.Column="1"/> <StackPanel Orientation="Horizontal" Grid.Column="2"> <Button x:Name="btnClose" Width="15" Margin="5" Click="CloseClick" Content="X" WindowChrome.IsHitTestVisibleInChrome="True" /> <Button x:Name="btnRestore" Width="15" Margin="5" Click="MaximizeRestoreClick" Content="#" WindowChrome.IsHitTestVisibleInChrome="True" /> <Button x:Name="btnMinimize" Width="15" Margin="5" VerticalContentAlignment="Bottom" Click="MinimizeClick" Content="_" WindowChrome.IsHitTestVisibleInChrome="True" /> </StackPanel> </Grid> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> </ResourceDictionary> 

而对于后面的代码:

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; namespace WpfApp7 { public partial class WindowStyle : ResourceDictionary { public WindowStyle() { InitializeComponent(); } private void CloseClick(object sender, RoutedEventArgs e) { var window = (Window)((FrameworkElement)sender).TemplatedParent; window.Close(); } private void MaximizeRestoreClick(object sender, RoutedEventArgs e) { var window = (Window)((FrameworkElement)sender).TemplatedParent; if (window.WindowState == System.Windows.WindowState.Normal) { window.WindowState = System.Windows.WindowState.Maximized; } else { window.WindowState = System.Windows.WindowState.Normal; } } private void MinimizeClick(object sender, RoutedEventArgs e) { var window = (Window)((FrameworkElement)sender).TemplatedParent; window.WindowState = System.Windows.WindowState.Minimized; } private void Button_Click(object sender, RoutedEventArgs e) { MessageBox.Show("Hello!"); } } }