WPF 去除系统窗体边框,自定义移动窗体

时间:2022-06-08 10:33:26

方法一:

去除系统窗体边框:

<Window x:Class="PracticeProject.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:PracticeProject"
mc:Ignorable="d"
Title="MainWindow" Height="642" Width="1280"
     WindowChrome.WindowChrome="{DynamicResource WindowChromeKey}">
<Window.Resources>
<WindowChrome x:Key="WindowChromeKey">
<WindowChrome.ResizeBorderThickness>
<Thickness>0</Thickness>
</WindowChrome.ResizeBorderThickness>
</WindowChrome>
</Window.Resources>

移动窗体事件:

        <Border Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="4" Background="#336699" MouseDown="Border_MouseDown"></Border>

移动窗体后台隐藏代码:

        /// <summary>
/// 移动窗体
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Border_MouseDown(object sender, MouseButtonEventArgs e)
{
this.DragMove();
}

别忘记,移动窗体的那个Border被元素遮挡的部分是移动不了的。

方法二:

<Window x:Class="PracticeProject.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:PracticeProject"
mc:Ignorable="d"
Title="MainWindow" Height="642" Width="1280" WindowStyle="None">