ro精灵弓:关于如何截获程序消息的问题

来源:百度文库 编辑:中科新闻网 时间:2024/05/17 05:06:17
想截获某一个程序的菜单上的一个功能,使这个功能失效,该如何编写啊
知道使用钩子函数,但不知如何截获这个菜单消息,如何在操作系统中捕获它,希望高手指点,最好给出源代码
急用
请知道的人帮忙啊

Win98se+delphi7,我自己编写的,不过对qq没有测试过,我自己写了个象qq的界面接收发送消息的窗口,用来测试的,给你吧,写的不好.看看对你的问题是否有提示。
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons;

type
TForm1 = class(TForm)
BitBtn1: TBitBtn;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
procedure FormCreate(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
function GetWndText(hWnd:HWND):string;
procedure SetWndText(hWnd:HWND;szText:string);
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
hMouse:HHook;
hKey:HHook;
const
sendStr='就让我面对冰冷的电脑,想象你的笑容......';
implementation

{$R *.dfm}

function TForm1.GetWndText(hWnd: HWND): string;
var
bufTemp:pchar;
bufSize:longint;
L:longint;
begin
bufSize:=SendMessage(hWnd,WM_GETTEXTLENGTH,0,0)+1;
GetMem(bufTemp,bufSize);
try
L:=longint(bufTemp);
SendMessage(hWnd,WM_GETTEXT,bufSize,L);
result:=StrPas(bufTemp);
finally
FreeMem(bufTemp,bufSize);
end;
end;

procedure TForm1.SetWndText(hWnd: HWND;szText:string);
var
mText:pchar;
begin
GetMem(mText,length(szText));
StrCopy(mText,pchar(szText));
try
SendMessage(hWnd,WM_SETTEXT,0,integer(mText));
finally
FreeMem(mText,length(szText));
end;
end;

function MouseHookProc(iCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;
stdcall;export;
var
P:TPoint;
hWindow,hChat,hButton_1,hButton_2:HWND;
s:string;
begin
result:=0;
if wParam=WM_LBUTTONDOWN then
begin
hWindow:=FindWindow(NIL,'发送消息');
if hWindow<>0 then
begin
GetCursorPos(P);
hButton_1:=WindowFromPoint(P);
hButton_2:=FindWindowEx(hWindow,0,NIL,'送讯息');
if ((hButton_1<>0) and (hButton_2<>0) and
(hButton_1=hButton_2)) then
begin
hChat:=FindWindowEx(hWindow,0,'TRichEdit',NIL);
if hChat<>0 then
begin
s:=Form1.GetWndText(hChat);
s:=s+#13+#10+sendStr;
Form1.SetWndText(hChat,s);
end
else
Exit;
end;
end;
end;
if iCode<0 then
result:=CallNextHookEx(hMouse,iCode,wParam,lParam);
end;

function KeyHookProc(iCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;
stdcall;export;
var
hWindow:HWND;
hChat:HWND;
s:string;
begin
result:=0;
//截获oicq发送消息的快捷键(Ctrl+Enter)
//if ((wParam=VK_RETURN) and (GetKeyState(VK_CONTROL)<>0)) then
if((GetKeyState(VK_CONTROL)<>0) and (wParam=VK_RETURN)) then
begin
hWindow:=FindWindow(NIL,'发送消息');
if hWindow<>0 then
begin
hChat:=FindWindowEx(hWindow,0,'TRichEdit',NIL);
if hChat<>0 then
begin
s:=Form1.GetWndText(hChat);
s:=s+#13+#10+sendStr;
Form1.SetWndText(hChat,s);
end;
end
else
Exit;
end;
if iCode<0 then
result:=CallNextHookEx(hKey,iCode,wParam,lParam);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
hMouse:=SetWindowsHookEx(WH_MOUSE,@MouseHookProc,HInstance,0);
hKey:=SetWindowsHookEx(WH_KEYBOARD,KeyHookProc,HInstance,0);
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
UnHookWindowsHookEx(hMouse);
UnHookWindowsHookEx(hKey);
Application.Terminate;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
UnHookWindowsHookEx(hMouse);
UnHookWindowsHookEx(hKey);
end;

end.