Click here to Skip to main content
14,970,700 members
Articles / Desktop Programming / WTL
Posted 23 May 2004


64 bookmarked

WTL Browser

Rate me:
Please Sign up or sign in to vote.
4.68/5 (23 votes)
23 May 20046 min read
How to create a simple browser using IE engine and WTL.

Sample Image - wtlbrowser.jpg


This tutorial will help you build a mini browser using IE engine. It is based on WTL and uses a wrapper class which I've wrote for handling IWebBrowser2 interface. Since I know it can be hard to read code, this tutorial will help you develop a mini browser - step by step.


Most of my projects are related to internet browsing. I'm using SDI with HTML view a lot of time. Sometimes, I need to use real browser capabilities, so I've written a wrapper for IWebBrowser2. This wrapper class can handle a window which is embedded in IE. It can also handle events sinking in a very easy way (for example: OnDocumentComplete).

Creating a new project

We first start by creating a new WTL project. I assume you have WTL files installed (if not, check here). On the first wizard screen, select an SDI application and check for generating .CPP files.

Image 2

On the second screen, change the default view to an HTML view.

Image 3

First step will be to edit stdafx.h. Please include atlmisc.h (we'll use CString from time to time) and atlctrlx.h (for CMultiPaneStatusBarCtrl). We also need to comment out the _ATL_DLL definition (we don't want our executable to depend on atl.dll) and to change IE version to version 5.

// Change these values to use different versions
#define WINVER      0x0400
//#define _WIN32_WINNT   0x0400
#define _WIN32_IE   0x0500
#define _RICHEDIT_VER   0x0100

// This is required for hosting browser in ATL7
//#define _ATL_DLL

#include <atlbase.h>
#include <atlapp.h>

extern CAppModule _Module;

#include <atlcom.h>
#include <atlhost.h>
#include <atlwin.h>
#include <atlctl.h>
#include <atlmisc.h>
#include <atlframe.h>
#include <atlctrls.h>
#include <atldlgs.h>
#include <atlctrlw.h>
#include <atlctrlx.h>
// Microsoft Visual C++ will insert additional
// declarations immediately before the previous line.

Updating the view

In out view class, we need to include browser.h and inherit the view class from it. We also need to chain it to the message map so the class can handle several messages (WM_CREATE, WM_DESTROY).

#include "browser.h"

class CWTLBrowserView : public CWindowImpl<CWTLBrowserView, 
  CAxWindow>, public CWebBrowser2<CWTLBrowserView>
    DECLARE_WND_SUPERCLASS(NULL, CAxWindow::GetWndClassName())

    BOOL PreTranslateMessage(MSG* pMsg);


// Handler prototypes (uncomment arguments if needed):
// LRESULT MessageHandler(UINT /*uMsg*/, WPARAM /*wParam*/, 
//       LPARAM /*lParam*/, BOOL& /*bHandled*/)
// LRESULT CommandHandler(WORD /*wNotifyCode*/, 
//       WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
// LRESULT NotifyHandler(int /*idCtrl*/,
//       LPNMHDR /*pnmh*/, BOOL& /*bHandled*/)

Creating the menu

We need to add some new items to the menu. A typical browser handles back, forward, home, stop and refresh. We'll add those commands to the menu and the toolbar.

Image 4

Since some of the items aren't allowed from time to time, we need to handle their UI (we can't always use back and forward). First, we need to add them to the UI update map (in mainfrm.h).


We update them via the OnIdle function.


CWebBrowser2 exposes 2 functions (CanBack & CanForward) which can determine the status of the back and forward actions.

Since the default wizard starts on, we need to change the code to about:blank, and start on the normal home page. We need to change the code in CMainFrame::OnCreate.

m_hWndClient = m_view.Create(m_hWnd, rcDefault, 
return 0;

Creating the address bar

We now have a working framework, but we still can't enter any URL there! To solve the problem, we'll create an address bar, so the user can enter a URL for navigating. We'll add a new member variable (CEdit) to our mainframe class - called m_URL. We'll create and initialize it from CMainFrame::OnCreate. Since we also want auto completion for our address bar, we'll use SHAutoComplete function on the edit control.

AddSimpleReBarBand(hWndToolBar, NULL, TRUE);

CString szAddress;


If we try to compile the project, we'll get an error while linking the files. This error occurs since SHAutoComplete is exported from shlwapi.dll. To solve it, we need to add the library (shlwapi.lib) to our project.

Image 5

After compiling the project, we'll see our new bar waiting for us to type in. But hey! If we try to press the Enter key, our browser would just keep sleeping! Let's fix it!


Since the HTML framework forwards all keystrokes to the HTML document, we can't just wait for the WM_CHAR message. We need to add some code to the PreTranslateMessage function. We need to grab the WM_CHAR message from the address bar and handle the VK_RETURN character.

BOOL CMainFrame::PreTranslateMessage(MSG* pMsg)
        return TRUE;
    if (pMsg->message==WM_CHAR && m_URL==pMsg->hwnd)
        switch (pMsg->wParam)
        case VK_RETURN:
                CString szURL;
                int nLength=m_URL.GetWindowTextLength();
                return TRUE;
        return FALSE;
    return m_view.PreTranslateMessage(pMsg);


We can now enter a URL and watch our browser navigate it. But something is still missing. Why can't we use back, forward and the rest of our new commands? We need to add their functions to our mainframe class (since there is nothing special there, simply check the source code to see how it's done).

UI Tweaks

Let's test our browser. Try to go to and click on the WTL section. Why can't we see the new location in our address bar? To fix it, we need to update the address bar each time our location changes. A good place is to handle the OnNavigateComplete2 from our view class. Since we need to update m_URL which resides inside the mainframe class, we'll create a new reference to it inside our view class and pass the variable on the constructor. We now can handle our message and update the address bar.

void CWTLBrowserView::OnNavigateComplete2(IDispatch* pDisp, const String& szURL)

Another useful tweak includes a progress notification, secured icon, and status bar text. They all reside on the status bar. It's time to bring our big guns - CMultiPaneStatusBarCtrl! We'll create a new variable inside the mainframe class, plus a reference inside our view class. The status bar should contain 3 parts: the default text, a secured icon, and a progress notification. Since the default text has a unique id (ID_DEFAULT_PANE), all we need to do is to create additional two identifiers. From the "View->Resource Symbols" menu, we need to create our new symbols: IDR_LOCK and IDR_PROGRESS. After creating them, we can initialize our new status bar from the CMainFrame::OnCreate function.


We also need to add a new icon to our project (IDI_LOCK) and load it into a variable (m_hSecured). To handle proper UI updating, we'll add a new line to the UI update map:


In our view class, we'll add a new variable (m_bSecured) and some code to handle our status bar updates:

void CWTLBrowserView::OnStatusTextChange(const String& szText)

void CWTLBrowserView::OnProgressChange(long nProgress, long nProgressMax)
    CString szText;
    if (nProgressMax>0)

void CWTLBrowserView::OnSetSecureLockIcon(long nSecureLockIcon)

Finally, we need to add some code to CMainFrame::OnIdle:

m_StatusBar.SetPaneIcon(IDR_LOCK,m_view.IsSecured() ? m_hSecured : NULL);
return FALSE;

File Command

To allow the browser to save and print files, we need to handle file messages. To send a command to the browser, we need to use the ExecWB function. To query a command status (for the UI map), we can use the QueryStatusWB function. We need to add the proper functions (for saving/printing), update the UI update map and handle their UI from the OnIdle function. Since it is fairly simple, I won't include it in the article (check the source code for more details).

Edit Command

Edit commands are a special case. Since they can be used with the browser, as well as with our address bar, we need to check our focus window each time we use those commands. First, we add them to the UI update map, then we handle their update via the OnIdle function:

if (GetFocus()==m_URL)
    DWORD dwSelection=m_URL.GetSel();
    BOOL bEnable=HIWORD(dwSelection)!=LOWORD(dwSelection);
    if (m_URL.OpenClipboard())

We also need to distinguish them while executing the actual commands:

LRESULT CMainFrame::OnEditCut(WORD /*wNotifyCode*/, 
    WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
    if (GetFocus()==m_URL)
    return 0;

LRESULT CMainFrame::OnEditCopy(WORD /*wNotifyCode*/, 
    WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
    if (GetFocus()==m_URL)
    return 0;

LRESULT CMainFrame::OnEditPaste(WORD /*wNotifyCode*/, 
    WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
    if (GetFocus()==m_URL)
    return 0;

LRESULT CMainFrame::OnEditUndo(WORD /*wNotifyCode*/, 
    WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
    if (GetFocus()==m_URL)
    return 0;

Final Words

Please excuse me for any typing/grammar mistakes I might have made. English is not my natural language, so I make mistakes from time to time. You can use any part of the code posted with the article. If you use it within your software, please email me and let me know (don't worry - no money will be asked for it). Special thanks to Michael Dunn, Ed Gadziemski and PJ Naughter. I've learned a lot from their articles.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Gilad Novik
Web Developer
Israel Israel
Gilad was born accidently to a pair of old lesbians. His childhood was full of vibrators and drugs. Married without kids. Has 14 grandsons around the world, 4 crocodiles, 2 mushrooms and a green alien living behind the refrigerator.

Hobbies: Watching hardcore porn, sculpturing with snot, skydiving from stairs.

Check my Homepage for additional resources.

Quote: "There's always one more bug"

Comments and Discussions

Generalseems javascript doesn't work... Pin
lemonObrien7-Jul-08 19:07
MemberlemonObrien7-Jul-08 19:07 
QuestionHow do you change the font of dialog of VC IDE? Pin
zhangyuwu8-Jan-07 21:54
Memberzhangyuwu8-Jan-07 21:54 
QuestionProblems with VC++2005 Pin
Haydel27-May-06 4:01
MemberHaydel27-May-06 4:01 
AnswerRe: Problems with VC++2005 Pin
Gilad Novik27-May-06 7:38
MemberGilad Novik27-May-06 7:38 
GeneralRe: Problems with VC++2005 Pin
Haydel27-May-06 11:41
MemberHaydel27-May-06 11:41 
GeneralRe: Problems with VC++2005 Pin
Gilad Novik27-May-06 11:47
MemberGilad Novik27-May-06 11:47 
GeneralRe: Problems with VC++2005 Pin
Haydel27-May-06 12:13
MemberHaydel27-May-06 12:13 
GeneralRe: Problems with VC++2005 Pin
Gilad Novik27-May-06 13:00
MemberGilad Novik27-May-06 13:00 
GeneralRe: Problems with VC++2005 Pin
Satie Zhao30-May-06 23:06
MemberSatie Zhao30-May-06 23:06 
GeneralRe: Problems with VC++2005 Pin
ChesterFool5-Oct-06 13:44
MemberChesterFool5-Oct-06 13:44 
GeneralRe: Problems with VC++2005 Pin
LazyDave23-Jan-07 2:01
professionalLazyDave23-Jan-07 2:01 
QuestionThank you for your good program. Pin
urax4-May-06 9:15
Memberurax4-May-06 9:15 
AnswerRe: Thank you for your good program. Pin
Gilad Novik4-May-06 9:47
MemberGilad Novik4-May-06 9:47 
GeneralRe: Thank you for your good program. Pin
urax4-May-06 9:54
Memberurax4-May-06 9:54 
GeneralRe: Thank you for your good program. Pin
umeca747-Aug-08 5:20
Memberumeca747-Aug-08 5:20 
Generalproblem with VC7 and WTL7 Pin
daveice4-Sep-05 0:49
Memberdaveice4-Sep-05 0:49 
GeneralRe: problem with VC7 and WTL7 Pin
Gilad Novik27-May-06 7:41
MemberGilad Novik27-May-06 7:41 
GeneralDoesn't call OnBeforeNavigate2 Pin
erez_me25-Mar-05 14:44
Membererez_me25-Mar-05 14:44 
GeneralRe: Doesn't call OnBeforeNavigate2 Pin
davidbak21-Jul-05 14:25
Memberdavidbak21-Jul-05 14:25 
GeneralOnNewWindow2 Pin
hosch17914-Jul-04 5:26
Memberhosch17914-Jul-04 5:26 
GeneralSadly this won't build with ATL3 / VC++6 Pin
Mike Diack26-May-04 2:04
MemberMike Diack26-May-04 2:04 
GeneralWhat error do you get? Pin
Gilad Novik26-May-04 4:44
MemberGilad Novik26-May-04 4:44 
GeneralRe: What error do you get? Pin
Mike Diack30-May-04 21:44
MemberMike Diack30-May-04 21:44 
GeneralRe: What error do you get? Pin
Mike Diack30-May-04 21:48
MemberMike Diack30-May-04 21:48 
GeneralSDK Update Pin
Gilad Novik30-May-04 22:48
MemberGilad Novik30-May-04 22:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.