检测如果Windows关闭或应用程序尝试从系统菜单(WM_CLOSE)关闭 [英] Detect if Windows closing or application tries to close from system menu (WM_CLOSE)

查看:133
本文介绍了检测如果Windows关闭或应用程序尝试从系统菜单(WM_CLOSE)关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有托盘应用程序。

Onj FormCloseQuery我检查程序是否应该托盘,而不是关闭它,我把它放在托盘(CanClose:= False)

Onj FormCloseQuery I check if program should goto tray and instead of closing it I put it in tray (CanClose := False)

但是如果Windows尝试关闭我的应用程序,因为Windows关闭,我不想将我的应用程序移入托盘,但要关闭它。

But if Windows tries to close my application because of Windows shutdown I want not to move my app into tray but to close it.

Win7终止我的应用程序,但XP不会关闭,因为我的应用程序仍然在托盘中。

Win7 terminates my app, but XP doesn't close because my app remains in Tray.

如何检测Windows是否关闭 或者模式?

How can I detect if Windows is some "shutting down" mode or not?

谢谢!

推荐答案

从使用 OnCloseQuery 这是错误的事件要使用。 Remy的答案解释了如何解决Windows关闭被默认的VCL结束会话消息处理阻塞。而这又是因为在 OnCloseQuery CanClose False $ c>事件。

Your problems stem from the use of OnCloseQuery which is the wrong event to be using. Remy's answer explains how to workaround Windows shutdown being blocked by the default VCL end session message handling. And this in turn is caused by setting CanClose to False in the OnCloseQuery event.

该解决方法将完成工作,但有一个更简单的方法来处理此问题。
而不是停止关闭窗体,让它继续关闭。一起删除你的 OnCloseQuery 事件。将其替换为 OnClose 事件。

That workaround will get the job done but there's a much simpler way to deal with this. Instead of stopping the form from closing, let it go ahead and close. Remove your OnCloseQuery event altogether. Replace it with an OnClose event.

procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caNone;
  Visible := False;
end;

这个相当微不足道的代码足以使您的应用程序最小化到托盘主表格关闭。

This rather trivial bit of code is enough to make your app minimize to the tray when the main form is closed.

这篇关于检测如果Windows关闭或应用程序尝试从系统菜单(WM_CLOSE)关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆