Hi, I need to create a control that the user may move within a panel. It
works fine until I add it to a panel that has AutoScroll. The control
has a transparent background, but I don't think it would be the problem.
This is my actual code, most taken from web pages:
> bool dragging = false;
> int mousex, mousey;
>
> void DrawViewer_MouseUp(object sender, MouseEventArgs e)
> {
> if (dragging)
> {
> dragging = false;
> Cursor.Clip = Rectangle.Empty;
> this.Invalidate();
> }
> }
>
> void DrawViewer_MouseMove(object sender, MouseEventArgs e)
> {
> if (dragging)
> {
> Point MPosition = ((ScrollableControl)this.Parent).PointToClient(MousePosition);
> MPosition.Offset(mousex, mousey);
> this.Location = MPosition;
> //this.Invalidate();
> }
> }
>
> void DrawViewer_MouseDown(object sender, MouseEventArgs e)
> {
> if (e.Button == MouseButtons.Left)
> {
> dragging = true;
> mousex = -e.X;
> mousey = -e.Y;
> int clipleft = ((ScrollableControl)this.Parent).PointToClient(MousePosition).X - this.Location.X;
> int cliptop = ((ScrollableControl)this.Parent).PointToClient(MousePosition).Y - this.Location.Y;
> int clipwidth = ((ScrollableControl)this.Parent).ClientSize.Width - (this.Width - clipleft);
> int clipheight = ((ScrollableControl)this.Parent).ClientSize.Height - (this.Height - cliptop);
> Cursor.Clip = ((ScrollableControl)this.Parent).RectangleToScreen(new Rectangle(clipleft, cliptop, clipwidth, clipheight));
> //this.Invalidate();
> }
> }
I tried both overriding and handling the mouse events. I also thought at
first the problem could be the Clip, but I finally discarded it. The
main two problems are:
1) If you move the mouse fast, it starts mis-behaving.
2) If you go beyond the limits of the actual portion of the panel (when
AutoScroll starts to work), the scrollbars start to go up and down, and
the control gets out of control :-)
Any ideas?
Thanks in advance