如何识别鼠标在线上的单击?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个工作示例:(实现 Bala 在他的评论中建议的内容)
考虑到您已经有了将行添加到画布中的逻辑,
只需为您添加的每一行添加两个事件处理程序(如上所述)。
Here is a working example: (implementing what Bala suggested in his comment)
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.
我建议您向画布添加自定义 MouseDown 事件处理程序。事实上,如果您的线条非常细,您需要让用户能够单击线条附近来选择它。
为此,在自定义 MouseDown 处理程序中,迭代您的线条并执行以下操作:
对于每条线条:
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: