Form Constraints
When you choose a resizable border for a form, users can generally resize the form as they like and also maximize it to full screen. Windows informs you that the form's size has changed with the wm_Size message, which generates the OnResize event. OnResize takes place after the size of the form has already been changed. Modifying the size again in this event (if the user has reduced or enlarged the form too much) would be silly. A preventive approach is better suited to this problem.
Delphi provides a specific property for forms and also for all controls: the Constraints property. Simply setting the subproperties of the Constraints property to the proper maximum and minimum values creates a form that cannot be resized beyond those limits. Here is an example:
object Form1: TForm1
Constraints.MaxHeight = 300 Constraints.MaxWidth = 300 Constraints.MinHeight = 150 Constraints.MinWidth = 150 end
Notice that as you set up the Constraints property, it has an immediate effect even at design time, changing the size of the form if it is outside the permitted area.
Delphi also uses the maximum constraints for maximized windows, producing an awkward effect. For this reason, you should generally disable the Maximize button of a window that has a maximum size. There are cases in which maximized windows with a limited size make sense— this is the behavior of Delphi's main window. In case you need to change constraints at run time, you can also consider using two specific events, OnCanResize and OnConstrainedResize. The first of the two can also be used to disable resizing a form or control in given circumstances.
Post a comment