要實現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();
}
}
這樣就實現了一個不規則窗體。可以根據需要自定義窗體的樣式和布局。