工学院实验报告
实验项目:Web浏览器应用程序(网络编程实验)
计算机科学与技术 专业 0801 班
姓 名: 于佳涛
学 号: 080102060051
指导老师: 赵福祥
2011年 11 月
实现目标:
使用CHtmlView类可以实现一个应用程序,使之具有Wed浏览器的功能,包括浏览网页、前进、后退、返回主页和搜索功能。
实验程序的技术要点:
(1)利用MFC AppWizard生成应用程序框架.
(2)掌握利用CHtmlView类开发Web客户机端程序的方法.
程序代码及运行结果:
1. 利用MFC AppWizard生成应用程序框架.
(1) 应用程序类:Cyujiatao0051App,对应yujiatao0051.h和yujiatao0051.cpp文件.
(2) 框架类:CMainFrame,对应MainFrm.h和MainFrm.cpp文件.
(3) 文档类:C yujiatao0051Doc,对应yujiatao0051Doc.h和yujiatao0051.cpp文件.
(4) HtmlView类:Cyujiatao0051View,对应yujiatao0051View.h和yujiatao0051.cpp文件.
2. 程序代码
(1) yujiatao0051View.cpp文件的实现
// yujiatao0051View.cpp : implementation of the C yujiatao0051View class
//
#include "stdafx.h"
#include " yujiatao0051.h"
#include " yujiatao0051Doc.h"
#include " yujiatao0051View.h"
#include "MainFrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// C yujiatao0051View
IMPLEMENT_DYNCREATE(CYujiatao0051View, CHtmlView)
BEGIN_MESSAGE_MAP(CYujiatao0051View, CHtmlView)
//{{AFX_MSG_MAP(CYujiatao0051View)
ON_COMMAND(ID_BACK, OnBack)
ON_COMMAND(ID_FOWARD, OnFoward)
ON_COMMAND(ID_HOMEPAGE, OnHomepage)
ON_COMMAND(ID_SEARCH, OnSearch)
ON_COMMAND(ID_STOP, OnStop)
ON_COMMAND(ID_UPDATE, OnUpdate)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CHtmlView::OnFilePrint)
END_MESSAGE_MAP()
// CYujiatao0051View construction/destruction
CYujiatao0051View::CYujiatao0051View()
{
// TODO: add construction code here
}
CYujiatao0051View::~CYujiatao0051View()
{
}
BOOL CYujiatao0051View::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CHtmlView::PreCreateWindow(cs);
}
// CYujiatao0051View drawing
void CYujiatao0051View::OnDraw(CDC* pDC)
{
CYujiatao0051Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
void CYujiatao0051View::OnInitialUpdate()
{
CHtmlView::OnInitialUpdate();
// TODO: This code navigates to a popular spot on the web.
// change the code to go where you'd like.
Navigate2(_T("http://www.microsoft.com/visualc/"),NULL,NULL);
}
// CYujiatao0051View printing
// CYujiatao0051View diagnostics
#ifdef _DEBUG
void CYujiatao0051View::AssertValid() const
{
CHtmlView::AssertValid();
}
void CYujiatao0051View::Dump(CDumpContext& dc) const
{
CHtmlView::Dump(dc);
}
CYujiatao0051Doc* CYujiatao0051View::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CYujiatao0051Doc)));
return (CYujiatao0051Doc*)m_pDocument;
}
#endif //_DEBUG
// CYujiatao0051View message handlers
void CYujiatao0051View::OnBack()
{
// TODO: Add your command handler code here
GoBack();
}
void CYujiatao0051View::OnFoward()
{
// TODO: Add your command handler code here
GoForward();
}
void CYujiatao0051View::OnHomepage()
{
// TODO: Add your command handler code here
GoHome();
}
void CYujiatao0051View::OnSearch()
{
// TODO: Add your command handler code here
GoSearch();
}
void CYujiatao0051View::OnStop()
{
// TODO: Add your command handler code here
Stop();
}
void CYujiatao0051View::OnUpdate()
{
// TODO: Add your command handler code here
Refresh();
}
void CYujiatao0051View::OnDocumentComplete(LPCTSTR lpszURL)
{
// TODO: Add your specialized code here and/or call the base class
((CMainFrame*)GetParentFrame())->SetPage(lpszURL);
//CHtmlView::OnDocumentComplete(lpszURL);
}
(2) MainFrm.h文件的实现
// MainFrm.h : interface of the CMainFrame class
#if !defined(AFX_MAINFRM_H__46D187BD_7941_4BD8_9652_A780FF979164__INCLUDED_)
#define AFX_MAINFRM_H__46D187BD_7941_4BD8_9652_A780FF979164__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CMainFrame : public CFrameWnd
{
protected: // create from serialization only
CMainFrame();
DECLARE_DYNCREATE(CMainFrame)
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMainFrame)
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CMainFrame();
void OnNew();
void SetPage(LPCTSTR lpszURL);
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
protected: // control bar embedded members
CStatusBar m_wndStatusBar;
CToolBar m_wndToolBar;
CReBar m_wndReBar;
CDialogBar m_wndDlgBar;
// Generated message map functions
protected:
//{{AFX_MSG(CMainFrame)
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
// NOTE - the ClassWizard will add and remove member functions here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_MAINFRM_H__46D187BD_7941_4BD8_9652_A780FF979164__INCLUDED_)
(3) MainFrm.cpp文件的实现
// MainFrm.cpp : implementation of the CMainFrame class
#include "stdafx.h"
#include "Yujiatao0051.h"
#include "Yujiatao0051View.h"
#include "MainFrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// CMainFrame
IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code !
ON_WM_CREATE()
//}}AFX_MSG_MAP
ON_COMMAND(IDOK,OnNew)
END_MESSAGE_MAP()
static UINT indicators[] =
{
ID_SEPARATOR, // status line indicator
ID_INDICATOR_CAPS,
ID_INDICATOR_NUM,
ID_INDICATOR_SCRL,
};
// CMainFrame construction/destruction
CMainFrame::CMainFrame()
{
// TODO: add member initialization code here
}
CMainFrame::~CMainFrame()
{
}
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
if (!m_wndToolBar.CreateEx(this) ||
!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
{
TRACE0("Failed to create toolbar\n");
return -1; // fail to create
}
if (!m_wndDlgBar.Create(this, IDR_MAINFRAME,
CBRS_ALIGN_TOP, AFX_IDW_DIALOGBAR))
{
TRACE0("Failed to create dialogbar\n");
return -1;// fail to create
}
if (!m_wndReBar.Create(this) ||
!m_wndReBar.AddBar(&m_wndToolBar) ||
!m_wndReBar.AddBar(&m_wndDlgBar))
{
TRACE0("Failed to create rebar\n");
return -1; // fail to create
}
if (!m_wndStatusBar.Create(this) ||
!m_wndStatusBar.SetIndicators(indicators,
sizeof(indicators)/sizeof(UINT)))
{
TRACE0("Failed to create status bar\n");
return -1; // fail to create
}
// TODO: Remove this if you don't want tool tips
m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() |
CBRS_TOOLTIPS | CBRS_FLYBY);
return 0;
}
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CFrameWnd::PreCreateWindow(cs) )
return FALSE;
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return TRUE;
}
void CMainFrame::OnNew()
{
CString pp;
//获得用户在地址栏中输入的URL
m_wndDlgBar.GetDlgItem(IDC_ADDR)->GetWindowText(pp);
//浏览制定的网页
((CYujiatao0051View*)GetActiveView())->Navigate(pp);
}
void CMainFrame::SetPage(LPCTSTR lpszURL)
{
m_wndDlgBar.GetDlgItem(IDC_ADDR)->SetWindowText(lpszURL);
}
// CMainFrame diagnostics
#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
CFrameWnd::AssertValid();
}
void CMainFrame::Dump(CDumpContext& dc) const
{
CFrameWnd::Dump(dc);
}
#endif //_DEBUG
// CMainFrame message handlers
展开阅读全文