用另一种方法画图class
Drawing graphics with an method in another class
我想用另一个Class中的方法画一个圆。但是它在我的 class 中的第 41 行和第 42 行显示 无效参数。当我完成这个项目时,它应该是一个模拟时钟。这是我的第一个项目,我正在画画 Event.Im 看不懂,不知道如何解决我的问题。我是表格的新手。我已经尝试在主体中绘制圆圈并且它工作正常但是它没有放入 class。对不起,我的 class 名字可能看起来很奇怪,因为我是德国人。感谢您的帮助。
那是我的 class:
class Ziffernblatt
{
Size RectSize;
Point RectPoint = new Point(5, 10);
Rectangle Myrect;
Rectangle MyCircle;
Pen MyPen = new Pen(Color.Black, 1);
Pen Invpen = new Pen(Color.White, 1);
Graphics gObject;
public Ziffernblatt(Graphics NgObject)
{
gObject = NgObject;
}
public void Draw(int PosX, int PosY)
{
RectSize.Width = PosX / 2;
RectSize.Height = PosY / 2;
RectPoint.X = PosX / 2 - RectSize.Width / 2;
RectPoint.Y = PosY / 2 - RectSize.Height / 2;
Myrect = new Rectangle(RectPoint, RectSize);
MyCircle = new Rectangle(RectPoint, RectSize);
gObject.DrawRectangle(Pens.Red, Myrect);
gObject.DrawEllipse(Pens.Black, MyCircle);
}
这是我的主要内容:
public partial class Form1 : Form
{
Ziffernblatt[] Ziffern = new Ziffernblatt[1];
Graphics gObject;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
gObject = e.Graphics;
Ziffern[0] = new Ziffernblatt(gObject);
}
private void Form1_SizeChanged(object sender, EventArgs e)
{
Ziffern[0].Draw(this.ClientSize.Width, this.ClientSize.Height);
Invalidate();
}
}
您收到此错误是因为您向您封装了 Graphics 对象 class。只需从 paint 事件中调用 draw 方法即可。
绘图时的最佳实践是从 Form1
的 paint 方法执行的。考虑到 Graphics
对象将针对每个绘制事件更改。
您的代码可能如下所示:
public partial class Form1 : Form
{
Ziffernblatt[] Ziffern = new Ziffernblatt[1];
public Form1()
{
InitializeComponent();
Ziffern[0] = new Ziffernblatt();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Ziffern[0].Draw(e.Graphics, this.ClientSize.Width, this.ClientSize.Height);
}
private void Form1_SizeChanged(object sender, EventArgs e)
{
//Ziffern[0].Draw(this.ClientSize.Width, this.ClientSize.Height);
Invalidate();
}
}
public class Ziffernblatt
{
Size RectSize;
Point RectPoint = new Point(5, 10);
Rectangle Myrect;
Rectangle MyCircle;
Pen MyPen = new Pen(Color.Black, 1);
Pen Invpen = new Pen(Color.White, 1);
public Ziffernblatt() //Graphics NgObject)
{
//gObject = NgObject;
}
public void Draw(Graphics g, int PosX, int PosY)
{
RectSize.Width = PosX / 2;
RectSize.Height = PosY / 2;
RectPoint.X = PosX / 2 - RectSize.Width / 2;
RectPoint.Y = PosY / 2 - RectSize.Height / 2;
Myrect = new Rectangle(RectPoint, RectSize);
MyCircle = new Rectangle(RectPoint, RectSize);
g.DrawRectangle(Pens.Red, Myrect);
g.DrawEllipse(Pens.Black, MyCircle);
}
}
For additional information about the paint event see Control.Paint
Event
我想用另一个Class中的方法画一个圆。但是它在我的 class 中的第 41 行和第 42 行显示 无效参数。当我完成这个项目时,它应该是一个模拟时钟。这是我的第一个项目,我正在画画 Event.Im 看不懂,不知道如何解决我的问题。我是表格的新手。我已经尝试在主体中绘制圆圈并且它工作正常但是它没有放入 class。对不起,我的 class 名字可能看起来很奇怪,因为我是德国人。感谢您的帮助。
那是我的 class:
class Ziffernblatt
{
Size RectSize;
Point RectPoint = new Point(5, 10);
Rectangle Myrect;
Rectangle MyCircle;
Pen MyPen = new Pen(Color.Black, 1);
Pen Invpen = new Pen(Color.White, 1);
Graphics gObject;
public Ziffernblatt(Graphics NgObject)
{
gObject = NgObject;
}
public void Draw(int PosX, int PosY)
{
RectSize.Width = PosX / 2;
RectSize.Height = PosY / 2;
RectPoint.X = PosX / 2 - RectSize.Width / 2;
RectPoint.Y = PosY / 2 - RectSize.Height / 2;
Myrect = new Rectangle(RectPoint, RectSize);
MyCircle = new Rectangle(RectPoint, RectSize);
gObject.DrawRectangle(Pens.Red, Myrect);
gObject.DrawEllipse(Pens.Black, MyCircle);
}
这是我的主要内容:
public partial class Form1 : Form
{
Ziffernblatt[] Ziffern = new Ziffernblatt[1];
Graphics gObject;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
gObject = e.Graphics;
Ziffern[0] = new Ziffernblatt(gObject);
}
private void Form1_SizeChanged(object sender, EventArgs e)
{
Ziffern[0].Draw(this.ClientSize.Width, this.ClientSize.Height);
Invalidate();
}
}
您收到此错误是因为您向您封装了 Graphics 对象 class。只需从 paint 事件中调用 draw 方法即可。
绘图时的最佳实践是从 Form1
的 paint 方法执行的。考虑到 Graphics
对象将针对每个绘制事件更改。
您的代码可能如下所示:
public partial class Form1 : Form
{
Ziffernblatt[] Ziffern = new Ziffernblatt[1];
public Form1()
{
InitializeComponent();
Ziffern[0] = new Ziffernblatt();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Ziffern[0].Draw(e.Graphics, this.ClientSize.Width, this.ClientSize.Height);
}
private void Form1_SizeChanged(object sender, EventArgs e)
{
//Ziffern[0].Draw(this.ClientSize.Width, this.ClientSize.Height);
Invalidate();
}
}
public class Ziffernblatt
{
Size RectSize;
Point RectPoint = new Point(5, 10);
Rectangle Myrect;
Rectangle MyCircle;
Pen MyPen = new Pen(Color.Black, 1);
Pen Invpen = new Pen(Color.White, 1);
public Ziffernblatt() //Graphics NgObject)
{
//gObject = NgObject;
}
public void Draw(Graphics g, int PosX, int PosY)
{
RectSize.Width = PosX / 2;
RectSize.Height = PosY / 2;
RectPoint.X = PosX / 2 - RectSize.Width / 2;
RectPoint.Y = PosY / 2 - RectSize.Height / 2;
Myrect = new Rectangle(RectPoint, RectSize);
MyCircle = new Rectangle(RectPoint, RectSize);
g.DrawRectangle(Pens.Red, Myrect);
g.DrawEllipse(Pens.Black, MyCircle);
}
}
For additional information about the paint event see Control.Paint Event