Steven Du
Steven Du

Reputation: 1691

BufferedGraphics doesn't work in Form load event?

all

I follow this http://msdn.microsoft.com/en-us/library/ka0yazs1.aspx to create an application and would like to draw something on the background in a C# WinForm.

BufferedGraphics is initiated in Form1_Load event. If I put the render method in a mouse click event, it can simply clean up background like this:

    BufferedGraphicsContext myContext;
    BufferedGraphics myBuffer;

private void button1_Click(object sender, EventArgs e)
            {
                myBuffer.Graphics.FillRegion(Brushes.Black,new Region(this.ClientRectangle));
                myBuffer.Render();
            }

When I put the render method in FormLoad event it will not draw anything :

 private void Form1_Load(object sender, EventArgs e)
        {
            myContext = BufferedGraphicsManager.Current;
            myBuffer = myContext.Allocate(this.CreateGraphics(), new Rectangle(0, 0, this.Width,this.Height));
            myBuffer.Graphics.FillRegion(Brushes.Black,new Region(this.ClientRectangle));
            myBuffer.Render();
         }

Can anyone figure out what is the problem? I don't think it's a bug.

Upvotes: 2

Views: 627

Answers (1)

Hans Passant
Hans Passant

Reputation: 941505

Yes, that can't work. The window is not visible yet at the Load event, the Shown event is the first event where you can be sure you can see what you render.

Which doesn't actually solve anything either, you'll lose whatever you render when the form repaints itself. Only draw stuff in the Paint event. You'll get the double-buffered graphics buffer for free when you set the this.DoubleBuffered property to true in the constructor.

Upvotes: 2

Related Questions