[英]pass form as an argument to graphics c#
I'm working with graphics in windows forms. 我正在使用Windows窗体中的图形。 I need to pass the form itself (named
form1
) which is calling the graphics class Grafika()
so that while drawing I can use form1.CreateGraphics()
to draw on that specific form. 我需要传递调用图形类
Grafika()
的表单本身(名为form1
),以便在绘制时可以使用form1.CreateGraphics()
在该特定表单上绘制。
The constructor for the graphics class Grafika
looks like this: 图形类
Grafika
的构造函数如下所示:
public Grafika(ref Form predan)
{
grafika=predan.CreateGraphics();
//drawing on the form
}
How to pass a form into the constructor of Grafika
so that I can use it? 如何将表单传递给
Grafika
的构造函数,以便我可以使用它?
The same way you have posted, have your constructor accepts a parameter of that specific form. 与您发布的方式相同,让构造函数接受该特定形式的参数。 But you don't need the
ref
keyword since Form
is of reference type itself. 但是您不需要
ref
关键字,因为Form
本身就是引用类型。 Here Form1
is a specific Form
object considering you have a form of that name 这里
Form1
是一个特定的Form
对象,考虑到您具有该名称的表单
public Grafika(Form1 predan)
{
grafika=predan.CreateGraphics();
//drawing on the form
}
If you need to pass it along don't use ref
. 如果您需要传递它,请不要使用
ref
。 If you remove the ref you can pass it like so: 如果删除引用,您可以像这样传递它:
//From Form1
Grafika g = new Grafika(this);
//If called from the From, 'this' will be the form itself
Also your constructor should look like this: 另外,您的构造函数应如下所示:
public Grafika(Form1 predan)//Notice Form1 instead of just Form
{
grafika=predan.CreateGraphics();
//drawing on the form
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.