devxlogo

Preventing a Form from Re-sizing Using a Windows Message

Preventing a Form from Re-sizing Using a Windows Message

Question:
Preventing a form from re-sizing using a Windows message

Answer:
There are some cases in your development where you want to prevent your users from re-sizing a form. This is especially true if youwant a form to behave similarly to a dialog box, but want to maintain the look of a regular window without building the form as a dialog box. True, you can easily set the form’s BorderStyle property to bsSingle, but to me, that type of border style looks flat andplain. Not too exciting.

The example I have below employs a Windows message called WM_GETMINMAXINFO. It’s a message that is sent to a window when its size or position is about to change. And it can also be used to override a window’s default maximized size and position. It can also override the window’s default minimum or maximum tracking size (that is, when the user tries to resize the form using the mouse), thus restricting window sizing at runtime. Let’s look at the code…

unit Unit1;interfaceuses  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;type  TForm1 = class(TForm)  private    { Private declarations }    procedure WMGetMinMaxInfo(var Msg: TWMGetMinMaxInfo); message WM_GETMINMAXINFO;  public    { Public declarations }  end;var  Form1: TForm1;implementation{$R *.DFM}procedure TForm1.WMGetMinMaxInfo(var Msg: TWMGetMinMaxInfo);begin  inherited;  with Msg.MinMaxInfo^ do begin     ptMinTrackSize.x:= Form1.width;     ptMaxTrackSize.x:= Form1.width;     ptMinTrackSize.y:= Form1.height;     ptMaxTrackSize.y:= Form1.height;  end;end;end.

In the private section of our code, we make the procedure declaration for our message handler. Notice the message WM_GETMINMAXINFO. This tells the compiler that with the preceding procedure, we’re trapping the WM_GETMINMAXINFO message. This actually allows us to name the procedure anything we want so we could make the declaration of the procedure GetWinMinMaxInformation(var Msg: TWMGetMinMaxInfo); message WM_GETMINMAXINFO. But by convention, message handlers have names that as closely as possible approximate the name of their prospective windows messages.

See also  How to Create and Deploy QR Codes Online: A Comprehensive Guide

Looking at the message-handling code itself, we first make a call to the inherited WMGetMinMaxInfo handler. Next, we set the x and y values of the tracking size fields of the message structure to the default width and height of the form. Maybe we should look at the structure itself. The WMGetMinMaxInfo message has a single parameter called MinMaxInfo, which is a structure defined as follows:

typedef struct tagMINMAXINFO {  // mmi     POINT ptReserved;     POINT ptMaxSize;     POINT ptMaxPosition;     POINT ptMinTrackSize;     POINT ptMaxTrackSize; } MINMAXINFO; 

In ObjectPascal, this would be a record with five TPoint fields. What we did above was set the ptMin and ptMaxTrackSize fields to the size of the form. However, as you can see in the structure, we can even mess around with the ptMaxSize and ptMaxPosition fields. But that’s beyond the scope of this discussion.

If you think about it, this was actually a very simple thing to do. Unfortunately, with Windows, a lot of really simple things are hidden behind a veil of complexity that requires pushing through to get at what you want. In any case, have fun with the code!

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist