Sometimes the program is running, does not allow other programs or artificially shut down the computer unless the application knows that Windows will exit, it is very simple, we all know that the system will send WM_QUERYENDSESSION this shutdown message to each program when the system will be closed. As long as our program accepts this message, we will do what we need to do properly.
There are several ways to handle Windows messages. Here we use Application's onMessage events to establish a process of responding to events! As in the following example: unit unit1; interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs; type TForm1 = class (TForm) private {Private declarations} public procedure AppMessageHandler (var Msg: TMsg; var Handled : Boolean; // Declare the system to process the message process, respond to the pro process of the onMessage event of Application must be TMESSAGEVENT type; {public declarations} end; var form1: tform1; importation {$ r * .dfm} processation {$ r * .dfm} processation {$ r * .dfm} Procedure TFORM1.AppMessageHandler (var MSG) : Tmsg; var handled: boolean; begin if msg.Message = WM_QueryEndSession dam // If the message received is to turn off the computer's message, special processing, because only an example, I only write the pop-up dialog box, everyone can Depending on the needs of your own procedure; begin if Messagedlg ('shutdown?', Mtconfirmation, mbyesnocancel, 0) = mryes dam: = true else handled: = false; end; end; end. Finally in the program's DPR file In the creation of a form, add Application.onMessage: = Form1.AppMessageHandler before calling Application.Run;