2014-02-18 94 views
0

我想在屏幕上移动矩形。 这是代码,我所做的同时:如何用鼠标移动矩形

internal class GraphicContainer : Control 
{ 
    //---------------------METHODS--------------------- 
    public GraphicContainer(Control control, string text, int left, int top) 
     : base(control, text, left, top, 400, 200) 

    protected override void OnPaint(PaintEventArgs pe) 
    { 
     // Call the OnPaint method of the base class. 
     base.OnPaint(pe); 

     // Declare and instantiate a new pen. 
     Pen pen = new Pen(Color.Fuchsia, 15); 
     SolidBrush myBrush= new System.Drawing.SolidBrush(Color.HotPink); 
     // Draw an aqua rectangle in the rectangle represented by the control. 
     //pe.Graphics.DrawRectangle(pen, new Rectangle(this.Location,this.Size)); 
     Rectangle blublublu = new Rectangle(this.Location, this.Size - new Size(25, 25)); 
     pe.Graphics.DrawRectangle(pen,blublublu); 
     pe.Graphics.FillRectangle(myBrush,blublublu); 
    } 

    protected override void OnMouseMove(MouseEventArgs e) 
    {   
    } 

    protected override void OnClick(EventArgs e) 
    { 
    } 
} 

我搜索了很多,并没有发现我应该写什么代码“的OnMouseMove”和“点击”,以使鼠标移动。

+0

为什么不直接存储OnMouseMove中给定的坐标,并用它们来填充blublublu – Rob

+0

听起来像某种Drag'n'Drop。在野外有很多教程。 – TGlatzer

回答

0

Ryan Reynolds!

使blublublu成为一个领域。 你将不得不使用MouseMoveMouseUpMouseDown

Rectangle blublublu = new Rectangle(...); // possibly init in constructor 

    protected override void OnMouseDown(MouseEventArgs e) 
    { 
     if (e.Button == MouseButtons.Left) 
     { 
      Capture = true; 
      _x = e.X; // remember coords 
      _y = e.Y; 
     } 
     base.OnMouseDown(e); 
    } 

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

    protected override void OnMouseMove(MouseEventArgs e) 
    { 
     if (e.Button == MouseButtons.Left) 
     { 
      blublublu.X += _x - e.X; // not tested, maybe use Rectangle.Offset or create a new Rectangle 
      blublublu.Y += _y - e.Y; 
      Invalidate(); 
     } 
    } 

当按下鼠标时,你捕捉它(即使接收鼠标移动事件,当鼠标的控制权外)并记住坐标。在鼠标移动过程中,您通过差异(保存的鼠标位置减去当前值)更改blublublu的当前坐标并致电Invalidate

0

您可能会使Location变量并使鼠标移动时的控件失效。此外,只有在按下鼠标左键时才会发生这种情况。

请注意,我不会覆盖OnMouseMove,而是通过添加其他处理程序来订阅该事件。

Point mouseLocation; 

public GraphicContainer(Control control, string text, int left, int top) 
    : base(control, text, left, top, 400, 200) { 
    //prevents flickering 
    this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true); 

    //subscribes to mousemove 
    this.MouseMove += (obj,e) => { 
     //only when left mousebutton is pressed 
     if (e.Button == System.Windows.Forms.MouseButtons.Left) 
     { 
      //update the location 
      mouseLocation = e.Location; 
      //invalidate the control 
      this.Invalidate(); 
     } 
    }; 
} 

protected override void OnPaint(PaintEventArgs pe) 
{ 
    // Call the OnPaint method of the base class. 
    base.OnPaint(pe); 

    // Declare and instantiate a new pen. 
    Pen pen = new Pen(Color.Fuchsia, 15); 
    SolidBrush myBrush = new System.Drawing.SolidBrush(Color.HotPink); 
    // Draw an aqua rectangle in the rectangle represented by the control. 
    Rectangle blublublu = new Rectangle(mouseLocation, this.Size - new Size(25, 25)); 
     pe.Graphics.DrawRectangle(pen, blublublu); 
    pe.Graphics.FillRectangle(myBrush, blublublu); 
} 

protected override void OnMouseMove(MouseEventArgs e) 
{ 
    //nothing here 
} 

请注意,我在构造函数中添加this.SetStyle(...),因为这将防止闪烁,当你移动矩形。我还在OnPaint方法中将this.Location更改为mouseLocation