钩子问题 ( 积分: 50 )

  • 主题发起人 主题发起人 actionteam
  • 开始时间 开始时间
A

actionteam

Unregistered / Unconfirmed
GUEST, unregistred user!
我想用delphi获得另一个VC程序的EDIT,当按下回车时获得该VC程序的EDIT的内容,这钩子如何写。能给些参考吗?急用。谢谢各位
 
findwindow 找到窗口句柄
SetWindowsHookEx(WH_JOURNALRECORD, HookProc, HInstance, 0);
做一个钩子
在HookProc中判断回车键
然后取出Edit中的数据
 
给个例子好吗?
 
findwindow edit所在窗口的句柄
然后在枚举edit的句柄
有edit的句柄就可以取数据了

SetWindowsHookEx(WH_JOURNALRECORD, HookProc, HInstance, 0);
HookProc 中判断 enter

可以参考下面的单元
unit uScanHook;

interface

uses Windows,Classes ;

type

TScanHook = class(TObject)
private
FHandle: THandle;
hHook: integer;
Findex: integer;
FKey: String;
function KeyHookResult(lP: integer; wP: integer): pchar;
procedure KeyUp(wParam: wParam; lParam: lParam);
public
FMarkLs: TStrings;
function StartHook: boolean;
procedure EndHook;
constructor Create(MainHandle: THandle);
destructor Destroy;
end;

function HookProc(iCode: integer; wParam: wParam; lParam: lParam): LResult; stdcall;

var
ScanHook: TScanHook;

implementation

uses Messages;

function HookProc(iCode: integer; wParam: wParam; lParam: lParam): LResult; stdcall;
begin
if (PEventMsg(lparam)^.message = WM_KEYUP) then
ScanHook.KeyUp(wParam,lParam);
end;

{ TScanHook }

function TScanHook.KeyHookResult(lP, wP: integer): pchar;
begin
result := '[Print Screen]';
{ VK_0 thru VK_9 are the same as ASCII '0' thru '9' ($30 - $39) }
{ VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' ($41 - $5A) }
case lp of
14354: result := '[Alt]'; //不能识别
10688: result := '`';
561: Result := '1';
818: result := '2';
1075: result := '3';
1332: result := '4';
1589: result := '5';
1846: result := '6';
2103: result := '7';
2360: result := '8';
2617: result := '9';
2864: result := '0';
3261: result := '-';
3515: result := '=';
4177: result := 'Q';
4439: result := 'W';
4677: result := 'E';
4946: result := 'R';
5204: result := 'T';
5465: result := 'Y';
5717: result := 'U';
5961: result := 'I';
6223: result := 'O';
6480: result := 'P';
6875: result := '[';
7133: result := ']';
11228: result := '/';
7745: result := 'A';
8019: result := 'S';
8260: result := 'D';
8518: result := 'F';
8775: result := 'G';
9032: result := 'H';
9290: result := 'J';
9547: result := 'K';
9804: result := 'L';
10170: result := ';';
10462: result := '''';
11354: result := 'Z';
11608: result := 'X';
11843: result := 'C';
12118: result := 'V';
12354: result := 'B';
12622: result := 'N';
12877: result := 'M';
13244: result := ',';
13502: result := '.';
13759: result := '/';
13840: result := '[Right-Shift]';
14624: result := '[Space]';
283: result := '[Esc]';
15216: result := '[F1]';
15473: result := '[F2]';
15730: result := '[F3]';
15987: result := '[F4]';
16244: result := '[F5]';
16501: result := '[F6]';
16758: result := '[F7]';
17015: result := '[F8]';
17272: result := '[F9]';
17529: result := '[F10]';
22394: result := '[F11]';
22651: result := '[F12]';
10768: Result := '[Left-Shift]';
14868: result := '[CapsLock]';
3592: result := '[Backspace]';
3849: result := '[Tab]';
7441:
if wp > 30000 then
result := '[Right-Ctrl]'
else
result := '[Left-Ctrl]';
13679: result := '[Num /]';
17808: result := '[NumLock]';
300: result := '[Print Screen]';
18065: result := '[Scroll Lock]';
17683: result := '[Pause]';
21088: result := '[Num0]';
21358: result := '[Num.]';
20321: result := '[Num1]';
20578: result := '[Num2]';
20835: result := '[Num3]';
19300: result := '[Num4]';
19557: result := '[Num5]';
19814: result := '[Num6]';
18279: result := '[Num7]';
18536: result := '[Num8]';
18793: result := '[Num9]';
19468: result := '[*5*]';
14186: result := '[Num *]';
19053: result := '[Num -]';
20075: result := '[Num +]';
21037: result := '[Insert]';
21294: result := '[Delete]';
18212: result := '[Home]';
20259: result := '[End]';
18721: result := '[PageUp]';
20770: result := '[PageDown]';
18470: result := '[UP]';
20520: result := '[DOWN]';
19237: result := '
';
19751: result := '
';
7181: result := '[Enter]';
end;
end;

procedure TScanHook.KeyUp(wParam: wParam; lParam: lParam);
var
KeyChr: string;
begin
KeyChr := Keyhookresult(PEventMsg(lparam)^.paramL,PEventMsg(lparam)^.paramH);
if KeyChr = 'C' then
Findex := 0;
if Findex > -1 then
begin
Inc(Findex);
FKey := FKey + KeyHookResult(PEventMsg(lparam)^.paramL,
PEventMsg(lparam)^.paramH);
end;
if Findex = 7 then
begin
if FMarkLs.IndexOf(FKey) > -1 then
PostMessage(FHandle,WM_USER+1,Integer(FKey),0);
OutPutDebugString(Pchar(FKey+ ' ' + KeyChr));
FKey := '';
Findex := -1;

end;
end;

function TScanHook.StartHook;
begin
hHook := SetWindowsHookEx(WH_JOURNALRECORD, HookProc, HInstance, 0);
result := hHook > 0;
end;

constructor TScanHook.Create(MainHandle: THandle);
begin
FMarkLs := TStringList.Create;
FHandle := MainHandle;
Findex := -1;
end;

destructor TScanHook.Destroy;
begin
FMarkLs.Free;
inherited Destroy;
end;

procedure TScanHook.EndHook;
begin
UnHookWindowsHookEx(hHook);
hHook := 0;
end;


end.​
 
枚举窗口部分的例子
unit uJingMin;

interface

uses Windows,SysUtils,StrUtils,Messages;

var
GJmWorkId,GJmWorkPriId: integer;
OutDutyId: String;
GJMCqHwnd: LongWord;
bChanged: Boolean = True;
bPanel: Boolean;
function EnumChildWindowsProc(WinHwnd: LongWord; lparam: LongWord): boolean; stdcall;
function EnumWindowsProc(WinHwnd: LongWord; Param: LongWord): Boolean; stdcall;
function EnumChildControlProc(WinHwnd: LongWord; lparam: LongWord): boolean; stdcall;
Procedure WorkId(AWorkId: String);
Procedure GetCJmWorkId(iMark: integer);


implementation
var
InterType: Integer;
PreHwnd: LongWord;
PreLeft: LongInt;

Const
CtRect: TRect = (Left:0;Top: 0;Right:45;Bottom: 18);


Procedure GetCJmWorkId(iMark: integer);
var
WndCaption: array[0..254] of char;
begin
InterType := iMark;
PreHwnd := 0;
PreLeft := 0;
if IsWindow(GJMCqHwnd) then
begin
SendMessage(GJMCqHwnd,WM_GETTEXT,254,LongInt(@WndCaption));
WorkId(WndCaption);
end else
EnumWindows(@EnumWindowsProc, 0);
end;

Procedure WorkId(AWorkId: String);
begin
case InterType of
3: GJmWorkId := StrToIntDef(AWorkId,0); //株洲
5: GJmWorkId := StrToIntDef(MidStr(AWorkId,2,5),0); //京铭
end;

if GJmWorkPriId <> GJmWorkId then
begin
GJmWorkPriId := GJmWorkId;
bChanged := True;
end;
end;

{枚举 fTimerAlarm窗口}

function EnumWindowsProc(WinHwnd: LongWord; Param: LongWord): Boolean;
var
WindowClass: String;
WindowText: String;
begin
{--继续遍历--}
Result := True;
{--过滤条件--}
// if IsWindowVisible(WinHwnd) then
Try
SetLength(WindowClass,512);
GetClassName(WinHwnd, PChar(WindowClass), 512);
WindowClass := string( Pchar(WindowClass) );
SetLength(WindowText, GetWindowTextLength(WinHwnd)+2);
Getwindowtext(WinHwnd, PChar(WindowText), GetWindowTextLength(WinHwnd)+2);
WindowText := string( Pchar(WindowText) );

if (Trim(WindowClass) = 'TfTimerAlarm') and
(Trim(WindowText) = 'fTimerAlarm') then
begin
EnumChildWindows(WinHwnd,@EnumChildWindowsProc, 0);
Result := False;
end;

if (Trim(WindowClass) = 'Tfrm_man') and
(Trim(WindowText) = '出勤信息') then
begin
EnumChildWindows(WinHwnd,@EnumChildWindowsProc, 0);
Result := False;
end;

Except
end;
end;

function EnumChildWindowsProc(WinHwnd: LongWord; lparam: LongWord):Boolean;
var
WindowClass: String;
WindowText: String;
ClientRect: TRect;
wInfo: tagwindowinfo;
begin
{--继续遍历--}
Result := True;
{--过滤条件--}
Try
SetLength(WindowClass,512);
GetClassName(WinHwnd, PChar(WindowClass), 512);
WindowClass := string( Pchar(WindowClass) );
SetLength(WindowText, GetWindowTextLength(WinHwnd)+2);
Getwindowtext(WinHwnd, PChar(WindowText), GetWindowTextLength(WinHwnd)+2);
WindowText := string( Pchar(WindowText));
GetClientRect(WinHwnd,ClientRect);
GetWindowInfo(WinHwnd,wInfo);

case InterType of
3: begin
if (Trim(WindowClass) = 'TEdit') and EqualRect(ClientRect,ctRect) then
begin
if (PreHwnd = 0) and (PreLeft = 0) then
begin
PreHwnd := WinHwnd;
PreLeft := wInfo.rcClient.Left;
end else
begin
if wInfo.rcClient.Left < PreLeft then
begin
WinHWnd := PreHwnd;
SetLength(WindowText, GetWindowTextLength(WinHwnd)+2);
Getwindowtext(WinHwnd, PChar(WindowText), GetWindowTextLength(WinHwnd)+2);
WindowText := string( Pchar(WindowText));
end;
WorkId(Trim(WindowText));
GJMCqHwnd := WinHwnd;
Result := False;
end;
end;
end;
5: begin
if (Trim(WindowClass) = 'TPanel') then
begin
if BPanel then
begin
WorkId(Trim(WindowText));
GJMCqHwnd := WinHwnd;
end else EnumChildWindows(WinHwnd,@EnumChildControlProc, 0);
Result := False;
end;
end;
end;

Except
end;
end;

function EnumChildControlProc(WinHwnd: LongWord; lparam: LongWord):Boolean;
var
WindowClass: String;
WndCaption: array[0..254] of char;
begin
{--继续遍历--}
Result := True;
{--过滤条件--}
Try
SetLength(WindowClass,512);
GetClassName(WinHwnd, PChar(WindowClass), 512);
WindowClass := string( Pchar(WindowClass) );
if (Trim(WindowClass) = 'TMemo') then
begin
GJMCqHwnd := WinHwnd;
SendMessage(WinHwnd,WM_GETTEXT,254,LongInt(@WndCaption));
OutputDebugString(PChar('WndCaption'+ WndCaption));
WorkId(WndCaption);
Result := False;
end;
Except
end;
end;

end.
 
后退
顶部