dragable control

  • Thread starter Thread starter Lloyd Dupont
  • Start date Start date
L

Lloyd Dupont

I have a new control I could drag.
when you clic on it, it capture the mouse and move itself OnMouseMove()
event.

however the refresh is unwillingly off when dragging.
I try to add
Parent.Invalidate();
Application.DoEvents();

in the mouse event handler, but I can't get any redraw, just when I release
the stylus does the control appear where it should be.

any idea ? (of how to redraw during the drag operation !) ?
 
I bet you're calling Invalidate() instead of Refresh().



using System;
using System.Drawing;
using System.Windows.Forms;

public class Form1 : System.Windows.Forms.Form
{
public Form1()
{
this.Text = "myForm";

myControl c = new myControl();
c.Parent = this;
c.Bounds = new Rectangle(10, 10, 80, 30);
}

protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}

static void Main()
{
Application.Run(new Form1());
}
}


public class myControl : Control
{
bool mouseCaptured = false;
Point pMouseDown = Point.Empty;

protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.FillRectangle(new SolidBrush(Color.Tomato),
this.ClientRectangle);

e.Graphics.DrawString(string.Format("{0}, {1}", this.Location.X,
this.Location.Y),
this.Font,
new SolidBrush(Color.Black),
10, 10);

base.OnPaint (e);
}

protected override void OnMouseDown(MouseEventArgs e)
{
this.mouseCaptured = true;

// remember where the mouse was clicked
this.pMouseDown = new Point(e.X, e.Y);
base.OnMouseDown (e);
}

protected override void OnMouseMove(MouseEventArgs e)
{
if(this.mouseCaptured)
{
this.Location = new Point(this.Left + e.X - this.pMouseDown.X,
this.Top + e.Y - this.pMouseDown.Y);
this.Refresh();
}

base.OnMouseMove (e);
}

protected override void OnMouseUp(MouseEventArgs e)
{
this.mouseCaptured = false;
base.OnMouseUp (e);
}
}








NETCF Beta FAQ's -- http://www.gotdotnet.com/team/netcf/FAQ.aspx
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top