private Point downPoint;
private Rectangle downRectangle;
private Rectangle lastRectangle;
private void panelMain_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left)
{
return;
}
downPoint = e.Location;
downRectangle = new Rectangle(0, 0, ((Control)sender).Width, control.Height);
downRectangle.Offset(((Control)sender).PointToScreen(new Point(0, 0)));
ControlPaint.DrawReversibleFrame(downRectangle, Color.White, FrameStyle.Thick);
lastRectangle = downRectangle;
} private void panelMain_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left)
{
return;
}
ControlPaint.DrawReversibleFrame(lastRectangle, Color.White, FrameStyle.Thick);
Rectangle rectangle = downRectangle;
rectangle.Offset(e.X - downPoint.X, e.Y - downPoint.Y);
ControlPaint.DrawReversibleFrame(rectangle, Color.White, FrameStyle.Thick);
lastRectangle = rectangle;
}
private void panelMain_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left)
{
return;
}
ControlPaint.DrawReversibleFrame(lastRectangle, Color.White, FrameStyle.Thick);
control.Location = new Point(((Control)sender).Location.X + e.X - downPoint.X, ((Control)sender).Location.Y + e.Y - downPoint.Y);
IsDepartControl(sender);
}
private Rectangle downRectangle;
private Rectangle lastRectangle;
private void panelMain_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left)
{
return;
}
downPoint = e.Location;
downRectangle = new Rectangle(0, 0, ((Control)sender).Width, control.Height);
downRectangle.Offset(((Control)sender).PointToScreen(new Point(0, 0)));
ControlPaint.DrawReversibleFrame(downRectangle, Color.White, FrameStyle.Thick);
lastRectangle = downRectangle;
} private void panelMain_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left)
{
return;
}
ControlPaint.DrawReversibleFrame(lastRectangle, Color.White, FrameStyle.Thick);
Rectangle rectangle = downRectangle;
rectangle.Offset(e.X - downPoint.X, e.Y - downPoint.Y);
ControlPaint.DrawReversibleFrame(rectangle, Color.White, FrameStyle.Thick);
lastRectangle = rectangle;
}
private void panelMain_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left)
{
return;
}
ControlPaint.DrawReversibleFrame(lastRectangle, Color.White, FrameStyle.Thick);
control.Location = new Point(((Control)sender).Location.X + e.X - downPoint.X, ((Control)sender).Location.Y + e.Y - downPoint.Y);
IsDepartControl(sender);
}
你的form需要做的事情就是初始化form,并传入form即可。Init(Control control){}
把那些画图的工作都交给ControlPaint这个类就行了。
另外,画图时尽量在Control_Paint(objece,e)里画。别用在mousemove里也画,mousedown里也画。