假如我在form1里有个command的控件我如何得到他的窗口句柄?不要command.hwnd
也不要返回鼠标位置的窗口句柄那个api.我的意思是通过form1的句柄得到command的句柄.或其他的方法

解决方案 »

  1.   

    FindWindowEx-------------------------Option Explicit
    Private Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As LongPrivate Sub Form_Load()
        Dim hCommand1 As Long
        hCommand1 = FindWindowEx(Me.hWnd, 0&, "ThunderCommandButton", "Command1")
        MsgBox CStr(hCommand1) & vbCrLf & CStr(Command1.hWnd)
    End Sub
    放上一个Command1
      

  2.   

    GetChild(hWnd, HWND_FIRST)
    GetChild(hWnd, HWND_NEXT)
      

  3.   

    是在这个程序里,还是在别的程序里要通过另一个程序的form1得到command
      

  4.   

    Private Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long可以找到父窗口下的所有子窗口句柄!
      

  5.   

    先用findwindow找到窗体form1的句柄formhwnd然后用findwindowEx这个api找这个窗体内包含的控件句柄。FindWindowEx(formhwnd, 0&, "ThunderCommandButton", "Command1")
      

  6.   

    方法有很多,可以用findwindow结合findwindowex实现,也可以用EnumChildWindows进行枚举结合sendmessage发送wm_gettext消息进行判定,还可以用GetDlgItem获得(需要知道控件的id,可用spy++获得).......
      

  7.   

    用sendmessage函数,它的句柄传递用如:richtextbox1.hwnd等很方便。
      

  8.   

    你的这个command和窗体上其它command的区别是什么呢????标签。
    Const SWP_HIDEWINDOW = &H80
    Const SWP_SHOWWINDOW = &H40
    Const GW_CHILD = 5
    Const GW_HWNDNEXT = 2
    Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
    Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
    Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
    Private Sub Command2_Click()
    Dim frmHwnd         As Long
    Dim bWnd            As Long
    Dim strClassName    As String * 250
    Dim MyStr           As String
    Dim intLen          As Integer
        frmHwnd = Me.hwnd           '已知的窗口句柄
            
        bWnd = GetWindow(frmHwnd, GW_CHILD)
        Do While bWnd <> 0
            GetClassName bWnd, strClassName, 250
            '如果是VB的按钮,则下面的是thundercommandbutton,如里不是,那么则可能是button
            If InStr(1, strClassName, "ThunderCommandButton") <> -1 Then
                '判断按按钮的标签
                intLen = GetWindowTextLength(bWnd)
                MyStr = String(intLen + 1, Chr$(0))
                Call GetWindowText(bWnd, MyStr, Len(MyStr))
                If InStr(1, MyStr, "按钮名") <> -1 Then MsgBox "已搜索出句柄:" & bWnd
            End If
            bWnd = GetWindow(bWnd, GW_HWNDNEXT)
        Loop
                
    End Sub