Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1530

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

$
0
0
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)


Viewing all articles
Browse latest Browse all 1530

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>