此文將講述如何捕獲自己發送出去的消息:
// hottey 於2004-6-2號
QQ從本機發出消息無非就是兩種方式.(1)按發送按鈕,(2)按Ctrl+Enter組合鍵.當然自定義鍵除外.也不在本文考慮范圍之內:
基於這兩種發送的方式我選用:WH_CALLWNDPROC 和 WH_KEYBOARD兩種鉤子.Sorry,今天心情太爛(學校裡的一些瑣事,郁悶).實在無心繼續.只能貼上源碼了.大家有興趣自己看看...有什麼問題可以和我聯系[email protected]
//監控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;
完整的程序運行以後的圖為: