我是 c# 的新手(使用 visual c# 2010),想做一个简单的游戏,它会有弹跳球类型的东西,
我用图形 fillellipse 做了一个球,现在我有了这段代码,
protected override void OnPaint( PaintEventArgs e)
{
//System.Drawing.Graphics gobj;
gobj = this.CreateGraphics();
Pen pen = new Pen(System.Drawing.Color.LightSkyBlue, 6);
SolidBrush brush = new SolidBrush(System.Drawing.Color.Magenta);
Rectangle myRectangle = new Rectangle((PointToClient(Cursor.Position).X), PointToClient(Cursor.Position).Y, 250, 200);
gobj.DrawRectangle(pen, myRectangle);
gobj.FillEllipse(brush, myRectangle);
}
当我运行这段代码时,我不断得到许多圆圈和矩形,只在屏幕的一部分下弹出,但它不应该只画一个圆圈吗??
请帮我理解这个??
最佳答案
您使用 DrawRectangle 方法绘制矩形,然后使用 FillEllipse 绘制圆,您只需要使用 FillEllipse。就像 Mikant 提到的,您不需要创建图形 - 使用 e.Graphics。这是代码:
protected override void OnPaint(PaintEventArgs e)
{
SolidBrush brush = new SolidBrush(System.Drawing.Color.Magenta);
e.Graphics.FillEllipse(brush, (this.Height / 2) - 40, (this.Width / 2) - 40, 80, 80);
}
这会在表单中心绘制圆圈。
祝您实验和学习顺利!
关于c# - 了解涉及 paint() 的小 C# 代码片段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6161227/