想实现的目的:当按下键盘上的Power键(关机)时,系统获得按键关机消息,并打出一个提醒框,如果确认,则关机,否则不关机。
在应用程序里已实现,但是做成Service程序,安装成功且已启动之后,并没有实现我要的效果! 请各位大虾看一下有什么问题?能否指点一二?服务为什么不响应系统消息?
代码如下:
unit MyPowerKey;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr, Dialogs;
type
TPower_Key = class(TService)
procedure ServiceCreate(Sender TObject);
private
{ Private declarations }
public
function GetServiceController TServiceController; override;
procedure WMPowerBroadCast(var msg TMessage);message WM_POWERBROADCAST;键盘Power键广播消息
{ Public declarations }
end;
var
Power_Key TPower_Key;
implementation
uses MsgBox;
{$R .DFM}
procedure TPower_Key.WMPowerBroadCast(var msg TMessage);
var Form_MsgBox TForm_MsgBox;
begin
SetForeGroundWindow(Application.MainForm.Handle);在应用程序里指定主窗口前置,可获取按键消息,服务没有窗口,所以就屏蔽这行了
Form_MsgBox = TForm_MsgBox.Create(nil);自定义提示窗口
try
Form_MsgBox.suiForm1.Caption = ‘提醒框‘;
Form_MsgBox.HintsLabel.Caption = ‘确认是否关闭系统‘;
if Form_MsgBox.ShowModal mrOK then
msg.Result = BROADCAST_QUERY_DENY;修改消息值,使不关机
finally
FreeAndNil(Form_MsgBox);
end;
end;
procedure ServiceController(CtrlCode DWord); stdcall;
begin
Power_Key.Controller(CtrlCode);
end;
function TPower_Key.GetServiceController TServiceController;
begin
Result = ServiceController;
end;
procedure TPower_Key.ServiceCreate(Sender TObject);
begin
Self.Interactive = true;
end;
end.