简体   繁体   中英

Draw a rectangle on mouse click

Can I draw a rectangle with mouseClick? My code is not working so far. Can you help me?

private void panel1_MouseClick(object sender, MouseEventArgs e)
{
    Graphics g = this.CreateGraphics();
    Pen pen = new Pen(Color.Black, 2);

    g.DrawRectangle(pen, 100,100, 100, 200);
}

Edited version:

Without much assumpition of what you trying to do:

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

        g.DrawRectangle(pen, 100,100, 100, 200);

        pen.Dispose();
    }
}

Your code did not work as it is drawing the rectangle on the window (this) and the drawn rectangle is then hidden by your panel.

Generally overriding Paint for such a simple case is just too much effort for just drawing a rectangle on a panel. However, drawing the rectangle in this way works, but the rectangle will disapear when the form is redrawn (eg by minimizing and subsequently showing the form again. If the rectangle has to be persistent you will have to use the paint method and for this you will have to (eg) create the rectangle in the click event and then draw it in the paint event. (See roygbiv 's solution for such an approach). Note: If you go along with the paint method, you should keep it as efficient as possible, as the paint method gets called verry frequently.

Edit 2

You do not need to clear the background as your rectangle will be drawn always at the same place. In order to draw the rectangle at the point where the user cliced ( it is an assumption that this is what you want ) you should move the code to the mouse down event, eg:

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);  // redraws background
        g.DrawRectangle(pen, e.X, e.Y, 20, 20);

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

Try this code with a PictureBox instead (just to get you started - there are many different ways of doing this):

private void pictureBox1_Click(object sender, EventArgs e)
{
    if (pictureBox1.Image == null)
    {
            pictureBox1.Image = new Bitmap(pictureBox1.width, 
                    pictureBox1.height);
    }
    using (Graphics g = Graphics.FromImage(pictureBox1.Image))
    {
        // draw black background
        g.Clear(Color.Black);
        Rectangle rect = new Rectangle(100, 100, 200, 200);
        g.DrawRectangle(Pens.Red, rect);
    }
    pictureBox1.Invalidate();
}

This technique will automatically "persist" your drawing, meaning that it won't disappear if another windows gets dragged across it. When you draw to a control directly (what you're trying to do with the CreateGraphics() call) you usually run into the problem of non-persistency.

Update : here's another answer with a more detailed example of drawing something in response to where the mouse is clicked:

how to draw drawings in picture box

Can I draw a rectangle with mouseClick?

If you mean "When the mouse is clicked on my panel I want to display a rectangle" then you can do that like this:

        private bool displayRectangle = false;

        private void panel1_MouseClick(object sender, MouseEventArgs e)
        {
            displayRectangle = true;
            panel1.Invalidate(false);
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            if (displayRectangle)
            {
                using (Pen p = new Pen(Color.Black, 2))
                {
                    e.Graphics.DrawRectangle(p, 100, 100, 100, 200);
                }
            }
        }

If you mean "I want to drag the mouse on my panel to create rectangles" then you have a little more work to do.

You need to handle the mouse up, move and down events tracking the delta between the mouse down point and the current position. Finally, on mouse up, you would draw your rectangle. It gets more complicated because you need to use double buffering or an 'xor' rectangle to draw the "drag" rectangle.

These two threads may help:

dragging picturebox inside winform on runtime

Snap to grid mouse locking up

您应该在控件的“Paint”事件中绘制它(在本例中为panel1)。

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