Home |
Created By Wesley Griffin |
||||||||||||||||||||||||
|
|||||||||||||||||||||||||
|
|||||||||||||||||||||||||
|
|||||||||||||||||||||||||
Home > Source Code > Forms and Controls > Retrieve WebBrowser Handle | |||||||||||||||||||||||||
|
|||||||||||||||||||||||||
|
|||||||||||||||||||||||||
With nearly every other windowed control that you can drop onto a VB form you can retrieve the handle by simply calling: | |||||||||||||||||||||||||
|
|||||||||||||||||||||||||
But
with the WebBrowser Control when you call this function you get the error
message:
< Method 'HWND' of object 'IWebBrowser2' failed > Why? Well who knows, maybe this was a ploy by Microsoft to stop the standard programmer from subclassing the webbrowser control. Whatever the reason for Microsoft blocking us from retrieving the handle, like in everything in life, there's a workaround. The way we are going to approach this is to use the parent object of the webbrowser control and then enumerate every child object until we find the child with the class name of the webbrowser control and alas! We have found the handle. Place this code into a module, it contains the API declarations, variables, call-back function to handle the enumurated child windows and the main function which returns the webbrowsers handle as a long value.
|
|||||||||||||||||||||||||
Now all you have to do to find the handle of the webbrowser is use the following syntax in the code window of your form: | |||||||||||||||||||||||||
|
|||||||||||||||||||||||||
Please remember the WebBrowser control will not return a handle until it has loaded a page. So in the Form_Load procedure in you form code window add code to navigate to a page for example: | |||||||||||||||||||||||||
|
|||||||||||||||||||||||||
Note: If for some reason you have two webbrowser controls on one form this code is not for you. As it enumerates the child windows of the form it will only return the last window of the class "Internet Explorer_Server", therefore ignoring the first instance of the window. I will post a solution to solving the solution of having two webbrowser controls soon. To download the sample project which contains the above code click the picture below.
|
|||||||||||||||||||||||||
![]() |
Visual Basic 6 Example | ||||||||||||||||||||||||
If you do not have an application to unzip zip archives please click here. | |||||||||||||||||||||||||
|
|||||||||||||||||||||||||
![]() |
Warning: | ||||||||||||||||||||||||
All material from this site must be used at own risk. VB First Aid will take no responsibility for any loss of data or any other errors. The material on this site was written for Visual Basic Version 6, therefore compatibility with other versions may cause errors. | |||||||||||||||||||||||||
|