Paint a method from another class

2019-02-20 20:35发布

问题:

I have made a draw method in a separate class to that of the form.

public class Object : Form1
 {   
    public void Draw()
    {
            SolidBrush brush = new SolidBrush(Color.Yellow);
            Graphics mapGraphics = this.CreateGraphics();
            mapGraphics.FillEllipse(brush, new Rectangle(0, 0, 12, 12));
            pacBrush.Dispose();
            mapGraphics.Dispose();
    }
}

There are no exceptions thrown, nor errors. I have tried to call the Draw method from the Form_Paint method, but nothing happens at all. How can I fix this?

Thanks

回答1:

To draw into a window, you need to use the Graphics object for that window. Your method creates a new Graphics object, so it draws "somewhere else".

Pass the e.Graphics object that you have in your Form_Paint handler into the method as a parameter, and draw using that instead.



标签: c# graphics