Title: Drawing on the MDI Parent Form
Question: Sometimes you want to draw something on the background of an MDI Parent form. Here`e an easy way.
Answer:
Many online FAQ's document how to paint a bitmap on the background of a MDI parent, however, few describe how to draw anything you want on the background of the parent canvas. Fortunately its pretty easy.
First I will show the code and describe it below.
procedure drawoncanvas;
var
tempcanvas:Tcanvas;
begin
tempcanvas:=tcanvas.create;
try
tempcanvas.handle:=getdc(mdiparent.clienthandle);
with tempcanvas do begin
// do all your drawing here with standard canvas commands
end;
finally
releasedc(mdiparent.clienthandle,tempcanvas.handle);
tempcanvas.free;
end;
end;
In an MDI parent window, there is actually an invisible client window that contains the MDI parent. Drawing on the actual canvas of the MDI parent does nothing, one must draw on the client for the drawn objects to be visible. However, Delphi does not provide an easy way to do this. The above code does it my mixing using some API commands to create a device context to draw on and then passing that device context to a standard Tcanvas object so we can take advantage of the built in functionality of Tcanvas.
However, in a normal form, we would just put this code in the onpaint event and forget about it. However, there is no equivalent for the clienthandle. A simple few commands provides the solution.
In the definition of the form place the following command:
procedure WMerasebkgnd(var message:TWMerasebkgnd);message WM_erasebkgnd;
Then in the implemetation of the unit create the procedure wmerasebkgnd and place the above code in it.
Sometimes you may want to call the inherited method to clear the background before you draw it.. however, this may cause flicker. I usually just include code that will draw over the entire visible background. However, you have to find what works best for your own application.