Draggable Control ?

  • Thread starter Thread starter JezB
  • Start date Start date
J

JezB

I'm trying to create a user control which the user can drag around with the
mouse. Within my control the OnPaint method paints the control correctly,
and I handle OnMouseDown and OnMouseMove to control the dragging. Within the
OnMouseMove I calculate the new co-ordinates and move the control there
(this.Location = ...).

I'm sure my logic is correct in calculating the new co-ordinates. Trouble is
I'm getting a curious flashing double image moving as I drag, and neither
seems to follow the mouse exactly, but I dont know why.

Am I approaching this from the wrong angle ? Do I have to handle dragging
the control within the Parent of that control ? I was trying to put
everything inside the control itself.

Any guidance appreciated. I'm sure someone must have created a draggable
control before, who could help me. Thanks.
 
* "JezB said:
I'm trying to create a user control which the user can drag around with the
mouse. Within my control the OnPaint method paints the control correctly,
and I handle OnMouseDown and OnMouseMove to control the dragging. Within the
OnMouseMove I calculate the new co-ordinates and move the control there
(this.Location = ...).

Post the code...
 
Are you using the coordinates your receive as event handler parameters?
If so - try to use Control.MousePosition instead.
 
Yes I think that is my mistake.

This gives screen co-ordinates - how can I translate these into new
co-ordinates for my control relative to its parent control ?
 
Hi,

Take a look at the PointToClient and PointToScreen methods.

Hope this helps

Chris Taylor
 
I do not believe it is your mistake, it's just Windows' behaviour you have
to find workaround.
You can convert coordinates using PointToClient of the control which is
parent to the one you are moving.
You may also avoid conversion (that what I did).
When the "drag" begins (in OnMouseDown, for example) you can remember the
current mouse cursor's position.
Each time you receive mouse move event (in OnMouseMove) you can calculate
the distance between the remembered position and the current and update
position accordingly. (Certainly, if the mouse button is yet pressed.) After
that, you'll certainly need to update the remembered position as well.
I could post my code here, but it contains also resize functionality and
might mislead you.
 
Well actually mine has resize functionality too ! But I've played around
with it and managed to get it all to work. Thanks.
 
Back
Top