我已得到窗体的句柄,现在想通过该窗体的句柄获得窗体中button控件的句柄

解决方案 »

  1.   

    FindWindowEx(窗体的句柄,0,"","按钮名称")
      

  2.   

    Const WS_CHILD = &H40000000
    Const WM_LBUTTONDOWN = &H201
    Const WM_LBUTTONUP = &H202
    Const SW_HIDE = 0
    Const SW_NORMAL = 1
    Private Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
    End Type
    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 FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
    Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
    Private Declare Function CreateWindowEx Lib "user32" Alias "CreateWindowExA" (ByVal dwExStyle As Long, ByVal lpClassName As String, ByVal lpWindowName As String, ByVal dwStyle As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hWndParent As Long, ByVal hMenu As Long, ByVal hInstance As Long, lpParam As Any) As Long
    Private Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long
    Dim tWnd As Long, bWnd As Long, ncWnd As Long
    Private Sub Form_Load()
        'KPD-Team 1998
        'URL: http://www.allapi.net/
        'E-Mail: [email protected]
        Dim R As RECT
        'Get the taskbar's window handle
        tWnd = FindWindow("Shell_TrayWnd", vbNullString)
        'Get the start-button's window handle
        bWnd = FindWindowEx(tWnd, ByVal 0&, "BUTTON", vbNullString)
        'Get the start button's position
        GetWindowRect bWnd, R
        'Create a new button
        ncWnd = CreateWindowEx(ByVal 0&, "BUTTON", "Hello !", WS_CHILD, 0, 0, R.Right - R.Left, R.Bottom - R.Top, tWnd, ByVal 0&, App.hInstance, ByVal 0&)
        'Show our button
        ShowWindow ncWnd, SW_NORMAL
        'Hide the start button
        ShowWindow bWnd, SW_HIDE
    End Sub
    Private Sub Form_Unload(Cancel As Integer)
        'show the start button
        ShowWindow bWnd, SW_NORMAL
        'destroy our button
        DestroyWindow ncWnd
    End Sub
      

  3.   

    '*************************************************************************
    '**函 数 名:GetMessage
    '**输    入:hWnd(Long)                     -
    '**        :strData(String)                -
    '**        :Optional strClass(String = "") -
    '**        :Optional strName(String = "")  -
    '**        :Optional strSP(String = "")    -
    '**输    出:(Long) -
    '**功能描述:获得父窗体下所有子窗体的类名和窗体名
    '**全局变量:
    '**调用模块:
    '**作    者:
    '**日    期:
    '**修 改 人:
    '**日    期:
    '**版    本:版本1.0
    '*************************************************************************
    Public Function GetMessage(hWnd As Long, Optional strClass As String = "", Optional strName As String = "", Optional strData As String = "", Optional strSP As String = "") As Long
        Dim sClass As String * 250
        Dim sName As String * 250
        Dim hStartbutton As Long
        Dim lngLen(1) As Long
        Dim num As Integer
        Dim strSpace As String
           
        If hWnd < 0 Then Exit Function
         
        If Len(strData) > 0 Then strSpace = strSP + "   "
        
        hStartbutton = GetWindow(hWnd, GW_CHILD)
       
      Do
        '---------------------
             
             
             lngLen(0) = GetClassName(hStartbutton, sClass, 250)            '取类名
             lngLen(1) = GetWindowText(hStartbutton, sName, 250)            '取窗体名
             
             If lngLen(0) = 0 And lngLen(1) = 0 Then
                Exit Do
             End If
             
             num = num + 1
             strData = strData + strSpace + Format(num, "0、") + Format(hStartbutton, "0") + "[" + Left$(sClass, lngLen(0)) + "] -- (" + Left$(sName, lngLen(1)) + ")" + vbCrLf
             
             '---------------
             If Len(strClass) > 0 Or Len(strName) > 0 Then
                If Len(strClass) > 0 And Len(strName) > 0 Then
                    If strClass = Left$(sClass, lngLen(0)) And strName = Left$(sName, lngLen(1)) Then
                       GetMessage = hStartbutton
                       Exit Do
                    End If
                End If
                If Len(strClass) > 0 Then
                    If strClass = Left$(sClass, lngLen(0)) Then
                       GetMessage = hStartbutton
                       Exit Do
                    End If
                End If
                If Len(strName) > 0 Then
                    If strName = Left$(sName, lngLen(1)) Then
                       GetMessage = hStartbutton
                       Exit Do
                    End If
                End If
             End If
                     
             '----------------
             Call GetMessage(hStartbutton, "", "", strData, strSpace)
             
             hStartbutton = GetWindow(hStartbutton, GW_HWNDNEXT)
         '----------------------
       LoopEnd Function