Mirror

Prevent a user from closes Windows (Views: 100)


Problem/Question/Abstract:

When the user closes Windows having our application opened, it is possible that occur in a wrong moment (without recording data, in the middle of a process, etc).

Answer:

This can be avoided capturing the message that send Windows to all the applications when the user wants to close Windows: the  message WM_QUERYENSESSION

To capture the message and to send it to our treatment code of the message:

     TForm1 = class(TForm)
     ..........
       private
         procedure WMQueryEndSession(var Msg: TWMQueryEndSession); message WM_QUERYENDSESSION;
     ..........
     end;

Simply...we must add the next line in the Private part of the form:

    procedure WMQueryEndSession(var Msg: TWMQueryEndSession); message WM_QUERYENDSESSION;

and later, in the implementation, we put the treatment code of the message:

procedure TForm1.WMQueryEndSession(var Msg: TWMQueryEndSession);
begin
  MessageBox(Handle, 'Cierra antes el programa', nil, MB_OK);
  Msg.result := 0;
end;

If instead of aborting the close of Windows, we want that follows being accomplished, enough with which we change the Msg.result:=0 for Msg.result:=1.

NOTE: The behavior of the message defers in win98 and WinNT, as soon as watches you the help of the message WM_QUERYENDSESSION.

<< Back to main page