• 使用钩子函数[6] 数据传递


    上一个例子是从 DLL 中接受数据, 那怎么给 DLL 传递数据呢? 还有, 在接受数据时, 让 Timer 一直在那扫描也不是个好办法呀. 本例解决了这些问题(但不知解决了博友 "鹏" 的问题没有?).

    为了方便测试, 提供一个源码下载吧: https://files.cnblogs.com/del/MouseHook_2.rar

    本例效果图(和上一例是一样的):



    DLL 文件:
    library MyHook;
    
    uses
      SysUtils,
      Windows,
      Messages,
      Classes;
    
    {$R *.res}
    
    const WM_MyMessage = WM_USER + 1; {自定义消息}
    
    var
      hook: HHOOK;
      info: string;
      h: HWND; {用作外部窗口的句柄}
    
    {获取外部窗口的句柄}
    function SetHWnd(hwnd: HWND): Boolean; stdcall;
    begin
      h := hwnd;
      Result := True;
    end;
    
    function MouseHook(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
    begin
      case wParam of
        WM_MOUSEMOVE   : info := '鼠标位置';
        WM_LBUTTONDOWN : info := '按下';
        WM_LBUTTONUp   : info := '放开';
      end;
      info := Format('%s: %d,%d', [info, PMouseHookStruct(lParam)^.pt.X, PMouseHookStruct(lParam)^.pt.Y]);
    
      {通过消息把数据传递给指定窗口}
      PostMessage(h, WM_MyMessage, 0, Integer(PChar(info)));
    
      Result := CallNextHookEx(hook, nCode, wParam, lParam);
    end;
    
    function SetHook: Boolean; stdcall;
    const
      WH_MOUSE_LL =14;
    begin
      hook := SetWindowsHookEx(WH_MOUSE_LL, @MouseHook, HInstance, 0);
      Result := hook <> 0;
    end;
    
    function DelHook: Boolean; stdcall;
    begin
      Result := UnhookWindowsHookEx(hook);
    end;
    
    exports SetHook, DelHook, MouseHook, SetHWnd;
    begin
    end.
    
    测试代码:
    unit Unit1;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, ExtCtrls;
    
    const WM_MyMessage = WM_USER + 1;
    
    type
      TForm1 = class(TForm)
        Button1: TButton;
        Button2: TButton;
        procedure FormCreate(Sender: TObject);
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
        procedure FormDestroy(Sender: TObject);
        procedure MyMessage(var msg: TMessage); message WM_MyMessage; {定义一个消息方法接受消息}
      end;
    
      function SetHook: Boolean; stdcall;
      function DelHook: Boolean; stdcall;
      function SetHWnd(hwnd: HWND): Boolean; stdcall;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    function SetHook; external 'MyHook.dll';
    function DelHook; external 'MyHook.dll';
    function SetHWnd; external 'MyHook.dll';
    
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      SetHook;
      SetHWnd(Handle);
    end;
    
    procedure TForm1.Button2Click(Sender: TObject);
    begin
      DelHook;
    end;
    
    procedure TForm1.FormCreate(Sender: TObject);
    begin
      Button1.Caption := '安装钩子';
      Button2.Caption := '载卸钩子';
      FormStyle := fsStayOnTop; {为了测试, 让窗口一直在前面}
    end;
    
    procedure TForm1.FormDestroy(Sender: TObject);
    begin
      DelHook;
    end;
    
    {把接受到的内容显示在窗体}
    procedure TForm1.MyMessage(var msg: TMessage);
    begin
      Text := PChar(msg.LParam);
    end;
    
    end.
    
    测试窗体:
    object Form1: TForm1
      Left = 0
      Top = 0
      Caption = 'Form1'
      ClientHeight = 78
      ClientWidth = 271
      Color = clBtnFace
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -11
      Font.Name = 'Tahoma'
      Font.Style = []
      OldCreateOrder = False
      OnCreate = FormCreate
      OnDestroy = FormDestroy
      PixelsPerInch = 96
      TextHeight = 13
      object Button1: TButton
        Left = 48
        Top = 32
        Width = 75
        Height = 25
        Caption = 'Button1'
        TabOrder = 0
        OnClick = Button1Click
      end
      object Button2: TButton
        Left = 144
        Top = 32
        Width = 75
        Height = 25
        Caption = 'Button2'
        TabOrder = 1
        OnClick = Button2Click
      end
    end
    
  • 相关阅读:
    谷歌Google Chrome浏览器打开新的标签页设置指定固定网址
    Vue子组件和父组件、子组件调用父组件的方法、父组件调用子组件方法、子组件与父组件间的传值
    查询Linux服务器出口IP、curl命令查询Linux公网出口IP、Windows服务器查询出口IP
    mysql查询是对字段进行补0操作,可用于树状结构整体排序
    mysql批量update更新,mybatis中批量更新操作
    CentOS 6.8下网卡配置、桥接模式和NAT连接模式、VMware虚拟机克隆网卡配置
    杂七杂八
    解决SpringMVC拦截器中Request数据只能读取一次的问题
    Redis安装教程及可视化工具RedisDesktopManager下载安装
    JAVA获取客户端请求的当前网络ip地址(附:Nginx反向代理后获取客户端请求的真实IP)
  • 原文地址:https://www.cnblogs.com/del/p/1242391.html
Copyright © 2020-2023  润新知