问题描述
我看到网站上的一些类似的问题,但他们没有真的帮了我。
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
您必须继承按钮
类并重写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));
}
编辑:
添加属性到您的类和像众彩MyFancyTextColor {获取;集;}
,并在的OnPaint 方法。 Alsow它会在Visual Studio窗体设计器的控件的属性编辑器apear。
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事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!