简体   繁体   中英

Draw a rectangle

For some reason the rectangle doesn't show up when I run the program. But the code runs without any errors. What am I doing wrong?

(I am using csc.exe to compile the code, and I'm writing it in notepad++)

Drawing code:

Graphics g = myform.CreateGraphics();
Pen selPen = new Pen(Color.Blue);
g.DrawRectangle(selPen, 10, 10, 50, 50);
g.Dispose();

Complete Code:

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

public class Hello1
{

    public static void Main()
    {

        Form myform = new Form();

        myform.Text = "Main Window";
        myform.Size = new Size(640, 400);
        myform.FormBorderStyle = FormBorderStyle.FixedDialog;
        myform.StartPosition = FormStartPosition.CenterScreen;


        Graphics g = myform.CreateGraphics();
        Pen selPen = new Pen(Color.Blue);
        g.DrawRectangle(selPen, 10, 10, 50, 50);
        g.Dispose();

        myform.ShowDialog();

    }

}

You can draw on a form in the Form.OnPaint method override or in the Form.Paint event handler only.
So you need to create a new class inherited from Form :

public class MyForm : Form
{
}

Add the following code to your form:

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);

    Graphics g = e.Graphics;
    using (Pen selPen = new Pen(Color.Blue))
    {
        g.DrawRectangle(selPen, 10, 10, 50, 50);
    }
}

Alternatively, you could subscribe to the myform.Paint event as follows:

myform.Paint += (o, e) => {
    Graphics g = e.Graphics;
    using (Pen selPen = new Pen(Color.Blue))
    {
        g.DrawRectangle(selPen, 10, 10, 50, 50);
    }
};

This is because the form is painted when it is shown (in your case when ShowDialog is called), and that erases the rectangle you drew.

You have to draw the rectangle:

  • After the form is shown. For instance, in the Shown event of the form - but beware that when the form will be painted again, the rectangle will disappear (for instance when you minimize/maximize the form),
  • Or better, while the form is painted (in the Paint event, see Dmitry's answer).

If you rearrange the code to put the drawing code after the showing part, you can see the rectangle. As no lines are read after ShowDialog() until the shown form is closed, you might need to call the Show() method.

public static void Main()
{

    Form myform = new Form();

    myform.Text = "Main Window";
    myform.Size = new Size(640, 400);
    myform.FormBorderStyle = FormBorderStyle.FixedDialog;
    myform.StartPosition = FormStartPosition.CenterScreen;

    myform.Show();    //  ->  First Show

    //  -> Then Draw

    Graphics g = myform.CreateGraphics();
    Pen selPen = new Pen(Color.Blue);
    g.DrawRectangle(selPen, 10, 10, 50, 50);
    g.Dispose();   

}

By doing so, you can see the rectangle. But it will not remain there unless you add the drawing part on its OnPaint event Handler. The drawn rectangle will be vanished when you try to minimize or move the side containing the rectangle, or wheneever the form needs to be Drawn by the OS.

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