I have a WindowsForm application.
In this application i draw some circles so I have custom class named “Circle”
that know to draw herself (in this class there is a method public void draw(Graphics g))
now when the form is loading and i draw some circles, i want to let the user to be
able to drag the circles..
so my question is how to add a mouseHandler to the custom class Circle ?
i thought that this class should have it’s own mouseHandler but i don’t understand how
to do it..
Letting your Circle object directly deal with mouse events requires it derive from the Control class. That would be a big mistake, the Control class has several undesirable properties that makes it a poor fit for a shape doodah. Starting with it being a rectangular window and not supporting overlapping very well. These things are fixable but it is fairly painful and it just adds costly overhead to a class that is already very expensive.
Instead let it just be a class. With a method
bool HitTest(Point pos). And a methodvoid Move(Size dist). And create another class named ShapeCollection that stores a list of shapes. Also with a Paint and a HitTest method, they just iterate the collection of shapes.You can now easily implement the form’s Paint event, call the ShapeCollection.Paint method. And you can implement the MouseDown event, call HitTest and remember the index of the shape that returned true. And implement the MouseMove event, call Move on the selected shape and call Invalidate so the shape paints itself in the new position.
It is easier to get going in WPF, it doesn’t have the same problems as the Control class and has shape support built-in.