如何区分终止我的程序的不同方法? [英] How do I distinguish different ways of terminating my program?

查看:181
本文介绍了如何区分终止我的程序的不同方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C ++ Builder 2007为Windows编写程序。

I'm coding a program for Windows, using C++Builder 2007.

当我的程序关闭时,它的行为应该有所不同,已关闭:

When my program is closed, it is supposed to behave differently depending on how it was closed:


  1. 用户点击了应用程序窗口的X:返回应用程序的用户登录。

  2. 程序通过TaskManager终止:然后终止程序。这是特别重要的,因为程序不能阻止Windows关闭。

获取一个或另一个很容易:实现< c $ c> TForm :: OnClose()或 OnCloseQuery()来处理事件。然而,两者都不给我指示事件的原因。

Getting one OR the other is easy: Implement a TForm::OnClose() or OnCloseQuery() to handle the event. However, both don't give me an indication of what caused the event. Is there another way to know what actually caused the close event?

推荐答案

你可以使你的主窗体lsiten到 WM_SYSCOMMAND 消息。如果您收到此命令类型 SC_CLOSE 的消息,则您知道用户已单击关闭按钮,按Alt + F4或从系统菜单中选择关闭。

You can make your main form lsiten to the WM_SYSCOMMAND message. If you receive this message with command type SC_CLOSE, then you know that the user has clicked the close button, pressed Alt+F4, or selected 'Close' from the system menu. Then you can do whatever you like (as opposed to letting the default action close the form).

例如,您可以再次显示登录对话框。

For example, you could display the login dialog again.

这不会影响关闭主窗体/应用程序的其他方法,因此您仍然可以从任务管理器中的(第一个选项卡)关闭应用程序。

This will not affect other ways of closing the main form/application, so you can still close the application from the (first tab) in the Task Manager.

[以前,这个Q被标记为Delphi。正因为如此,我的答案的第一个版本包含了这个想法的Delphi实现。]

[Previously, this Q was tagged Delphi. Because of this, the first version of my answer contains the Delphi implementation of this idea.]

这篇关于如何区分终止我的程序的不同方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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