移動Mouse後,如何得知Form上某個物件的hwnd及其上的文字?

可利用GetWindowText或SendMessage,以下是範例:

Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Type POINTAPI
    x As Long
    y As Long
End Type
Dim a As POINTAPI
Dim p, gethwnd As Long
Dim title As String * 255

Private Sub Form_Activate()
    Do
        DoEvents
        Call GetCursorPos(a) '取得mouse的xy值
        gethwnd = WindowFromPoint(a.x, a.y) '重mouse的xy值取得hwnd
        p = GetWindowText(gethwnd, title, 255) '取得視窗文字
        Text1 = title
    Loop
End Sub


上一頁