Wie zu zeichnen und zu bewegen Formen mit der Maus in C#

Ich bin neu in der Programmierung in C# und wollte mal Fragen, für ein bisschen Hilfe. Ich bin derzeit versuchen zu bewegen eine Farbe gefülltes Rechteck, zeichne ich auf einem Windows-Formular mit meiner linken Maus-Knopf, und ich bin versucht zu drag-und-drop an eine andere Position mit der rechten Maustaste. Derzeit habe ich es geschafft um das Rechteck zu zeichnen, aber die Rechte Maustaste ist, ziehen Sie die gesamte form zusammen.

Hier ist mein 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;

        }
    }

}

Brauche ich nur um die drag-und-drop die Rechtecke mit der rechten Maus-Taste.

BEARBEITEN: Dank an Euch alle, ich habe meine Antwort sehr schnell und hier ist der code, der funktioniert:

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

}

InformationsquelleAutor User_T | 2013-04-12

Schreibe einen Kommentar