Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1257772
  • 博文数量: 788
  • 博客积分: 4000
  • 博客等级: 上校
  • 技术积分: 7005
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-19 15:52
文章存档

2017年(81)

2011年(1)

2009年(369)

2008年(337)

分类:

2009-02-03 09:56:32

消息Hook hook Focus 事件时遇到的问题 Delphi / Windows SDK/API

我想实现一个功能:  
  安装一个全局消息HOOK,捕获桌面上任意输入框的获得焦点和失去焦点的事件,为了便于测试我先写了一个线程hook做测试,hook自己窗体上的输入框的获得焦点和失去焦点的事件,但是不奏效,请大家帮忙看看哪里有问题  
   
  unit   Unit1;  
   
  interface  
   
  uses  
      Windows,   Messages,   SysUtils,   Classes,   Graphics,   Controls,   Forms,   Dialogs,  
      StdCtrls;  
   
  type  
      TForm1   =   class(TForm)  
        Edit1:   TEdit;  
        Edit2:   TEdit;  
          procedure   FormCreate(Sender:   TObject);  
      private  
          {   Private   declarations   }  
      public  
          {   Public   declarations   }  
      end;  
   
  var  
      Form1:   TForm1;  
   
  implementation  
   
  {$R   *.DFM}  
  var  
      HookHandle:   HHOOK;  
   
  function   TestHookProc(code:   Integer;   wparam:   WPARAM;   lparam:   LPARAM):   LRESULT;stdcall;  
  begin  
          if   code   =   HC_ACTION   then  
          if   PMsg(lparam)^.Message   =   WM_SETFOCUS   then  
          begin  
              ShowMessage('we   get   it');  
          end;  
          Result   :=   CallNextHookEx(HookHandle,   Code,   WParam,   longint(@lparam));  
  end;  
   
  procedure   TForm1.FormCreate(Sender:   TObject);  
  begin  
      HookHandle:=SetWindowsHookEx(Windows.WH_CALLWNDPROC,TestHookProc,0,GetCurrentThreadID);  
  end;  
   
  end.

用WH_SHELL类型的钩子,具体看一下帮助

不对,是WH_CBT

回调函数是这样的  
   
  function   CBTProc(nCode:integer;wParam:wParam;lParam:lParam):LRESULT;stdcall;  
   
  对于nCode的说明:  
  nCode  
   
  Specifies   a   code   that   the   hook   procedure   uses   to   determine   how   to   process   the   message.   This   parameter   can   be   one   of   the   following   values:    
   
  Value Meaning  
  HCBT_ACTIVATE The   system   is   about   to   activate   a   window.  
  HCBT_CLICKSKIPPED The   system   has   removed   a   mouse   message   from   the   system   message   queue.   Upon   receiving   this   hook   code,   a   CBT   application   must   install   a   WH_JOURNALPLAYBACK   hook   procedure   in   response   to   the   mouse   message.  
  HCBT_CREATEWND A   window   is   about   to   be   created.   The   system   calls   the   hook   procedure   before   sending   the   WM_CREATE   or   WM_NCCREATE   message   to   the   window.   If   the   hook   procedure   returns   a   nonzero   value,   the   system   destroys   the   window;   the   CreateWindow   function   returns   NULL,   but   the   WM_DESTROY   message   is   not   sent   to   the   window.   If   the   hook   procedure   returns   zero,   the   window   is   created   normally.  
  At   the   time   of   the   HCBT_CREATEWND   notification,   the   window   has   been   created,   but   its   final   size   and   position   may   not   have   been   determined   and   its   parent   window   may   not   have   been   established.   It   is   possible   to   send   messages   to   the   newly   created   window,   although   it   has   not   yet   received   WM_NCCREATE   or   WM_CREATE   messages.   It   is   also   possible   to   change   the   position   in   the   Z   order   of   the   newly   created   window   by   modifying   the   hwndInsertAfter   member   of   the   CBT_CREATEWND   structure.  
  HCBT_DESTROYWND A   window   is   about   to   be   destroyed.  
  HCBT_KEYSKIPPED The   system   has   removed   a   keyboard   message   from   the   system   message   queue.   Upon   receiving   this   hook   code,   a   CBT   application   must   install   a   WH_JOURNALPLAYBACK_hook   hook   procedure   in   response   to   the   keyboard   message.  
  HCBT_MINMAX A   window   is   about   to   be   minimized   or   maximized.  
  HCBT_MOVESIZE A   window   is   about   to   be   moved   or   sized.  
  HCBT_QS The   system   has   retrieved   a   WM_QUEUESYNC   message   from   the   system   message   queue.  
  HCBT_SETFOCUS A   window   is   about   to   receive   the   keyboard   focus.  
  HCBT_SYSCOMMAND A   system   command   is   about   to   be   carried   out.   This   allows   a   CBT   application   to   prevent   task   switching   by   means   of   hot   keys.  
   
  If   nCode   is   less   than   zero,   the   hook   procedure   must   pass   the   message   to   the   CallNextHookEx   function   without   further   processing   and   should   return   the   value   returned   by   CallNextHookEx.    
   
  如果nCode的值为HCBT_SETFOCUS,则表示检测到了焦点消息。这个时候wParam包含了获得焦点的控件的句柄等信息,lParam包含了失去焦点的控件的句柄等信息。  
   
  以上信息在DELPHI帮助里面都有,请看帮助



--------------------------
新闻:阿里巴巴宣布今年将新招聘5000人
导航:博客园首页  知识库  新闻  招聘  社区  小组  博问  网摘  找找看
阅读(458) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~