分享
 
 
 

在vb中实现鼠标手势

王朝vb·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

在vb中实现鼠标手势

1.什么是鼠标手势:

我的理解,按着鼠标某键(一般是右键)移动鼠标,然后放开某键,程序会识别你的移动轨迹,做出相应的响应.

2.实现原理:

首先说明一下,我在网上没有找到相关的文档,我的方法未必与其他人是一致的,实际效果感觉还可以.

鼠标移动的轨迹我们可以将其看成是许多小段直线组成的,然后这些直线的方向就是鼠标在这段轨迹中的方向了.

3.实现代码:

还要说明一下,

a)要捕获鼠标的移动事件,可以使用vb中的mousemove事件,但这个会受到一些限制(例如,在webbrowser控件上就没有这个事件).于是这个例子中,我用win api,在程序中安装个鼠标钩子,这样就能够捕获整个程序的鼠标事件了.

b)这个里只是个能捕获鼠标向上,下,左,右的移动的例子.(呵呵,其实这四方向一般也足够了:))

新建Standrad EXE,添加一个Module

form1的代码如下

Option Explicit

Private Sub Form_Load()

Call InstallMouseHook

End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)

Call UninstallMouseHook

End Sub

Module1的代码如下

Option Explicit

Public Const HTCLIENT As Long = 1

Private hMouseHook As Long

Private Const KF_UP As Long = &H80000000

Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)

Private Type POINTAPI

X As Long

Y As Long

End Type

Public Type MOUSEHOOKSTRUCT

pt As POINTAPI

hwnd As Long

wHitTestCode As Long

dwExtraInfo As Long

End Type

Public Declare Function CallNextHookEx Lib "user32" _

(ByVal hHook As Long, _

ByVal ncode As Long, _

ByVal wParam As Long, _

ByVal lParam As Long) As Long

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 UnhookWindowsHookEx Lib "user32" _

(ByVal hHook As Long) As Long

Public Const WH_KEYBOARD As Long = 2

Public Const WH_MOUSE As Long = 7

Public Const HC_SYSMODALOFF = 5

Public Const HC_SYSMODALON = 4

Public Const HC_SKIP = 2

Public Const HC_GETNEXT = 1

Public Const HC_ACTION = 0

Public Const HC_NOREMOVE As Long = 3

Public Const WM_LBUTTONDBLCLK As Long = &H203

Public Const WM_LBUTTONDOWN As Long = &H201

Public Const WM_LBUTTONUP As Long = &H202

Public Const WM_MBUTTONDBLCLK As Long = &H209

Public Const WM_MBUTTONDOWN As Long = &H207

Public Const WM_MBUTTONUP As Long = &H208

Public Const WM_RBUTTONDBLCLK As Long = &H206

Public Const WM_RBUTTONDOWN As Long = &H204

Public Const WM_RBUTTONUP As Long = &H205

Public Const WM_MOUSEMOVE As Long = &H200

Public Const WM_MOUSEWHEEL As Long = &H20A

Public Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

Public Const MK_RBUTTON As Long = &H2

Public Declare Function ScreenToClient Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long

Public Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer

Public Const VK_LBUTTON As Long = &H1

Public Const VK_RBUTTON As Long = &H2

Public Const VK_MBUTTON As Long = &H4

Dim mPt As POINTAPI

Const ptGap As Single = 5 * 5

Dim preDir As Long

Dim mouseEventDsp As String

Dim eventLength As Long

'######### mouse hook #############

Public Sub InstallMouseHook()

hMouseHook = SetWindowsHookEx(WH_MOUSE, AddressOf MouseHookProc, _

App.hInstance, App.ThreadID)

End Sub

Public Function MouseHookProc(ByVal iCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

Dim Cancel As Boolean

Cancel = False

On Error GoTo due

Dim i&

Dim nMouseInfo As MOUSEHOOKSTRUCT

Dim tHWindowFromPoint As Long

Dim tpt As POINTAPI

If iCode = HC_ACTION Then

CopyMemory nMouseInfo, ByVal lParam, Len(nMouseInfo)

tpt = nMouseInfo.pt

ScreenToClient nMouseInfo.hwnd, tpt

'Debug.Print tpt.X, tpt.Y

If nMouseInfo.wHitTestCode = 1 Then

Select Case wParam

Case WM_RBUTTONDOWN

mPt = nMouseInfo.pt

preDir = -1

mouseEventDsp = ""

Cancel = True

Case WM_RBUTTONUP

Debug.Print mouseEventDsp

Cancel = True

Case WM_MOUSEMOVE

If vkPress(VK_RBUTTON) Then

Call GetMouseEvent(nMouseInfo.pt)

End If

End Select

End If

End If

If Cancel Then

MouseHookProc = 1

Else

MouseHookProc = CallNextHookEx(hMouseHook, iCode, wParam, lParam)

End If

Exit Function

due:

End Function

Public Sub UninstallMouseHook()

If hMouseHook <> 0 Then

Call UnhookWindowsHookEx(hMouseHook)

End If

hMouseHook = 0

End Sub

Public Function vkPress(vkcode As Long) As Boolean

If (GetAsyncKeyState(vkcode) And &H8000) <> 0 Then

vkPress = True

Else

vkPress = False

End If

End Function

Public Function GetMouseEvent(nPt As POINTAPI) As Long

Dim cx&, cy&

Dim rtn&

rtn = -1

cx = nPt.X - mPt.X: cy = -(nPt.Y - mPt.Y)

If cx * cx + cy * cy > ptGap Then

If cx > 0 And Abs(cy) <= cx Then

rtn = 0

ElseIf cy > 0 And Abs(cx) <= cy Then

rtn = 1

ElseIf cx < 0 And Abs(cy) <= Abs(cx) Then

rtn = 2

ElseIf cy < 0 And Abs(cx) <= Abs(cy) Then

rtn = 3

End If

mPt = nPt

If preDir <> rtn Then

mouseEventDsp = mouseEventDsp & DebugDir(rtn)

preDir = rtn

End If

End If

GetMouseEvent = rtn

End Function

Public Function DebugDir(nDir&) As String

Dim tStr$

Select Case nDir

Case 0

tStr = "右"

Case 1

tStr = "上"

Case 2

tStr = "左"

Case 3

tStr = "下"

Case Else

tStr = "无"

End Select

Debug.Print Timer, tStr

DebugDir = tStr

End Function

运行程序后,在程序窗口上,按着右键移动鼠标,Immediate Window就会显示出鼠标移动的轨迹了.

这里面的常数 ptGap 就是"鼠标移动的轨迹我们可以将其看成是许多小段直线组成的"中的小段的长度的平方.里面用到的api函数的用法,可以参考msdn.这里我就懒说了.

lingll (lingll2001@21cn.com)

2004-7-23

没有注释?懒啊,各位就将就着看吧:)

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有