我想从一个绘制形状的外部脚本调用一个函数。
表单当前打开空白,上面没有形状,也没有错误。
下面是从不同文件调用函数的空窗体
public partial class Form1 : Form
{
other_fff functions1 = new other_fff();
public Form1()
{
InitializeComponent();
functions1.draw_circle(this);
}
}
下面是我获得绘图函数的地方
class other_fff
{
public void draw_circle(Form1 the_form)
{
Pen Pen1 = new Pen(Color.Blue, 9);
Graphics g = the_form.CreateGraphics();
g.DrawEllipse(Pen1, 50, 50, 10, 5); // does not work
}
}
我怎么抽签呢
您可以向paint-event添加一个事件处理程序,如下所示:
public static void DrawCircle(Form form)
{
form.Paint += OnPaint;
void OnPaint(object sender, PaintEventArgs e)
{
using (var Pen1 = new Pen(Color.Blue, 9))
{
e.Graphics.DrawEllipse(Pen1, 50, 50, 10, 5);
}
}
}
有一些缺点:
您可能希望通知表单它应该绘制一个椭圆,并让表单代之以绘制。