VB6.0, get the handle of an activex control on a form


I have a form with an ActiveX control on it for a html editor window.  The activex control does not have an inbuilt hWnd property.

How do I find the handle of thso control so I can pass it to a spellchecker etc that requires the handle.

Code samples would be appreciated.

Who is Participating?
SiddharthRoutConnect With a Mentor Commented:
It seems like a user defined control created with IE and Scrollbars.

It doesn't have a property "hwnd" but spy++ did give me a hwnd.

I tried this code but since that control doesn't have a caption, I couldn't get the caption.

Here is the code that I tried.

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long

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 GetWindowTextLength Lib "user32.dll" Alias _
"GetWindowTextLengthA" (ByVal hwnd As Long) As Long

Private Declare Function GetWindowText Lib "user32.dll" Alias _
"GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As _
String, ByVal cch As Long) As Long

Dim sInput As String
Dim hhhdc As Long, lhWndChild As Long
Dim textlen As Long '<~~ Length of text of title
Dim titleText As String '<~~ Text of the title
Dim slength As Long '<~~ Length of the returned string

Private Sub Command1_Click()
    '~~> Form Caption
    sInput = "Form1"
    '~~> Get Handle of the Form
    hhhdc = FindWindow(vbNullString, sInput)
    Debug.Print "Value of Parent Window " & titleText; " is , " & hhhdc
    '~~> loop through the Child Windows
        lhWndChild = FindWindowEx(hhhdc, lhWndChild, vbNullString, vbNullString)
        textlen = GetWindowTextLength(lhWndChild)
        titleText = Space(textlen + 1)
        '~~> Get the text of the window
        slength = GetWindowText(lhWndChild, titleText, textlen + 1)
        '~~> Extract information from the buffer
        titleText = Left(titleText, slength)
        Debug.Print "Value of Child Window " & titleText; " is , " & lhWndChild

    Loop While lhWndChild

End Sub

Open in new window

Use FindWindow API to get the hwnd of the form and then either use FindwindowEx API or EnumWindows API to get the hwnd of the activex control.

If you are unable to do it then upload the form.


ps: If you are an API fan like me, then I would suggest you to bookmark this page :)

have you tried iterating through the CONTROLS collection?  If not mistaken, most controls should have an hwnd property.
Dim ctlThing As Control
For Each ctlThing In Me.Controls
  If ctlThing.Name = "the activex control you are seeking" Then
    Debug.Print ctlThing.Name, ctlThing.hWnd
  End If

Open in new window

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

jcwiatrAuthor Commented:
hi aikimark - sounds easy - on testing the statement -
For Each ctlThing In Me.Controls

generates an error on me.controls..

method or data method not found
jcwiatrAuthor Commented:
aikimark, my mistake, i got it working and am getting the name of the control but when I add, ctlThing.hwnd I am getting the error Object does not support this property or method.  This makes sense I guess given there is no internal hWnd for this control.  I will give the API calls a go.
Can you upload your userform?

Also what type of control is it?

jcwiatrAuthor Commented:
just a basic form at this stage with an ActiveX control pinedit.ocx which is a html editor control.  I need the handle so I can bind a spell checker to it.

I have uploaded the form and control.  The file pinedit.txt in the archive needs to be renamed to pinedit.ocx and registerd.

Cheers. Form1.zip
I wonder if the ActiveX control is merely a container and might not be capable of handling messages, so doesn't have hWnd.
Yeah, seems like it is. It definitely has IE in it.

jcwiatrAuthor Commented:
Thanks guys. I think you are right about the container.  Points awarded based on code to get the handle.  But I dont think i will be able to bind our spellchecker to it because of its internals.
Thanks for all the input.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.