我目前正在研究一种包含某些选择工具的图像处理工具。为了向用户显示当前选择(例如一个圆圈),使用了三个事件(都在某个画布内):
public MainWindow()
{
InitializeComponent();
}
private Ellipse _ellip = new Ellipse();
private Point _p = new Point();
private bool _mouse = false;
private double _xcor, _ycor;
private void mouseDown(object sender, MouseButtonEventArgs e)
{
_p = Mouse.GetPosition(canvas1);
_xcor = _p.X;
_ycor = _p.Y;
_mouse = true;
}
private void newPos(object sender, MouseEventArgs e)
{
if (mouse)
{
_ellip.Visibility = Visibility.Visible;
_p = Mouse.GetPosition(canvas1);
_ellip.Margin = new Thickness((_xcor + _p.X) / 2 - Math.Sqrt((_xcor - _p.X) * (_xcor - _p.X) + (_ycor - _p.Y) * (_ycor - _p.Y)) / 2, (_ycor + _p.Y) / 2 - Math.Sqrt((_xcor - _p.X) * (_xcor - _p.X) + (_ycor - _p.Y) * (_ycor - _p.Y)) / 2, 0, 0);
ellip.Height = Math.Sqrt((_xcor - _p.X) * (_xcor - _p.X) + (_ycor - _p.Y) * (_ycor - _p.Y));
ellip.Width = Math.Sqrt((_xcor - _p.X) * (_xcor - _p.X) + (_ycor - _p.Y) * (_ycor - _p.Y));
}
InvalidateVisual();
}
private void mouseUp(object sender, MouseButtonEventArgs e)
{
mouse = false;
ellip.Visibility = Visibility.Hidden;
}
这在一个条件下一切正常:圆圈后面有某种形状。如果在 canvas1 中的空白区域使用事件 MouseLeftButtonDown/Up 和 MouseMove,不知何故不会引发。一旦我创建了一个填充画布的矩形,一切正常。但这带来了另一个问题:如果矩形比画布大,它基本上会超出画布,并且事件也在画布之外工作(只要它位于矩形顶部)。我真的不明白这里出了什么问题。