Mega Code Archive

 
Categories / Delphi / Forms
 

How to Override Delphi Forms Restore Operation

Title: How to Override Delphi Form's Restore Operation? 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.