分享

VB获取鼠标点击的位置坐标

 hdzgx 2017-10-13

当鼠标在窗口中某一点单击时,显示鼠标坐标数值!

方法一: 直接用 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

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多