The Ex09b Example: The Web Browser ActiveX Control
Most of the functionality in Microsoft Internet Explorer is contained in one big ActiveX control, Shdocvw.dll. When you run Internet Explorer, you launch a small shell program that loads this Web browser control in its main window.
Note | You can find complete documentation for the Web browser control's properties, methods, and events in the online MSDN Library included with Visual Studio. NET. |
Because of this modular architecture, you can write your own custom browser program with little effort. Ex09b creates a two-window browser that displays a search engine page side-by-side with the target page, as shown here:

This view window contains two Web browser controls that are sized to occupy the entire client area. When the user clicks an item in the search (right-hand) control, the program intercepts the command and routes it to the target (left-hand) control.Here are the steps for building the example:
Be sure the Web browser control is registered. You undoubtedly have the latest version of Internet Explorer installed, since Visual Studio .NET requires it, so the Web browser control should be registered. You can download Internet Explorer from http://www.microsoft.com if necessary.
Run the MFC Application Wizard to generate the Ex09b project. Accept all the default settings but two: Select Single Document and deselect Printing And Print Preview. Be sure the ActiveX Controls option is selected, as in Ex09a.
Install the Web browser control in the Ex09b project. Choose Add Class from the Project menu, and select the MFC Class From ActiveX Control template. Then select Microsoft Web Browser. Visual Studio .NET will present two interfaces for which to generate wrapper classes: IWebBrowser and IWebBrowser2. Select IWebBrowser2. Visual Studio .NET will generate the wrapper class CWebBrowser2 and add the files to your project.
Add two CWebBrowser2 data members to the CEx09bView class. It's easiest to add these member variables by hand in the header file:
private:
CWebBrowser2 m_target;
CWebBrowser2 m_search;
Be sure to add an #include statement for the

Add the child window ID constants for the two controls. Right-click on the dialog class in Resource View and choose Resource Symbols from the shortcut menu. Add the symbols ID_BROWSER_SEARCH and ID_BROWSER_TARGET.
Add a static character array data member for the Google URL. Add the following static data member to the class declaration in

private:
static const char s_engineGoogle[];
Then add the following definition in

const char CEx09bView::s_engineGoogle[] = "http://www.google.com/";
Use Class View's Properties window to map the view's WM_CREATE and WM_SIZE messages. Edit the handler code in

int CEx09bView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
DWORD dwStyle = WS_VISIBLE | WS_CHILD;
if (m_search.Create(NULL, dwStyle, CRect(0, 0, 100, 100),
this, ID_BROWSER_SEARCH) == 0) {
AfxMessageBox("Unable to create search control!\n");
return -1;
}
m_search.Navigate(s_engineGoogle, NULL, NULL, NULL, NULL);
if (m_target.Create(NULL, dwStyle, CRect(0, 0, 100, 100),
this, ID_BROWSER_TARGET) == 0) {
AfxMessageBox("Unable to create target control!\n");
return -1;
}
m_target.GoHome(); // as defined in Internet Explorer options
return 0;
}
void CEx09bView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
CRect rectClient;
GetClientRect(rectClient);
CRect rectBrowse(rectClient);
rectBrowse.right = rectClient.right / 2;
CRect rectSearch(rectClient);
rectSearch.left = rectClient.right / 2;
m_target.put_Width(rectBrowse.right - rectBrowse.left);
m_target.put_Height(rectBrowse.bottom - rectBrowse.top);
m_target.UpdateWindow();
m_search.put_Left(rectSearch.left);
m_search.put_Width(rectSearch.right - rectSearch.left);
m_search.put_Height(rectSearch.bottom - rectSearch.top);
m_search.UpdateWindow();
}
The OnCreate function creates two browser windows inside the view window. The right-hand browser displays the top-level Google page, and the left-hand browser displays the "home" page as defined through the Internet Options icon in Control Panel. The OnSize function, which is called whenever the view window changes size, ensures that the browser windows completely cover the view window. The CWebBrowser2 member functions put_Width and put_Height set the browser's Width and Height properties.
Add the event sink macros in the CEx09bView files. The code wizards available from Class View's Properties window can't map events from a dynamic ActiveX control, so you must do it manually. Add the following lines inside the class declaration in the file

protected:
afx_msg void OnBeforeNavigateExplorer1(LPCTSTR URL,
long Flags, LPCTSTR TargetFrameName,
VARIANT FAR* PostData, LPCTSTR Headers, BOOL FAR* Cancel);
afx_msg void OnTitleChangeExplorer2(LPCTSTR Text);
DECLARE_EVENTSINK_MAP()
Then add the following code in

BEGIN_EVENTSINK_MAP(CEx09bView, CView)
ON_EVENT(CEx09bView, ID_BROWSER_SEARCH, 100,
OnBeforeNavigateExplorer1, VTS_BSTR VTS_I4 VTS_BSTR
VTS_PVARIANT VTS_BSTR VTS_PBOOL)
ON_EVENT(CEx09bView, ID_BROWSER_TARGET, 113,
OnTitleChangeExplorer2, VTS_BSTR)
END_EVENTSINK_MAP()
Add two event handler functions. Add the following member functions in

void CEx09bView::OnBeforeNavigateExplorer1(LPCTSTR URL,
long Flags, LPCTSTR TargetFrameName,
VARIANT FAR* PostData, LPCTSTR Headers, BOOL FAR* Cancel)
{
TRACE("CEx09bView::OnBeforeNavigateExplorer1 -URL = %s\n", URL);
if (!strnicmp(URL, s_engineGoogle, strlen(s_engineGoogle))) {
return;
}
m_target.Navigate(URL, NULL, NULL, PostData, NULL);
*Cancel = TRUE;
}
void CEx09bView::OnTitleChangeExplorer2(LPCTSTR Text)
{
// Careful! Event could fire before we're ready.
CWnd* pWnd = AfxGetApp()->m_pMainWnd;
if (pWnd != NULL) {
if (::IsWindow(pWnd->m_hWnd)) {
pWnd->SetWindowText(Text);
}
}
}
The OnBeforeNavigateExplorer1 handler is called when the user clicks on a link in the search page. The function compares the clicked URL (in the URL string parameter) with the search engine URL. If they match, the navigation proceeds in the search window; otherwise, the navigation is cancelled and the Navigate method is called for the target window. The OnTitleChangeExplorer2 handler updates the Ex09b window title to match the title on the target page.
Build and test the Ex09b application. Search for something on the Google page, and then watch the information appear in the target page.