Home > OS >  How to detect whether the Mouse Hovers above a drawn line in C#
How to detect whether the Mouse Hovers above a drawn line in C#

Time:05-07

I have a panel on a form. On this panel there are drawn a few lines. I would like to know how to detect when the Mouse is above one of the lines and get the details of the line.

CodePudding user response:

  1. Keep track of your lines in a data structure, e.g. an array, as you draw them.

  2. Create a handler for the panel's MouseMove event.

  3. In the handler, iterate over the (array of) lines and compute the distance from the mouse's position to the nearest point on the line (see this article for the math).

  4. If the distance is below a certain threshold, display the information.

  • Related