1. Home
  2. Computing & Technology
  3. Delphi Programming

How to Override Delphi Form's Restore Operation?
Example: Create a Minimize/Maximize Only Form.

By , About.com Guide

By handling the WM_SYSCOMAND Windows message, you can catch and handle the mimimize and maximize Delphi Form's title button clicks.

The WM_SYSCOMAND can also be used to trap the restore window operation. When the form is maximized the "Maximize" button changes its look and operation. Clicking the restore button restores the window to its previous (normal, before it was maximized) position and size.

By overriding (changing) the default "restore" action, you can for example, create a Delphi form that can be only mimimized or maximized.

SC_RESTORE and WM_SysCommand

To catch and react on the form's restore operation, handle the WM_SysCommand Windows message.

Start by creating the message handler (procedure) declaration in the private section of the form declaration:

type
  TForm1 = class(TForm)
  private
    procedure WMSysCommand (var Msg: TWMSysCommand) ; message WM_SYSCOMMAND;
...

Note: Hit CTRL + C to activate Delphi code completion.

Here's how to allow only maximized or minimized form state:

//Allowing ONLY Mimized and Maximized state
procedure TForm1.WMSysCommand(var Msg: TWMSysCommand) ;
begin
  if Msg.CmdType = SC_RESTORE then
  begin
    if self.WindowState = wsMaximized then
    begin
      self.WindowState := wsMinimized;
      Msg.Result := 0;
      Exit;
    end;
    if self.WindowState = wsMinimized then
    begin
      self.WindowState := wsMaximized;
      Msg.Result := 0;
      Exit;
    end;
  end;

  DefaultHandler(Msg) ;
end;

Upon receiving the restore message, check the form state, then set it (to either "max" or "min") using the WindowState property.

Note: Don't confuse restoring an application with restoring a form or window to its original size. The TApplication's OnRestore event is triggered when the application is restored from the TaskBar.

Delphi tips navigator:
» IsDirectoryEmpty - Delphi function to Determine if a Direcotry is Empty (no files, no sub-folders)
« How to Declare a Constant Record in Delphi

More Delphi Programming Quick Tips
Explore Delphi Programming
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. Delphi 2007 Tips
  7. How to Override Delphi Form's Restore Operation?

©2009 About.com, a part of The New York Times Company.

All rights reserved.