Delphi实现全局鼠标钩子
其中涉及到的一些API,网上均能查到详细的解释,这里不再熬述。源码下载
因为是全局钩子,所以要用dll注入。用到的鼠标消息结构如下:
- PMouseHookStruct = ^TMouseHookStruct;
- {$EXTERNALSYM tagMOUSEHOOKSTRUCT}
- tagMOUSEHOOKSTRUCT = packed record
- pt: TPoint;
- hwnd: HWND;
- wHitTestCode: UINT;
- dwExtraInfo: DWORD;
- end;
- TMouseHookStruct = tagMOUSEHOOKSTRUCT;
DLL代码,Mouse_HookDLL
- library Mouse_HookDLL;
- { Important note about DLL memory management: ShareMem must be the
- first unit in your library's USES clause AND your project's (select
- Project-View Source) USES clause if your DLL exports any procedures or
- functions that pass strings as parameters or function results. This
- applies to all strings passed to and from your DLL--even those that
- are nested in records and classes. ShareMem is the interface unit to
- the BORLNDMM.DLL shared memory manager, which must be deployed along
- with your DLL. To avoid using BORLNDMM.DLL, pass string information
- using PChar or ShortString parameters. }
- uses
- SysUtils,
- Windows,
- Messages,
- Classes;
- {$R *.res}
- var
- NextHook : HHook;
- //调用者的Handle,用来给其发消息
- CallHandle : HWND;
- //通知调用者的消息,由调用者传进来
- MessageID : Word;
- //挂钩子函数 ,这里只处理鼠标移动,其他的鼠标动作,道理一样
- function HookProc(code:Integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;
- begin
- Result := 0;
- if code < 0 then
- Result := CallNextHookEx(NextHook,code,wParam,lParam);
- case wParam of
- WM_NCMOUSEMOVE,WM_MOUSEMOVE:
- begin
- //给调用者发消息
- SendMessage(CallHandle,MessageID,wParam,Integer(@pMouseHookStruct(lParam)^));
- end;
- end;
- end;
- //启动钩子
- function StartHook(MsgID:Word):Bool;stdcall;
- begin
- Result := False;
- if NextHook <> 0 then
- Exit;
- MessageID := MsgID;
- //挂钩,SetWindowsHookEx的参数dwThreadId=0,表示挂全局的,不知道为什么,我系统是2003,用WH_MOUSE只能在本进程中实现钩子,WH_MOUSE_LL可以实现全局,在Delphi7中,是没有WH_MOUSE_LL定义的,你可以自己定义,值是14
- NextHook := SetWindowsHookEx(WH_MOUSE_LL,@HookProc,HInstance,0);
- Result := NextHook <> 0;
- end;
- //脱钩
- function StopHook:Bool;stdcall;
- begin
- if NextHook <> 0 then
- begin
- UnHookWindowsHookEx(NextHook);
- NextHook := 0;
- end;
- Result := NextHook = 0;
- end;
- //传递调用者句柄
- procedure SetCallHandle(sender:HWND);stdcall;
- begin
- CallHandle := sender;
- NextHook := 0;
- end;
- exports
- StartHook name 'StartHook',
- StopHook name 'StopHook',
- SetCallHandle name 'SetCallHandle';
- begin
- end.
调用者代码,HookTest
- unit HookTest;
- interface
- uses
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
- Dialogs, StdCtrls;
- type
- TfrmHookTest = class(TForm)
- Label1: TLabel;
- procedure FormCreate(Sender: TObject);
- procedure FormClose(Sender: TObject; var Action: TCloseAction);
- private
- { Private declarations }
- //重载消息处理
- procedure WndProc(var Message: TMessage);override;
- public
- { Public declarations }
- end;
- var
- frmHookTest: TfrmHookTest;
- const
- WM_TestMsg = WM_User + 100;
- implementation
- {$R *.dfm}
- function StartHook(MsgID:Word):Bool;stdcall;external 'Mouse_HookDLL.dll';
- function StopHook:Bool;stdcall;external 'Mouse_HookDLL.dll';
- procedure SetCallHandle(sender:HWND);stdcall;external 'Mouse_HookDLL.dll';
- procedure TfrmHookTest.FormClose(Sender: TObject; var Action: TCloseAction);
- begin
- StopHook;
- end;
- procedure TfrmHookTest.FormCreate(Sender: TObject);
- begin
- SetCallHandle(Self.Handle);
- if not StartHook(WM_TestMsg) then
- begin
- ShowMessage('挂钩失败!');
- end;
- end;
- procedure TfrmHookTest.WndProc(var Message: TMessage);
- var
- x,y:integer;
- begin
- //得到符合条件的钩子
- if Message.Msg = WM_TestMsg then
- begin
- x := pMouseHookStruct(Message.LParam)^.pt.X;
- y := pMouseHookStruct(Message.LParam)^.pt.Y;
- //显示x,y坐标
- Self.Label1.Caption := '鼠标当前位置:x='+IntToStr(x)+' : y='+IntToStr(y);
- end;
- inherited;
- end;
- end.
运行结果
http://blog.csdn.net/bdmh/article/details/5888287
Delphi实现全局鼠标钩子的更多相关文章
- 全局鼠标钩子:WH_MOUSE_LL, 在【 win 10 上网本】上因为太卡,运行中丢失全局鼠标钩子
一台几年前买的上网本,让我安装了一个 win 10,然后用来测试程序的时候, 发现 使用 SetWindowsHookEx(WH_MOUSE_LL, mouseHook, GetModuleHandl ...
- 用Delphi实现Windows的鼠标钩子函数
Delphi是基于PASCAL语言的Windows编程工具,功能十分强大.然而在Delphi的帮助文件中,对Windows API函数的说明沿袭了 VC 的格式,和VC一样,对很多API函数的用法没有 ...
- c#全局鼠标事件以及鼠标事件模拟
最近在编写Max插件时,其主容器FlowLayoutPanel由于隐藏了滚动条,要实现按住鼠标中键上下拖动的功能,因此尝试了全局鼠标事件.以及鼠标勾子,可惜由于Max不争气?都未能实现,于是代码报废, ...
- 在C#中使用全局鼠标、键盘Hook
今天,有个同事问我,怎样在C#中使用全局钩子?以前写的全局钩子都是用unmanaged C或C++写个DLL来实现,可大家都知道,C#是基于.Net Framework的,是managed,怎么实现全 ...
- 如何在C#中使用全局鼠标、键盘Hook
今天,有个同事问我,怎样在C#中使用全局钩子?以前写的全局钩子都是用unmanaged C或C++写个DLL来实现,可大家都知道,C#是基于.Net Framework的,是managed,怎么实现全 ...
- HOOK API (一)——HOOK基础+一个鼠标钩子实例
HOOK API (一)——HOOK基础+一个鼠标钩子实例 0x00 起因 最近在做毕业设计,有一个功能是需要实现对剪切板的监控和进程的防终止保护.原本想从内核层实现,但没有头绪.最后决定从调用层入手 ...
- VC6 鼠标钩子 最简单样例
Windows系统是建立在事件驱动的机制上的,说穿了就是整个系统都是通过消息的传递来实现的.而钩子是Windows系统中非常重要的系统接口,用它能够截获并处理送给其它应用程序的消息,来完毕普通应用程序 ...
- C#全局鼠标键盘Hook
原文出自:http://www.cnblogs.com/iEgrhn/archive/2008/02/17/1071392.html using System; using System.Collec ...
- C#键盘钩子 鼠标钩子
最新对C#模拟键盘按键,鼠标操作产生了兴趣.特从网上收集了一些常用的API用来调用键盘,鼠标操作. class Win32API { #region DLL导入 /// <summary> ...
随机推荐
- C# 当前程序所有线程
using System.Linq; var threads = System.Diagnostics.Process.GetCurrentProcess().Threads;var count = ...
- 初识HTML5
1, 新增canvas标签,允许通过JS在客户端完成2D绘图 2, 新增Video/Audio标签,能取代flash实现媒体播放 3, 新增本地存储功能:localStorage/sessionSto ...
- php 数组 (3) reset() end() count() current() key()
<?php /* count()统计数组中元素的个数 reset() 把数组内部指针移动到数组第一个元素,并返回元素值 end() 把数组内部指针移动到数组最后一个元素,并返回元素值 prev( ...
- [汇编语言]-第九章 在屏幕中间分别显示绿底红色,白底蓝色字符串"welcome to masm!"
;在屏幕中间分别显示绿色,绿底红色,白底蓝色字符串"welcome to masm!" assume cs:codesg,ds:datasg,ss:stacksg datasg s ...
- 喷水装置(一)--nyoj题目6
喷水装置(一) 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述现有一块草坪,长为20米,宽为2米,要在横中心线上放置半径为Ri的喷水装置,每个喷水装置的效果都会让以它为中 ...
- linux shell if语句
#!/bin/bash read -p "please input Y/N" keyWord if [ "$keyWord" == "Y" ...
- fragment低版本
http://bbs.csdn.net/topics/390271980 Fragment框架开发东西确实很方便,但是恼人的是从4.0才开始支持.以前的版本必须用兼容模式开发,本人在网上找了大量资料, ...
- poj1163 dp入门
The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 36811 Accepted: 22048 De ...
- poj 1573 Robot Motion_模拟
又是被自己的方向搞混了 题意:走出去和遇到之前走过的就输出. #include <cstdlib> #include <iostream> #include<cstdio ...
- Caused by: java.lang.ClassNotFoundException: com.mchange.v2.c3p0.ComboPooledDataSource
1.错误描写叙述 usage: java org.apache.catalina.startup.Catalina [ -config {pathname} ] [ -nonaming ] { -he ...