帮忙看一下哪里不对, 关于消息 (80分)

  • 主题发起人 主题发起人 phenix_sd
  • 开始时间 开始时间
P

phenix_sd

Unregistered / Unconfirmed
GUEST, unregistred user!
请看第五段



我想用WH_GETMESSAGE类型的钩子截获WM_GETTEXT消息,设好钩子后
我向form1发送了一个WM_GETTEXT消息,在edit1里显示出来,在钩子里
截获消息,将消息里的字符串显示在edit2中,但edit2显示不出来.....
unit Unit_Main;

interface

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

type
TForm1 = class(TForm)
Button1: TButton;//设钩子
Button2: TButton;//发消息
Edit1: TEdit;
Button4: TButton;//取消钩子,退出
Edit2: TEdit;
procedure Button1Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
hhk:hHook;

function GetTextHookProc(nCode:Integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;
implementation

{$R *.dfm}
function GetTextHookProc(nCode:Integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;
var //str:array[0..255] of char;
buffer:TMSG;
begin
buffer:=PMSG(lParam)^;
if nCode<0 then result:=CallNextHookEx(hhk,nCode,wParam,lParam)
else if buffer.message=WM_GETTEXT then
begin
form1.Edit2.Text:=PChar(buffer.lParam)^;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
hhk:=SetWindowsHookEx(WH_GETMESSAGE,GetTextHookProc,Hinstance,0);
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
UnHookWindowsHookEx(hhk);
close;
end;


procedure TForm1.Button2Click(Sender: TObject);
var str:array[0..255] of char;
begin
Sendmessage(form1.Handle,WM_GETTEXT,256,integer(@str));
edit1.Text:=str;
end;

end.
 
注意,钩子一般钩到 GetMessage 或者 PeekMessage 得到的消息
就是说,从消息队列里取出的
但是,sendmessage它是不经过消息队列的,直接调用WndProc的,这样的消息不能钩到
(简单的说,PostMessage发出的消息可以钩到(它放在了消息队列),SendMessage发去的消息不能钩到)
 
不错,你说的有道理,我再试试
 
我想用PostMessage发消息到消息队列,再用PeekMesage检索出来
BOOL PeekMessage(

LPMSG lpMsg, // pointer to structure for message
HWND hWnd, // handle to window
UINT wMsgFilterMin, // first message
UINT wMsgFilterMax, // last message
UINT wRemoveMsg // removal flags
);
wMsgFilterMin和UINT wMsgFilterMax该则么用?我的代码如下

procedure TForm1.Button2Click(Sender: TObject);
var str:array[0..255] of char;
begin
str:='lol';
PostMessage(HWND_BROADCAST,WM_SETTEXT,0,integer(@str));
end;
procedure TForm1.Button1Click(Sender: TObject);
var Buffer:MSG;
begin
PeekMessage(Buffer,edit1.Handle,0,WM_SETTEXT,PM_REMOVE);
edit2.Text:=inttostr(buffer.Message)

end;
然后在edit1的消息队列里查WM_SETTEXT消息,wMsgFilterMin和wMsgFilterMax
该则么设?
 
接受答案了.
 
1、你可以使用Application.ProcessMessages() ,它就是peekmessage的
2、wMsgFilterMin和wMsgFilterMax都为0的时候,可以peek到所有类型的消息,
否则,只能peek到 wMsgFilterMin和wMsgFilterMax 之间的消息

是了,不用你peek消息的,你哪来得及peek,人家已经在消息循环里面处理了
你在钩子里面等待就是了
 

Similar threads

S
回复
0
查看
751
SUNSTONE的Delphi笔记
S
I
回复
0
查看
778
import
I
I
回复
0
查看
625
import
I
后退
顶部