学习WPF的第一天,尝试写比较常用的功能,无标题窗体拖动。
先在设计界面给它加上MouseDown事件
<Window x:Class="MyFirstWPFAPP.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:MyFirstWPFAPP"
mc:Ignorable="d"
Title="MainWindow" MouseDown="Window_MouseDown" Height="350" Width="525" BorderBrush="Black" BorderThickness="0.5" WindowStyle="None" SnapsToDevicePixels="True" ResizeMode="NoResize"> </Window>
在后台给事件加上代码
private void Window_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
this.DragMove();
}
}
需要注意的是如果不判断左右键的话会发生右键点击窗体时报异常,所以代码里if是判断是否是左键按下。:)