比如一个窗体里有多个控件,如果通过点鼠标来获取相应控件名。

解决方案 »

  1.   

    Module Code:
    Option ExplicitPublic Declare Function WindowFromPoint Lib "user32" (ByVal X As Long, ByVal y As Long) As Long
    Public Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
    Public Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As LongPublic Type POINTAPI
        X As Long
        y As Long
    End TypePublic Type DGfor3
        ponX As Long
        ponY As Long
        MDC As Long
    End TypePublic Function MouseDC() As DGfor3
        On Error Resume Next
        Dim Cur As POINTAPI
        GetCursorPos Cur
        MouseDC.MDC = WindowFromPoint(Cur.X, Cur.y)
        MouseDC.ponX = Cur.X
        MouseDC.ponY = Cur.y
    End Function
    -------------------------------------------------------------
    Public Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As LongPublic Function CName(ByVal ChWnd As Long) As String
        CName = Space(85)
        GetClassName ChWnd, CName, Len(CName)
    End Function'shell
    'String=Cname(hWnd)
    --------------------------------------------------Form Code:
    Option ExplicitPrivate Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Any) As LongPrivate Const WM_GETTEXT = &HDDim Gtxt As String
    Dim GSize As LongPrivate Sub Timer1_Timer()
        
        Gtxt = Space(255)
        GSize = Len(Gtxt)
        
        SendMessage MouseDC.MDC, WM_GETTEXT, GSize, Gtxt
        Text1.text = Gtxt
        Text2.text = MouseDC.MDC    GetWindowThreadProcessId MouseDC.MDC, GSize
        Text3.text = "Proc:" & Hex(GSize)
        Text4.text = "X:" & MouseDC.ponX & "Y:" & MouseDC.ponY
        Text5.text = "Len:" & Len(lblTxt)
        Text6.text = CName(MouseDC.MDC)
    End Sub
    'Text1=文本
    'Text2=句柄
    'Text3=进程名
    'Text4=Mouse的坐标
    'Text5=类名
    'Text6
      

  2.   

    PS:
    'Text5=文本长度
    'Text6=类名
      

  3.   

    MouseDC返回的是鼠标的(MDC=句柄,PONX=X坐标,PONY=Y坐标)
    CName返回的是控件的类名
      

  4.   

    大哥,我把你提到的控件都加进去了,text里头不显示东西!
    请再给小弟指导一下!
      

  5.   

    Timer控件加了吧,我这是一个工程,写的程序我也在用,你再看看吧。
    它的属性=Timer1.Interval = 1
    Public Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
    把上面的API加进去。
      

  6.   

    那你能发给我吗?E-mail: [email protected]感激不尽
      

  7.   

    不用这样啦,Me.ActiveControl.Name返回当前拥有焦点的控件名