WEBINAR:

On-Demand

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 you
want 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 and
plain. 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...

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:

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!