Question

I'm new to programming in C# and wanted to ask for a little bit of help. I'm currently trying to move a color-filled rectangle that I draw on a Windows Application form with my left mouse button, and I'm trying to drag-and-drop it to another location using my right mouse button. Currently I've managed to draw the rectangle, but the right click is dragging the entire form along.

Here's my code:

public partial class Form1 : Form
{
    private Point MouseDownLocation;

    public Form1()
    {
        InitializeComponent();
        this.DoubleBuffered = true;
    }
    Rectangle rec = new Rectangle(0, 0, 0, 0);

    protected override void OnPaint(PaintEventArgs e)
    {   
        e.Graphics.FillRectangle(Brushes.DeepSkyBlue, rec);
    }


    protected override void OnMouseDown(MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left) 
        {
            rec = new Rectangle(e.X, e.Y, 0, 0);
            Invalidate();

        }
        if (e.Button == MouseButtons.Right)
        {
            MouseDownLocation = e.Location;
        }
    }
    protected override void OnMouseMove(MouseEventArgs e)
    {             

        if (e.Button == MouseButtons.Left)
        {
            rec.Width = e.X - rec.X;
            rec.Height = e.Y - rec.Y;
            Invalidate();
        }

        if (e.Button == MouseButtons.Right)
        {
            this.Left = e.X + this.Left - MouseDownLocation.X;
            this.Top = e.Y + this.Top - MouseDownLocation.Y;

        }
    }

}

I only need to drag-and-drop the rectangles with right mouse button.

EDIT: Thanks to you all i got my answer very fast and here's the code that works:

public partial class Form1 : Form
{
    private Point MouseDownLocation;

    public Form1()
    {
        InitializeComponent();
        this.DoubleBuffered = true;            
    }

    Rectangle rec = new Rectangle(0, 0, 0, 0);

    protected override void OnPaint(PaintEventArgs e)
    {
        e.Graphics.FillRectangle(Brushes.DeepSkyBlue, rec);
        //Generates the shape            
    }

    protected override void OnMouseDown(MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        //can also use this one:
        //if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            rec = new Rectangle(e.X, e.Y, 0, 0);
            Invalidate();

        }
        if (e.Button == MouseButtons.Right)
        {
            MouseDownLocation = e.Location;
        }
    }
    protected override void OnMouseMove(MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            rec.Width = e.X - rec.X;
            rec.Height = e.Y - rec.Y;
            this.Invalidate();
        }

        if (e.Button == MouseButtons.Right)
        {
            rec.Location = new Point((e.X - MouseDownLocation.X) + rec.Left, (e.Y - MouseDownLocation.Y) + rec.Top);
            MouseDownLocation = e.Location;
            this.Invalidate();
        }
    }

}
Was it helpful?

Solution

This seems to work

    protected override void OnMouseMove(MouseEventArgs e)
    {


        if (e.Button == MouseButtons.Left)
        {
            rec.Width = e.X - rec.X;
            rec.Height = e.Y - rec.Y;
            this.Invalidate();
        }

        if (e.Button == MouseButtons.Right)
        {
            rec.Location = new Point((e.X-MouseDownLocation.X) + rec.Left, (e.Y-MouseDownLocation.Y) + rec.Top);
            MouseDownLocation = e.Location;
            this.Invalidate();
        }
    }

OTHER TIPS

try this one : Notice that I add timer to the form in this method you don't need to call Invalidate on mouse event the timertick is calling to the Refresh() so the form will paint himself in each tick..

public partial class Form1 : Form
{
   private Point MouseDownLocation;

   public Form1()
   {
      InitializeComponent();
      this.DoubleBuffered = true;
      timer1.Start(); // add timer to the form
   }
   Rectangle rec = new Rectangle(0, 0, 0, 0);

   protected override void OnPaint(PaintEventArgs e)
   {
       e.Graphics.FillRectangle(Brushes.DeepSkyBlue, rec);
   }

   private void timer1_Tick(object sender, EventArgs e)
   {
       Refresh();
   }


  protected override void OnMouseMove(MouseEventArgs e)
  {
      if (e.Button == MouseButtons.Left)
      {
         rec.Width = e.X - rec.X;
         rec.Height = e.Y - rec.Y;
      }
      else if (e.Button == MouseButtons.Right)
      {
        rec.X = e.X - MouseDownLocation.X;
        rec.Y = e.Y - MouseDownLocation.Y;
      }
  }

   protected override void OnMouseUp(object sender, MouseEventArgs e)
   {
       if (e.Button == MouseButtons.Right)
          MouseDownLocation = e.Location;
   }
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top