VB中如何在后台获取鼠标点击屏幕的事件,并记录鼠标点击时的坐标

VB中如何在后台获取鼠标点击屏幕的事件,并记录鼠标点击时的坐标,第1张

新出炉的底层鼠标钩子(Low

Level

Mouse

Hook)

'==================窗体代码=================

Private

Sub

Form_Load()

hHook

=

SetWindowsHookEx(WH_MOUSE_LL,

AddressOf

MouseHookProc,

ApphInstance,

0)

If

hHook

<>

0

Then

MsgBox

"已钩住鼠标事件,请按本窗体右上方的X按钮关闭窗体,不要使用VB调试工具栏的“结束”按钮,以免VB崩溃"

End

If

End

Sub

Private

Sub

Form_Unload(Cancel

As

Integer)

UnhookWindowsHookEx

hHook

End

Sub

'=============模块代码==================

Public

Const

WH_MOUSE

=

7

'本地钩子

Public

Const

WH_MOUSE_LL

=

14

'全局钩子

Public

Declare

Function

SetWindowsHookEx

Lib

"user32"

Alias

"SetWindowsHookExA"

(ByVal

idHook

As

Long,

ByVal

lpfn

As

Long,

ByVal

hmod

As

Long,

ByVal

dwThreadId

As

Long)

As

Long

Public

Declare

Function

CallNextHookEx

Lib

"user32"

(ByVal

hHook

As

Long,

ByVal

nCode

As

Long,

ByVal

wParam

As

Long,

lParam

As

Any)

As

Long

Public

Declare

Function

UnhookWindowsHookEx

Lib

"user32"

(ByVal

hHook

As

Long)

As

Long

Public

Declare

Sub

CopyMemory

Lib

"kernel32"

Alias

"RtlMoveMemory"

(Destination

As

Any,

Source

As

Any,

ByVal

Length

As

Long)

Public

Const

WM_LBUTTONDOWN

=

&H201

'窗口中按下鼠标左键

Public

Const

WM_LBUTTONUP

=

&H202

'窗口中松开鼠标左键

Public

Const

WM_MOUSEMOVE

=

&H200

'窗口中移动鼠标

Public

Const

WM_RBUTTONDOWN

=

&H204

'窗口中按下鼠标右键

Public

Const

WM_RBUTTONUP

=

&H205

'窗口中松开鼠标右键

Public

Const

WM_MOUSEWHEEL

=

&H20A

'鼠标滚轮

Public

Const

WM_NCLBUTTONDOWN

=

&HA1

'窗口标题栏中按下鼠标左键

Public

Const

WM_NCLBUTTONUP

=

&HA2

'窗口标题栏中左开鼠标左键

Public

Const

WM_NCMOUSEMOVE

=

&HA0

'窗口标题栏中移动鼠标

Public

Const

WM_NCRBUTTONDOWN

=

&HA4

'窗口标题栏中按下鼠标右键

Public

Const

WM_NCRBUTTONUP

=

&HA5

'窗口标题栏中松开鼠标右键

Public

hHook

As

Long

Public

Type

POINTAPI

x

As

Long

y

As

Long

End

Type

Type

MSLLHOOKSTRUCT

pt

As

POINTAPI

'相对于屏幕左上角的坐标x,y

mouseData

As

Long

'鼠标数据

flags

As

Long

'标记

time

As

Long

'时间戳

dwExtraInfo

As

Long

'其他信息

End

Type

Type

MOUSEHOOKSTRUCT

pt

As

POINTAPI

'相对于屏幕左上角的坐标x,y

hwnd

As

Long

'鼠标光标下窗口的句柄

wHitTestCode

As

Long

'鼠标光标在窗口中的位置,标题栏、左边框、右边框,下边框。。。

dwExtraInfo

As

Long

'其他信息,通常为0

End

Type

Dim

oMouseHookStruct

As

MSLLHOOKSTRUCT

Public

Function

MouseHookProc(ByVal

idHook

As

Long,

ByVal

wParam

As

Long,

ByVal

lParam

As

Long)

As

Long

CopyMemory

oMouseHookStruct,

ByVal

lParam,

Len(oMouseHookStruct)

DebugPrint

"当前鼠标位置-x:"

&

oMouseHookStructptx

&

";

y:"

&

oMouseHookStructpty

Select

Case

wParam

Case

WM_LBUTTONDOWN,

WM_NCLBUTTONDOWN

DebugPrint

"左键按下"

Case

WM_LBUTTONUP,

WM_NCLBUTTONUP

DebugPrint

"左键d起"

Case

WM_RBUTTONDOWN,

WM_NCRBUTTONDOWN

DebugPrint

"右键按下"

Case

WM_RBUTTONUP,

WM_NCRBUTTONUP

DebugPrint

"右键d起"

Case

WM_MOUSEMOVE,

WM_NCMOUSEMOVE

DebugPrint

"鼠标移动"

Case

WM_MOUSEWHEEL

DebugPrint

"鼠标滚轮"

End

Select

MouseHookProc

=

CallNextHookEx(hHook,

idHook,

wParam,

ByVal

lParam)

End

Function

之前对源码的阅读,总是用时一通乱七八糟的跳转,以学会使用为目的;过了一段时间,就忘记了,因此打算将一些源码的阅读经历记录下来,也通过敲一遍的过程,加深理解。

最开始,用一个比较简单的例子来小试牛刀吧

对于View(Button、TextView等)的点击事件,常用的写法是通过 findViewById 获取View的实例,然后通过 setOnClickListener 设置监听事件,比如我们有如下Button控件。

设置点击事件(假设在Activity中)

但是还有一种写法是在xml布局中通过android:onClick属性直接指定点击执行的函数。

思考

首先我们知道诸如 android:xxx 之类的属性是会在某个attrs文件中定义的,此处的 android:onClick 是View的属性,定义在如下文件中。

在View的构造函数中,会解析出此属性的值。

看这里, 如果变量handlerName不为空,就会为此View设置点击事件了 ,这个handlerName就是onClick属性的值doSubmit,但这个点击事件,并不是我们所熟悉的OnClickListener。

进一步看看这个 DeclaredOnClickListener 类

DeclaredOnClickListener 实现了 OnClickListener ,其中重点是参数 mResolvedMethod 和 mResolvedContext 。

在onClick事件中最终通过反射 mResolvedMethodinvoke(mResolvedContext, v); 执行了doSubmit方法。

doSubmit的访问权限是否可以设置为private呢?

答案:不可以,因为源码中没有调用 mMethodsetAccessible(true); 注入所有修饰符。

其实在onClick属性的注释中就已经说明了。

以上就是关于VB中如何在后台获取鼠标点击屏幕的事件,并记录鼠标点击时的坐标全部的内容,包括:VB中如何在后台获取鼠标点击屏幕的事件,并记录鼠标点击时的坐标、Android源码追踪—android:onClick、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9438915.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-28
下一篇 2023-04-28

发表评论

登录后才能评论

评论列表(0条)

保存