How can I paint an MDI background image?
Applies To: C++Builder 1 or higher
Category: Knowledge Base
- As shown in How can I paint a background image?, I can use the TCanvas object to draw on my form. This technique does not work for MDI applications. How can I solve this problem?
-
- Create an MDI application. For more information, read
- How can I create MDI applications?
-
- Edit the header file of your main form:
-
- private:
- FARPROC Client, PrevClient;
- void __fastcall ClientProc(TMessage &Message);
-
- Insert in your source code:
-
- void __fastcall TForm1::ClientProc(TMessage &Message)
- {
- if(Message.Msg == WM_ERASEBKGND)
- {
- TCanvas *canvas = new TCanvas();
- canvas->Handle = (HDC)Message.WParam;
- // Now you have the canvas
- canvas->Brush->Bitmap = Image1->Picture->Bitmap;
- canvas->FillRect(Rect(0, 0, ClientWidth, ClientHeight));
- Message.Result = true;
- delete canvas;
- }
- else
- Message.Result = CallWindowProc(PrevClient, ClientHandle,
- Message.Msg, Message.WParam, Message.LParam);
- }
-
- Double-click the OnCreate event of your form:
-
- void __fastcall TForm1::FormCreate(TObject *Sender)
- {
- Client = (FARPROC)MakeObjectInstance(ClientProc);
- PrevClient = (FARPROC)GetWindowLong(ClientHandle,
- GWL_WNDPROC);
- SetWindowLong(ClientHandle, GWL_WNDPROC, (long)Client);
- }
-
- Add an Image. Load the bitmap of your choice.
C++Builder Developer's Network
Copyright © Yoto Yotov