如何在C#中使用OnPaint事件? [英] How to use the OnPaint event in C#?

查看:1334
本文介绍了如何在C#中使用OnPaint事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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?

推荐答案

这里你去,simpe MSDN教程用户绘制控件

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

你必须继承 Button class并覆盖OnPaint方法。

You must inherit Button class and override OnPaint method.

代码示例:

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));
}

编辑:

将属性添加到您的类中,如 public Color MyFancyTextColor {get; set;} 并将其用于 OnPaint 方法。 Alsow将在视觉工作室表单设计师的控制属性编辑器中看到。

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

这篇关于如何在C#中使用OnPaint事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆