Restricting Minimum and Maximum window sizes in Delphi

'

This article was written by Peter Johnson. Peter has written a number of articles, components and applications, unfortunately the web address we have is no longer valid, if you know where it is now please let us know.

This method of restricting a window or controls size is now redundant. Current versions of C++ Builder and Delphi support the Constraints property (implemented in TControl and descendants), which means now you only need to set the relevant properties.

Often it is desirable to set the minimum size that a user can resize a window to. One method is putting code behind the 'OnResize' event that puts the window back to a predefined minimum size. This works but produces unprofessional looking results. It is better to override the WM_GetMinMaxInfo message to set the minimum and maximum tracking sizes.

The following example demonstrates the technique. The minimum width of the window is set to a value that doesn't allow the 'HelpButton' to be obscured. The minimum height of the form is set to 200 pixels. Note that this message can (and does) get sent before controls are created on the form so if we wish to reference a control in the procedure it is necessary to check that it has been created first. The following example simply exits until the required controls have been created.

Note that the same techniques can be used to set the maximum tracking size by modifying the contents of 'ptMaxTrackSize'.

Private declarations for class:

procedure WMGetMinMaxInfo(var MSG: Tmessage); 
    message WM_GETMINMAXINFO; 

Within the implementation do:

procedure TMyForm.WMGetMinMaxInfo(var MSG: TMessage);
begin
   inherited;
   if not Assigned(HelpButton) then 
      Exit;
   with PMinMaxInfo(MSG.lparam)^ do
   begin
      with ptMinTrackSize do
      begin
         X := (Width - ClientWidth) + HelpButton.Left +
                  HelpButton.Width;
         Y := 200;
      end;
   end;
end;

Google
Web www.Delphi-Central.com
Delphi Central - Delphi Programming Tutorials, Hints and Tips