如何用DELPHI制作1个钩子钩住某个程序,在这个程序中我可以使用快捷键呼出我原来用DEIPHI制作的FORM,因为小弟学习不久,还请各位大大帮忙,最好有事例和部分说明,小弟在次先谢谢拉!

解决方案 »

  1.   

    做个键盘钩子,截取键盘消息,响应快捷键调用你的程序;具体查询hook的相关资料吧,我也没用过
      

  2.   

    我之前在CSDN发过一个键盘钩子的示例,你所需要做的是扩展一下,对键盘特定按键进行响应
      

  3.   

    我有类似的代码可供你参考,键盘和鼠标的HOOK
    主程序:
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, ExtCtrls;const WM_MyMessage = WM_USER + 1;type
      TForm1 = class(TForm)
        procedure FormCreate(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.FormCreate(Sender: TObject);
    begin
      FormStyle := fsStayOnTop; {为了测试, 让窗口一直在前面}
      SetHook;
      SetHWnd(Handle);
    end;procedure TForm1.FormDestroy(Sender: TObject);
    begin
      DelHook;
    end;{把接受到的内容显示在窗体}
    procedure TForm1.MyMessage(var msg: TMessage);
    begin
      Text := PChar(msg.LParam);
    end;end.
    DLL如下:library MyHook;uses
      SysUtils,
      Windows,
      Messages,
      Classes,
      dialogs;{$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 KeyboardHookHandler(iCode: Integer;wParam: WPARAM;lParam: LPARAM): LRESULT; stdcall; export;
    const
    _KeyPressMask = $80000000;
    begin  Result := 0;     //注释后就可以屏蔽键盘
      If iCode < 0 Then
      begin
        Result := CallNextHookEx(hook,iCode,wParam,lParam);
        Exit;
      end;
      if  ((lParam and _KeyPressMask) = 0)  then
      begin
        
        info:=chr(wparam);
        PostMessage(h, WM_MyMessage, 0, Integer(PChar(info)));
      end;
    end;function SetHook: Boolean; stdcall;
    const
      WH_MOUSE_LL =14;
    begin
      //hook := SetWindowsHookEx(WH_MOUSE_LL, @MouseHook, HInstance, 0);
      Hook :=SetWindowsHookEx(WH_KEYBOARD,@KeyboardHookHandler,HInstance,0);
      Result := hook <> 0;
    end;function DelHook: Boolean; stdcall;
    begin
      Result := UnhookWindowsHookEx(hook);
    end;exports SetHook, DelHook, MouseHook, SetHWnd;
    begin
    end.
      

  4.   

    使用 钩子对 那个引用程序下一个热键,之后就能处理了。
    如何使用钩子,可以参考:
    HookInjEx
    在网上有很多介绍,不过是C++版本的。