ソフトウェアの実行中にWindowsがシャットダウンするのを防ぐために、私たちのシステム用に小さなツールを作ろうとしています。それを独立させるために、私はthis情報を使ってシャットダウンを防止する別のアプリケーションを作った。Delphi - トレイアプリケーションからWindowsがシャットダウンするのを防ぐ
ただし、アプリケーションをトレイに最小化すると、Windowsは単にアプリケーションを終了して正常にシャットダウンします。フォームが表示されている場合(つまり、Form.OnCreateイベントからApplication.Minimizeコールにコメントする)、正常にシャットダウンすることはありません。
MainWindowフックを使用してライブを維持する方法や、アプリをトレイに隠してシステムのシャットダウンを防止する方法がありますか?
ありがとうございました。
現在のコード:
unit Main;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, TlHelp32, dateutils, Vcl.AppEvnts, Vcl.ExtCtrls;
type
TForm1 = class(TForm)
TrayIcon1: TTrayIcon;
ApplicationEvents1: TApplicationEvents;
procedure FormCreate(Sender: TObject);
function HookEndSession(var Message: TMessage): Boolean;
procedure WMQueryEndSession(var Msg : TWMQueryEndSession) ;
message WM_QueryEndSession;
procedure ApplicationEvents1Minimize(Sender: TObject);
procedure ApplicationEvents1Restore(Sender: TObject);
procedure TrayIcon1DblClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var Form1: TForm1;
var Mutex : THandle;
implementation
{$R *.dfm}
procedure TForm1.WMQueryEndSession(var Msg: TWMQueryEndSession);
begin
Msg.Result := 0;
end;
function TForm1.HookEndSession(var Message: TMessage): Boolean;
begin
result := false;
if Message.Msg = WM_ENDSESSION then begin
Message.Result := 0;
result := true;
end;
end;
procedure TForm1.TrayIcon1DblClick(Sender: TObject);
begin
WindowState := wsNormal;
Application.Terminate;
end;
procedure TForm1.ApplicationEvents1Minimize(Sender: TObject);
begin
Hide();
WindowState := wsMinimized;
TrayIcon1.Visible := True;
end;
procedure TForm1.ApplicationEvents1Restore(Sender: TObject);
begin
Application.Minimize;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Mutex := CreateMutex(nil, True, 'preventWinShutdown');
if (Mutex = 0) OR (GetLastError = ERROR_ALREADY_EXISTS) then
Application.Terminate;
Application.HookMainWindow(HookEndSession);
TrayIcon1.Hint := 'Windows Shutdown prevented.';
//Application.Minimize;
end;
end.