VB中控制鼠标的移动
各位大虾好,小弟初学VB,现在想用键盘控制一下鼠标的移动,通过找资料,我知道是要通过SetCursorpos函数来实现了,但是就是不知道怎么用这个函数,希望大家能指点一下...
各位大虾好,小弟初学VB,现在想用键盘控制一下鼠标的移动,通过找资料,我知道是要通过SetCursorpos函数来实现了,但是就是不知道怎么用这个函数,希望大家能指点一下小弟,感激不尽!!!(我下面是想通过键盘的方向键来控制鼠标的移动,但是系统报错说找不到DLL入口点SetCursorpos in user32),请大家帮我看看,谢谢啦
代码:Option Explicit
Private Type pointapi
x As Long
y As Long
End Type
Dim d As pointapi
Private Declare Function SetCursorpos Lib "user32 " (ByVal x As Long, ByVal y As Long) As Long
Private Declare Function GetCursorpos Lib "user32 " (lppoint As pointapi) As Long
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 37 Then d.x = d.x - 5
If KeyCode = 38 Then d.y = d.y - 5
If KeyCode = 39 Then d.x = d.x + 5
If KeyCode = 40 Then d.y = d.y + 5
SetCursorpos d.x, d.y
End Sub 展开
代码:Option Explicit
Private Type pointapi
x As Long
y As Long
End Type
Dim d As pointapi
Private Declare Function SetCursorpos Lib "user32 " (ByVal x As Long, ByVal y As Long) As Long
Private Declare Function GetCursorpos Lib "user32 " (lppoint As pointapi) As Long
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 37 Then d.x = d.x - 5
If KeyCode = 38 Then d.y = d.y - 5
If KeyCode = 39 Then d.x = d.x + 5
If KeyCode = 40 Then d.y = d.y + 5
SetCursorpos d.x, d.y
End Sub 展开
若以下回答无法解决问题,邀请你更新回答
2个回答
展开全部
Option Explicit
'注意,声明时API函数名的大小写不能出任何差错。
Private Declare Function GetCursorPos Lib "user32" ( _
ByVal lpPoint As Long _
) As Long
Private Declare Function SetCursorPos Lib "user32" ( _
ByVal x As Long, _
ByVal y As Long _
) As Long
Private Type POINTAPI
x As Long
y As Long
End Type
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Dim P As POINTAPI
'一次移动量。
Const MovVal As Integer = 2
'获取鼠标指针位置。
'用不用VarPtr取决于申明时有没有ByVal
GetCursorPos VarPtr(P)
'移位。
If KeyCode = vbKeyUp Then P.y = P.y - MovVal
If KeyCode = vbKeyDown Then P.y = P.y + MovVal
If KeyCode = vbKeyLeft Then P.x = P.x - MovVal
If KeyCode = vbKeyRight Then P.x = P.x + MovVal
'设置鼠标位置。
SetCursorPos P.x, P.y
End Sub
'注意,声明时API函数名的大小写不能出任何差错。
Private Declare Function GetCursorPos Lib "user32" ( _
ByVal lpPoint As Long _
) As Long
Private Declare Function SetCursorPos Lib "user32" ( _
ByVal x As Long, _
ByVal y As Long _
) As Long
Private Type POINTAPI
x As Long
y As Long
End Type
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Dim P As POINTAPI
'一次移动量。
Const MovVal As Integer = 2
'获取鼠标指针位置。
'用不用VarPtr取决于申明时有没有ByVal
GetCursorPos VarPtr(P)
'移位。
If KeyCode = vbKeyUp Then P.y = P.y - MovVal
If KeyCode = vbKeyDown Then P.y = P.y + MovVal
If KeyCode = vbKeyLeft Then P.x = P.x - MovVal
If KeyCode = vbKeyRight Then P.x = P.x + MovVal
'设置鼠标位置。
SetCursorPos P.x, P.y
End Sub
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这不是一个函数的使用问题
这个问题是:在VB中调用系统函数
去搜索这个问题吧,我也不会
这个问题是:在VB中调用系统函数
去搜索这个问题吧,我也不会
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询