Title: Add a Minimize event to your form
Question: Have you ever wanted, that your form gets an event when it was minimized?
Answer:
Try this extension to the normal form. You can assign a handler to the OnMnimize property and choose from three "automatic" actions built in in the form:
tmNone = Dont minimize the Window
tmWindow = minimize the Window as usual
tmAll = Minimze all windows of the application
Installation: Put the Unit in the library folder of your delphi compiler and import it to your form sourcecode.
Usage: Replace the
type TMyForm = class (TForm)
of your form with
type TMyForm = class (TMiniForm)
Thats all. Assign the OnMinimze property at runtime. Example:
procedure TMyForm.MyMinimize (Sender: TObject; var state: TMiniState);
begin
state:= tmAll; // minimize all windows
end;
procedure TMyForm.FormCreate;
begin
OnMinimize:= MyMinimize;
end;
//---------------------------------------------------------
unit MiniForm;
interface
uses Windows, Messages, SysUtils, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls, ExtCtrls, ComCtrls, Mask, Menus;
// state flags for automatic handling of minimize events
type TMiniState = (tmNone, // dont react to the message
tmWindow, // minimize the window
tmAll); // minimze all windows of the app
// the minimize event himself
type TMinimizeEvent = procedure (Sender: TObject;
var state: TMiniState) of object;
type TMiniForm = class (TForm) // derived from TForm
private
FOnMinimize: TMinimizeEvent;
FOnRestore: TNotifyEvent;
procedure WMSIZE(Var Msg: TWMSIZE); Message WM_SIZE;
procedure WinRestore (Sender: TObject);
public
constructor Create (AOwner: TComponent); override;
destructor Destroy; override;
published
property OnMinimize: TMinimizeEvent read FOnMinimize
write FOnminimize;
end;
implementation
destructor TMiniForm.Destroy;
begin
inherited
end;
constructor TMiniForm.Create (AOwner: TComponent);
begin
FOnMinimize:= NIL;
inherited;
end;
procedure TMiniForm.WMSIZE (VAR msg: TWMSIZE);
// catch the WM_SIZE message to know what happend
var status: TMiniState;
begin
if msg.SizeType = integer (wsMinimized) THEN BEGIN
IF Assigned (FOnMinimize) then begin
status:= tmWindow; // Default action
FOnMinimize (self, status);
case status OF
tmNone: // Tell windows to restore the window immedately
PostMessage(Handle, WM_SYSCOMMAND, SC_RESTORE, 0);
tmWindow: ; // All is done by windows and Delphi
tmAll: // Minimize all windows of the app
begin
// Store the OnRestore event
FOnRestore:= Application.OnRestore;
// Replace it with own handler
Application.OnRestore:= WinRestore;
// Minimize all
Application.Minimize;
end;
end;
end;
end;
inherited; // Call other hanlder of WMSIZE event
end;
procedure TMiniForm.WinRestore (Sender: TObject);
// handles restoring of the windows
begin
// made Window normal
WindowState:= wsNormal;
// Restore other Windows of the app
Application.Restore;
// Restore the old Restore handler
Application.OnRestore:= FOnRestore;
// Call the restore handler
IF Assigned (FOnRestore) then FOnRestore (sender);
end;
end.