begin CreateMutex(nil, True,'fi7keQQwbdemo'); //创建互斥量 if (GetlastError() <> ERROR_ALREADY_EXISTS) then //如果创建失败的话,则退出,保持总只有一个实例在运行 begin HookOn; //开钩 while GetMessage(Msg, 0, 0, 0) do ; //建立消息循环,让无窗体程序不至于退出 HookOff; end else exit; end.
DLL文件源代码:
library Help;
uses Windows, Messages;
{变量定义部分} var hmain,hramus, hramus1, hramus2: HWNd; KeyHook: HHook;
{获取QQ信息输入窗体句柄}
function isQQ: thandle; var i: integer; begin for i := 0 to 30 do //强制循环30次找句柄,这个部分写得很烂,可以再优化,不过如今CPU这么强,没大影响了 begin hmain := findwindowex(0, hmain, '#32770', nil); hramus := findwindowEX(hmain, 0, '#32770', nil); hramus1 := findwindowex(hramus, 0, 'AfxWnd42', nil); hramus2 := findwindowex(hramus1, 0, 'RICHEDIT', nil); if hramus2 <> 0 then break; end; Result := hramus2; end;
function isQQbutton: thandle; //取QQ发送按纽的句柄 var qqbutton: HWND; begin qqbutton := findwindowex(hramus, 0, nil, '发送(S)'); Result := qqbutton; end;
{键盘钩子回调函数}
function HookKey(Code: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall; const _KeyPressMask = $80000000; begin if ((lParam and _KeyPressMask) = 0) and (GetKeyState(vk_Control) < 0) and (wParam = 13) and (getfocus = isQQ) then begin //拦截Ctrl+回车,自动追加自己设置的信息 SendMessage(isQQ, EM_ReplaceSel, 0, Integer(pchar(#13 + '欢迎光临我的Blog:http://www.mycode.ful.com'))); end; if ((lParam and _KeyPressMask) = 0) and (GetKeyState(VK_MENU) < 0) and (wParam = 83) and (getfocus = isQQ) then begin //拦截ALT+S,自动追加自己设置的信息 SendMessage(isQQ, EM_ReplaceSel, 0, Integer(pchar(#13 + '欢迎光临我的Blog:http://www.mycode.ful.com'))); end; Result := CallNextHookEx(KeyHook, code, Wparam, lParam); end;
function HookMouse(iCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall; export; var buffer: array[0..255] of char; begin if isQQ <> 0 then //如果找到QQ聊天窗体 begin if wparam = $0201 then //截获鼠标按下消息 begin if pMOUSEHOOKSTRUCT(lparam)^.hwnd = isQQButton then //截获鼠标点击发送按纽消息 begin SendMessage(isQQ, EM_ReplaceSel, 0, Integer(pchar(#13 + '欢迎光临我的Blog:http://www.mycode.ful.com'))); end; end; Result := CallNextHookEx(MouseHook, iCode, wParam, lParam); //传递给下面的消息 end else Result := CallNextHookEx(MouseHook, iCode, wParam, lParam); end;