如何用API枚举窗体中的所有控件,列出这些控件的句柄?

解决方案 »

  1.   

    但不是所有的控件都有句柄的。例如label就没有。Private Sub Form_Load()Dim obj As Control
    For Each obj In Me.Controls
      MsgBox obj.hwnd
    Next
    End Sub
      

  2.   

    告诉你一个通用的外部的也行Private Const GW_HWNDNEXT = 2
    Private Const GW_CHILD = 5
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd 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 LongPrivate Function FindControlHwnd(ByVal nHwnd As Long,ByVal findStr As String) As Long
        Dim fHwnd As Long, myStr As String, sHwnd As Long
        fHwnd = GetWindow(nHwnd, GW_CHILD)
        If fHwnd = 0 Then Exit Function
        Do While fHwnd > 0
            myStr = String(100, Chr$(0))
            GetWindowText fHwnd, myStr, 100
            
            If Left(myStr,InStr(myStr, Chr$(0))-1)=findStr Then FindControlHwnd=fHwnd:Exit Function
            sHwnd = GetWindow(fHwnd, GW_CHILD)
            If sHwnd > 0 Then
                FindControlHwnd fHwnd
            End If
            fHwnd = GetWindow(fHwnd, GW_HWNDNEXT)
        Loop
    End FunctionPrivate Sub Form_Load()
        FindControlHwnd FindWindow(vbNullString, "Form1"),"确定"
    End Sub
    如果上面这个方法不行再试这个其实应该是差不多的新建一个窗体用下面代码EnumChildWindows FindWindow(vbNullString, "Form1"), AddressOf EnumChildWindowsProc, ByVal 0&再建个模块用下面代码Public Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam 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
    Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
    Public findHwnd As LongPublic Function EnumChildWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean
        Dim str As String * 255,hLen As Long
        hLen = GetWindowTextLength(hwnd)+1
        GetWindowText hwnd, str, 255
        if Left(str, hLen) = "确定" Then findHwnd=hwnd : EnumChildWindowsProc=False
        EnumChildWindowsProc = True
    End Function
      

  3.   

    赴日软件工程师:1.两年工作经验,至少有一年软件项目经验
    2.日语可基本交流,能流利交流更佳
    3.学历:本科学士学位,计算机相关专业,或大专(正规全日制大专,同时拥有信息产业部的程序员证书)
    国内开发部SE:1.熟悉java、 vb、.net其中一种;
    2.有一定日语基础,能看懂日文式样书;
    3.两年以上软件开发经验。待遇:国内两年经验在6K左右,三年或以上另议.
    赴日根据日语能力,结合技术能力给定待遇。联系方式:[email protected](邮件并MSN)
      

  4.   

    獲取表單控件中的內容Declare Function FindWindowA Lib "user32" (ByVal lpClassName As String, ByVal _
                     lpWindowName As String) As Long
    Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
    Declare Function GetClassNameA Lib "user32" (ByVal hwnd As Long, ByVal lpClassName _
                     As String, ByVal nMaxCount As Long) As Long
    Declare Function SendMessageA Lib "user32" (ByVal hwnd As Long, ByVal wMsg As Long, _
                     ByVal wParam As Long, lParam As Any) As Long
    Declare Function EnableWindow Lib "user32" (ByVal hwnd As Long, ByVal fEnable As Long) As LongConst GW_HWNDNEXT = 2  '為源窗口尋找下一個兄弟窗口
    Const GW_CHILD = 5     '尋找源窗口的第一個子窗口Const WM_GETTEXT = &HD
    Const WM_GETTEXTLENGTH = &HE
    Private Sub Form_Load()
      Dim hwnd As Long
      hwnd = FindWindowA(vbNullString, "Microsoft Excel - Book11")
      FindControlHwnd hwnd
    End SubFunction FindControlHwnd(ByVal nHwnd As Long) As Long
      Dim fHwnd As Long
      Dim myStr As String
      Dim sHwnd As Long
      fHwnd = GetWindow(nHwnd, GW_CHILD)
      If fHwnd = 0 Then Exit Function
      Do While fHwnd > 0
         Debug.Print fHwnd, GetClassNameStr(fHwnd), GetWinText(fHwnd)
         sHwnd = GetWindow(fHwnd, GW_CHILD)
         If sHwnd > 0 Then FindControlHwnd fHwnd
         fHwnd = GetWindow(fHwnd, GW_HWNDNEXT)
      Loop
    End Function'獲取控件內容
    Function GetWinText(hwnd As Long) As String
      Dim txtLen As Long
      Dim txt    As String
      GetWinText = ""
      If hwnd = 0 Then Exit Function
      txtLen = SendMessageA(hwnd, WM_GETTEXTLENGTH, 0, 0)
      If txtLen = 0 Then Exit Function
        
      txtLen = txtLen + 1
      txt = Space$(txtLen)
      txtLen = SendMessageA(hwnd, WM_GETTEXT, txtLen, ByVal txt)
      GetWinText = Left$(txt, txtLen)
      If InStr(GetWinText, "列印") <> 0 And GetClassNameStr(hwnd) = "Button" Then _
         EnableWindow hwnd, False
    End Function
            
    '獲取物件類名
    Function GetClassNameStr(hwnd As Long) As String
      Dim TempStr As String
      Dim Rc      As Long
      TempStr = Space(260)
      Rc = GetClassNameA(hwnd, TempStr, Len(TempStr))
      GetClassNameStr = StrConv(LeftB$(StrConv(TempStr, vbFromUnicode), Rc), vbUnicode)
    End Function
      

  5.   

    是不是想做这个程序??http://www.m5home.com/blog/blogview.asp?logID=46&cateID=2这里是VB版的:http://www.m5home.com/bbs/dispbbs.asp?boardID=2&ID=20&page=1