防止 Windows 关闭
为了检测并防止关闭计算机,我使用了非常简单的程序。它只有一种形式和一个私有过程,如下所示:
TForm3 = class(TForm)
private
procedure WMQueryEndSession(var Msg : TWMQueryEndSession) ;
message WM_QueryEndSession;
end;
以及我在 Delphi 5 和 Delphi 2010 上编译的实现。
procedure TForm3.WMQueryEndSession(var Msg: TWMQueryEndSession);
begin
Msg.Result := 0; //so I don't want to shutdown while my program is running
end;
它们都检测关闭。但是当我在Delphi 2010中编译时;防止关闭后我的程序关闭。 (电脑没有关闭)
我如何从两者中得到相同的结果?
To detect and prevent shutdown the computer I use very simple program. It has only one form and one private procedure like below:
TForm3 = class(TForm)
private
procedure WMQueryEndSession(var Msg : TWMQueryEndSession) ;
message WM_QueryEndSession;
end;
and the implementation
procedure TForm3.WMQueryEndSession(var Msg: TWMQueryEndSession);
begin
Msg.Result := 0; //so I don't want to shutdown while my program is running
end;
I compiled it Delphi 5 and Delphi 2010. Both of them detect shutdown. But when I compiled in Delphi 2010; after preventing shutdown my program closes. (PC doesn't shutdown)
How do I get the same result from both of them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
编辑:更改为拦截 WM_ENDSESSION 而不是 WM_QUERYENDSESSION。
由于您无法直接更改 TApplication 的行为,因此您可以安装 TApplication 消息挂钩来中和 WM_ENDSESSION 消息。
安装这样的钩子非常简单,您只需在主窗体中添加类似于以下的方法并在 FormCreate 中注册该钩子即可。
EDIT: changed to intercept WM_ENDSESSION instead of WM_QUERYENDSESSION.
As you cannot directly change the behaviour of TApplication, you can install a TApplication message hook instead that neutralizes the WM_ENDSESSION message.
Installing such a hook is quite simple, you only have to add a method similar to the following to your mainform and register the hook in FormCreate.
我通常运行“shutdown -a”命令。您可以从代码中执行相同的操作来中断 Windows 关闭。
问候
I usually run "shutdown -a" command. You can do the same from your code to interrupt Windows from shutdown.
Regards
这看起来像是 Delphi 中的一个错误。我建议您将其发布到 Quality Central。
This looks like a bug in Delphi. I suggest you to post this on Quality Central.
编辑:这是一种行不通的方法。谢谢
Edit: Here's an approach that doesn't work. Thanks
您是否在同一操作系统上进行测试? Vista 中有一些应用程序关闭更改。阅读以下内容:Windows Vista 中的应用程序关闭更改
如果您在同一操作系统上进行测试,则 Delphi 2010 可能会以不同的方式处理 WM_ENDSESSION 消息。在Delphi 7中,WM_ENDSESSION消息在Application.WndProc中处理。
Are you testing on the same OS? There are some application shutdown changes in Vista. Read this: Application Shutdown Changes in Windows Vista
If you are testing on the same OS, maybe Delphi 2010 handles WM_ENDSESSION messages in a different way. In Delphi 7, WM_ENDSESSION message are handled in Application.WndProc.
在所有版本中,您不应该使用 FormCloseQuery 事件吗?
哎呀 - 只需阅读“这不起作用”的评论:( win 7 是否有所不同?
在我的所有应用程序中,如果 Windows 试图关闭,则会调用此...
In all versions should you not be using the FormCloseQuery event?
Oops - just read comments to "this does not work" :( Is win 7 different?
In all my apps this gets called if windows is trying to shut down...
ShutdownGuard 是用 Delphi 构建的,它是开源的,您可以下载它并根据您的需要进行调整
ShutdownGuard is built with Delphi and it's open source, you can download it tweak it for your needs