开发者

NOT prevent windows from shutting down OnCloseQuery

I have an application that hides itself on closing by the red cross. User can exit it by right-clicking tray icon and choosing Exit. But it would apparently block windows from shutting down, so I made a procedure to respond to a WM_QUERYENDSESSION to enable closing, this is the relevant code:

procedure TMainForm.OnWindowsEnd(var Msg: TMessage); // responds to message WM_QUERYENDSESSION;
begin
  AllowClose:=true;
  Close;
end;

procedure TMainForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  CanClose:=AllowClose;
  if NOt AllowClose then
    Hide;
end;

But weird thing keeps happening. When I issue a shutdown, this application closes nice开发者_开发技巧ly. But that is all. When I issue a second shutdown, system quits fine. (I'm testing this in WinXP).

What can be the cause? Thank you


ANSWER Code should look like this

procedure TMainForm.OnWindowsEnd(var Msg: TMessage); // responds to message WM_ENDSESSION;
begin
  // Possible checking for flags, see http://msdn.microsoft.com/en-us/library/aa376889%28v=vs.85%29.aspx
  AllowClose:=true;
  Msg.Result:=1;
end;

procedure TMainForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  CanClose:=AllowClose;
  if NOt AllowClose then
    Hide;
end;


WM_QUERYENDSESSION is a "query", not a shutdown command: Windows asks you if you're OK with shutting down, doesn't ask you to shut down. You shouldn't call Close!

Secondly Windows expects you to return TRUE when processing that message, so it knows you're OK with a potential Shut Down. I assume you're not setting the result to something TRUE, so Windows aborts the first Shut Down request.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜