我用findwindow()得到了一个程序窗口的句柄,但是用findwindowex()无法获得该窗口中,一个caption为“安装”的按钮的句柄,不知错在哪里,望高手指教。(该按钮上除了有“安装”二字外,还有一幅图案)
程序如下:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Any, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Any, ByVal lpsz1 As Any, ByVal lpsz2 As String) As Longhwndinstall = FindWindow(0&, "installwatch pro 2.5c - [default.iwc]")
hwndinstall = FindWindowEx(hwndinstall, 0&, 0&, "安装")

解决方案 »

  1.   

    不出意外的话,“安装”为图片上的文字,caption=未知。你当然查不到了。要用WindowFromPoint进行查找
      

  2.   

    WindowFromPoint
    【VB声明】
      Private Declare Function WindowFromPoint Lib "user32" Alias "WindowFromPoint" (ByVal xPoint As Long, ByVal yPoint As Long) As Long【说明】
      返回包含了指定点的窗口的句柄。忽略屏蔽、隐藏以及透明窗口 【返回值】
      Long,包含了指定点的窗口的句柄。如指定的点处没有窗口存在,则返回零 【参数表】
      xPoint ---------  Long,x点值  yPoint ---------  Long,y点值示例:
    Private Type POINTAPI
        X As Long
        Y As Long
    End Type
    Private Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
    End Type
    Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
    Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    Private Declare Function ExtTextOut Lib "gdi32" Alias "ExtTextOutA" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long, ByVal wOptions As Long, ByVal lpRect As Any, ByVal lpString As String, ByVal nCount As Long, lpDx As Long) As Long
    Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
    Private Declare Function GetTextExtentPoint32 Lib "gdi32" Alias "GetTextExtentPoint32A" (ByVal hdc As Long, ByVal lpsz As String, ByVal cbString As Long, lpSize As POINTAPI) As Long
    Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long
    Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
        Dim Pt As POINTAPI, mWnd As Long, WR As RECT, nDC As Long
        Dim TextSize As POINTAPI, CX As Long, CY As Long
        'Get the current cursor position
        GetCursorPos Pt
        'Get the window under the cursor
        mWnd = WindowFromPoint(Pt.X, Pt.Y)
        'Get the window's position
        GetWindowRect mWnd, WR
        'Get the window'zs device context
        nDC = GetWindowDC(mWnd)
        'Get the height and width of our text
        GetTextExtentPoint32 nDC, "Hello !", Len("Hello !"), TextSize
        For CX = 1 To WR.Right - WR.Left Step TextSize.X
            For CY = 1 To WR.Bottom - WR.Top Step TextSize.Y
                'Draw the text on the window
                ExtTextOut nDC, CX, CY, 0, ByVal 0&, "Hello !", Len("Hello !"), ByVal 0&
            Next
        Next
    End Sub
    Private Sub Form_Paint()
        Me.CurrentX = 0
        Me.CurrentY = 0
        Me.Print "Click on this form," + vbCrLf + "Hold the mouse button," + vbCrLf + "drag the mouse over another window," + vbCrLf + "release the mouse button" + vbCrLf + "and see what happens!"
    End Sub
      

  3.   

    谢谢rainstormmaster(rainstormmaster) 
    我想获得"installwatch"的下一级子窗口句柄,就是点击"installwatch"上的一个按钮后弹出的窗口,该窗口的标题是"设置安装过程中""需要""监视的内容",可是不成功,望继续指正.Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Any, ByVal lpsz1 As Any, ByVal lpsz2 As String) As Long
    hwndnext = FindWindowEx(hwndinstall, 0&, 0&, "设置安装过程中""需要""监视的内容")
      

  4.   

    纠正一下:点击"installwatch"上的一个按钮后弹出的窗口,未必是"installwatch"的下一级子窗口,如果窗口有标题的话可用FindWindow查找,不建议用FindWindowEx进行查找。
      

  5.   

    感谢rainstormmaster(rainstormmaster) 我成功了。