Click here to Skip to main content
15,879,474 members
Articles / Web Development / HTML

Programming IE- Creating a Browser using WebBrowser Control.

Rate me:
Please Sign up or sign in to vote.
3.17/5 (13 votes)
3 Oct 2008CPOL8 min read 75.8K   5K   47  
This article shows how to Create your own Browser using WebBrowser Control.
// Browser_ControlDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Browser_Control.h"
#include "Browser_ControlDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBrowser_ControlDlg dialog

CBrowser_ControlDlg::CBrowser_ControlDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CBrowser_ControlDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CBrowser_ControlDlg)
	m_sURL = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDI_ICON1);
}

void CBrowser_ControlDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CBrowser_ControlDlg)
	DDX_Control(pDX, IDC_URL, m_eURL);
	DDX_Control(pDX, IDC_ANIMATE1, m_Animate);
	DDX_Control(pDX, IDC_EXPLORER1, m_WebBrowser);
	DDX_Text(pDX, IDC_URL, m_sURL);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CBrowser_ControlDlg, CDialog)
	//{{AFX_MSG_MAP(CBrowser_ControlDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(ID_BACK, OnBack)
	ON_BN_CLICKED(ID_FORWARD, OnForward)
	ON_BN_CLICKED(ID_STOP, OnStop)
	ON_BN_CLICKED(ID_REFRESH, OnRefresh)
	ON_BN_CLICKED(ID_HOME, OnHome)
	ON_BN_CLICKED(ID_SEARCH, OnSearch)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBrowser_ControlDlg message handlers

BOOL CBrowser_ControlDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	CWnd *pWnd = GetDlgItem(IDC_URL);
		if (pWnd)
			pWnd->SetWindowText("www.google.com");
	m_WebBrowser.Navigate("http://www.google.com", NULL, NULL, NULL, NULL);
	m_Animate.Open("progress.avi");
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CBrowser_ControlDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CBrowser_ControlDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CBrowser_ControlDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}


BEGIN_EVENTSINK_MAP(CBrowser_ControlDlg, CDialog)
    //{{AFX_EVENTSINK_MAP(CBrowser_ControlDlg)
	ON_EVENT(CBrowser_ControlDlg, IDC_EXPLORER1, 106 /* DownloadBegin */, OnDownloadBeginExplorer1, VTS_NONE)
	ON_EVENT(CBrowser_ControlDlg, IDC_EXPLORER1, 104 /* DownloadComplete */, OnDownloadCompleteExplorer1, VTS_NONE)
	ON_EVENT(CBrowser_ControlDlg, IDC_EXPLORER1, 102 /* StatusTextChange */, OnStatusTextChangeExplorer1, VTS_BSTR)
	ON_EVENT(CBrowser_ControlDlg, IDC_EXPLORER1, 259 /* DocumentComplete */, OnDocumentCompleteExplorer1, VTS_DISPATCH VTS_PVARIANT)
	//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()

void CBrowser_ControlDlg::OnOK() 
{
	// TODO: Add extra validation here
	
	UpdateData(true);
	m_WebBrowser.Navigate(m_sURL , NULL, NULL, NULL, NULL);
	//OR you can also do the following
 	// CString str;
 	// m_eURL.GetWindowText(str);
	// m_WebBrowser.Navigate(m_sURL , NULL, NULL, NULL, NULL);
}

void CBrowser_ControlDlg::OnBack() 
{
	// TODO: Add your control notification handler code here
	m_WebBrowser.GoBack();
}

void CBrowser_ControlDlg::OnForward() 
{
	// TODO: Add your control notification handler code here
	m_WebBrowser.GoForward();
}

void CBrowser_ControlDlg::OnStop() 
{
	// TODO: Add your control notification handler code here
	m_WebBrowser.Stop();
}


void CBrowser_ControlDlg::OnRefresh() 
{
	// TODO: Add your control notification handler code here
	m_WebBrowser.Refresh();
}

void CBrowser_ControlDlg::OnHome() 
{
	// TODO: Add your control notification handler code here
	m_WebBrowser.GoHome();
}


void CBrowser_ControlDlg::OnSearch() 
{
	// TODO: Add your control notification handler code here
	m_WebBrowser.GoSearch();
}

void CBrowser_ControlDlg::OnDownloadBeginExplorer1() 
{
	// TODO: Add your control notification handler code here
	
	m_Animate.Play(0,-1,-1);

}

void CBrowser_ControlDlg::OnDownloadCompleteExplorer1() 
{
	// TODO: Add your control notification handler code here
 	m_Animate.Stop();
	m_Animate.Seek(0);
 
}

void CBrowser_ControlDlg::OnStatusTextChangeExplorer1(LPCTSTR Text) 
{
	// TODO: Add your control notification handler code here
		if (GetSafeHwnd())
	{
		CWnd *pWnd = GetDlgItem(IDC_STATUS_TEXT);
		if (pWnd)
			pWnd->SetWindowText(Text);
	}


}

 
void CBrowser_ControlDlg::OnDocumentCompleteExplorer1(LPDISPATCH pDisp, VARIANT FAR* URL) 
{
	// TODO: Add your control notification handler code here
	CString locURL;
	locURL=	m_WebBrowser.GetLocationURL();
	m_eURL.SetWindowText(locURL); //(CString(URL->bstrVal));
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions