gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 22:53:31 27 4
gpt4 key购买 nike

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

我有一个函数,可以在单击按钮时在表单上绘制几条线,这些线的形状会根据用户在某些文本框中输入的值而变化。

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

任何人都可以给我一个简单的示例,说明使用函数在使用 OnPaint 事件按下按钮时绘制内容吗?

最佳答案

Here you go , 简单的用户绘制控件的 MSDN 教程

您必须继承Button 类并重写OnPaint 方法。

代码示例:

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 方法中使用它。并且它会出现在 visual studio 窗体设计器的控件属性编辑器中。

关于c# - 如何在 C# 中使用 OnPaint 事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10076116/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com