展开全部
方法一: 直接用 MouseDown事件
举例:(这种方法获取的是点的位置在窗体中,却该对象有MouseDown事件才行,其坐标值为所在容器的相对坐标值)
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then
Cls
Print "X:"; X, "Y:"; Y
End If
End Sub
方法二:用API函数GetCursorPos来获取位置,用GetAsyncKeyState来获取是否按下左键
这种方法在任何时候都可以获取,哪怕鼠标不在应用程序内也行
在窗体上添加一个计时器,设置Interval属性为10
获取的位置的数字,是屏幕中的位置(不是相对窗体的)
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Type POINTAPI
X As Long
Y As Long
End Type
Private Sub Timer1_Timer()
Dim P As POINTAPI
X = GetAsyncKeyState(1)
If X = -32767 Then 'x返回的是16位整数,最高位为1,表明按下
Cls
Print "左键按下",
GetCursorPos P
Print " 鼠标x位置:" & P.X & " 鼠标y位置:" & P.Y
End If
X = GetAsyncKeyState(2)
If X = -32767 Then
Cls
Print "右键按下",
GetCursorPos P
Print " 鼠标x位置:" & P.X & " 鼠标y位置:" & P.Y
End If
End Sub
举例:(这种方法获取的是点的位置在窗体中,却该对象有MouseDown事件才行,其坐标值为所在容器的相对坐标值)
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then
Cls
Print "X:"; X, "Y:"; Y
End If
End Sub
方法二:用API函数GetCursorPos来获取位置,用GetAsyncKeyState来获取是否按下左键
这种方法在任何时候都可以获取,哪怕鼠标不在应用程序内也行
在窗体上添加一个计时器,设置Interval属性为10
获取的位置的数字,是屏幕中的位置(不是相对窗体的)
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Type POINTAPI
X As Long
Y As Long
End Type
Private Sub Timer1_Timer()
Dim P As POINTAPI
X = GetAsyncKeyState(1)
If X = -32767 Then 'x返回的是16位整数,最高位为1,表明按下
Cls
Print "左键按下",
GetCursorPos P
Print " 鼠标x位置:" & P.X & " 鼠标y位置:" & P.Y
End If
X = GetAsyncKeyState(2)
If X = -32767 Then
Cls
Print "右键按下",
GetCursorPos P
Print " 鼠标x位置:" & P.X & " 鼠标y位置:" & P.Y
End If
End Sub
展开全部
两钟方法:
1..在MouseDown()或者MouseUp()事件里直接用参数X,Y
2..在Click()事件里用API取: GetCursorPos()
此函数声明如下:
Declare Function GetCursorPos Lib "user32" Alias "GetCursorPos" (lpPoint As POINTAPI) As Long
类型:
Type POINTAPI
x As Long
y As Long
End Type
1..在MouseDown()或者MouseUp()事件里直接用参数X,Y
2..在Click()事件里用API取: GetCursorPos()
此函数声明如下:
Declare Function GetCursorPos Lib "user32" Alias "GetCursorPos" (lpPoint As POINTAPI) As Long
类型:
Type POINTAPI
x As Long
y As Long
End Type
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这么简单的问题,楼上不回就别光给个API完事!
直接复制到vb中运行,顺便给你一个显示坐标的代码,需要添加两个 label~呵呵,满意的话,给分哦~
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Type POINTAPI
X As Long
Y As Long
End Type
Private Sub Form_Click()
Dim firendless As POINTAPI
GetCursorPos firendless
MsgBox " 鼠标x位置:" & firendless.X & " 鼠标y位置:" & firendless.Y
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Label1.Caption = (X)
Label2.Caption = (Y)
End Sub
直接复制到vb中运行,顺便给你一个显示坐标的代码,需要添加两个 label~呵呵,满意的话,给分哦~
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Type POINTAPI
X As Long
Y As Long
End Type
Private Sub Form_Click()
Dim firendless As POINTAPI
GetCursorPos firendless
MsgBox " 鼠标x位置:" & firendless.X & " 鼠标y位置:" & firendless.Y
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Label1.Caption = (X)
Label2.Caption = (Y)
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询