Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
}
like image 988
Dinu Avatar asked Mar 27 '10 14:03

Dinu


People also ask

How do you draw a mouse click?

Position your index (left mouse button) and middle finger (right mouse button) over the upper edge of the mouse button. To drag click, simply flick your wrist slightly at an angle while gently pressing the mouse button in a downward direction (towards the front of the mouse).

How do you draw a rectangle mouse in canvas?

Draw a Rectangle on the HTML Canvas with a Mouse //... // window. addEventListener("mousedown", startDrawing); //... // window. addEventListener("mouseup", endDrawing); //... // window. addEventListener("mousemove", draw);

How do you draw a rectangle in Java?

In Java, to draw a rectangle (outlines) onto the current graphics context, we can use the following methods provided by the Graphics/Graphics2D class: drawRect(int x, int y, int width, int height) draw3DRect(int x, int y, int width, int height, boolean raised) draw(Rectangle2D)


2 Answers

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 (e.g. 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 (e.g.) 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, e.g.:

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();
    }
}
like image 176
AxelEckenberger Avatar answered Nov 15 '22 14:11

AxelEckenberger


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

like image 20
MusiGenesis Avatar answered Nov 15 '22 13:11

MusiGenesis