When using the MDI interface, sometimes it is necessary to display some graphics or software covers in the MDI client window so that the software interface does not appear empty and the software functions can be clear at a glance. However, these interfaces are not directly provided in Delphi. Any graphical controls placed in the MDI form cannot be displayed at runtime. Therefore, the MDI form needs to be modified.
Statement:
This solution is only for MDI forms. If it is applied to non-MDI forms, the consequences are difficult to say. Try it yourself.
Remember, the form's FormStyle property should be set to: fsMDIForm.
Solution:
1. The MDI client window message (Message) cannot be received in the MDI main form. Therefore, you need to customize the client window processing process (Window PROcedure) and take over the MDI client window (needs to be implemented in the overloaded CreateWnd process ):
procedure TMDIForm.CreateWnd;
begin
inherited;
FNewWndProc := MakeObjectInstance(ClientWndProc);
FOldWndProc := Pointer(GetWindowLong(ClientHandle, GWL_WNDPROC));
SetWindowLong(ClientHandle, GWL_WNDPROC, Longint(FNewWndProc));
end;
Among them, ClientWndProc is a custom window procedure: procedure ClientWndProc(var Message: TMessage);
FOldWndProc is used to store the pointer of the old window procedure.
2. Implement your own customer window process:
procedure TMDIForm.ClientWndProc(var Message: TMessage);
var
R: TRECT;
procedure Default;
begin
with Message do
Result := CallWindowProc(FOldWndProc, ClientHandle, Msg, wParam, lParam);
end;
var
PS: TPaintStruct;
begin
R := ClientRect;
case Message.Msg of
WM_PAINT:
begin
BeginPaint(ClientHandle,PS);
try
Canvas.Lock;
try
Canvas.Handle := PS.hdc;
try
Paint;
if ControlCount > 0 then
PaintControls(PS.hdc,Controls[0]);
finally
Canvas.Handle := 0;
end;
finally
Canvas.Unlock;
end;
finally
EndPaint(ClientHandle,PS);
end;
end;
WM_ERASEBKGND:
begin
DrawBG(TWMEraseBkGnd(Message).DC);
Message.Result := 1;
end;
WM_VSCROLL,WM_HSCROLL:
begin
InvalidateRect(ClientHandle,@R,true);
Default;
end;
WM_SIZE:
begin
InvalidateRect(ClientHandle,@R,true);
Default;
end;
else
Default;
end;
end;
The DrawBG above is used to draw the window background.
3. Implement window background.
In order to allow successors to define their own background, the process is described as virtual:
protected
procedure DrawBG(DC: HDC); virtual;
Here, the DrawBG process simply fills the window background:
procedure TMDIForm.DrawBG(DC: HDC);
begin
if Brush.Color <> clNone then
FillRect(DC, ClientRect, Brush.Handle);
end;
4. In summary, the TMDIFrom class definition is summarized as follows:
TMDIForm = class(TForm)
private
FOldWndProc: TFarProc;
FNewWndProc: TFarProc;
procedure ClientWndProc(var Message: TMessage);
protected
procedure DrawBG(DC: HDC);virtual;
procedure CreateWnd; override;
end;
5. After the above transformation, you can draw the specified background in DrawBG (you need to directly call the Windows GUI interface), or directly use graphic controls, or implement the OnPaint event of the form, and the MDI window will be colorful from now on.