我正在尝试在 C# 表单上绘图,但在阅读了有关 Graphics 类和 Draw/Fill 方法的文档后,它仍然对我不起作用。
它是代码:
using System.Drawing;
using System.Windows.Forms;
namespace Drawing_Example
{
public partial class Form1 : Form
{
#region Constructors
public Form1()
{
InitializeComponent();
Pen pen = new Pen(Color.Black, 3f);
Graphics surface = CreateGraphics();
surface.DrawEllipse(pen, new Rectangle(0, 0, 200, 300));
pen.Dispose();
surface.Dispose();
}
#endregion Constructors
}
}
当我按开始时,会出现一个空表格,没有图纸。你能告诉我我做错了什么吗?
您不能在构造函数中进行绘制,因为该OnPaint()
方法稍后被框架调用时会覆盖所有内容。
相反,覆盖该OnPaint()
方法并在那里进行绘图。
不要Graphics
使用CreateGraphics()
;创建自己的对象 相反,根据我链接的示例使用传递给OnPaint()
via 的e.Graphics
那个。
(此外,不要在使用完毕后处理e.Graphics
-框架会为您管理。)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句