200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > delphi 注册热键 隐藏窗口

delphi 注册热键 隐藏窗口

时间:2024-04-11 00:43:46

相关推荐

delphi 注册热键 隐藏窗口

时间:-5-26来源:yang 作者: peng点击: 45次

主要需要解决两个问题,即隐藏窗口和设定热键。

一. 隐藏窗口

通过API函数 GETACTIVEWINDOW获取当前窗口;函数ShowWindow(HWND,nCmdShow)的参数nCmdShow取SW_HIDE时将之隐 藏,取SW_SHOW时将之显示。例如:showwindow(getactivewindow,sw_hide)。隐藏好窗体后,须记住窗体句柄以便恢 复。

二. 键盘监控

为了实现键盘监控须用到钩子。

以下是程序的源文件:

---HKHide.pas---

unit HKHide;

interface

uses

Windows, Messages, sysutils;

var

hNextHookHide: HHook;

HideSaveExit: Pointer;

hbefore:longint;

function KeyboardHookHandler(iCode: Integer;wParam: WPARAM;

lParam: LPARAM): LRESULT; stdcall; export;

function EnableHideHook: BOOL; export;

function DisableHideHook: BOOL; export;

procedure HideHookExit; far;

implementation

function KeyboardHookHandler(iCode: Integer;wParam: WPARAM;

lParam: LPARAM): LRESULT; stdcall; export;

const _KeyPressMask = $80000000;

var

f:textfile;

temp:string;

begin

Result := 0;

If iCode < 0 Then

begin

Result := CallNextHookEx(hNextHookHide, iCode, wParam, lParam);

Exit;

end;

// 侦测 Ctrl + Alt + F12 组合键

if ((lParam and _KeyPressMask) = 0) //按下时生效

and (GetKeyState(vk_Control) < 0)

and (getkeystate(vk_menu)<0) and (wParam = vk_F12) then

begin

Result := 1;

//文件不存在则创建

if not fileexists(c:\test.txt) then

begin

assignfile(f,c:\test.txt);

rewrite(f);

writeln(f,0);

closefile(f);

end

else begin

assignfile(f,c:\test.txt);

reset(f);

readln(f,temp);

hbefore:=strtoint(temp);

begin

hbefore:=getactivewindow;

temp:=inttostr(hbefore);

rewrite(f);

writeln(f,temp);

closefile(f);

ShowWindow(hbefore, SW_HIDE);

end

else begin

showwindow(hbefore,sw_show);

rewrite(f);

writeln(f,0);

closefile(f);

end;

end;

end;

end;

function EnableHideHook: BOOL; export;

begin

Result := False;

if hNextHookHide <> 0 then Exit;

// 挂上 WH_KEYBOARD 这型的 HOOK, 同时, 传回值必须保留下

// 来, 免得 HOOK 呼叫链结断掉

hNextHookHide := SetWindowsHookEx(WH_KEYBOARD,

KeyboardHookHandler,HInstance,0);

Result := hNextHookHide <> 0;

end;

function DisableHideHook: BOOL; export;

begin

if hNextHookHide <> 0 then

begin

Result:=True;

UnhookWindowshookEx(hNextHookHide); // 解除 Keyboard Hook

hNextHookHide:=0;

end

else

Result:=False;

end;

procedure HideHookExit;

begin

// 如果忘了解除 HOOK, 自动代理解除的动作

if hNextHookHide <> 0 then DisableHideHook;

ExitProc := HideSaveExit;

end;

end.

---HKPHide.dpr---

library HKPHide;

uses

HKHide in HKHide.pas;

exports

EnableHideHook,

DisableHideHook;

begin

hNextHookHide := 0;

hbefore:=0;

HideSaveExit := ExitProc;

ExitProc := @HideHookExit;

end.

文件制作好后选Build All编译成HKPHide.dll。

新建一个工程Project1

---Unit1.pas---

unit Unit1;

interface

uses

Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;

type

TForm1 = class(TForm)

Button1: TButton;

Button2: TButton;

procedure Button1Click(Sender: TObject);

procedure Button2Click(Sender: TObject);

private

{ Private declarations }

public

{ Public declarations }

end;

var

Form1: TForm1;

implementation

{$R *.DFM}

function EnableHideHook: BOOL; external HKPHide.DLL;

function DisableHideHook: BOOL; external HKPHide.DLL;

procedure TForm1.Button1Click(Sender: TObject);

begin

if EnableHideHook then

ShowMessage(HotKey Testing...);

end;

procedure TForm1.Button2Click(Sender: TObject);

begin

if DisableHideHook then

ShowMessage(HotKey Testing..., DONE!!);

end;

end.

运行程序按Button1后启动钩子,这时运行其他程序,按Ctrl+Alt+F12可将之隐藏,再按一下则恢复。以下程序在Delphi 4下通过。

摘自/jibing/lunwen/process/02/lunwen_50788.shtml

后台监控软件,为了达到隐蔽监控的目的,应该满足正常运行时,不显示在任务栏上,在按Ctrl+Alt+Del出现的任务列表中也不显示,管理员可以通过热键调出隐藏的运行界面。要作到这些,必须把当前进程变为一个系统服务,并且定义全局热键。

一、把当前进程变为一个系统服务:

目的是在任务列表中把程序隐藏起来。调用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.

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。