Using the Web Browser control (IE3)

Internet Explorer 3+ comes with a Web Browser ActiveX
control that you can use to add HTML viewing capabilities to your application.
In fact, IExplore.exe is a very small application that is simply a host
for this control. The most important interface that is used to interact
with it is IWebBrowser, declared in the exdisp.h file that
is included with Visual C 5.0. The newer IWebBrowser2 interface
adds a few new methods but is only supported by Internet Explorer 4, and
its definition is not included with Visual C 5.0. However you can use Developer
Studio to generate a wrapper class for it.

The Visual C 5.0 uuid.lib doesnt include the definition
of IID_IWebBrowser and CLSID_WebBrowser, but these GUIDs can be easly found
in exdisp.h or the registry. This is what I came up this:

/* EAB22AC1-30C1-11CF-A7EB-0000C05BAE0B
*/


IID const IID_IWebBrowser={0xEAB22AC1,
0x30C1, 0x11CF, 0xA7, 0xEB, 0x00, 0x00, 0xC0, 0x5B, 0xAE, 0x0B};

/* 8856F961-340A-11D0-A96B-00C04FD705A2
*/


CLSID const CLSID_WebBrowser={0x8856F961,
0x340A, 0x11D0, 0xA9, 0x6B, 0x00, 0xC0, 0x4F, 0xD7, 0x05, 0xA2};

You can use an ActiveX control in you application by using
CWnd::CreateControl. Make sure that you call AfxEnableControlContainer()
in your CWinApp::InitInstance(). After you created it you can use
CWnd::GetControlUnknown to retreive the controls IUnknown and then you
can QueryInterface it for IID_IWebBrowser.

m_wndBrowser.CreateControl(CLSID_WebBrowser,
lpszWindowName, WS_VISIBLE|WS_CHILD, rect, this, AFX_IDW_PANE_FIRST);


IUnknown *pUnk=m_wndBrowser.GetControlUnknown();

IWebBrowser *pBrowser;

HRESULT hr=pUnk->QueryInterface(IID_IWebBrowser,
(void **)&pBrowser);

After the control is set up you can use the Navigate
method to browse a page.

CString url(“http://www.microsoft.com/”);

BSTR bUrl=url.AllocSysString();

COleVariant vNull(LPCTSTR)NULL,
VT_BSTR);


hr=pBrowser->Navigate(bUrl, &vNull,
&VNull, &vNull, &vNull);

The example I wrote views a web page in a MDI child window.
Click here to download it. For more information
on this control download the Internet Client SDK documentation that is
available at Microsofts web site.



More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read