Results 1 to 2 of 2

Thread: vb6 Get Control Hwnd,Webbrowser Hwnd,Get Activex Control Hwnd

  1. #1

    Thread Starter
    Taking a Break
    Join Date
    Jan 2020
    Posts
    1,340

    vb6 Get Control Hwnd,Webbrowser Hwnd,Get Activex Control Hwnd

    This is a great invention, I wonder if you have a better way?
    It took a few hours to complete. Itís not easy. If you are interested, try some suggestions.


    if usercontrol with windowless=true,also can get hwnd,it's the form hwnd.
    commandbutton,label,can't use this way to get hwnd.
    can use vb6 Method

    How to Get Control Hwnd like Webbrowser,all Activex Control
    Code:
    Private Declare Function IUnknown_GetWindow Lib "shlwapi.dll" (ByVal punk As IUnknown, ByRef phwnd As Long) As Long
    Private Declare Function GetAncestor Lib "user32.dll" (ByVal hwnd As Long, ByVal gaFlags As Long) As Long
    Private Const GA_ROOT As Long = 2
     
    
    Function GetOcxHwnd(ocx As IUnknown, Optional WindowLess As Boolean) As Long
    On Error Resume Next
    WindowLess = False
    '无窗口的自定义控件(WindowLess=true),取到的句柄就是窗体
    'Usercontrol.WindowLess=true,IUnknown_GetWindow Get Hwnd is Form hwnd
        Dim Obj As Object, Hwnd1 As Long
        Set Obj = ocx
        On Error Resume Next
        Hwnd1 = Obj.hwnd 'vb6 normal method get hwnd
        If Hwnd1 = 0 Then
            IUnknown_GetWindow Obj.object, Hwnd1
            If Hwnd1 <> 0 Then
                Dim Hwnd2 As Long
                Hwnd2 = GetAncestor(Hwnd1, GA_ROOT)
                
                ''Get Form Hwnd // like GetAncestor(hwnd=0,GA_ROOT)
                
    '            Dim Parent As Object, LastParent As Object
    '            Set Parent = Obj.Container
    '            While Not Parent Is Nothing
    '                'MsgBox Parent.Hwnd
    '                Set LastParent = Parent
    '                Set Parent = Nothing
    '                Set Parent = LastParent.Container
    '            Wend
    '            Hwnd2 = LastParent.hwnd
    
                If Hwnd2 = Hwnd1 Then
                    WindowLess = True
                    Debug.Print "It's Usercontrol WindowLess=true"
                    Hwnd1 = 0
                End If
            End If
        End If
        GetOcxHwnd = Hwnd1
    End Function
    
    
    Function GetOcxHwnd2(ocx As IUnknown) As Long
        Dim HwndA As Long
        Dim Obj As Object
        Set Obj = ocx
        IUnknown_GetWindow Obj.object, GetOcxHwnd2
    End Function
    
        MsgBox GetOcxHwnd(DataGrid1)
        MsgBox GetOcxHwnd(Webbrowser1)
    MsgBox GetOcxHwnd(UserControl11)
    Last edited by xiaoyao; May 3rd, 2021 at 01:18 AM.

  2. #2

    Thread Starter
    Taking a Break
    Join Date
    Jan 2020
    Posts
    1,340

    Re: vb6 Get Control Hwnd like Webbrowser,all Activex Control

    By default, the IUnknown_GetWindow method is used to get the handle first, and then the control.hwnd method is used if it fails.

    Code:
    Private Declare Function IUnknown_GetWindow Lib "shlwapi.dll" (ByVal punk As IUnknown, ByRef phwnd As Long) As Long
    Private Declare Function GetAncestor Lib "user32.dll" (ByVal hwnd As Long, ByVal gaFlags As Long) As Long
    Private Const GA_ROOT As Long = 2
    
    Function GetOcxHwnd(ocx As IUnknown, Optional WindowLess As Boolean) As Long
    On Error Resume Next
    '默认使用IUnknown_GetWindow 这个方法先取句柄,取不成功再用 控件.hwnd的方法
    WindowLess = False
    '无窗口的自定义控件(WindowLess=true),取到的句柄就是窗体
    'Usercontrol.WindowLess=true,IUnknown_GetWindow Get Hwnd is Form hwnd
        Dim Obj As Object, Hwnd1 As Long
        Set Obj = ocx
        On Error Resume Next
         'vb6 normal method get hwnd
        Dim IsObject2 As Boolean
        IsObject2 = Not Obj.object Is Nothing
        If IsObject2 Then
            IUnknown_GetWindow Obj.object, Hwnd1
            If Hwnd1 <> 0 Then
                Dim Hwnd2 As Long
                Hwnd2 = GetAncestor(Hwnd1, GA_ROOT)
                
                ''Get Form Hwnd // like GetAncestor(hwnd=0,GA_ROOT)
                
    '            Dim Parent As Object, LastParent As Object
    '            Set Parent = Obj.Container
    '            While Not Parent Is Nothing
    '                'MsgBox Parent.Hwnd
    '                Set LastParent = Parent
    '                Set Parent = Nothing
    '                Set Parent = LastParent.Container
    '            Wend
    '            Hwnd2 = LastParent.hwnd
    
                If Hwnd2 = Hwnd1 Then
                    WindowLess = True
                    Debug.Print "It's Usercontrol WindowLess=true"
                    Hwnd1 = 0
                    Exit Function
                End If
            End If
        End If
            If Hwnd1 = 0 Then
                Hwnd1 = Obj.hwnd
            End If
        GetOcxHwnd = Hwnd1
    End Function
    Last edited by xiaoyao; May 3rd, 2021 at 01:12 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width