一是依靠时钟不停的读鼠标的消息,还有就是使用Hook
下面的例程可以完全满足你的要求,程序执行后,双击得到输入框中的内容,可以得到密码框中的***呦
Hook代码:
library GetCharDLL;
uses
SysUtils,
Classes,
Windows,
MainDLL in 'MainDLL.pas';
{$R *.RES}
exports
InstallMouseHook,
UninstallMouseHook;
begin
DLLProc := @MouseHookDLLProc;
MouseHookDLLProc(DLL_Process_Attach);
end.
//------------------------------------------------------------------
unit MainDLL;
interface
uses
SysUtils,
Classes,
Windows,
Messages,
Dialogs;
var
hMouseHook : HHook;
function MyMouseProc(nCode : integer; wParam : WPARAM; lParam : LPARAM) : LRESULT; stdcall;
function InstallMouseHook : boolean;
function UninstallMouseHook : boolean;
procedure MouseHookDLLProc(Reason : integer);
implementation
function MyMouseProc(nCode : integer; wParam : WPARAM; lParam : LPARAM) : LRESULT; stdcall;
var
MyMouseHookStruct : ^TMouseHookStruct;
x, y : integer;
p : PChar;
begin
if (nCode >= 0) then
begin
if (wParam = WM_LBUTTONDBLCLK) then
begin
MyMouseHookStruct := Pointer(lParam);
x := MyMouseHookStruct^.pt.x;
y := MyMouseHookStruct^.pt.y;
p := StrAlloc(50);
try
if (GetWindowText(MyMouseHookStruct^.hwnd, p, 40) <> 0) then
begin
ShowMessage(IntToStr(x) + ', ' + IntToStr
+ #13#10 + p);
end;
finally
StrDispose(p);
end;
end;
end;
Result := CallNextHookEx(hMouseHook, nCode, wParam, lParam);
end;
function InstallMouseHook : boolean;
begin
Result := False;
if (hMouseHook <> 0) then Exit;
hMouseHook := SetWindowsHookEx(WH_MOUSE, MyMouseProc, HInstance, 0);
Result := (hMouseHook <> 0);
end;
function UninstallMouseHook : boolean;
begin
if (hMouseHook <> 0) then
begin
UnhookWindowsHookEx(hMouseHook);
hMouseHook := 0;
MessageBeep(0); MessageBeep(0); MessageBeep(0);
end;
Result := (hMouseHook = 0);
end;
procedure MouseHookDLLProc(Reason : integer);
begin
case Reason of
DLL_Process_Attach: //整个DLL的初始化
begin
hMouseHook := 0;
end;
DLL_Process_Detach: //整个DLL的善後
begin
UninstallMouseHook;
end;
DLL_Thread_Attach: //开始一个Thread
begin
//
end;
DLL_Thread_Detach: //终止一个Thread
begin
//
end;
end;
end;
end.
程序代码:
unit Main;
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;
function InstallMouseHook : boolean; external 'GetCharDLL.dll' name 'InstallMouseHook';
function UninstallMouseHook : boolean; external 'GetCharDLL.dll' name 'UninstallMouseHook';
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
InstallMouseHook;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
UninstallMouseHook;
end;
end.