利用Visual C++打造自己的IE浏览器
2010-06-29 20:42:23 来源:WEB开发网三、程序代码
//////////////////////////////////////////////
#if !defined(AFX_MAINFRM_H__47FF4081_CE1B_11D0_BEB6_00C04FC99F83__INCLUDED_)
#define AFX_MAINFRM_H__47FF4081_CE1B_11D0_BEB6_00C04FC99F83__INCLUDED_
#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
class CMainFrame : public CFrameWnd
{
protected: // create from serialization only
CMainFrame();
DECLARE_DYNCREATE(CMainFrame)
// Attributes
public:
CStringArray m_astrFavoriteURLs;
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMainFrame)
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CMainFrame();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
void SetAddress(LPCTSTR lpszUrl);
void StartAnimation();
int BuildFavoritesMenu(LPCTSTR pszPath, int nStartPos, CMenu* pMenu);
protected: // control bar embedded members
CStatusBar m_wndStatusBar;
CToolBar m_wndToolBar;
CReBar m_wndReBar;
// Generated message map functions
protected:
CComboBoxEx m_wndAddress;
CAnimateCtrl m_wndAnimate;
//{{AFX_MSG(CMainFrame)
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
//}}AFX_MSG
afx_msg void DoNothing();
afx_msg void OnNewAddress();
afx_msg void OnNewAddressEnter();
afx_msg void OnFavorite(UINT nID);
afx_msg void OnDropDown(NMHDR* pNotifyStruct, LRESULT* pResult);
DECLARE_MESSAGE_MAP()
};
#endif
///////////////////// MainFrm.cpp : implementation of the CMainFrame class
#include "stdafx.h"
#include "mfcie.h"
#include <wininet.h>
#include "MainFrm.h"
#include "mfcieDoc.h"
#include "mfcieVw.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
ON_WM_CREATE()
//}}AFX_MSG_MAP
ON_CBN_SELENDOK(AFX_IDW_TOOLBAR + 1,OnNewAddress)
ON_COMMAND_RANGE(0xe00, 0xfff, OnFavorite)
ON_COMMAND(IDOK, OnNewAddressEnter)
ON_NOTIFY(TBN_DROPDOWN, AFX_IDW_TOOLBAR, OnDropDown)
ON_COMMAND(ID_FAVORITES_DROPDOWN, DoNothing)
ON_COMMAND(ID_FONT_DROPDOWN, DoNothing)
END_MESSAGE_MAP()
static UINT indicators[] =
{
ID_SEPARATOR, // status line indicator
ID_INDICATOR_CAPS,
ID_INDICATOR_NUM,
ID_INDICATOR_SCRL,
};
CMainFrame::CMainFrame()
{
}
CMainFrame::~CMainFrame()
{
}
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
CImageList img;
CString str;
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
if (!m_wndReBar.Create(this))
{
TRACE0("Failed to create rebarn");
return -1; // fail to create
}
if (!m_wndToolBar.CreateEx(this))
{
TRACE0("Failed to create toolbarn");
return -1; // fail to create
}
// set up toolbar properties
m_wndToolBar.GetToolBarCtrl().SetButtonWidth(50, 150);
m_wndToolBar.GetToolBarCtrl().SetExtendedStyle(TBSTYLE_EX_DRAWDDARROWS);
img.Create(IDB_HOTTOOLBAR, 22, 0, RGB(255, 0, 255));
m_wndToolBar.GetToolBarCtrl().SetHotImageList(&img);
img.Detach();
img.Create(IDB_COLDTOOLBAR, 22, 0, RGB(255, 0, 255));
m_wndToolBar.GetToolBarCtrl().SetImageList(&img);
img.Detach();
m_wndToolBar.ModifyStyle(0, TBSTYLE_FLAT | TBSTYLE_TRANSPARENT);
m_wndToolBar.SetButtons(NULL, 9);
// set up each toolbar button
m_wndToolBar.SetButtonInfo(0, ID_GO_BACK, TBSTYLE_BUTTON, 0);
str.LoadString(IDS_BACK);
m_wndToolBar.SetButtonText(0, str);
m_wndToolBar.SetButtonInfo(1, ID_GO_FORWARD, TBSTYLE_BUTTON, 1);
str.LoadString(IDS_FORWARD);
m_wndToolBar.SetButtonText(1, str);
m_wndToolBar.SetButtonInfo(2, ID_VIEW_STOP, TBSTYLE_BUTTON, 2);
str.LoadString(IDS_STOP);
m_wndToolBar.SetButtonText(2, str);
m_wndToolBar.SetButtonInfo(3, ID_VIEW_REFRESH, TBSTYLE_BUTTON, 3);
str.LoadString(IDS_REFRESH);
m_wndToolBar.SetButtonText(3, str);
m_wndToolBar.SetButtonInfo(4, ID_GO_START_PAGE, TBSTYLE_BUTTON, 4);
str.LoadString(IDS_HOME);
m_wndToolBar.SetButtonText(4, str);
m_wndToolBar.SetButtonInfo(5, ID_GO_SEARCH_THE_WEB, TBSTYLE_BUTTON, 5);
str.LoadString(IDS_SEARCH);
m_wndToolBar.SetButtonText(5, str);
m_wndToolBar.SetButtonInfo(6, ID_FAVORITES_DROPDOWN, TBSTYLE_BUTTON |
TBSTYLE_DROPDOWN, 6);
str.LoadString(IDS_FAVORITES);
m_wndToolBar.SetButtonText(6, str);
m_wndToolBar.SetButtonInfo(7, ID_FILE_PRINT, TBSTYLE_BUTTON, 7);
str.LoadString(IDS_PRINT);
m_wndToolBar.SetButtonText(7, str);
m_wndToolBar.SetButtonInfo(8, ID_FONT_DROPDOWN, TBSTYLE_BUTTON |
TBSTYLE_DROPDOWN, 8);
str.LoadString(IDS_FONT);
m_wndToolBar.SetButtonText(8, str);
CRect rectToolBar;
// set up toolbar button sizes
m_wndToolBar.GetItemRect(0, &rectToolBar);
m_wndToolBar.SetSizes(rectToolBar.Size(), CSize(30,20));
// create a combo box for the address bar
if (!m_wndAddress.Create(CBS_DROPDOWN | WS_CHILD, CRect(0, 0, 100, 120),
this, AFX_IDW_TOOLBAR + 1))
{
TRACE0("Failed to create comboboxn");
return -1; // fail to create
}
// create the animation control
m_wndAnimate.Create(WS_CHILD | WS_VISIBLE, CRect(0, 0, 10, 10),
this, AFX_IDW_TOOLBAR + 2);
m_wndAnimate.Open(IDR_MFCAVI);
// add the toolbar, animation, and address bar to the rebar
m_wndReBar.AddBar(&m_wndToolBar);
m_wndReBar.AddBar(&m_wndAnimate, NULL, NULL, RBBS_FIXEDSIZE | RBBS_FIXEDBMP);
str.LoadString(IDS_ADDRESS);
m_wndReBar.AddBar(&m_wndAddress, str, NULL, RBBS_FIXEDBMP | RBBS_BREAK);
m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() |
CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_FIXED);
if (!m_wndStatusBar.Create(this) || !m_wndStatusBar.SetIndicators(indicators,
sizeof(indicators)/sizeof(UINT)))
{
TRACE0("Failed to create status barn");
return -1; // fail to create
}
// set up Favorites menu
TCHAR sz[MAX_PATH];
TCHAR szPath[MAX_PATH];
HKEY hKey;
DWORD dwSize;
CMenu* pMenu;
// first get rid of bogus submenu items.
pMenu = GetMenu()->GetSubMenu(3);
while(pMenu->DeleteMenu(0, MF_BYPOSITION));
// find out from the registry where the favorites are located.
if(RegOpenKey(HKEY_CURRENT_USER, _T("Software\Microsoft\Windows\CurrentVersion\
Explorer\User Shell Folders"), &hKey) != ERROR_SUCCESS)
{
TRACE0("Favorites folder not foundn");
return 0;
}
dwSize = sizeof(sz);
RegQueryValueEx(hKey, _T("Favorites"), NULL, NULL, (LPBYTE)sz, &dwSize);
ExpandEnvironmentStrings(sz, szPath, MAX_PATH);
RegCloseKey(hKey);
BuildFavoritesMenu(szPath, 0, pMenu);
return 0;
}
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
cs.lpszClass = AfxRegisterWndClass(0);
return CFrameWnd::PreCreateWindow(cs);
}
#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
CFrameWnd::AssertValid();
}
void CMainFrame::Dump(CDumpContext& dc) const
{
CFrameWnd::Dump(dc);
}
#endif //_DEBUG
int CMainFrame::BuildFavoritesMenu(LPCTSTR pszPath, int nStartPos, CMenu* pMenu)
{
CString strPath(pszPath);
CString strPath2;
CString str;
WIN32_FIND_DATA wfd;
HANDLE h;
int nPos;
int nEndPos;
int nNewEndPos;
int nLastDir;
TCHAR buf[INTERNET_MAX_PATH_LENGTH];
CStringArray astrFavorites;
CStringArray astrDirs;
CMenu* pSubMenu;
// make sure there’s a trailing backslash
if(strPath[strPath.GetLength() - 1] != _T(’\’))
strPath += _T(’\’);
strPath2 = strPath;
strPath += "*.*";
// now scan the directory, first for .URL files and then for subdirectories
// that may also contain .URL files
h = FindFirstFile(strPath, &wfd);
if(h != INVALID_HANDLE_VALUE)
{
nEndPos = nStartPos;
do
{
if((wfd.dwFileAttributes & (FILE_ATTRIBUTE_DIRECTORY|
FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM))==0)
{
str = wfd.cFileName;
if(str.Right(4) == _T(".url"))
{
// an .URL file is formatted just like an .INI file, so we can
// use GetPrivateProfileString() to get the information we want
::GetPrivateProfileString(_T("InternetShortcut"),
_T("URL"), _T(""), buf, INTERNET_MAX_PATH_LENGTH, strPath2 + str);
str = str.Left(str.GetLength() - 4);
// scan through the array and perform an insertion sort
// to make sure the menu ends up in alphabetic order
for(nPos = nStartPos ; nPos < nEndPos ; ++nPos)
{
if(str.CompareNoCase(astrFavorites[nPos]) < 0)
break;
}
astrFavorites.InsertAt(nPos, str);
m_astrFavoriteURLs.InsertAt(nPos, buf);
++nEndPos;
}
}
} while(FindNextFile(h, &wfd));
FindClose(h);
// Now add these items to the menu
for(nPos = nStartPos ; nPos < nEndPos ; ++nPos)
{
pMenu->AppendMenu(MF_STRING | MF_ENABLED, 0xe00 + nPos,
astrFavorites[nPos]);
}
// now that we’ve got all the .URL files, check the subdirectories for more
nLastDir = 0;
h = FindFirstFile(strPath, &wfd);
ASSERT(h != INVALID_HANDLE_VALUE);
do
{
if(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
// ignore the current and parent directory entries
if(lstrcmp(wfd.cFileName, _T(".")) == 0 || lstrcmp(wfd.cFileName,
_T("..")) == 0)
continue;
for(nPos = 0 ; nPos < nLastDir ; ++nPos)
{
if(astrDirs[nPos].CompareNoCase(wfd.cFileName) > 0)
break;
}
pSubMenu = new CMenu;
pSubMenu->CreatePopupMenu();
// call this function recursively.
nNewEndPos = BuildFavoritesMenu(strPath2 + wfd.cFileName,
nEndPos, pSubMenu);
if(nNewEndPos != nEndPos)
{
// only intert a submenu if there are in fact .URL files in the subdirectory
nEndPos = nNewEndPos;
pMenu->InsertMenu(nPos, MF_BYPOSITION | MF_POPUP | MF_STRING,
(UINT)pSubMenu->m_hMenu, wfd.cFileName);
pSubMenu->Detach();
astrDirs.InsertAt(nPos, wfd.cFileName);
++nLastDir;
}
delete pSubMenu;
}
} while(FindNextFile(h, &wfd));
FindClose(h);
}
return nEndPos;
}
void CMainFrame::OnFavorite(UINT nID)
{
((CMfcieView*)GetActiveView())->Navigate2(m_astrFavoriteURLs[nID-0xe00],
0, NULL);
}
void CMainFrame::SetAddress(LPCTSTR lpszUrl)
{
// This is called when the browser has completely loaded the new location,
// so make sure the text in the address bar is up to date and stop the
// animation.
m_wndAddress.SetWindowText(lpszUrl);
m_wndAnimate.Stop();
m_wndAnimate.Seek(0);
}
void CMainFrame::StartAnimation()
{
// Start the animation. This is called when the browser begins to
// navigate to a new location
m_wndAnimate.Play(0, -1, -1);
}
void CMainFrame::OnNewAddress()
{
// gets called when an item in the Address combo box is selected
// just navigate to the newly selected location.
CString str;
m_wndAddress.GetLBText(m_wndAddress.GetCurSel(), str);
((CMfcieView*)GetActiveView())->Navigate2(str, 0, NULL);
}
void CMainFrame::OnNewAddressEnter()
{
// gets called when an item is entered manually into the edit box portion
// of the Address combo box.
// navigate to the newly selected location and also add this address to the
// list of addresses in the combo box.
CString str;
m_wndAddress.GetEditCtrl()->GetWindowText(str);
((CMfcieView*)GetActiveView())->Navigate2(str, 0, NULL);
COMBOBOXEXITEM item;
item.mask = CBEIF_TEXT;
item.iItem = -1;
item.pszText = (LPTSTR)(LPCTSTR)str;
m_wndAddress.InsertItem(&item);
}
void CMainFrame::DoNothing()
{
// this is here only so that the toolbar buttons for the dropdown menus
// will have a callback, and thus will not be disabled.
}
void CMainFrame::OnDropDown(NMHDR* pNotifyStruct, LRESULT* pResult)
{
// this function handles the dropdown menus from the toolbar
NMTOOLBAR* pNMToolBar = (NMTOOLBAR*)pNotifyStruct;
CRect rect;
// translate the current toolbar item rectangle into screen coordinates
// so that we’ll know where to pop up the menu
m_wndToolBar.GetToolBarCtrl().GetRect(pNMToolBar->iItem, &rect);
rect.top = rect.bottom;
::ClientToScreen(pNMToolBar->hdr.hwndFrom, &rect.TopLeft());
if(pNMToolBar->iItem == ID_FONT_DROPDOWN)
{
CMenu menu;
CMenu* pPopup;
// the font popup is stored in a resource
menu.LoadMenu(IDR_FONT_POPUP);
pPopup = menu.GetSubMenu(0);
pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON,
rect.left, rect.top + 1, AfxGetMainWnd());
}
else if(pNMToolBar->iItem == ID_FAVORITES_DROPDOWN)
{
CMenu* pPopup;
// for the favorties popup, just steal the menu from the main window
pPopup = GetMenu()->GetSubMenu(3);
pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON,
rect.left, rect.top + 1, AfxGetMainWnd());
}
*pResult = TBDDRET_DEFAULT;
}
///////////////////////////////////
#if !defined(AFX_MFCIEVIEW_H__47FF4085_CE1B_11D0_BEB6_00C04FC99F83__INCLUDED_)
#define AFX_MFCIEVIEW_H__47FF4085_CE1B_11D0_BEB6_00C04FC99F83__INCLUDED_
#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
class CMfcieView : public CHtmlView
{
protected: // create from serialization only
CMfcieView();
DECLARE_DYNCREATE(CMfcieView)
// Attributes
public:
CMfcieDoc* GetDocument();
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMfcieView)
public:
virtual void OnDraw(CDC* pDC); // overridden to draw this view
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
virtual void OnInitialUpdate();
//}}AFX_VIRTUAL
void OnTitleChange(LPCTSTR lpszText);
void OnDocumentComplete(LPCTSTR lpszUrl);
void OnBeforeNavigate2(LPCTSTR lpszURL, DWORD nFlags,
LPCTSTR lpszTargetFrameName, CByteArray& baPostedData,
LPCTSTR lpszHeaders, BOOL* pbCancel);
// Implementation
public:
virtual ~CMfcieView();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
protected:
// Generated message map functions
protected:
//{{AFX_MSG(CMfcieView)
afx_msg void OnGoBack();
afx_msg void OnGoForward();
afx_msg void OnGoSearchTheWeb();
afx_msg void OnGoStartPage();
afx_msg void OnViewStop();
afx_msg void OnViewRefresh();
afx_msg void OnHelpWebTutorial();
afx_msg void OnHelpOnlineSupport();
afx_msg void OnHelpMicrosoftOnTheWebFreeStuff();
afx_msg void OnHelpMicrosoftOnTheWebFrequentlyAskedQuestions();
afx_msg void OnHelpMicrosoftOnTheWebGetFasterInternetAccess();
afx_msg void OnHelpMicrosoftOnTheWebMicrosoftHomePage();
afx_msg void OnHelpMicrosoftOnTheWebSearchTheWeb();
afx_msg void OnHelpMicrosoftOnTheWebSendFeedback();
afx_msg void OnHelpMicrosoftOnTheWebInternetStartPage();
afx_msg void OnHelpMicrosoftOnTheWebBestOfTheWeb();
afx_msg void OnViewFontsLargest();
afx_msg void OnViewFontsLarge();
afx_msg void OnViewFontsMedium();
afx_msg void OnViewFontsSmall();
afx_msg void OnViewFontsSmallest();
afx_msg void OnFileOpen();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
#ifndef _DEBUG // debug version in mfcieView.cpp
inline CMfcieDoc* CMfcieView::GetDocument()
{ return (CMfcieDoc*)m_pDocument; }
#endif
#endif
////////////////////////////////////////////////
#include "stdafx.h"
#include "mfcie.h"
#include "MainFrm.h"
#include "mfcieDoc.h"
#include "mfcieVw.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
////////////////////////////////////// CMfcieView
IMPLEMENT_DYNCREATE(CMfcieView, CHtmlView)
BEGIN_MESSAGE_MAP(CMfcieView, CHtmlView)
//{{AFX_MSG_MAP(CMfcieView)
ON_COMMAND(ID_GO_BACK, OnGoBack)
ON_COMMAND(ID_GO_FORWARD, OnGoForward)
ON_COMMAND(ID_GO_SEARCH_THE_WEB, OnGoSearchTheWeb)
ON_COMMAND(ID_GO_START_PAGE, OnGoStartPage)
ON_COMMAND(ID_VIEW_STOP, OnViewStop)
ON_COMMAND(ID_VIEW_REFRESH, OnViewRefresh)
ON_COMMAND(ID_HELP_WEB_TUTORIAL, OnHelpWebTutorial)
ON_COMMAND(ID_HELP_ONLINE_SUPPORT, OnHelpOnlineSupport)
ON_COMMAND(ID_HELP_MICROSOFT_ON_THE_WEB_FREE_STUFF, OnHelpMicrosoftOnTheWebFreeStuff)
ON_COMMAND(ID_HELP_MICROSOFT_ON_THE_WEB_FREQUENTLY_ASKED_QUESTIONS, OnHelpMicrosoftOnTheWebFrequentlyAskedQuestions)
ON_COMMAND(ID_HELP_MICROSOFT_ON_THE_WEB_GET_FASTER_INTERNET_ACCESS, OnHelpMicrosoftOnTheWebGetFasterInternetAccess)
ON_COMMAND(ID_HELP_MICROSOFT_ON_THE_WEB_MICROSOFT_HOME_PAGE, OnHelpMicrosoftOnTheWebMicrosoftHomePage)
ON_COMMAND(ID_HELP_MICROSOFT_ON_THE_WEB_SEARCH_THE_WEB, OnHelpMicrosoftOnTheWebSearchTheWeb)
ON_COMMAND(ID_HELP_MICROSOFT_ON_THE_WEB_SEND_FEEDBACK, OnHelpMicrosoftOnTheWebSendFeedback)
ON_COMMAND(ID_HELP_MICROSOFT_ON_THE_WEB_INTERNET_START_PAGE, OnHelpMicrosoftOnTheWebInternetStartPage)
ON_COMMAND(ID_HELP_MICROSOFT_ON_THE_WEB_BEST_OF_THE_WEB, OnHelpMicrosoftOnTheWebBestOfTheWeb)
ON_COMMAND(ID_VIEW_FONTS_LARGEST, OnViewFontsLargest)
ON_COMMAND(ID_VIEW_FONTS_LARGE, OnViewFontsLarge)
ON_COMMAND(ID_VIEW_FONTS_MEDIUM, OnViewFontsMedium)
ON_COMMAND(ID_VIEW_FONTS_SMALL, OnViewFontsSmall)
ON_COMMAND(ID_VIEW_FONTS_SMALLEST, OnViewFontsSmallest)
ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
CMfcieView::CMfcieView()
{
}
CMfcieView::~CMfcieView()
{
}
BOOL CMfcieView::PreCreateWindow(CREATESTRUCT& cs)
{
cs.lpszClass = AfxRegisterWndClass(0);
return CView::PreCreateWindow(cs);
}
void CMfcieView::OnDraw(CDC* pDC)
{
CMfcieDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
}
#ifdef _DEBUG
void CMfcieView::AssertValid() const
{
CView::AssertValid();
}
void CMfcieView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CMfcieDoc* CMfcieView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMfcieDoc)));
return (CMfcieDoc*)m_pDocument;
}
#endif //_DEBUG
void CMfcieView::OnTitleChange(LPCTSTR lpszText)
{
// this will change the main frame’s title bar
if (m_pDocument != NULL)
m_pDocument->SetTitle(lpszText);
}
void CMfcieView::OnDocumentComplete(LPCTSTR lpszUrl)
{
// make sure the main frame has the new URL. This call also stops the animation
((CMainFrame*)GetParentFrame())->SetAddress(lpszUrl);
}
void CMfcieView::OnInitialUpdate()
{
// Go to the home page initially
CHtmlView::OnInitialUpdate();
GoHome();
}
void CMfcieView::OnBeforeNavigate2(LPCTSTR /*lpszURL*/, DWORD /*nFlags*/,
LPCTSTR /*lpszTargetFrameName*/, CByteArray& /*baPostedData*/,
LPCTSTR /*lpszHeaders*/, BOOL* /*pbCancel*/)
{
// start the animation so that is plays while the new page is being loaded
((CMainFrame*)GetParentFrame())->StartAnimation();
}
// these are all simple one-liners to do simple controlling of the browser
void CMfcieView::OnGoBack()
{
GoBack();
}
void CMfcieView::OnGoForward()
{
GoForward();
}
void CMfcieView::OnGoSearchTheWeb()
{
GoSearch();
}
void CMfcieView::OnGoStartPage()
{
GoHome();
}
void CMfcieView::OnViewStop()
{
Stop();
}
void CMfcieView::OnViewRefresh()
{
Refresh();
}
// these all go to specific web pages, just like Internet Explorer’s help menu
void CMfcieView::OnHelpWebTutorial()
{
CString str;
str.LoadString(IDS_WEB_TUTORIAL);
Navigate2(str, 0, NULL);
}
void CMfcieView::OnHelpOnlineSupport()
{
CString str;
str.LoadString(IDS_ONLINE_SUPPORT);
Navigate2(str, 0, NULL);
}
void CMfcieView::OnHelpMicrosoftOnTheWebBestOfTheWeb()
{
CString str;
str.LoadString(IDS_BEST_OF_THE_WEB);
Navigate2(str, 0, NULL);
}
void CMfcieView::OnHelpMicrosoftOnTheWebFreeStuff()
{
CString str;
str.LoadString(IDS_FREE_STUFF);
Navigate2(str, 0, NULL);
}
void CMfcieView::OnHelpMicrosoftOnTheWebFrequentlyAskedQuestions()
{
CString str;
str.LoadString(IDS_FREQUENTLY_ASKED_QUESTIONS);
Navigate2(str, 0, NULL);
}
void CMfcieView::OnHelpMicrosoftOnTheWebGetFasterInternetAccess()
{
CString str;
str.LoadString(IDS_GET_FASTER_INTERNET_ACCESS);
Navigate2(str, 0, NULL);
}
void CMfcieView::OnHelpMicrosoftOnTheWebMicrosoftHomePage()
{
CString str;
str.LoadString(IDS_MICROSOFT_HOME_PAGE);
Navigate2(str, 0, NULL);
}
void CMfcieView::OnHelpMicrosoftOnTheWebSearchTheWeb()
{
CString str;
str.LoadString(IDS_SEARCH_THE_WEB);
Navigate2(str, 0, NULL);
}
void CMfcieView::OnHelpMicrosoftOnTheWebSendFeedback()
{
CString str;
str.LoadString(IDS_SEND_FEEDBACK);
Navigate2(str, 0, NULL);
}
void CMfcieView::OnHelpMicrosoftOnTheWebInternetStartPage()
{
CString str;
str.LoadString(IDS_INTERNET_START_PAGE);
Navigate2(str, 0, NULL);
}
// these functions control the font size. There is no explicit command in the
// CHtmlView class to do this, but we can do it by using the ExecWB() function.
void CMfcieView::OnViewFontsLargest()
{
COleVariant vaZoomFactor(4l);
ExecWB(OLECMDID_ZOOM, OLECMDEXECOPT_DONTPROMPTUSER,
&vaZoomFactor, NULL);
}
void CMfcieView::OnViewFontsLarge()
{
COleVariant vaZoomFactor(3l);
ExecWB(OLECMDID_ZOOM, OLECMDEXECOPT_DONTPROMPTUSER,
&vaZoomFactor, NULL);
}
void CMfcieView::OnViewFontsMedium()
{
COleVariant vaZoomFactor(2l);
ExecWB(OLECMDID_ZOOM, OLECMDEXECOPT_DONTPROMPTUSER,
&vaZoomFactor, NULL);
}
void CMfcieView::OnViewFontsSmall()
{
COleVariant vaZoomFactor(1l);
ExecWB(OLECMDID_ZOOM, OLECMDEXECOPT_DONTPROMPTUSER,
&vaZoomFactor, NULL);
}
void CMfcieView::OnViewFontsSmallest()
{
COleVariant vaZoomFactor(0l);
ExecWB(OLECMDID_ZOOM, OLECMDEXECOPT_DONTPROMPTUSER,
&vaZoomFactor, NULL);
}
// This demonstrates how we can use the Navigate2() function to load local files
// including local HTML pages, GIFs, AIFF files, etc.
void CMfcieView::OnFileOpen()
{
CString str;
str.LoadString(IDS_FILETYPES);
CFileDialog fileDlg(TRUE, NULL, NULL, OFN_HIDEREADONLY, str);
if(fileDlg.DoModal() == IDOK)
Navigate2(fileDlg.GetPathName(), 0, NULL);
}
四、小结
上面的代码相对较多,对某些函数的使用不清楚的话,请参考MSDN,它包含了高级界面处理、注册表的操作等内容,也许刚开始看起来可能感到有些困难,但是如果读者朋友细细品味的话,一定可以学到一些东西,对今后程序的界面开发有所帮助。
更多精彩
赞助商链接