基于上篇《QQ聊天记录器演示程序(一)》,此文将讲述如何捕获自己发送出去的消息: // hottey 于2004-6-2号 QQ从本机发出消息无非就是两种方式.(1)按发送按钮,(2)按Ctrl+Enter组合键.当然自定义键除外.也不在本文考虑范围之内: 基于这两种发送的方式我选用:WH_CALLWNDPROC 和 WH_KEYBOARD两种钩子.Sorry,今天心情太烂(学校里的一些琐事,郁闷).实在无心继续.只能贴上源码了.大家有兴趣自己看看...有什么问题可以和我联系.delphi21@163.com
| //监控Ctrl+Enter组合键 function KeyboardProc(iCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall; begin if (wParam = VK_RETURN) and (GetKeyState(VK_CONTROL) < 0) and (lParam >= 0) then begin SendMessage(Shared^.MainWnd,WM_USERCMD, UC_WINDESTROY, GetForegroundWindow); end; Result := CallNextHookEx(Shared^.KeyHook,iCode,wParam,lParam); end; //监控"发送"按钮 function CallWndProc(iCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall; type Msg = ^CWPSTRUCT; var p : Msg; begin p := Msg(lParam); //只对前台窗口进行处理 if (p^.message = WM_COMMAND) and (LOWORD(p^.wParam) = 1) then begin SendMessage(Shared^.MainWnd,WM_USERCMD, UC_WINDESTROY, GetForegroundWindow); end; Result := CallNextHookEx(Shared^.CallHook,iCode,wParam,lParam); end; | 演示程序相关代码:
| procedure TForm1.WndProc(var Msg: TMessage); begin with Msg do begin if Msg = WM_USERCMD then begin case wParam of UC_WINDESTROY: begin GetText(Findhwd(HWND(lParam))); end; end; end; end; inherited; end; |
| function TForm1.Findhwd(parent: HWND):HWND; var hwd,hBtn,hMemo:HWND; begin hwd:=findwindowex(parent,0,'#32770',nil); Result := 0; if (parent<>0) then begin hBtn := FindwindowEX(hwd,0,nil,'发送(&S)'); if (hBtn<>0) then begin hMemo := GetDlgItem(hwd,$00000000); if (hMemo<>0) then begin result := GetWindow(hMemo,GW_CHILD); end; end; end; end; |
| procedure TForm1.GetText(hwd: HWND); var Ret: LongInt; QQText: PChar; Buf: integer; begin GetMem(QQText,1024); if (hwd<>0) then begin try Ret := SendMessage(hwd, WM_GETTEXTLENGTH, 0, 0) + 1; Buf := LongInt(QQText); SendMessage(hwd, WM_GETTEXT, Min(Ret, 1024), Buf); memo1.Lines.Add(QQText); finally FreeMem(QQText, 1024); end; end; end; | 完整的程序运行以后的图为: |