Responding when the user minimizes or maximizes a Form

Question:

How can I respond to the user minimizing or maximizing my form before the resizing actually takes place?

Answer:

The following example demonstrates trapping the Windows WM_SYSCOMMAND message. If the command specifies to minimize or maximize the form, we will beep the PC speaker instead;

Example:

type

  TForm1 = class(TForm)

  private

  { Private declarations }

  procedure WMSysCommand(var Msg: TWMSysCommand);

  message WM_SYSCOMMAND;

  public

  { Public declarations }

  end;

 

var

  Form1: TForm1;

 

implementation

 

{$R *.DFM}

 

procedure TForm1.WMSysCommand;

begin

  if (Msg.CmdType = SC_MINIMIZE) or

  (Msg.CmdType = SC_MAXIMIZE) then

  MessageBeep(0) else

  inherited;

end;