How best to handle mouse events for custom drawn objects?

G

Guest

I'm going to be creating shapes in a OnPaint method of an extended Panel
object, and I need mouse event functionality (onClick, onMouseOver) that kind
of stuff... what's the best way to go about doing this? Are there any design
patterns, algorithms for best performance?

Best way to translate mouse's x,y coords into a shape on my Panel
 
N

Nicholas Paldino [.NET/C# MVP]

MrNobody,

The best way I can think of doing this would be to have a class that
represents the shapes that you are going to perform hit tests against. Of
course, you would start off from an abstract class.

This class would have two methods, one to indicate the max rectangle
that it could represent, given the shapes size, and then a method to see if
the coordinates actually fall within the shape. The reason for two passes
is that some shapes could be overly complex, and finding out if a point
exists in the shape could be a lengthy calc. This way, by saying "the max
area that my shape can take up is this", you can perform a simple calc to
see if the point has a shot of existing in the shape. If it does, you can
then do the second, lengthy calc and see if the point falls in the shape.

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top