如何在 C# 中使用 OnPaint 事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10076116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 12:21:36  来源:igfitidea点击:

How to use the OnPaint event in C#?

c#eventsonpaint

提问by Stefan Manciu

i saw some similar questions on the site but none of them really helped me.

我在网站上看到了一些类似的问题,但没有一个真正对我有帮助。

I have a function that draws a few lines on the form when a button is clicked that vary in shape depending on the values the user enters in some textboxes.

我有一个函数,当单击一个按钮时,它会在表单上绘制几行,该按钮的形状取决于用户在某些文本框中输入的值。

My problem is that when i minimize the form, the lines disappear and i understood that this can be resolved by using the OnPaint event, but i don't really understand how.

我的问题是,当我最小化表单时,线条消失了,我知道这可以通过使用 OnPaint 事件来解决,但我真的不明白如何解决。

Can anyone give me a simple example of using a function to draw something at the push of a button using the OnPaint event?

谁能给我一个简单的例子,使用函数在按下按钮时使用 OnPaint 事件绘制一些东西?

采纳答案by Pavel Krymets

Here you go, simpe MSDN tutorial on User-Drawn Controls

给你,简单的 MSDN 用户绘制控件教程

You must inherit Buttonclass and override OnPaint method.

您必须继承Button类并覆盖 OnPaint 方法。

Code example:

代码示例:

protected override void OnPaint(PaintEventArgs pe)
{
   // Call the OnPaint method of the base class.
   base.OnPaint(pe);

   // Declare and instantiate a new pen.
   System.Drawing.Pen myPen = new System.Drawing.Pen(Color.Aqua);

   // Draw an aqua rectangle in the rectangle represented by the control.
   pe.Graphics.DrawRectangle(myPen, new Rectangle(this.Location, 
      this.Size));
}

EDIT:

编辑:

Add property to your class and like public Color MyFancyTextColor {get;set;}and use it in your OnPaintmethod. Alsow it will apear in control property editor of visual studio form designer.

将属性添加到您的类中,public Color MyFancyTextColor {get;set;}并在您的OnPaint方法中使用它。它也将出现在 Visual Studio 表单设计器的控件属性编辑器中。

回答by Miroslav Mares

You can write all the code responsible for (re)drawing the scene into method called when Paintevent occurs.

您可以将负责(重新)绘制场景的所有代码编写到Paint事件发生时调用的方法中。

So, you can register you method to be called when Paint occurs like this:

因此,您可以注册要在 Paint 发生时调用的方法,如下所示:

this.Paint += new PaintEventHandler(YourMethod);

Then YourMethod will be called whenever the form needs to be redrawn.

然后,只要表单需要重绘,就会调用 YourMethod。

Also remember that you method must have the same arguments as delegate, in this case:

还要记住,您的方法必须与委托具有相同的参数,在这种情况下:

void YourMethod(object sender, PaintEventArgs pea)
{
   // Draw nice Sun and detailed grass
   pea.Graphics.DrawLine(/* here you go */);
}

EDIT

编辑

Or, as mentioned in another answer, you can override OnPaintmethod. Then you don't have to take care about adding event handler with your own method.

或者,如另一个答案中所述,您可以覆盖OnPaint方法。然后你不必关心用你自己的方法添加事件处理程序。