鼠标在控件上右键,鼠标光标移动到该控件右下角,但Label1控件时 Call GetWindowRect(objControl.hwnd, rect5)这句错误。如何解决?代码如下,贴上即可运行:
Form文件:Private Sub Check1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 2 Then moveMouseOn_Control Check1
End SubPrivate Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 2 Then moveMouseOn_Control Command1
End SubPrivate Sub Label1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 2 Then moveMouseOn_Control Label1
End SubPrivate Sub Option1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 2 Then moveMouseOn_Control Option1
End SubPrivate Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 2 Then moveMouseOn_Control Picture1
End SubPrivate Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 2 Then moveMouseOn_Control Text1
End Sub
Module1文件:Type RECT
Left As Long
ToP As Long
Right As Long
Bottom As Long
End Type
Type POINTAPI
X As Long
Y As Long
End TypeDeclare Function SetCursorPos Lib "user32" (ByVal X As Long, ByVal Y As Long) As Long
Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As LongPublic Sub MoveCursor(FromP As POINTAPI, ToP As POINTAPI) '移动Mouse
Dim stepx As Long, stepy As Long, k As Long
Dim i As Long, j As Long, sDelay As Long
stepx = 1
stepy = 1
i = (ToP.X - FromP.X)
If i < 0 Then stepx = -1
i = (ToP.Y - FromP.Y)
If i < 0 Then stepy = -1
For i = FromP.X To ToP.X Step stepx
Call SetCursorPos(i, FromP.Y)
Next i
For i = FromP.Y To ToP.Y Step stepy
Call SetCursorPos(ToP.X, i)
Next i
End Sub
Public Sub moveMouseOn_Control(ByVal objControl As VB.Control)
If objControl Is Nothing Then Exit Sub
Dim rect5 As RECT
Dim p1 As POINTAPI, p2 As POINTAPI
Call GetWindowRect(objControl.hwnd, rect5) '注释:取得objControl相对Screen的座标
p1.X = (rect5.Left + rect5.Right) \ 2
p1.Y = (rect5.ToP + rect5.Bottom) \ 2
Call GetWindowRect(objControl.hwnd, rect5)
p2.X = rect5.Right
p2.Y = rect5.Bottom
Call MoveCursor(p1, p2)
End Sub
Form文件:Private Sub Check1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 2 Then moveMouseOn_Control Check1
End SubPrivate Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 2 Then moveMouseOn_Control Command1
End SubPrivate Sub Label1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 2 Then moveMouseOn_Control Label1
End SubPrivate Sub Option1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 2 Then moveMouseOn_Control Option1
End SubPrivate Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 2 Then moveMouseOn_Control Picture1
End SubPrivate Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 2 Then moveMouseOn_Control Text1
End Sub
Module1文件:Type RECT
Left As Long
ToP As Long
Right As Long
Bottom As Long
End Type
Type POINTAPI
X As Long
Y As Long
End TypeDeclare Function SetCursorPos Lib "user32" (ByVal X As Long, ByVal Y As Long) As Long
Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As LongPublic Sub MoveCursor(FromP As POINTAPI, ToP As POINTAPI) '移动Mouse
Dim stepx As Long, stepy As Long, k As Long
Dim i As Long, j As Long, sDelay As Long
stepx = 1
stepy = 1
i = (ToP.X - FromP.X)
If i < 0 Then stepx = -1
i = (ToP.Y - FromP.Y)
If i < 0 Then stepy = -1
For i = FromP.X To ToP.X Step stepx
Call SetCursorPos(i, FromP.Y)
Next i
For i = FromP.Y To ToP.Y Step stepy
Call SetCursorPos(ToP.X, i)
Next i
End Sub
Public Sub moveMouseOn_Control(ByVal objControl As VB.Control)
If objControl Is Nothing Then Exit Sub
Dim rect5 As RECT
Dim p1 As POINTAPI, p2 As POINTAPI
Call GetWindowRect(objControl.hwnd, rect5) '注释:取得objControl相对Screen的座标
p1.X = (rect5.Left + rect5.Right) \ 2
p1.Y = (rect5.ToP + rect5.Bottom) \ 2
Call GetWindowRect(objControl.hwnd, rect5)
p2.X = rect5.Right
p2.Y = rect5.Bottom
Call MoveCursor(p1, p2)
End Sub
解决方案 »
- listview再问
- 那个英雄能让这个程序的菜单项 d 变为有效?打开后会有美眉出现!
- OLEView的问题
- 一个世界上最简单的"hello"为什么出错? 55~
- 大家帮我看看这个SQL语句有什么错误
- 请问怎么用代码来显示datatrid控件里的空白行数?(散分题)
- 高分求助我要做一个简单的个人信息管理程序,类似与window的通讯录,但通讯录的存储格式很特别,那位高手能给详细解释一下或提供一些质料
- 简单问题,在线请教,请各位大虾指点!大门为你敞开,请进---
- 如何方便的使用键盘啊!!(在线等待了)
- 关于程序打包问题,谁能解救呀
- 急求水晶报表8.0的下载地址
- 为什吗我用open for input as #1 打开文件得到的却是乱码?
Public Declare Function ClientToScreen Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long把你的 moveMouseOn_Control 改为下面的就行了:
Public Sub moveMouseOn_Control(ByVal objControl As VB.Control)
Dim p As POINTAPI
ClientToScreen objControl.Parent.hwnd, p
p.X = p.X + objControl.Left + objControl.Width
p.Y = p.Y + objControl.Top + objControl.Height
SetCursorPos p.X, p.Y
End Sub
^_^ 是不是简单多了?