简体   繁体   中英

How to draw simple Graphics

I want to draw on the form on load in a C# program. Does the following code have to be in the paint() method? What does the InitializeComponent() do, exactly? what I was asking was what does the InitializeComponent() method do exactly. Basically I wasn't sure if there has to be an override on the OnPaint() method or if I can just draw where ever I want to put my code that will draw something on the form. The loadForm calls DrawIt() which will paint on the form. But that code for the paint is not in any specific OnPaint() or Paint() method.

    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        private void DrawIt()
        {
            System.Drawing.Graphics graphics = this.CreateGraphics();
            System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(
               50, 50, 150, 150);
            graphics.DrawEllipse(System.Drawing.Pens.Black, rectangle);
            graphics.DrawRectangle(System.Drawing.Pens.Red, rectangle);
        }

        private void Form3_Load(object sender, EventArgs e)
        {
            DrawIt();
        }
    }

InitializeComponent Method:

When you create a form VS creates two file containing ONE class (just take a look at partial classes):

1) Form.cs which is where you put your code.

2) Form.Designer.cs which is the auto-generated partial class. when you put some component on your form in the design view, VS generates required codes to build that component at run-time in this class.

The InitializeComponent is an auto-generated method in Form.Designer.cs which initializes the components that you dropped on the form. When you call it from your constructor, you are just telling it to do the initialization when the form object is constructed.

OnPaint Method:

You should just put your drawing code in the OnPaint method. This method is called by .Net whenever it's required. for example if your window goes behind another window and then comes to the front again, your drawings are cleared! So this method is called again to re-paint the drawings.

yes, you should invoke your code in paint event. why must do that? I think paint event is invoked automatically by the windows when it is neccessary to update the form. if you invoke your code in form_load event, it is just invoked once, then when the form is showing, the form paints again, and it cover your effect in the form_load, so you can't see the result. so, you can invoke your code in Form_paint() event or you can override OnPaint function, they are the same, like below:

private void DrawIt()
    {
        System.Drawing.Graphics graphics = this.CreateGraphics();
        System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(
           50, 50, 150, 150);
        graphics.DrawEllipse(System.Drawing.Pens.Black, rectangle);
        graphics.DrawRectangle(System.Drawing.Pens.Red, rectangle);
        this.Update();
    }

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

    }

    private void MainWindow_Paint(object sender, PaintEventArgs e)
    {
        //DrawIt();
    }

result of my test is like this: 绘画的结果

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