BOOL CHtmlCtrl::CreateFromStatic(UINT nID, CWnd* pParent) { CStatic wndStatic; //静态控件对象; if (!wndStatic.SubclassDlgItem(nID, pParent)) return FALSE; // 获取静态控制的矩形区域并转换为父窗口的客户区坐标 CRect rc; wndStatic.GetWindowRect(&rc); pParent->ScreenToClient(&rc); wndStatic.DestroyWindow(); // 创建 HTML 控制 (CHtmlView) return (Create(NULL, // 类名; NULL, // 标题; (WS_CHILD | WS_VISIBLE ), // 风格; rc, // 矩形区域; pParent, // 父窗口; nID, // 控制的ID号; NULL)); //取消文档框架支持; } |
int CHtmlCtrl::OnMouseActivate(CWnd* pDesktopWnd, UINT nHitTest, UINT msg) { //旁路 CView 文档/框架 return CWnd::OnMouseActivate(pDesktopWnd, nHitTest, msg); } void CHtmlCtrl::OnDestroy() { if (m_pBrowserApp) { m_pBrowserApp->Release(); m_pBrowserApp = NULL; } CWnd::OnDestroy(); // 旁路 CView 文档/框架 } |
void CHtmlCtrl::OnBeforeNavigate2( LPCTSTR lpszURL, DWORD nFlags, LPCTSTR lpszTargetFrameName, CByteArray& baPostedData, LPCTSTR lpszHeaders, BOOL* pbCancel ) { const char APP_PROTOCOL[] = "app:"; int len = _tcslen(APP_PROTOCOL); if (_tcsnicmp(lpszURL, APP_PROTOCOL, len)==0) { OnAppCmd(lpszURL + len); *pbCancel = TRUE; } } |
void CHtmlCtrl::OnAppCmd(LPCTSTR lpszWhere){ // default: do nothing} |
此代码假设图像文件"pd.jpg"存在当前目录(页面文件所在目录)中。如果图像文件是作为资源存在EXE文件中,我们如何引用呢?方法一样,此时,我们必须告诉浏览器Web页面文件的位置。为此要在Web页面文件的开头加上如下代码:
void CHtmlCtrl::OnBeforeNavigate2( LPCTSTR lpszURL, ..., BOOL* pbCancel) { if (_tcscmp(lpszURL,_T("ok"))==0) { // "ok" clicked: *pbCancel=TRUE; // abort GetParent()->SendMessage(WM_COMMAND,IDOK); // will close dialog } } |
void CMyHtmlCtrl::OnAppCmd( LPCTSTR lpszWhere ) { if (_tcsicmp(lpszWhere, _T("ok"))==0) { GetParent()->SendMessage(WM_COMMAND,IDOK); } } |
二、编程步骤
1、启动Visual C++6.0,生成一个单文档的应用程序,命名为"ShowHtml";
2、修改程序中的"About"对话框资源,在其中放置一个Static控件,设置它的ID为IDC_HTMLVIEW;
3、向程序中添加HTML文件资源,其ID设置为"About.htm";
4、向程序中添加CHtmlCtrl、CMyHtmlCtrl类文件;
5、在CAbout类中增加一个CMyHtmlCtrl类的对象m_page,并使用CLASSWIZARD重载CAbout类的OnInitDialog()函数;
6、编译运行程序。
三、程序代码
////////////////////////////////////////////////////CHtmlCtrl类的头文件; #include "afxhtml.h" class CHtmlCtrl : public CHtmlView { public: CHtmlCtrl() { } ~CHtmlCtrl() { } //使CHtmlCtrl控件与静态控件建立关联; BOOL CreateFromStatic(UINT nID, CWnd* pParent); virtual void PostNcDestroy() { } //重载下面两个函数,旁路ChtmlView类的文档视图结构; afx_msg void OnDestroy(); afx_msg int OnMouseActivate(CWnd* pDesktopWnd, UINT nHitTest, UINT msg); // 实现"app"伪协议; virtual void OnBeforeNavigate2( LPCTSTR lpszURL, DWORD nFlags, LPCTSTR lpszTargetFrameName, CByteArray& baPostedData, LPCTSTR lpszHeaders, BOOL* pbCancel ); virtual void OnAppCmd(LPCTSTR lpszWhere); DECLARE_MESSAGE_MAP(); DECLARE_DYNAMIC(CHtmlCtrl) }; //////////////////////////////////////////////CHtmlCtrl类的实现文件; #include "StdAfx.h" #include "HtmlCtrl.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif IMPLEMENT_DYNAMIC(CHtmlCtrl, CHtmlView) BEGIN_MESSAGE_MAP(CHtmlCtrl, CHtmlView) ON_WM_DESTROY() ON_WM_MOUSEACTIVATE() END_MESSAGE_MAP() BOOL CHtmlCtrl::CreateFromStatic(UINT nID, CWnd* pParent) { CStatic wndStatic; if (!wndStatic.SubclassDlgItem(nID, pParent)) return FALSE; // 获取静态控件的尺寸,并销毁该控件的窗口; CRect rc; wndStatic.GetWindowRect(&rc); pParent->ScreenToClient(&rc); wndStatic.DestroyWindow(); // 创建一个HtmlView控件; return Create(NULL, // class name NULL, // title (WS_CHILD | WS_VISIBLE ), // style rc, // rectangle pParent, // parent nID, // control ID NULL); // frame/doc context not used } void CHtmlCtrl::OnDestroy() { if (m_pBrowserApp) {释放浏缆器的m_pBrowserApp成员变量; m_pBrowserApp->Release(); m_pBrowserApp = NULL; } CWnd::OnDestroy(); // bypass CView doc/frame stuff } int CHtmlCtrl::OnMouseActivate(CWnd* pDesktopWnd, UINT nHitTest, UINT msg) { //旁路文档视图结构; return CWnd::OnMouseActivate(pDesktopWnd, nHitTest, msg); } //////////////////实现"app"伪协议; void CHtmlCtrl::OnBeforeNavigate2( LPCTSTR lpszURL, DWORD nFlags, LPCTSTR lpszTargetFrameName, CByteArray& baPostedData, LPCTSTR lpszHeaders, BOOL* pbCancel ) { const char APP_PROTOCOL[] = "app:"; int len = _tcslen(APP_PROTOCOL); if (_tcsnicmp(lpszURL, APP_PROTOCOL, len)==0) { OnAppCmd(lpszURL + len); *pbCancel = TRUE; } } void CHtmlCtrl::OnAppCmd(LPCTSTR lpszWhere) { // default: do nothing } ///////////////////////////////////////////////////// class CMyHtmlCtrl : public CHtmlCtrl { virtual void OnAppCmd(LPCTSTR lpszWhere); }; /////////////////// 处理HTML文件上的 "app:ok"链接,关闭对话框; void CMyHtmlCtrl::OnAppCmd(LPCTSTR lpszWhere) { if (_tcsicmp(lpszWhere,_T("ok"))==0) { GetParent()->SendMessage(WM_COMMAND,IDOK); } } //////////////////////////////////////// class CAboutDlg : public CDialog { public: CAboutDlg(); CMyHtmlCtrl m_page; // 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) virtual BOOL OnInitDialog(); //}}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) //}}AFX_MSG_MAP END_MESSAGE_MAP() // App command to run the dialog void CShowHtmlApp::OnAppAbout() { CAboutDlg aboutDlg; aboutDlg.DoModal(); } BOOL CAboutDlg::OnInitDialog() { CDialog::OnInitDialog(); VERIFY(CDialog::OnInitDialog()); VERIFY(m_page.CreateFromStatic(IDC_HTMLVIEW, this)); m_page.LoadFromResource(_T("ABOUT.HTM")); return TRUE; } |