Mega Code Archive

 
Categories / Delphi / Forms
 

Minimizing Application When a Form Minimizes

Title: Minimizing Application When a Form Minimizes Question: How do I minimize the application when I minimize a secondary form? I want to replicate the behavior that occurs when I minimize the main form. Answer: You can use the same logic found in \source\vcl\forms.pas that TCustomForm (in Delphi 3) uses when the form receives a system message to minimize. The form traps WM_SYSCOMMAND messages, checks to see if the command is to minimize the form, checks that the form is the MainForm, then performs the application object's Minimize method. If the message doesn't specify SC_MINIMIZE then we call inherited so that default processing can occur. Note: failure to include the call to inherited will probably cause bad things to happen. . . procedure WMSysCommand(var Message: TWMSysCommand); message WM_SYSCOMMAND; . . procedure TCustomForm.WMSysCommand(var Message: TWMSysCommand); begin if (Message.CmdType and $FFF0 = SC_MINIMIZE) and (Application.MainForm = Self) then Application.Minimize else inherited; end; To get this same behavior you can move this code to your form's unit, and remove the logic that checks that the form is a MainForm. unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button2: TButton; private procedure WMSysCommand(var Message: TWMSysCommand); message WM_SYSCOMMAND; end; var Form1: TForm1; implementation uses Unit2; {$R *.DFM} procedure TForm1.WMSysCommand(var Message: TWMSysCommand); begin if (Message.CmdType and $FFF0 = SC_MINIMIZE) then Application.Minimize else inherited; end; end.