想實(shí)現(xiàn)的目的:當(dāng)按下鍵盤上的Power鍵(關(guān)機(jī))時(shí),系統(tǒng)獲得按鍵關(guān)機(jī)消息,并打出一個(gè)提醒框,如果確認(rèn),則關(guān)機(jī),否則不關(guān)機(jī)。
在應(yīng)用程序里已實(shí)現(xiàn),但是做成Service程序,安裝成功且已啟動(dòng)之后,并沒(méi)有實(shí)現(xiàn)我要的效果! 請(qǐng)各位大蝦看一下有什么問(wèn)題?能否指點(diǎn)一二?服務(wù)為什么不響應(yīng)系統(tǒng)消息?
代碼如下:
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);在應(yīng)用程序里指定主窗口前置,可獲取按鍵消息,服務(wù)沒(méi)有窗口,所以就屏蔽這行了
Form_MsgBox = TForm_MsgBox.Create(nil);自定義提示窗口
try
Form_MsgBox.suiForm1.Caption = ‘提醒框‘;
Form_MsgBox.HintsLabel.Caption = ‘確認(rèn)是否關(guān)閉系統(tǒng)‘;
if Form_MsgBox.ShowModal mrOK then
msg.Result = BROADCAST_QUERY_DENY;修改消息值,使不關(guān)機(jī)
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.