I have an activex control which doesnt have any hWnd property, how can I get its handle.
Printable View
I have an activex control which doesnt have any hWnd property, how can I get its handle.
Use the FindWindowEx function.
Code: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
''Usage
hwndctl = FindWindowEx(Me.hWnd, 0, "ClassName", vbNullString)
I dont know why its returning 0, :confused:Code:wHandle = FindWindowEx(Me.hWnd, 0, "ThunderRT6TextBox", vbNullString)
Change ThunderRT6TextBox to ThunderTextBox.
But the classname is not ThunderTextBox, I got its classname by enumerating all the child windows. I think there is some other problem.
For me, the classname is ThunderTextBox, does it work for you? If it does, why you complainin'? :rolleyes:
Relax. ThunderRT6ControlName is the classname given when you EXE is compiled, and ThunderControlName is the classname given when working in the IDE.
Before you use FindWindow, check and make sure if there is an easier way of getting the hWnd (via the hwnd propety)
Code:hwndctl = MyControl.HWnd
Also, one question. Are you creating this yourself? (i.e: an ActiveX project)
The problem is that its a third party control and it doesnt have any hWnd property and the ThunderTextbox is not working for me, and the classname is ThunderRT6Textbox at design time.
I have attached the ocx with this post, I would be really thankful if anybody can disable the double click event for this control.
Code: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 Sub Command1_Click()
h = FindWindowEx(FindWindowEx(hWnd, 0, "ThunderRT6UserControlDC", vbNullString), 0, "ThunderRT6TextBox", vbNullString)
Print h
End Sub
Now to subclass it:
Add to a Module
Add to a FormCode:Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Declare Function SetWindowLong& Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long)
Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Const GWL_WNDPROC = (-4)
Const WM_LBUTTONDBLCLK = &H203
Public hwndctl As Long
Global WndProcOld As Long
Public Function WindProc(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If wMsg = WM_LBUTTONDBLCLK Then Exit Function
WindProc = CallWindowProc(WndProcOld&, hwnd&, wMsg&, wParam&, lParam&)
End Function
Sub SubClassWnd(hwnd As Long)
WndProcOld& = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf WindProc)
End Sub
Sub UnSubclassWnd(hwnd As Long)
SetWindowLong hwnd, GWL_WNDPROC, WndProcOld&
WndProcOld& = 0
End Sub
Code: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 Sub Form_Load()
hwndctl = FindWindowEx(FindWindowEx(hwnd, 0, "ThunderRT6UserControlDC", vbNullString), 0, "ThunderRT6TextBox", vbNullString)
SubClassWnd hwndctl
End Sub
Private Sub Form_Unload(Cancel As Integer)
UnSubclassWnd hwndctl
End Sub
Thanks a lot, but can you please tell me why you called the findwindowex function twice? and what should I do if I want to subclass all of these textboxes on my form?
The reason we call it twice is because ThunderRT6TextBox is a child of ThunderRT6UserControlDC, and this is a child of our main form. In other words, ThunderRT6TextBox is a grand-child of our form.