Tiling a TBitmap on a Control 

Tiling a bitmap is rather trivial on controls that have a Canvas property.  But what about those without?  The answer is that it can be done using the VCL's encapsulation of the "GetDC(Control->Handle)" routine, appropriately titled "TControlCanvas".  In the example below, we tile a bitmap on a TabControl, a control without a Canvas property.  The basic idea is to create a TControlCanvas, set it's Control property to the TabControl, and then "tile-Draw()" our bitmap on that canvas during the Form's OnPaint event... 

KEYWORDS: TControlCanvas, Control 
 

//in header, add... 
TControlCanvas *CCanvas; 

 


 
 

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

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

    CCanvas = new TControlCanvas; 
    CCanvas->Control = TabControl1;

    TabControl1->TabHeight = 40;

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

void __fastcall TForm1::FormPaint(TObject *Sender) 

    int w = Image1->Width; 
    int h = Image1->Height; 

    //here's where we tile the bitmap 
    for (int y = TabControl1->TabHeight; y < Height; y = y + h) 
    { 
        for (int x = 0; x < Width; x = x + w) 
        { 
            CCanvas->Draw(x, y, Image1->Picture->Bitmap); 
        } 
    } 

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

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

    delete CCanvas;