简体   繁体   中英

How to move rectangle with mouse

I want to move a rectangle on the screen. This is the code for what I did meanwhile:

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)
    {
    }
}

I searched a lot and didn't find what code should I write in "OnMouseMove" and "OnClick" in order for the mouse to move.

Ryan Reynolds!

Make blublublu a field. You will have to utilize MouseMove , MouseUp , MouseDown :

    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();
        }
    }

When mouse is down, you capture it (to receive mousemove events even when mouse it outside of control) and remember coordinates. During mouse movement you change current coordinates of blublublu by difference (saved mouse position minus current) and call Invalidate .

You may make the Location variable and invalidate the control on mouse move. In addition, this will only happen when the left mouse button is pressed

Please note that i won't override OnMouseMove , but rather subscribe to the event by adding another handler.

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
}

Notice that I added this.SetStyle(...) in the constructor, as this will prevent flickering when you move the rectangle. Also I changed this.Location to mouseLocation in the OnPaint method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM