桌面下有两个对话框,我要窗口2下的子窗口一的句柄。
窗口1    类名称:    "#32770"    窗口标题: "系统通知"窗口2    类名称:    "#32770"    窗口标题: "登录"
子窗口1  类名称:    "Edit"      窗口标题: 无    ,
子窗口2  类名称:    "Edit"      窗口标题: 无    ,
子窗口3  类名称:    "Edit"      窗口标题: 10    ,
....有5-6个 "Edit"hWnd2 = FindWindow("#32770", vbNullString)               ’有时候会是系统通知的句柄
hWnd3 = FindWindowEx(hWnd2, 0, "Edit", vbNullString)     ’有时候会是子窗口1的句柄
这个 hwnd3我有困惑很久了。
我怎么才能抓出我想要的子窗口2?
用工具我可以很好抓出来,现在在vb程序下我抓不出来。
请高手给点方法,谢谢。

解决方案 »

  1.   


    获得子窗口的句柄后,可以根据子窗口的位置再进行判断,比如:
        Dim hWndParent As Long
        Dim hWndChild As Long
        Dim rcWindow As RECT
        
        hWndChild = 0
        hWndParent = FindWindow(vbNullString, "Form1")
        Do
            SetRectEmpty rcWindow
            hWndChild = FindWindowEx(hWndParent, hWndChild, "Edit", vbNullString)
            GetWindowRect hWndChild, rcWindow
            If rcWindow.Left = xx And rcWindow.Top = xx Then
            'If rcWindow.Left最小 Then
                '找到你需要的子窗口
            End If
        Loop Until hWndChild = 0使用工具抓取时,实质先用GetCursorPos获得鼠标位置,然后用WindowFromPoint函数获得子窗口的句柄。这也说明子窗口的位置是不一样的,因此你可以根据位置进行判断。
      

  2.   

    谢谢,我来测试你的方法。不过窗口位置不固定。
    子窗口中的 "Edit“类可以根据他在主窗口中的相对位置来确定?
    要是有用列举法,列举出想要的窗口就是最好的,我想得到的效果。
    我的方法是:
    先用 spy++ 抓出窗口的类名称,根据类名称通过窗口标题筛选。人家程序中窗口标题都相同。是否可以在窗口上根据其他信息进行筛选?
    不知道有没有人遇到过跟我一样的情况?
      

  3.   


    人家的程序,我去抓人家的"Edit"类的位置。
    比如人家写的    卡号: ====
                    密码: ****其中卡号必须刷卡才能写入
    密码不需要。
    里面有N个"Edit"类,且没有标题。
    我想得到的是卡号的类。没好的方法。
      

  4.   

    我在测试这个根据控件位置的方法。(下面这个问题不影响结帖,只要上面的能抓倒"Edit",我就结贴了,感谢大家帮助。)真不好意思,顺便问一下,
    一个text本来是不可输入状态,但是刷卡输入内容后有一个button(本来是不可以使用状态)
    会因为这个填充行为而变成可以使用的button,
    然而我直接发送消息 sendmessage后,text里面的内容有了,而button还是灰色,
    结果我自作聪明,用api的EnableWindow强行把button弄成可以使用,
    我点了button,程序提示,请输入卡号。
    我晕,是不是可以关联着text刷新一下 button?SendMessage hWnd2, WM_SETTEXT, 0, ByVal Text1.Text
    SendMessage wHWnd, EM_SETMODIFY, True, Null
    UpdateWindow wHWnd
    不行啊!
      

  5.   


        Dim hWndParent As Long
        Dim hWndChild As Long
        Dim rcWindow As RECT    hWndChild = 0
        hWndParent = wHWnd
        Do
            DoEvents
            SetRectEmpty rcWindow
            hWndChild = FindWindowEx(hWndParent, hWndChild, "Edit", vbNullString)
            GetWindowRect hWndChild, rcWindow
            If rcWindow.Left = 2 And rcWindow.Top = 2 Then
            'If rcWindow.Left最小 Then
                '找到你需要的子窗口
            End If这里面
            'If rcWindow.Left最小 Then
                '找到你需要的子窗口
    如何得到这个位置的text的句柄?
        
         矩形(179.249)-(284.267),224*18 '估计是整个windows窗口的位置
        客户矩形 (2.2)-(112.16),110*14  '相对窗口的位置还是不行的,还有。
    Private Declare Function SetRectEmpty Lib "user32" Alias "SetRectEmpty" (lpRect As RECT) As Long提示用户定义类型未定义
    RECT是结构体?如何查相关信息?
      

  6.   


    Option ExplicitPrivate Type RECT
            Left As Long
            Top As Long
            Right As Long
            Bottom As Long
    End Type
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
    Private Declare Function SetRectEmpty Lib "user32" (lpRect As RECT) As LongPrivate Sub Command1_Click()
        Dim hWndParent As Long
        Dim hWndChild As Long
        Dim hWndChildLeft As Long
        Dim nMinLeft As Long
        Dim rcParent As RECT
        Dim rcClient As RECT
        
        nMinLeft = 100000
        
        hWndChild = 0
        hWndParent = FindWindow("#32770", vbNullString)
        
        If hWndParent = 0 Then Exit Sub
        
        GetWindowRect hWndParent, rcParent
        Do
            SetRectEmpty rcWindow
            hWndChild = FindWindowEx(hWndParent, hWndChild, "Edit", vbNullString)
            GetWindowRect hWndChild, rcClient
            If hWndChild = 0 Then Exit Do
            If rcClient.Left < nMinLeft Then
                nMinLeft = rcClient.Left
                hWndChildLeft = hWndChild
            End If
        Loop Until hWndChild = 0
        
        If hWndChildLeft > 0 Then '找到位于最左边的子窗口
            '
        End If
    End Sub
      

  7.   

    我找到了,差不多了,但是我抓出来的是相对windows的位置,是否还要剪掉主窗口位置,
    功能能实现了,我再看看。
    谢谢,等一小会就结帖,谢谢,感谢,很感谢