要实现WPF不规则窗体,可以按照以下步骤进行:
public partial class CustomWindow : Window
{
public CustomWindow()
{
InitializeComponent();
}
private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DragMove();
}
private void Window_Resize(object sender, MouseButtonEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
if (WindowState == WindowState.Maximized)
{
WindowState = WindowState.Normal;
}
else
{
WindowState = WindowState.Maximized;
}
}
}
}
<Window x:Class="WpfApp1.CustomWindow"
...
Style="{StaticResource CustomWindowStyle}"
MouseLeftButtonDown="Window_MouseLeftButtonDown"
MouseLeftButtonUp="Window_Resize">
<Grid>
<!-- 窗体内容 -->
</Grid>
</Window>
<Application.Resources>
<Style TargetType="Window" x:Key="CustomWindowStyle">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Window}">
<Border Background="Transparent">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Border Grid.Row="0" Background="Gray" Height="30">
<!-- 窗体标题栏 -->
</Border>
<ContentPresenter Grid.Row="1"/>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="WindowStyle" Value="None"/>
<Setter Property="AllowsTransparency" Value="True"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="ResizeMode" Value="CanResizeWithGrip"/>
</Style>
</Application.Resources>
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
MainWindow mainWindow = new CustomWindow();
mainWindow.Show();
}
}
这样就实现了一个不规则窗体。可以根据需要自定义窗体的样式和布局。