How can I paint a background image?
Applies To: C++Builder 1 or higher
Category: Knowledge Base
- Using the Color property of my form, I can modify the background color of my form. But is it possible to make my application more professional by painting an image? I want to use a tiled bitmap the same way a web page does.
-
- Start a new project using File | New Application.
-
- Edit the OnPaint event:
-
- void __fastcall TForm1::FormPaint(TObject *Sender)
- {
- Graphics::TBitmap *bitmap = new Graphics::TBitmap;
- try
- {
- // Load the bitmap from the hard drive
- bitmap->LoadFromFile("c:\\wood.bmp");
- Form1->Canvas->Brush->Bitmap = bitmap;
- // Fill the rectangular area of your form
- Form1->Canvas->FillRect(Rect(0, 0,
- Form1->Width, Form1->Height));
- }
- __finally
- {
- // Delete and free the bitmap
- Form1->Canvas->Brush->Bitmap = NULL;
- delete bitmap;
- }
- }
-
- Create the corresponding bitmap image (in this case
- c:\wood.bmp).
-
- When you need to paint on your form, you need to use the TCanvas. It gives you access to the display device context (DC). TCanvas provides simple methods such as FillRect, CopyRect or Draw which allow you to draw on the corresponding form.
- To load the bitmap image, you'll have to create a TBitmap object. Then, with the LoadFromFile method, you'll load the bitmap in memory.
C++Builder Developer's Network
Copyright © Yoto Yotov