天天看点

鼠标拖动这个矩形(GDI+)

public   partial   class   MoveRect   :   Form 
{ 
private   Rectangle   m_Rect; 
private   Point   m_LastMSPoint; 
public   MoveRect() 
{ 
InitializeComponent(); 
this.SetStyle(ControlStyles.AllPaintingInWmPaint   |   ControlStyles.OptimizedDoubleBuffer   |   ControlStyles.UserPaint,   true); 
m_Rect   =   new   Rectangle(10,   10,   50,   30); 
} 
protected   override   void   OnPaint(PaintEventArgs   e) 
{ 
base.OnPaint(e); 
e.Graphics.FillRectangle(SystemBrushes.ControlDark,   this.m_Rect); 
e.Graphics.DrawRectangle(SystemPens.ControlDarkDark,   this.m_Rect); 
} 
protected   override   void   OnMouseDown(MouseEventArgs   e) 
{ 
base.OnMouseDown(e); 
this.m_LastMSPoint   =   e.Location; 
} 
protected   override   void   OnMouseMove(MouseEventArgs   e) 
{ 
base.OnMouseMove(e); 
if   (e.Button   !=   MouseButtons.Left) 
{ 
return; 
} 
this.m_Rect.Offset(e.Location.X   -   this.m_LastMSPoint.X,   e.Location.Y   -   this.m_LastMSPoint.Y); 
//绘制新的图形 
this.Invalidate(); 
this.m_LastMSPoint   =   e.Location; 
} 
}
      

继续阅读