这是一个旧帖子,但是(我认为)这是一个更好的方法:
我认为你不应该使用布尔值来检查鼠标是否正在拖动当前控件。另外,使用PreviewMouse....
是获取鼠标位置的无用修复(当鼠标不在控件上方时)
你应该捕获鼠标。当您捕获鼠标时,即使鼠标不在控件上方,仍然会引发 mouse_move 事件(即使关闭当前窗口)。我认为这才是应该做的。它更简单、更容易阅读。
这是代码:
public partial class VisualBlock : UserControl
{
private Point _positionInBlock;
public VisualBlock()
{
InitializeComponent();
}
private void UserControl_MouseDown(object sender, MouseButtonEventArgs e)
{
// when the mouse is down, get the position within the current control. (so the control top/left doesn't move to the mouse position)
_positionInBlock = Mouse.GetPosition(this);
// capture the mouse (so the mouse move events are still triggered (even when the mouse is not above the control)
this.CaptureMouse();
}
private void UserControl_MouseMove(object sender, MouseEventArgs e)
{
// if the mouse is captured. you are moving it. (there is your 'real' boolean)
if (this.IsMouseCaptured)
{
// get the parent container
var container = VisualTreeHelper.GetParent(this) as UIElement;
if(container == null)
return;
// get the position within the container
var mousePosition = e.GetPosition(container);
// move the usercontrol.
this.RenderTransform = new TranslateTransform(mousePosition.X - _positionInBlock.X, mousePosition.Y - _positionInBlock.Y);
}
}
private void UserControl_MouseUp(object sender, MouseButtonEventArgs e)
{
// release this control.
this.ReleaseMouseCapture();
}
}
Update:
如果您有一个窗口,例如带有画布,则可以将此用户控件添加到画布中。
这是它的要点:WPF 移动控件
<Window x:Class="WPFTestControlMoveMouse.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:WPFTestControlMoveMouse"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30" />
<RowDefinition />
</Grid.RowDefinitions>
<Button Grid.Row="0" Click="Button_Click">New</Button>
<Canvas x:Name="MyCanvas" Grid.Row="1"></Canvas>
</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 WPFTestControlMoveMouse
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
MyCanvas.Children.Add(new VisualBlock());
}
}
}
<UserControl x:Class="WPFTestControlMoveMouse.VisualBlock"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WPFTestControlMoveMouse"
mc:Ignorable="d"
Width="300" Height="200" MouseDown="UserControl_MouseDown" MouseUp="UserControl_MouseUp" MouseMove="UserControl_MouseMove">
<Grid>
<Border BorderBrush="Black" BorderThickness="1" Background="Silver" />
<TextBlock FontSize="48" HorizontalAlignment="Center" VerticalAlignment="Center">Hi there!!</TextBlock>
</Grid>
</UserControl>
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 WPFTestControlMoveMouse
{
/// <summary>
/// Interaction logic for VisualBlock.xaml
/// </summary>
public partial class VisualBlock : UserControl
{
private Point _positionInBlock;
public VisualBlock()
{
InitializeComponent();
}
private void UserControl_MouseDown(object sender, MouseButtonEventArgs e)
{
// when the mouse is down, get the position within the current control. (so the control top/left doesn't move to the mouse position)
_positionInBlock = Mouse.GetPosition(this);
// capture the mouse (so the mouse move events are still triggered (even when the mouse is not above the control)
this.CaptureMouse();
}
private void UserControl_MouseMove(object sender, MouseEventArgs e)
{
// if the mouse is captured. you are moving it. (there is your 'real' boolean)
if (this.IsMouseCaptured)
{
// get the parent container
var container = VisualTreeHelper.GetParent(this) as UIElement;
// get the position within the container
var mousePosition = e.GetPosition(container);
// move the usercontrol.
this.RenderTransform = new TranslateTransform(mousePosition.X - _positionInBlock.X, mousePosition.Y - _positionInBlock.Y);
}
}
private void UserControl_MouseUp(object sender, MouseButtonEventArgs e)
{
// release this control.
this.ReleaseMouseCapture();
}
}
}
通过从标签控件派生来用鼠标移动标签:
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace WPFTestControlMoveMouse
{
public class MyLabel : Label
{
private Point _positionInBlock;
protected override void OnMouseDown(MouseButtonEventArgs e)
{
// when the mouse is down, get the position within the current control. (so the control top/left doesn't move to the mouse position)
_positionInBlock = Mouse.GetPosition(this);
// capture the mouse (so the mouse move events are still triggered (even when the mouse is not above the control)
this.CaptureMouse();
}
protected override void OnMouseMove(MouseEventArgs e)
{
// if the mouse is captured. you are moving it. (there is your 'real' boolean)
if (this.IsMouseCaptured)
{
// get the parent container
var container = VisualTreeHelper.GetParent(this) as UIElement;
// get the position within the container
var mousePosition = e.GetPosition(container);
// move the usercontrol.
this.RenderTransform = new TranslateTransform(mousePosition.X - _positionInBlock.X, mousePosition.Y - _positionInBlock.Y);
}
}
protected override void OnMouseUp(MouseButtonEventArgs e)
{
// release this control.
this.ReleaseMouseCapture();
}
}
}