在DELPHI中,无窗体的程序如何获取系统关机或注销的消息?
写了一个无窗体的监控程序,在系统关机或注销时,会出现“结束程序-XXXX”的提示窗口。因为无窗体,所以不能使用Form的onClose事件,那么该如何知道系统要关机或注销...
写了一个无窗体的监控程序,在系统关机或注销时,会出现“结束程序 - XXXX”的提示窗口。因为无窗体,所以不能使用Form的onClose事件,那么该如何知道系统要关机或注销了呢?
谢谢“yyibetter”的指点。 展开
谢谢“yyibetter”的指点。 展开
2个回答
展开全部
写一个类,给类分配一个窗口句柄,然后在窗口过程里查询关机或注销消息,然后再显示;不明白可以参考TTimer类
给你个例子吧,两个单元,拿回去保存编译一下就行了:
program NoFormMsg;
uses
SysUtils,
Windows,
Messages,
Classes,
NoFormMsgCls in 'NoFormMsgCls.pas';
var
MyNoForm: TNoFormMsgCls;
msg: tagMsg;
begin
{ TODO -oUser -cConsole Main : Insert code here }
MyNoForm := TNoFormMsgCls.Crerte;
try
while True do begin
PeekMessage(msg, MyNoForm.Handle, 0, 0, PM_NOREMOVE);
if msg.message = WM_CLOSE then break;
TranslateMessage(msg);
DispatchMessage(msg);
Sleep(1);
end;
finally
MyNoForm.Free;
end;
end.
unit NoFormMsgCls;
interface
uses
Windows, Classes, Messages, SysUtils;
type
TNoFormMsgCls = class
private
FHandle: THandle;
procedure WndProc(var msg: TMessage);
public
constructor Crerte();
destructor Destroy(); override;
property Handle: THandle read FHandle;
end;
implementation
{ TNoFormMsgCls }
constructor TNoFormMsgCls.Crerte;
begin
FHandle := Classes.AllocateHWnd(WndProc);
end;
destructor TNoFormMsgCls.Destroy;
begin
Classes.DeallocateHWnd(FHandle);
inherited;
end;
procedure TNoFormMsgCls.WndProc(var msg: TMessage);
begin
with Msg do
if Msg = WM_QUERYENDSESSION then begin
if (LParam and ENDSESSION_LOGOFF) > 0 then begin
Result := 0;
MessageBox(FHandle, '注销啦!', '结束任务', MB_OK);
//PostMessage(FHandle, WM_CLOSE, 0, 0);
end
else begin
Result := 0;
MessageBox(FHandle, '关机啦!', '结束任务', MB_OK);
//PostMessage(FHandle, WM_CLOSE, 0, 0);
end;
end
else
Result := DefWindowProc(FHandle, Msg, wParam, lParam);
end;
end.
给你个例子吧,两个单元,拿回去保存编译一下就行了:
program NoFormMsg;
uses
SysUtils,
Windows,
Messages,
Classes,
NoFormMsgCls in 'NoFormMsgCls.pas';
var
MyNoForm: TNoFormMsgCls;
msg: tagMsg;
begin
{ TODO -oUser -cConsole Main : Insert code here }
MyNoForm := TNoFormMsgCls.Crerte;
try
while True do begin
PeekMessage(msg, MyNoForm.Handle, 0, 0, PM_NOREMOVE);
if msg.message = WM_CLOSE then break;
TranslateMessage(msg);
DispatchMessage(msg);
Sleep(1);
end;
finally
MyNoForm.Free;
end;
end.
unit NoFormMsgCls;
interface
uses
Windows, Classes, Messages, SysUtils;
type
TNoFormMsgCls = class
private
FHandle: THandle;
procedure WndProc(var msg: TMessage);
public
constructor Crerte();
destructor Destroy(); override;
property Handle: THandle read FHandle;
end;
implementation
{ TNoFormMsgCls }
constructor TNoFormMsgCls.Crerte;
begin
FHandle := Classes.AllocateHWnd(WndProc);
end;
destructor TNoFormMsgCls.Destroy;
begin
Classes.DeallocateHWnd(FHandle);
inherited;
end;
procedure TNoFormMsgCls.WndProc(var msg: TMessage);
begin
with Msg do
if Msg = WM_QUERYENDSESSION then begin
if (LParam and ENDSESSION_LOGOFF) > 0 then begin
Result := 0;
MessageBox(FHandle, '注销啦!', '结束任务', MB_OK);
//PostMessage(FHandle, WM_CLOSE, 0, 0);
end
else begin
Result := 0;
MessageBox(FHandle, '关机啦!', '结束任务', MB_OK);
//PostMessage(FHandle, WM_CLOSE, 0, 0);
end;
end
else
Result := DefWindowProc(FHandle, Msg, wParam, lParam);
end;
end.
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询