Skip to content

DoubleBuffering

wael gg

Asked Apr 28, 2007, 12:32 AM EDT4 answers
Share
LinkedInX
hi all,

can i ask how to make DoubleBuffered in TabControl as i made it in form with

this.DoubleBuffered = true;

but this won't work in Tab so what can i do

thanx

4 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    What double buffering does is draw the display to an in memory bitmap for you. You can do the same for the tab control as follows: 
    (1) create your own bitmap in memory.
    (2) create a graphics object to draw with
    (3) draw the background onto the bitmap
    (4) paint the created bitmap in the OnPaintBackGround event.

    Cheers!
      Richard
    0
  • 0
    hi ,

    thanx for answer but can u give me the begin point
    some thing a Method to search about

    thanx again
    0
  • 0

    Here is some sample code that will illustrate the concepts of custom double buffering. 
    First you need to be aware of some things.  This code revolves around the tabpage.Paint event and the tabpage that I am using is simply called tabpage1.  You might want to rename it to match your tabpage name.  Follow the instructions below for creating this sample.

    (1)  In the code view page, copy and paste all of the code below:


            Bitmap bmpCanvas;
            private void tabPage1_Paint(object sender, PaintEventArgs e)
            {
                if (bmpCanvas == null)
                { InitalizeMyBitmap(); }
                //Now create graphics object to draw on tabpage
                Graphics g = tabPage1.CreateGraphics();
                //draw the in memory bitmap to the tabpage
                g.DrawImage(bmpCanvas, 0f, 0f);
                //Dispose the graphics object
                g.Dispose();
            }

            private void InitalizeMyBitmap()
            {
                //create our in memory bitmap
                bmpCanvas = new Bitmap(tabPage1.ClientRectangle.Width, tabPage1.ClientRectangle.Height);
                //create the graphics object and set it to draw on bmpCanvas
                Graphics gr = Graphics.FromImage(bmpCanvas);
                //draw some text with the graphics object
                gr.DrawString("I'm a graphics pro!", this.Font, Brushes.Black, 10f, 10f);
                //*********draw a smiley with shades****************
                //Define the dimensions of the shape
                Rectangle rect = new Rectangle(30, 30, 40, 40);
                //draw outline
                gr.FillEllipse(Brushes.Yellow, rect);
                gr.DrawEllipse(Pens.Black, rect);
                //draw shades
                gr.FillEllipse(Brushes.Black, rect.Left + 5, rect.Top + 14, 14, 9);
                gr.FillEllipse(Brushes.Black, rect.Left + 21, rect.Top + 14, 14, 9);
                //draw frames for shades
                gr.DrawLine(Pens.Black, rect.Left + 1, rect.Top + 15, rect.Left + 5, rect.Top + 18);
                gr.DrawLine(Pens.Black, rect.Left + 17, rect.Top + 18, rect.Left + 22, rect.Top + 18);
                gr.DrawLine(Pens.Black, rect.Left + 36, rect.Top + 16, rect.Left + 38, rect.Top + 13);
                //draw mouth
                gr.DrawLine(Pens.Black, rect.Left + 15, rect.Top + 30, rect.Left + 24, rect.Top + 30);
                gr.DrawLine(Pens.Black, rect.Left + 24, rect.Top + 30, rect.Left + 27, rect.Top + 27);
                //******************************************

                //Always dispose the graphics object when we are done with it
                gr.Dispose();
            }

    //End of sample code


    (2)  Switch to design mode, Select the desired tabpage and go to the properties window. Now click on the Events button. (The lighnting bolt) 

    (3)  Find the event called Paint.  Now in the dropdown list, select the tabpage1 event handler.
    You have now linked the paint event to the code you pasted in.

    Now run the program to test it.  You should see the background of the tabpage painted with your code.

    Cheers!
    Richard

    0
  • 0
    TypeScript
    public void tab_Paint(object sender, PaintEventArgs e)
            {
                TabPage tab = (TabPage)sender;
                BufferedGraphicsContext context;
                BufferedGraphics buffer;
                // context = BufferedGraphicsManager.Current;
                /// Dedicated context
                /// used when highly animated graphics
                context = new BufferedGraphicsContext();
                Graphics grfx = tab.CreateGraphics();
                buffer =
                context.Allocate(grfx, tab.ClientRectangle);
                drawSurfaceToBuffer(buffer.Graphics);
                buffer.Render(grfx);
                buffer.Dispose();
                grfx.Dispose();
            }
            public void drawSurfaceToBuffer(Graphics bufferGrfx)
            {
               /// some processing that used the buffer grfxs
            }

    First thanx for ur last code but it doesnot work with me (Graphics Flicker still existed) and
    this code i understand it from MSDN and write it to reduce Graphics Flicker but it does not make any thing at all also it make my background of tabpage black and the original is white

    is there any suggestions please

    0