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

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?

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:

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.

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


The Latest

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

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS