鼠标点击 [英] mouse clicked

查看:105
本文介绍了鼠标点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果单击绘制的线条,我现在该怎么办?线条可以区分为对象吗? :confused:

How could I now If I clicked the lines that I drawn? Are lines can be distinguish as objects? :confused:

推荐答案

我假设您使用Graphics.DrawLine绘制了线条?
如果是这样,则否,它们不是独立的对象,因此无法单击它们.

如果需要,则需要维护它们开始和结束的位置的列表-大概还是这样做,以便您每次在Paint事件中都可以重新绘制它们,否则它们将消失.

然后,您可以检查您单击的位置是否在表单或面板事件的行上.

但是请注意,仅当直线是水平或垂直时,这才是简单的操作:如果直线倾斜,则必须确切地确定其绘制方式,以确保单击或断开特定直线! >
您正在尝试做什么:可能有一种更简单的方法来实现它.
I assume you drew the lines using Graphics.DrawLine?
If so, then no, they are not separate objects, and you cannot click them.

If you need to, then you need to maintain a list of where they start and end - presumably you do this anyway so that you can re-draw them each time in the Paint event, otherwise they will disappear.

You can then check if where you clicked is on the line in your form or panel event.

Be aware though, it is only simple to do if the line is horizontal or vertical: if it slopes, you will have to work out exactly how it is drawn to be sure if a click on on or off a particular line!

What are you trying to do: there may be an easier way to achieve it.


这不是一件容易的事.我已经建议切换到WPF中解决此问题.随着问题的累积,System.Windows.Forms中的进度会变慢.

点击测试之类的问题就是其中之一.单击时会触发鼠标单击事件,但是如果未单击任何对象,则需要忽略它.对于线(细对象),重要的是在线周围定义一个区域,该区域被视为线的附近.它应该比1像素宽一些,否则用户几乎永远不会点击它.如果您说的是直线段,则可能需要一个覆盖您的线的矩形区域.所需邻域宽度的定义(通常使用半径"进行定义)是分辨率和访问方便性之间的权衡问题.使其具有可变性(取决于比例)或可配置(或两者都有)是很有意义的.
This is not very easy. This problem is solved in WPF which I already recommended to switch to. You progress in System.Windows.Forms gets slower as your problem accumulate.

A problem of like click test is one of them. You mouse click event fires when you click, but you need ignore it if no object is clicked. In case of line (thin object) it''s important to define an area around a line which is considered its vicinity. It should be something wider then 1 pixel, otherwise the user will almost never hit it. If you''re talking a straight line segment, you may need a rectangular area that covers your line. The definition of required width of vicinity (usually definitions works with "radius") is a matter of trade-of between resolution and ease of access. Having it variable (depending on scale) or configurable (or both) makes sense.


这篇关于鼠标点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆