devxlogo

How to prevent user shutting Windows down while Delphi app is running

How to prevent user shutting Windows down while Delphi app is running

Question:
How can I trap the API message sent by Windows 95 on closure, to prevent a user shutting Windows down when a Delphi application is running?

Answer:
There are two ways to approach this problem. One is strictly Delphi;the other involves creating a message handler for the WM_CLOSE message inWindows. Let’s look at the former method first.

The easiest way to decide whether to allow a user to close aprogram is with the OnCloseQuery method of the main form. If you openthat method, you’ll see the following:

procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);beginend;
The most important part of that method is the CanClose formal parameter. IfFalse, the window will not be allowed to close. If true, the programwill close. Here’s an example. Let’s say that while some backgroundprocessing is occuring, you have a Boolean variable called “ProcessRunning”set to true during the course of the process. If the user tries toclose the program during the run you could do something like this:
procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);begin  if ProcessRunning then    CanClose := False  else    CanClose := True;end;
The point to this is to check certain conditions within the program.If the criteria for closing is correct, you can close the window.Otherwise, leave it up.

You’d do the same thing with a custom message handler for theWM_CLOSE message. The only difference is, you have to create thehandler. To do this, look at the following section of code taken out of aprogram that I wrote that is set up to handle WM_CLOSE:

  private    procedure WMClose(var Msg : TWMClose); message WM_CLOSE;  public  end;var  Form1: TForm1;  CloseDown : Boolean;implementation{$R *.DFM}procedure TForm1.WMClose(var Msg : TWMClose);begin  if NOT CloseDown then    Msg.Result := 0  else    inherited;end;
If you’re new to creating custom Windows message handlers, while not atrivial thing, it’s not that hard to do. It just requires some work. I won’t go into a long discussion about Windows messagehandlers. WM_CLOSE happens to be a fairly simple message to handle, butthere are more complex ones out there that take much more spacethan a simple e-mail reply. So I’ll just tell you how to go about handlingthe message, then let you play with the code:
See also  How to Create and Deploy QR Codes Online: A Comprehensive Guide

First, in the private section of the form’s type declaration, you must declare the handler. In this case, I’ve declared it as:

procedure WMClose(var Msg : TWMClose); message WM_CLOSE;
You can name the procedure anything you like.

As far as the implementation is concerned, to deny closure, we have to setthe message’s result to 0. To allow closure, we just call the inheritedWM_CLOSE handler.

procedure TForm1.WMClose(var Msg : TWMClose);begin  if NOT CloseDown then    Msg.Result := 0  else    inherited;end;
By the way, in the sample program I have two buttons that set the CloseDownvariable to True or False.

As you can see, the principle is exactly the same as using the OnCloseQuerymethod of the form. But you can also see that to accomplish the same task, abit more work had to be done. So a good “rule of thumb” is to use Delphi’s built-in capabilities as much as possible, and to use only custom message handlers for events that aren’t handled by default in Delphi.

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