仅挂钩 WH_GETMESSAGE 和过滤器 WM_SETTEXT

Hook WH_GETMESSAGE and filter WM_SETTEXT only

使用 Microsoft Spy++ 我看到当您 open/create 一个新文档时,Notepad++ 会收到一条 WM_SETTEXT 消息。我需要挂钩 Windows 上的标题更改,所以我正在尝试进行 WH_GETMESSAGE 挂钩,并且仅过滤 WM_SETTEXT。但到目前为止我没有成功。这是我的 DLL:

uses
  System.SysUtils,
  Windows,
  Messages,
  System.Classes;

var
  CurrentHook: HHOOK;

{$R *.res}

function GetMessageHookProc(Code: Integer; iWParam: WPARAM; iLParam: LPARAM): LRESULT; stdcall;
begin
  Result:= CallNextHookEx(CurrentHook, Code, iWParam, iLParam);
  if (Code = HC_ACTION) and (PMSG(iLParam).message = wm_settext) then
  begin
    MessageBox(0, 'WM_SETTEXT', 'WM_SETTEXT', MB_OK);

    //this code below is just a prototype to what I will try when this works:
    if IntToStr(PMSG(iLParam).lParam) = 'new - Notepad++' then
      MessageBox(0, 'Notepad++', 'Notepad++', MB_OK);

  end;
end;

procedure SetHook; stdcall;
begin
  CurrentHook:= SetWindowsHookEx(WH_GETMESSAGE, @GetMessageHookProc, HInstance, 0);
  if CurrentHook <> 0 then
    MessageBox(0, 'HOOKED', 'HOOKED', MB_OK);
end;

procedure UnsetHook; stdcall;
begin
  UnhookWindowsHookEx(CurrentHook);
end;

exports
  SetHook,
  UnsetHook;

begin

end.

我得到了'HOOKED'消息框,表明钩子已经设置好了,但是我从来没有在回调过程的if里面得到'WM_SETTEXT'消息框。如何只过滤这种消息,并检查消息的字符串?

谢谢!

WM_SETTEXTsent 消息,而不是 posted 消息。 WH_GETMESSAGE 挂钩只会看到 发布 到目标线程的消息队列的消息,因此它永远不会看到 WM_SETTEXT 消息。要将 发送 的消息直接挂钩到 window 而无需通过消息队列,您需要使用 WH_CALLWNDPROCWH_CALLWNDPROCRET 挂钩。