如何在WPF中创建自定义窗口镶边?

如何为WPF窗口创建基本的自定义窗口镶边,该样式不包含关闭按钮,而仍然是可移动且可调整大小的窗口?



森栏
浏览 613回答 3
3回答

GCT1015

您设置窗口的WindowStyle="None",然后构建自己的窗口界面。您需要构建自己的“最小/最大/关闭/拖动”事件处理程序,但仍会保持“调整大小”。例如:<Window&nbsp;&nbsp; &nbsp; WindowState="Maximized"&nbsp;&nbsp; &nbsp; WindowStyle="None"&nbsp; &nbsp; WindowStartupLocation="CenterScreen"&nbsp; &nbsp; MaxWidth="{Binding Source={x:Static SystemParameters.WorkArea}, Path=Width}"&nbsp; &nbsp; MaxHeight="{Binding Source={x:Static SystemParameters.WorkArea}, Path=Height}">&nbsp; &nbsp; <DockPanel x:Name="RootWindow">&nbsp; &nbsp; &nbsp; &nbsp; <DockPanel x:Name="TitleBar" DockPanel.Dock="Top">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Button x:Name="CloseButton" Content="X"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Click="CloseButton_Click"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DockPanel.Dock="Right" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Button x:Name="MaxButton" Content="Restore"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Click="MaximizeButton_Click"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DockPanel.Dock="Right" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Button x:Name="MinButton" Content="Min"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Click="MinimizeButton_Click"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DockPanel.Dock="Right" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <TextBlock HorizontalAlignment="Center">Application Name</TextBlock>&nbsp; &nbsp; &nbsp; &nbsp; </DockPanel>&nbsp; &nbsp; &nbsp; &nbsp; <ContentControl Content="{Binding CurrentPage}" />&nbsp; &nbsp; </DockPanel></Window>下面是一些常见窗口功能的示例代码/// <summary>/// TitleBar_MouseDown - Drag if single-click, resize if double-click/// </summary>private void TitleBar_MouseDown(object sender, MouseButtonEventArgs e){&nbsp; &nbsp; if(e.ChangedButton == MouseButton.Left)&nbsp; &nbsp; &nbsp; &nbsp; if (e.ClickCount == 2)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AdjustWindowSize();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Application.Current.MainWindow.DragMove();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;}/// <summary>/// CloseButton_Clicked/// </summary>private void CloseButton_Click(object sender, RoutedEventArgs e){&nbsp; &nbsp;Application.Current.Shutdown();}/// <summary>/// MaximizedButton_Clicked/// </summary>private void MaximizeButton_Click(object sender, RoutedEventArgs e){&nbsp; &nbsp; AdjustWindowSize();}/// <summary>/// Minimized Button_Clicked/// </summary>private void MinimizeButton_Click(object sender, RoutedEventArgs e){&nbsp; &nbsp; this.WindowState = WindowState.Minimized;}/// <summary>/// Adjusts the WindowSize to correct parameters when Maximize button is clicked/// </summary>private void AdjustWindowSize(){&nbsp; &nbsp; if (this.WindowState == WindowState.Maximized)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; this.WindowState = WindowState.Normal;&nbsp; &nbsp; &nbsp; &nbsp; MaximizeButton.Content = "1";&nbsp; &nbsp; }&nbsp; &nbsp; else&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; this.WindowState = WindowState.Maximized;&nbsp; &nbsp; &nbsp; &nbsp; MaximizeButton.Content = "2";&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP