|
-
May 13th, 2007, 04:42 AM
#1
Thread Starter
Lively Member
[RESOLVED] webbrowser HWND
hello
i have a problem when i try to get the webbrowser's hwnd within my form
Code:
Private Sub Form_Load()
Dim x As Long
x = WebBrowser1.hWnd
End Sub
it tells me
runtimererror
Method 'HWND' of object 'IWebBrowser2' failed
So please can someone help me to get the hwnd of the webbrowser control?
thanx
Adel
-
May 13th, 2007, 07:14 AM
#2
Re: webbrowser HWND
Actually it depends on which window you actually wants to get the hWnd for... The WebBrowser control is actually just a wrapper called "Shell Embedding" that in turn contains the "Internet Explorer_Server". You can get the hWnd of the "Shell Embedding" window with a simple call to the FindWindowEx API function. However if you need the handle of the "Internet Explorer_Server" you'll need to enumerate the child windows using the (surprise, surprise) EnumChildWindow API function, that requires a callback function.
What do you want to do with the handle once you've got it?
-
May 13th, 2007, 07:35 AM
#3
Re: webbrowser HWND
Here's two examples. The first gets the hWnd of the "Shell Embedding" window, and the second for the "Internet Explorer_Server".
vb Code:
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
Private Sub Command1_Click()
Dim hWnd As Long
hWnd = FindWindowEx(Me.hWnd, 0, "Shell Embedding", vbNullString)
MsgBox hWnd
End Sub
For this second example, the code must reside in a regular BAS module.
vb Code:
Private Declare Function GetClassName Lib "user32.dll" Alias "GetClassNameA" ( _
ByVal hWnd As Long, _
ByVal lpClassName As String, _
ByVal nMaxCount As Long _
) As Long
Private Declare Function EnumChildWindows Lib "user32.dll" ( _
ByVal hWndParent As Long, _
ByVal lpEnumFunc As Long, _
ByVal lParam As Long _
) As Long
Private hWndIE As Long
Private Function EnumChildProc(ByVal hWnd As Long, ByVal lParam As Long) As Long
Dim sClassName As String
sClassName = String(255, vbNullChar)
Call GetClassName(hWnd, sClassName, 255)
sClassName = Left$(sClassName, InStr(sClassName, vbNullChar) - 1)
If sClassName <> "Internet Explorer_Server" Then
EnumChildProc = 1
Else
hWndIE = hWnd
End If
End Function
Public Function GetBrowserHandle(ByVal hWndParent) As Long
hWndIE = 0
Call EnumChildWindows(hWndParent, AddressOf EnumChildProc, 1)
GetBrowserHandle = hWndIE
End Function
To get the hWnd of the "Internet Explorer_Server" simply call the GetBrowserHandle function passing the hWnd of the Form containing the WebBrowser control:
vb Code:
Private Sub Command1_Click()
Dim hWnd As Long
hWnd = GetBrowserHandle(Me.hWnd)
MsgBox hWnd
End Sub
-
May 13th, 2007, 11:45 AM
#4
Thread Starter
Lively Member
Re: webbrowser HWND
thanx alot u really really helped me
that was the code i was looking for thanx
-
Jul 30th, 2008, 03:47 AM
#5
New Member
Re: [RESOLVED] webbrowser HWND
i have the same problem,thanks !!
-
Feb 3rd, 2009, 08:03 AM
#6
New Member
Re: [RESOLVED] webbrowser HWND
i have the same problem tooooo!! thanks !!
-
Feb 3rd, 2009, 10:11 AM
#7
Re: webbrowser HWND
 Originally Posted by Joacim Andersson
To get the hWnd of the "Internet Explorer_Server" simply call the GetBrowserHandle function passing the hWnd of the Form containing the WebBrowser control:
Code:
Private Sub Command1_Click()
Dim hWnd As Long
hWnd = GetBrowserHandle(Me.hWnd)
MsgBox hWnd
End Sub
Always returns zero.
-
Feb 3rd, 2009, 10:23 AM
#8
Re: webbrowser HWND
 Originally Posted by jmsrickland
Always returns zero.
For the GetBrowserHandle function to return anything but zero you need to have a webbrowser component on the form and navigated to some web site (it must have something loaded). Otherwise there are no "Internet Explorer_Server" loaded and you will only be able to find the "Shell Embedding" window.
-
Feb 3rd, 2009, 10:54 AM
#9
Re: [RESOLVED] webbrowser HWND
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|