Author: Radikal Q3
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:
1 2 TForm1 = class(TForm)
3 ..........
4 private5 procedure WMQueryEndSession(var Msg: TWMQueryEndSession); message6 WM_QUERYENDSESSION;
7 ..........
8 end;
Simply...we must add the next line in the Private part of the form:
9 10 procedure WMQueryEndSession(var Msg: TWMQueryEndSession); message11 WM_QUERYENDSESSION;
and later, in the implementation, we put the treatment code of the message:
12 13 procedure TForm1.WMQueryEndSession(var Msg: TWMQueryEndSession);
14 begin15 MessageBox(Handle, 'Cierra antes el programa', nil, MB_OK);
16 Msg.result := 0;
17 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.