我的程序中基本上有一个简单的问题,我只想确保正确。它应该在单击鼠标按钮时添加MouseEventHandler
,然后随着鼠标移动圆圈,直到删除事件处理程序。我将代码简化为最基本的:
XAML:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid Name="grid1" Background="White" MouseLeftButtonUp="grid_MouseUp">
<Ellipse Height="50" HorizontalAlignment="Left" Margin="12,12,0,0" Name="ellipse1" Stroke="{x:Null}" VerticalAlignment="Top" Width="50" Fill="Black" MouseLeftButtonDown="ellipse1_MouseDown" />
</Grid>
</Window>
C#:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private static Point _oldPoint = new Point(), _newPoint = new Point();
private void ellipse1_MouseDown(object sender, MouseButtonEventArgs e)
{
_oldPoint = e.GetPosition(grid1);
grid1.MouseMove += new MouseEventHandler(grid_MouseMove);
}
private void grid_MouseUp(object sender, MouseButtonEventArgs e)
{
grid1.MouseMove -= new MouseEventHandler(grid_MouseMove);
}
private void grid_MouseMove(object sender, MouseEventArgs e)
{
_newPoint = e.GetPosition(grid1);
ellipse1.Margin = new Thickness(ellipse1.Margin.Left - _oldPoint.X + _newPoint.X, ellipse1.Margin.Top - _oldPoint.Y + _newPoint.Y, 0, 0);
_oldPoint = _newPoint;
}
}
现在总的来说,这段代码工作正常,我认为它非常整洁,因为它在实际按下按钮之前不会检查鼠标的移动。但是,我的问题如下:
我不得不将MouseMove
事件添加到网格而不是圆圈,因为一旦鼠标指针失去圆的焦点(由于鼠标移动太快),它就不会再触发 MouseMove 事件。但究竟为什么会这样呢?在事件开始时,鼠标肯定在圆圈上方,然后它移动了。是的,它远离了圆圈,但这不应该仍然触发事件吗?
您可以捕获鼠标并处理椭圆中的所有事件。
<Grid Name="grid1" Background="White">
<Ellipse Height="50" HorizontalAlignment="Left" Margin="12,12,0,0" Name="ellipse1" Stroke="{x:Null}" VerticalAlignment="Top" Width="50" Fill="Black"
MouseLeftButtonDown="ellipse1_MouseDown" MouseLeftButtonUp="ellipse1_MouseUp" />
</Grid>
隐藏此代码
private void ellipse1_MouseDown(object sender, MouseButtonEventArgs e)
{
Mouse.Capture(ellipse1);
_oldPoint = e.GetPosition(grid1);
ellipse1.MouseMove += new MouseEventHandler(ellipse1_MouseMove);
}
private void ellipse1_MouseUp(object sender, MouseButtonEventArgs e)
{
Mouse.Capture(null);
ellipse1.MouseMove -= new MouseEventHandler(ellipse1_MouseMove);
}
我已移动并将grid_MouseMove重命名为ellipse1_MouseMove。
再加上彼得所说的,如果你使用Grid.MouseDown
事件并检查oldPoint
是否在Ellipse
范围内,然后处理了MouseMove
事件,这种奇怪的行为就不会被看到。
我还建议探索drag
事件。
只要鼠标悬停在该特定控件上,控件才会获取鼠标事件。
如果移动到新控件,鼠标将从旧控件中解脱并挂接到新控件。
有一些方法可以创建一个附加到整个过程的全局钩子,但我想这不是我们正在谈论的。