Canvases and ScrollBoxes 

Here is a method to draw on a PaintBox inside a ScrollBox, and allow the PaintBox to refresh during a scroll.  You may think this is a simple task with a TImage, but that path normally creates severe flicker.  To eliminate this, we use a method of double buffering with an off-screen TBitmap. 

KEYWORDS: double buffering, BitBlt 

 

//in header, add... 
Graphics::TBitmap *MyBitmap;

 


 
 

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner) 
        : TForm(Owner) 

    //create an off-screen Bitmap 
    MyBitmap = new Graphics::TBitmap(); 
}

//--------------------------------------------------------------------------------
 
//Draw on the offscreen Bitmap and blit it to the PaintBox 
void __fastcall TForm1::Button1Click(TObject *Sender) 

    //adjust the dimensions 
    MyBitmap->Width = PaintBox1->Width; 
    MyBitmap->Height = PaintBox1->Height; 

    //change the Bitmap's background color to that of the ScrollBox 
    MyBitmap->Canvas->Brush->Color = clBtnFace; 
    MyBitmap->Canvas->FillRect(MyBitmap->Canvas->ClipRect); 

    //draw the line on the off screen Bitmap 
    MyBitmap->Canvas->Pen->Color = clBlue; 
    MyBitmap->Canvas->Pen->Width = 4; 
    MyBitmap->Canvas->MoveTo(0,0); 
    MyBitmap->Canvas->LineTo(PaintBox1->Width, PaintBox1->Height); 

    //copy it to the onscreen Paintbox 
    BitBlt(PaintBox1->Canvas->Handle, 0, 0, PaintBox1->Width,
           PaintBox1->Height, MyBitmap->Canvas->Handle, 0, 0, SRCCOPY); 

//--------------------------------------------------------------------------------

void __fastcall TForm1::PaintBox1Paint(TObject *Sender) 

    //Refresh the PaintBo
    BitBlt(PaintBox1->Canvas->Handle, 0, 0, PaintBox1->Width,
           PaintBox1->Height, MyBitmap->Canvas->Handle, 0, 0, SRCCOPY); 

//--------------------------------------------------------------------------------

void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action) 

     delete MyBitmap;