我怎么能从另一个班级画画?

时间:2017-04-17 10:05:54

标签: c# winforms

我有MainForm类。在这里,我可以做出这样的事情。

private void MainForm_Paint(object sender, PaintEventArgs e)
{
    Graphics graphics = this.CreateGraphics();
    Rectangle rectangle = new Rectangle(50, 100, 150, 150);
    graphics.DrawEllipse(Pens.Black, rectangle);
    graphics.DrawRectangle(Pens.Red, rectangle);
}

我可以在表格中看到结果。

但我有另一个类图像。我想从这里画画。我该怎么办?

1 个答案:

答案 0 :(得分:3)

发送PaintEventArgs(以下来自我一直使用的)

class Draw
{
    public void Paint(PaintEventArgs e)
    {
          e.Graphics.DrawRectangles(Pens.Blue, GetRectangle());                        
    }
}

其中GetRectangle是另一种定义矩形的方法

您还应该能够发送您的对象(在您的情况下是MainForm的实例)

class Draw
{
    public void Paint(MainForm main)
    {
        Graphics graphics = main.CreateGraphics();
    }
}

或图形对象

class Draw
{
    public void Paint(Graphics graphics)
    {
        Rectangle rectangle = new Rectangle(50, 100, 150, 150);
        graphics.DrawEllipse(Pens.Black, rectangle);
        graphics.DrawRectangle(Pens.Red, rectangle);
    }
}

你仍然需要PictureBox的事件处理程序,所以你会做类似

的事情
private void MainForm_Paint(object sender, PaintEventArgs e)
{
    Graphics graphics = this.CreateGraphics();
    Draw image = new Draw();
    image.Paint(graphics);
}
相关问题