一、把當前進程變為一個系統服務:
目的是在任務列表中把程序隱藏起來。調用API函數RegisterServiceProcess實現。
二、定義全局熱鍵(本例中定義熱鍵Ctrl+Del+R),步驟:
1、定義捕獲Windows消息WM_HOTKEY的鉤子函數,即:procedure WMHotKey(var Msg : TWMHotKey); message WM_HOTKEY;
2、向Windows加入一個全局原子 Myhotkey: GlobalAddAtom(’MyHotkey’), 並保留其句柄。
3、向Windows登記熱鍵:調用API函數RegisterHotKey實現。
三、源程序:
unit Unit1;
interface
uses
Windows, Messages, Forms, Dialogs, Classes, Controls, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{熱鍵標識ID}
id: Integer;
procedure WMHotKey(var Msg : TWMHotKey); message WM_HOTKEY;
{ Privat-Declarations}
public
{ Public-Declarations}
end;
var
Form1 : TForm1;
implementation
const RSP_SIMPLE_SERVICE=1;
function RegisterServiceProcess (dwProcessID, dwType: DWord) : DWord; stdcall; external ’KERNEL32.DLL’;
{$R *.DFM}
{捕獲熱鍵消息}
procedure TForm1.WMHotKey (var Msg : TWMHotKey);
begin
if msg.HotKey = id then
ShowMessage(’Ctrl+Alt+R鍵被按下!’);
form1.Visible :=true;
end;
procedure TForm1.FormCreate(Sender: TObject);
Const
{ALT、CTRL和R鍵的虛擬鍵值}
MOD_ALT = 1;
MOD_CONTROL = 2;
VK_R = 82;
begin
{首先判斷程序是否已經運行}
if GlobalFindAtom(’MyHotkey’) = 0 then
begin
{注冊全局熱鍵Ctrl + Alt + R}
id:=GlobalAddAtom(’MyHotkey’);
RegisterHotKey(handle,id,MOD_CONTROL+MOD_Alt,VK_R);
end
else
halt;
end;
{把當前進程變為一個系統服務,從而在任務列表中把程序隱藏起來}
procedure TForm1.Button1Click(Sender: TObject);
begin
RegisterServiceProcess(GetCurrentProcessID,RSP_SIMPLE_SERVICE);
form1.Hide;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
close;
end;
{退出時釋放全局熱鍵}
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
UnRegisterHotKey(handle,id);
GlobalDeleteAtom(id);
end;
end.
四、說明:
在後台監控軟件中使用以上功能,可真正實現隱蔽運行,熱鍵調出,便於管理員進行管理。程序在Win98,Delphi5.0中運行通過。