防止 Windows 关闭

发布于 2024-09-06 21:51:21 字数 518 浏览 7 评论 0原文

为了检测并防止关闭计算机,我使用了非常简单的程序。它只有一种形式和一个私有过程,如下所示:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

温暖的光 2024-09-13 21:51:21

编辑:更改为拦截 WM_ENDSESSION 而不是 WM_QUERYENDSESSION。

由于您无法直接更改 TApplication 的行为,因此您可以安装 TApplication 消息挂钩来中和 WM_ENDSESSION 消息。

安装这样的钩子非常简单,您只需在主窗体中添加类似于以下的方法并在 FormCreate 中注册该钩子即可。

function TForm25.HookEndSession(var Message: TMessage): Boolean;
begin
  result := false;
  if Message.Msg = WM_ENDSESSION then begin
    Message.Result := 0;
    result := true;
  end;
end;

procedure TForm25.FormCreate(Sender: TObject);
begin
  Application.HookMainWindow(HookEndSession);
end;

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.

function TForm25.HookEndSession(var Message: TMessage): Boolean;
begin
  result := false;
  if Message.Msg = WM_ENDSESSION then begin
    Message.Result := 0;
    result := true;
  end;
end;

procedure TForm25.FormCreate(Sender: TObject);
begin
  Application.HookMainWindow(HookEndSession);
end;
梦中楼上月下 2024-09-13 21:51:21

我通常运行“shutdown -a”命令。您可以从代码中执行相同的操作来中断 Windows 关闭。

问候

I usually run "shutdown -a" command. You can do the same from your code to interrupt Windows from shutdown.

Regards

醉城メ夜风 2024-09-13 21:51:21

这看起来像是 Delphi 中的一个错误。我建议您将其发布到 Quality Central。

This looks like a bug in Delphi. I suggest you to post this on Quality Central.

段念尘 2024-09-13 21:51:21

编辑:这是一种行不通的方法。谢谢

Procedure TMyForm.FormClose(Sender: TObject;  Var Action: TCloseAction);
Begin
  Action := caNone;  //The form is not allowed to close, so nothing happens.
End;                 // Note: the OP says he tried this, doesn't help. See the comments.

Edit: Here's an approach that doesn't work. Thanks

Procedure TMyForm.FormClose(Sender: TObject;  Var Action: TCloseAction);
Begin
  Action := caNone;  //The form is not allowed to close, so nothing happens.
End;                 // Note: the OP says he tried this, doesn't help. See the comments.
咽泪装欢 2024-09-13 21:51:21

您是否在同一操作系统上进行测试? 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.

假面具 2024-09-13 21:51:21

在所有版本中,您不应该使用 FormCloseQuery 事件吗?

procedure TForm3.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  Canclose := Not StillDoingImportantStuff;
end;

哎呀 - 只需阅读“这不起作用”的评论:( win 7 是否有所不同?

在我的所有应用程序中,如果 Windows 试图关闭,则会调用此...

In all versions should you not be using the FormCloseQuery event?

procedure TForm3.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  Canclose := Not StillDoingImportantStuff;
end;

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...

烏雲後面有陽光 2024-09-13 21:51:21

ShutdownGuard 是用 Delphi 构建的,它是开源的,您可以下载它并根据您的需要进行调整

ShutdownGuard is built with Delphi and it's open source, you can download it tweak it for your needs

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文