우리 소프트웨어가 실행되는 동안 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.