窗口
2022/9/13 更新:
禁止关闭窗口
重写OnClosing函数
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
e.Cancel = true;
}
去掉窗口标题栏的关闭按钮
只是去掉标题栏的关闭按钮,窗体内仍然可以调用Close()函数。
在窗口类中加入以下代码:
using System.Runtime.InteropServices;
using System.Windows.Interop;
{
...
private const int GWL_STYLE = -16;
private const int WS_SYSMENU = 0x80000;
[DllImport("user32.dll", SetLastError = true)]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
private void Window_OnLoaded(object sender, RoutedEventArgs e) {
var hwnd = new WindowInteropHelper(this).Handle;
SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_SYSMENU);
}
...
}
同时修改xaml文件:
<Window ... Loaded="Window_OnLoaded" ...>
修改窗口标题栏格式(颜色、字体等)
<Window x:Class="xxxxxx.UserDefinedWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:shell="clr-namespace:Microsoft.Windows.Shell;assembly=Xceed.Wpf.AvalonDock"
xmlns:local="clr-namespace:"
mc:Ignorable="d"
Height="700" Width="500" ResizeMode="NoResize" Loaded="Window_OnLoaded" WindowStyle="None">
<shell:WindowChrome.WindowChrome>
<shell:WindowChrome GlassFrameThickness="0" CornerRadius="5" />
</shell:WindowChrome.WindowChrome>
<Grid>
<!-- 窗口背景颜色 -->
<Border Background="#F8F9F9" />
<!-- 标题栏颜色 -->
<Border Height="30" Background="#1C2833" VerticalAlignment="Top">
<!-- 标题栏内容 -->
<Grid>
<TextBlock Text="xxx" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="#F8F9F9" FontSize="18" FontWeight="SemiBold" />
</Grid>
</Border>
<Grid Margin="0,40,0,0">
<!-- 窗口的内容 -->
</Grid>
</Grid>
</Window>
(可能没用)窗口标题居中
在Title后直接写TextBlock居中即可。
<Window ... Title="Demo" TextBlock.TextAlignment="Center" ...>
窗口居中
public partial class WelcomeWindow : Window{
public WelcomeWindow(){
// 设置窗体居中
WindowStartupLocation = WindowStartupLocation.CenterScreen;
InitializeComponent();
}
}
窗口取消最大化按钮
只保留最小化和关闭按钮
<Window ... ResizeMode="CanMinimize" ...>
按钮
圆角按钮
先写资源,其中Border的CornerRadius控制了按钮的弧度
<Window.Resources>
<ControlTemplate x:Key="roundCornerBtnTemplate" TargetType="Button">
<Border BorderThickness="1" BorderBrush="DarkGray" CornerRadius="5" Background="{TemplateBinding Background}">
<ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Border>
</ControlTemplate>
</Window.Resources>
使用时:
<Button ...Template="{StaticResource roundCornerBtnTemplate}".../>
穿过按钮点击下面的控件
举例:写一个按钮样式的菜单,点击菜单,出现下拉列表
但是因为按钮本身可以点击,会禁掉菜单的效果,即无法出现下拉列表。此时,只要给button加个属性:IsHitTestVisible=“False”
<Menu Background="Transparent" Height="42" Width="150" HorizontalAlignment="Left">
<MenuItem HorizontalContentAlignment="Stretch">
<MenuItem.Header>
<Button x:Name="helpBtn" ... IsHitTestVisible="False">
...
</Button>
</MenuItem.Header>
<MenuItem Click="helpBtn_Click">
<MenuItem.Header>
<TextBlock Text="用户手册" HorizontalAlignment="Center"/>
</MenuItem.Header>
</MenuItem>
<Separator/>
<MenuItem>
<MenuItem.Header>
<TextBlock Text="设置参数" HorizontalAlignment="Center"/>
</MenuItem.Header>
</MenuItem>
</MenuItem>
</Menu>