This example describes preventing users from shutting down the operating system while the program is running.
Whether the user can shut down the operating system is done by processing the wm_queryendsession, you first need to add the WM_QUERYENDSESSION message Processing declaration to the program:
procedure QueryEndSession(var Msg:TMessage);Message WM_QueryEndSession;
Its response code is as follows:
procedure TForm1.QueryEndSession(var Msg:TMessage);
begin
Msg.Result:=0;
end;
When the user shuts down the operating system, the operating system publishes a wm_queryendsession message, and the queryendsession process in this instance is activated, and the program prevents the user from shutting down the operating system by setting the return value of the message to 0. Conversely, if the set message returns a value of 1, the user is allowed to shut down the operating system.
The program code is as follows:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
TForm1 = class(TForm)
private
{ Private declarations }
procedure QueryEndSession(var Msg:TMessage);Message WM_QueryEndSession;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.QueryEndSession(var Msg:TMessage);
begin
Msg.Result:=0;
end;
end.
Save the file, and then press the F9 key to run the program. Users will not be able to shut down the operating system as long as the program is running normally.
In engineering practice, it is often necessary to keep the application running uninterrupted, and to prevent accidental factors from shutting down the operating system, you can use the method described in this example.