如何识别鼠标在线上的单击?

发布于 2024-12-06 16:44:40 字数 208 浏览 0 评论 0原文

我有一个 WPF 应用程序。有一块画布。当用户将鼠标拖动到画布上(从鼠标向下到鼠标向上)时,我会画线。当按下鼠标时,我采用初始点;当用户向上移动鼠标时,我采用最终点。然后,我计算距离并在简单的鼠标按下、移动和向上事件中绘制线条。

在画布上绘制许多线条后,我单击其中任何一条线条。我想选择该行并向用户显示该行已被选择(例如通过更改该行的颜色)。所以用户可以删除它。

谢谢。

I have a WPF application. There is a canvas. I draw line when user drag the mouse over the canvas (from mouse down to mouse up). I take initial point, when mouse is pressed down and final point, when user does mouse up. Then I calculate the distance and draw the line in simple mouse down, move and up events.

After drawing many lines on canvas, I click on any one of the line. I want to select the line and show the user that line is selected (like by changing the color of the line). So user can delete it.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

零崎曲识 2024-12-13 16:44:40

这是一个工作示例:(实现 Bala 在他的评论中建议的内容)

private void myCanvas_Loaded(object sender, RoutedEventArgs e)
        {
            Line line = new Line();

            line.MouseDown += new MouseButtonEventHandler(line_MouseDown);
            line.MouseUp   += new MouseButtonEventHandler(line_MouseUp);

            line.Stroke = Brushes.Black;
            line.StrokeThickness = 2;
            line.X1 = 30; line.X2 = 80;
            line.Y1 = 30; line.Y2 = 30;

            myCanvas.Children.Add(line);
        }

void line_MouseUp(object sender, MouseButtonEventArgs e)
        {
            // Change line colour back to normal 
            ((Line)sender).Stroke = Brushes.Black;
        }

void line_MouseDown(object sender, MouseButtonEventArgs e)
        {
            // Change line Colour to something
            ((Line)sender).Stroke = Brushes.Red;
        }

考虑到您已经有了将行添加到画布中的逻辑,

只需为您添加的每一行添加两个事件处理程序(如上所述)。

Here is a working example: (implementing what Bala suggested in his comment)

private void myCanvas_Loaded(object sender, RoutedEventArgs e)
        {
            Line line = new Line();

            line.MouseDown += new MouseButtonEventHandler(line_MouseDown);
            line.MouseUp   += new MouseButtonEventHandler(line_MouseUp);

            line.Stroke = Brushes.Black;
            line.StrokeThickness = 2;
            line.X1 = 30; line.X2 = 80;
            line.Y1 = 30; line.Y2 = 30;

            myCanvas.Children.Add(line);
        }

void line_MouseUp(object sender, MouseButtonEventArgs e)
        {
            // Change line colour back to normal 
            ((Line)sender).Stroke = Brushes.Black;
        }

void line_MouseDown(object sender, MouseButtonEventArgs e)
        {
            // Change line Colour to something
            ((Line)sender).Stroke = Brushes.Red;
        }

Considering that you already have the logic to add the lines into canvas,

Simply add the two event handlers (as above) for every line that you add.

留蓝 2024-12-13 16:44:40

我建议您向画布添加自定义 MouseDown 事件处理程序。事实上,如果您的线条非常细,您需要让用户能够单击线条附近来选择它。

为此,在自定义 MouseDown 处理程序中,迭代您的线条并执行以下操作:

对于每条线条:

  • 创建一个矩形,线条长度为宽度和高度 = max(lineWidth, 10px),
  • 围绕矩形旋转鼠标坐标中心与线角相等(用math.atan2计算),
  • 检查新的鼠标坐标是否位于矩形内,
  • 如果是,则选择当前留置并断开。

I would advise you to add a custom MouseDown event handler to your canvas. Indeed, if your lines are very thin, you need to let the user be able to click near a line to select it.

For this, in your custom MouseDown handler, iterate over your lines and do the following:

For each line:

  • Create a rectangle with the length of the line as width and height = max(lineWidth, 10px),
  • Rotate your mouse coordinates around the rectangle center with an angle equals to the line angle (compute it with math.atan2),
  • Check if the new mouse coordinates lie inside the rectangle,
  • If so, select the current lien and break.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文