简体   繁体   English

不阻止 windows 关闭 OnCloseQuery

[英]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:但它显然会阻止 windows 关闭,所以我做了一个程序来响应 WM_QUERYENDSESSION 以启用关闭,这是相关代码:

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 nicely.当我发出关闭命令时,此应用程序会很好地关闭。 But that is all.但仅此而已。 When I issue a second shutdown, system quits fine.当我发出第二次关机时,系统退出正常。 (I'm testing this in WinXP). (我在 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. WM_QUERYENDSESSION是“查询”,而不是关闭命令:Windows 询问您是否可以关闭,而不是要求您关闭。 You shouldn't call Close !你不应该打电话给Close

Secondly Windows expects you to return TRUE when processing that message, so it knows you're OK with a potential Shut Down.其次,Windows 期望您在处理该消息时返回 TRUE,因此它知道您可以接受潜在的 Shut Down。 I assume you're not setting the result to something TRUE, so Windows aborts the first Shut Down request.我假设您没有将结果设置为 TRUE,因此 Windows 中止了第一个 Shut Down 请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM