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.

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:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as