On Wed, 25 Jul 2007 00:41:07 -0700, Beorne <ma*******@gmail.comwrote:
Hello, I have to ralize a graph with a pair of straight vertical lines
that have to be dragged in the left-right direction with the mouse.
I've never done something similar so I wonder if someone could tell me
the key C# classes to use.
How are you drawing the straight vertical lines now?
There are two basic issues here:
* Drawing the lines
* Handling user input
Drawing the lines is relatively simple, and you may have already
implemented that. In your Control instance's OnPaint method, you use the
PaintEventArgs.Graphics instance to draw whatever you want, including
lines. For example, the Graphics class has a DrawLine() method that can
be used to draw a single line.
Handling the user input is also relatively simple. It involves handling
the mouse events. Again, in your Control instance you can override the
OnMouseDown, OnMouseMove, and OnMouseUp methods so that you can track what
the user does in your control.
In the OnMouseDown method, you do whatever hit-testing you need to figure
out which if any line the mouse was clicked on (compare the mouse
coordinates with the line coordinates...typically you'd actually allow for
the mouse to be some distance away from the line, rather than requiring it
to be exactly on the line). If you hit a line, then you set some sort of
data that indicates which line is being dragged (or if both lines are
always dragged together, you might be able to just set a flag saying that
you're dragging). You'll also need to save the mouse position passed in
the MouseEventArgs for reference later.
In the OnMouseMove method, if you are in the dragging state set in the
OnMouseDown method, you take the current mouse position, subtract it from
the mouse position saved in OnMouseDown, and use that as the offset for
the position of the line or lines being dragged, relative to their
original positions.
Finally, in the OnMouseUp method, you finalize whatever changes were made,
based on the location of the mouse.
As you can see, practically everything is done in your Control-derived
class. There are, of course, other classes you'll need to get data (e.g.
event argument classes) and manage your own data (points, rectangles,
etc.) But there's no other classes per se that are needed. Note that
your Control-derived class could in fact be a Form-derived class.
Pete