Preventing a Form from Re-sizing Using a Windows Message

Preventing a form from re-sizing using a Windows message

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.

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!

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a