当WindowStyle=“None”时,设置AllowsTransparency="True",则不会出现黑色Border,然后可以另外设置外边的Border和BorderThickness;
当WindowStyle=“None”时,生成的窗体无法移动,此时需要定义MouseMove="Window_MouseMove"事件,实现鼠标左键按下可拖动窗口;
xaml代码如下:
<Window x:Class="CustomWindowDemo.MainWindow" 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:local="clr-namespace:CustomWindowDemo" mc:Ignorable="d" MouseMove="Window_MouseMove" WindowStyle="None" AllowsTransparency="True" BorderBrush="Orange" BorderThickness="3,0,3,3" Title="MainWindow" Height="300" Width="525"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="40"></RowDefinition> <RowDefinition Height="*"></RowDefinition> <RowDefinition Height="80"></RowDefinition> </Grid.RowDefinitions> <StackPanel Grid.Row="0" Background="Orange"> <TextBlock Margin="20,5,5,5" Foreground="Red" Text="TestWindow" FontSize="14" VerticalAlignment="Center"/> </StackPanel> <TextBlock Margin="20,30" FontSize="16" Text="This is a Test Window of customized." Grid.Row="1" TextAlignment="Center" VerticalAlignment="Center"></TextBlock> <StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Center"> <Button Name="Cancel" Click="Cancel_Click" Margin="0,5,20,5" Height="40" Width="60">Cancel</Button> <Button Name="Confirm" Click="Confirm_Click" Margin="20,5,0,5" Height="40" Width="60">Confirm</Button> </StackPanel> </Grid> </Window>
后台代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace CustomWindowDemo { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Confirm_Click(object sender, RoutedEventArgs e) { this.Close(); } private void Cancel_Click(object sender, RoutedEventArgs e) { this.Close(); } private void Window_MouseMove(object sender, MouseEventArgs e) { if (e.LeftButton == MouseButtonState.Pressed) { this.DragMove(); //Window.DragMove(); } } } }
时间: 2024-10-25 05:21:30