简体   繁体   English

如何绘制简单的图形

[英]How to draw simple Graphics

I want to draw on the form on load in a C# program. 我想在C#程序中加载表单。 Does the following code have to be in the paint() method? 以下代码是否必须在paint()方法中? What does the InitializeComponent() do, exactly? InitializeComponent()究竟做了什么? what I was asking was what does the InitializeComponent() method do exactly. 我问的是InitializeComponent()方法究竟做了什么。 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. 基本上我不确定是否必须在OnPaint()方法上进行覆盖,或者我是否可以在任何地方绘制我想要在表单上绘制内容的代码。 The loadForm calls DrawIt() which will paint on the form. loadForm调用DrawIt() ,它将在表单上绘制。 But that code for the paint is not in any specific OnPaint() or Paint() method. 但是,paint的代码不在任何特定的OnPaint()Paint()方法中。

    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: InitializeComponent方法:

When you create a form VS creates two file containing ONE class (just take a look at partial classes): 当您创建表单时,VS会创建两个包含ONE类的文件(只需看看部分类):

1) Form.cs which is where you put your code. 1)Form.cs,这是你放置代码的地方。

2) Form.Designer.cs which is the auto-generated partial class. 2)Form.Designer.cs,它是自动生成的分部类。 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. 当您在设计视图中将一些组件放在表单上时,VS会生成所需的代码,以便在此类的运行时构建该组件。

The InitializeComponent is an auto-generated method in Form.Designer.cs which initializes the components that you dropped on the form. InitializeComponent是Form.Designer.cs中自动生成的方法,它初始化您在表单上放置的组件。 When you call it from your constructor, you are just telling it to do the initialization when the form object is constructed. 当你从构造函数中调用它时,你只是告诉它在构造表单对象时进行初始化。

OnPaint Method: OnPaint方法:

You should just put your drawing code in the OnPaint method. 您应该将绘图代码放在OnPaint方法中。 This method is called by .Net whenever it's required. 只要需要,.Net就会调用此方法。 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. 是的,你应该在paint事件中调用你的代码。 why must do that? 为什么一定要这样做? I think paint event is invoked automatically by the windows when it is neccessary to update the form. 我认为当需要更新表单时,窗口会自动调用paint事件。 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. 如果你在form_load事件中调用你的代码,它只调用一次,然后当表单显示时,表单再次绘制,它覆盖了你在form_load中的效果,所以你看不到结果。 so, you can invoke your code in Form_paint() event or you can override OnPaint function, they are the same, like below: 所以,你可以在Form_paint()事件中调用你的代码,或者你可以覆盖OnPaint函数,它们是相同的,如下所示:

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: 我的测试结果是这样的: 绘画的结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM