0

he intentado crear un código en para crear un rectángulo con el mouse pero no me ha funcionado.

¿Alguien me puede ayudar?

    public partial class Form1 : Form
    {
        public Form1()
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        { }



        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            using (Graphics g = this.panel1.CreateGraphics())
            {
                Pen pen = new Pen(Color.Black, 2);
                Brush brush = new SolidBrush(this.panel1.BackColor);

                g.FillRectangle(brush, this.panel1.Bounds);  
                g.DrawRectangle(pen, e.X, e.Y, 20, 20);

                pen.Dispose();
                brush.Dispose();
            }
        }

        private Panel panel1;

        private void InitializeComponent()
        {
            this.panel1 = new System.Windows.Forms.Panel();
            this.SuspendLayout();

            this.panel1.Cursor = System.Windows.Forms.Cursors.Cross;
            this.panel1.Location = new System.Drawing.Point(-2, -2);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(676, 429);
            this.panel1.TabIndex = 0;
            this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.panel1_Paint);
            this.panel1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseDown);

            this.ClientSize = new System.Drawing.Size(674, 424);
            this.Controls.Add(this.panel1);
            this.Cursor = System.Windows.Forms.Cursors.Cross;
            this.Name = "Form1";
            this.Text = "rectangles";
            this.Load += new System.EventHandler(this.Form1_Load_1);
            this.MouseClick += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseDown);
            this.ResumeLayout(false);

        }

        private void Form1_Load_1(object sender, EventArgs e)
        {

        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }
    }
    }
1

1 respuesta 1

1

Intenta con esto:


using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {

        private int x = -1;
        private int y = -1;

        private Rectangle rect;
        private bool pintar = false;

        public Form1()
        {
            InitializeComponent();
        }

        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            x = e.X;
            y = e.Y;

            pintar = true;
        }

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (pintar)
            {
                rect = new Rectangle(Math.Min(e.X, x),
                                Math.Min(e.Y, y),
                                (e.X - y),
                                (e.Y - y));

                panel1.Invalidate();
                panel1.Refresh();
            }
        }

        private void panel1_MouseUp(object sender, MouseEventArgs e)
        {
            pintar = false;

            x = 0;
            y = 0;
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            using (Graphics g = this.panel1.CreateGraphics())
            {
                Pen pen = new Pen(Color.Black, 2);

                g.DrawRectangle(pen, rect);

                pen.Dispose();
            }
        }
    }
}


Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.