Delphi program prevents users from shutting down the operating system

Source: Internet
Author: User
Tags response code

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.