我正在使用托盘应用程序。
Onj FormCloseQuery 我检查程序是否应该转到托盘,而不是关闭它,而是将其放入托盘中(CanClose := False)
但是,如果 Windows 由于 Windows 关闭而尝试关闭我的应用程序,我不想将我的应用程序移至托盘中,而是将其关闭。
Win7 终止我的应用程序,但 XP 不会关闭,因为我的应用程序仍保留在托盘中。
如何检测 Windows 是否处于某种“关闭”模式?
Thanks!
If the OnCloseQuery
事件被触发以响应WM_QUERYENDSESSION
消息、设置CanClose=False
会导致消息返回FALSE
.
在 XP 及更早版本中,这将取消 Windows 关闭。到目前为止,任何已收到WM_QUERYENDSESSION
消息将收到WM_ENDSESSION
消息及其wParam
值设置为FALSE
告诉那些应用程序NOT终止自己。这就是为什么您的应用程序会进入托盘并且在 Windows 关闭期间不会退出。
Microsoft 在 Windows Vista 中更改了此行为,因此应用程序无法通过以下方式取消 Windows 关闭WM_QUERYENDSESSION
不再了。这就是 Windows Vista 及更高版本将终止您的应用程序的原因。如果应用程序需要故意停止 Windows 关闭,则会引入一个全新的 API。
MSDN 上对此进行了记录:
Windows Vista 中的应用程序关闭更改
要执行您所要求的操作,您必须拦截WM_QUERYENDSESSION
直接发送消息,以便您确定是否OnCloseQuery
由于 Windows 关闭或非关闭而被调用。例如:
type
TForm1 = class(TForm)
private
procedure WMQueryEndSession(var Message: TWMQueryEndSession); message WM_QUERYENDSESSION;
procedure WMEndSession(var Message: TWMEndSession); message WM_ENDSESSION;
end;
var
ShuttingDown: Boolean = False;
procedure TForm1.WMQueryEndSession(var Message: TWMQueryEndSession);
begin
ShuttingDown := True;
inherited;
end;
procedure TForm1.WMEndSession(var Message: TWMEndSession);
begin
ShuttingDown := Message.EndSession;
inherited;
end;
procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
CanClose := ShuttingDown;
if not ShuttingDown then
begin
// your Tray logic here ...
end;
end;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)