Preventing Windows shut down

前端 未结 7 619
梦毁少年i
梦毁少年i 2021-01-18 09:14

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)
priva         


        
7条回答
  •  春和景丽
    2021-01-18 09:49

    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;
    

提交回复
热议问题