您现在的位置:首页 >> API >> API >> 内容

Delphi中鼠标键盘钩子函数的应用(2)

时间:2011/9/3 14:53:16 点击:

unit   Unit1;  
            interface  
            uses       Windows,   Messages,   SysUtils,   Classes,   Graphics,   Controls,   Forms,  
                          Dialogs,   StdCtrls;  
            type  
                TForm1   =   class(TForm)  
                Button1:   TButton;  
                Button2:   TButton;  
                Button3:   TButton;  
                Edit1:   TEdit;  
                procedure   FormCreate(Sender:   TObject);  
                procedure   Button1Click(Sender:   TObject);  
                procedure   Button2Click(Sender:   TObject);  
                procedure   Button3Click(Sender:   TObject);  
                private  
                {   private   declarations   }  
                public  
                {   public   declarations   }  
                end;  
            var  
                Form1:   TForm1;  
                EventArr:array[0..1000]of   EVENTMSG;  
                EventLog:Integer;  
                PlayLog:Integer;  
                hHook,hPlay:Integer;  
                recOK:Integer;  
                canPlay:Integer;  
                bDelay:Bool;  
            implementation  
            {$R   *.DFM}  
     
           
            function   PlayProc(iCode:Integer;wParam:wParam;lParam:lParam):LRESULT;stdcall;  
            begin  
                canPlay:=1;  
                Result:=0;  
                if   iCode   <   0   then       //必须将消息传递到钩子链的下一个处理程序  
                    Result   :=   CallNextHookEx(hPlay,iCode,wParam,lParam)  
                else   if   iCode   =   HC_SYSMODALON   then     canPlay:=0  
                else   if   iCode   =   HC_SYSMODALOFF   then     canPlay:=1  
                else   if   ((canPlay   =1   )and(iCode=HC_GETNEXT))   then   begin  
                  if   bDelay   then   begin  
                      bDelay:=False;  
                      Result:=50;//控制回放的速度,数字越大,越慢  
                  end;  
                  pEventMSG(lParam)^:=EventArr[PlayLog];  
                end  
                else   if   ((canPlay   =   1)and(iCode   =   HC_SKIP))then   begin  
                  bDelay   :=   True;  
                  PlayLog:=PlayLog+1;  
                end;  
                if   PlayLog>=EventLog   then   begin  
                      UNHookWindowsHookEx(hPlay);  
                end;  
            end;  
   
   
            function   HookProc(iCode:Integer;wParam:wParam;lParam:lParam):LRESULT;stdcall;  
            begin  
                recOK:=1;     //可以记录  
                Result:=0;    
                if   iCode   <   0   then     //必须将消息传递到钩子链的下一个处理程序  
                  Result   :=   CallNextHookEx(hHook,iCode,wParam,lParam)  
                else   if   iCode   =   HC_SYSMODALON   then     recOK:=0  
                else   if   iCode   =   HC_SYSMODALOFF   then     recOK:=1  
                else   if   ((recOK>0)   and   (iCode   =   HC_ACTION))   then   begin  
                  EventArr[EventLog]:=pEventMSG(lParam)^;  
                  EventLog:=EventLog+1;  
                if   EventLog>=1000   then   begin  
                  UnHookWindowsHookEx(hHook);  
                end;  
                end;  
            end;  
   
           
            procedure   TForm1.FormCreate(Sender:   TObject);  
            begin  
                Button1.Caption:='纪录';  
                Button2.Caption:='停止';  
                Button3.Caption:='回放';  
                Button2.Enabled:=False;  
                Button3.Enabled:=False;  
            end;  
           
   
            procedure   TForm1.Button1Click(Sender:   TObject);  
            begin  
                EventLog:=0;  
                //建立键盘、鼠标操作消息纪录链  
                hHook:=SetwindowsHookEx(WH_JOURNALRECORD,HookProc,HInstance,0);  
                Button2.Enabled:=True;  
                Button1.Enabled:=False;  
            end;  
           
   
            procedure   TForm1.Button2Click(Sender:   TObject);  
            begin  
                UnHookWindowsHookEx(hHook);  
                hHook:=0;  
                Button1.Enabled:=True;  
                Button2.Enabled:=False;  
                Button3.Enabled:=True;  
            end;  
           
   
            procedure   TForm1.Button3Click(Sender:   TObject);  
            begin  
                PlayLog:=0;  
                //建立键盘鼠标操作消息纪录回放链  
                hPlay:=SetwindowsHookEx(WH_JOURNALPLAYBACK,PlayProc,HInstance,0);    
                Button3.Enabled:=False;  
            end;    
            end.    
            运行程序,点击“纪录”按钮开始纪录操作,这时你可以在文本控件中输入一些文字,然后点击“    
停止”按钮停止纪录,再点击“回放”按钮就可以将先前所做的操作回放。在上面的程序中,HookProc   
是纪录操作的消息函数,每当有鼠标、键盘消息发生时,系统都会调用该函数,消息信息就保存在地址     
lParam中,PlayProc是消息回放函数,当系统执行消息回放时调用该函数,程序就将先前纪录的消息值      
返回到lParam指向的区域中,系统就会执行该消息,从而实现了消息回放。                
三、小结  
Hook是应用程序在Microsoft   Windows   消息处理过程中设置的用来监控消息流并且处理系统中尚未   
到达目的窗口的某一类型消息过程的机制。这可以帮助应用程序实现实现某些特殊目的,如控制键盘或   
鼠标的输入消息,监视窗口的打开关闭以及实现记录宏等等,应用灵活,功能强大。如果Hook过程在应   
用程序中实现,若应用程序不是当前窗口时,该Hook就不起作用;如果Hook在DLL中实现,程序在运行中   
动态调用它,它能实时对系统进行监控。 

上一页12下一页

作者:网络 来源:转载
共有评论 0相关评论
发表我的评论
  • 大名:
  • 内容:
本类推荐
  • 没有
本类固顶
  • 没有
  • 盒子文章(www.2ccc.com) © 2024 版权所有 All Rights Reserved.
  • 沪ICP备05001939号