//------------Main--------------- procedure TForm1.Button1Click(Sender: TObject); var g_hQQ,hSend:Hwnd; begin g_hQQ:=0; hSend:=0; SetHook(0); while (g_hQQ=0) or (hSend=0) do begin g_hQQ := FindWindowEx(0, g_hQQ, '#32770', 0); hSend := FindWindowEx(g_hQQ, 0, 'Button', '发送(&S)'); end; if g_hQQ<>0 then SetHook(g_hQQ); end; //--------------Main---------
我做的Dll //---------hook.dll---------- unit hook1;
interface uses Windows,Messages; var g_hKey,g_hProc:HHOOK; g_hRich:HWND;
function Sethook(hQQ:HWND): boolean ;stdcall; function CallWndProc(nCode: Integer;WParam: WPARAM;LParam: LPARAM): LRESULT;stdcall; function KeyboardProc(nCode: Integer;WParam: WPARAM;LParam: LPARAM): LRESULT;stdcall;
implementation
procedure PasteText(hRich:HWND); begin SendMessage(g_hRich, WM_PASTE, 0, 0); end;
function CallWndProc(nCode: Integer;WParam: WPARAM;LParam: LPARAM): LRESULT;stdcall; begin if WParam = WM_COMMAND then begin PasteText(g_hRich); end; Result := CallNextHookEx(g_hProc, nCode, WParam, LParam); end;
function KeyboardProc(nCode: Integer;WParam: WPARAM;LParam: LPARAM): LRESULT;stdcall; begin PasteText(g_hRich); Result := CallNextHookEx(g_hKey, nCode, WParam, LParam); end;
function Sethook(hQQ:HWND): boolean ;stdcall; var bRet:boolean; dwThreadID:dword; begin if hQQ<>0 then begin dwThreadID:=GetWindowThreadProcessId(hQQ,nil); g_hrich:= findwindowex(hQQ,0,'AfxWnd42',nil); g_hrich:= findwindowex(g_hRICH,0,'RICHEDIT',nil); if g_hRich=0 then result:=false; g_hProc := SetWindowsHookEx(WH_CALLWNDPROC, CallWndProc, Hinstance, dwThreadID); g_hKey := SetWindowsHookEx(WH_KEYBOARD, KeyboardProc, Hinstance, dwThreadID); bRet := (g_hProc<>0) and (g_hKey <>0); end else begin bRet := UnhookWindowsHookEx(g_hProc) and UnhookWindowsHookEx(g_hKey); g_hProc := 0; g_hKey := 0; g_hRich := 0;