yoy.be "Why-o-Why"

Answering to the WM_QUERYENDSESSION message

2005-06-08 22:16  i95  delphi  [permalink]

Check if the Messages and Windows units are included in the uses clause in the start of your unit.
Add this bit to the protected clause in the form class declaration. By default there is no need for a protected clause, so if it's missing, type protected at the same level of public or private.

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

Declare the method something like this, adapt where neccessary to fit in your application.

procedure TMainWin.FormQueryEndSession(var Msg: TWMQueryEndSession);
begin
  Msg.Result:=1;
  PostMessage(Application.Handle,WM_CLOSE,0,0);
end;

We can't just call Close; because this method is called from the message loop from your application (have a look at TApplication.Run) and some things might need to happen after returning from this method. So we let Windows post us a close message so the internals of Application can do the close the correct way (something like simulating an Alt+F4).

If your application checks if the opened file has changed and needs to be saved, in the OnClose event or equivalent (never write clean-up-code in the OnClick of a 'Close' button or menuitem!!), you can do Msg.Result:=0; to let Windows know you're not ready to close and need the user to save the changes or press OK to discard the changes.


twitter reddit linkedin facebook