本篇文章主要介绍在对话框上显示html或者mht文件。
首先,新建对话框应用程序
添加Static控件到对话框中,此区域用于显示html文件。
然后,添加相应函数处理类
// HtmlViewDemoDlg.h : 头文件
//
#pragma once
#include "afxwin.h"
#include <afxhtml.h>
// CHtmlViewDemoDlg 对话框
class CHtmlViewDemoDlg : public CDialog
{
// 构造
public:
CHtmlViewDemoDlg(CWnd* pParent = NULL); // 标准构造函数
// 对话框数据
enum { IDD = IDD_HTMLVIEWDEMO_DIALOG };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 实现
protected:
HICON m_hIcon;
// 生成的消息映射函数
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
public:
CString m_sAdviceFilePathName;
CHtmlView* m_pDisplayView;
CStatic m_stcAdviceArea;
void MyInitDialog(void);
void GetPath(void);
void InitMhtFile(void);
};
void CHtmlViewDemoDlg::MyInitDialog(void)
{
GetPath();
InitMhtFile();
}
void CHtmlViewDemoDlg::GetPath(void)
{
CString sCurrentPath = _T("");
WCHAR Path[270];
DWORD len = 0;
DWORD i=0;
len = GetCurrentDirectory(270,Path);
for (i=0; i<len; i++)
{
sCurrentPath = sCurrentPath + CString(Path[i]);
}
sCurrentPath = sCurrentPath + _T("\\test.mht");
m_pDisplayView = NULL;
m_sAdviceFilePathName = _T("C:\\Users\\SONGLI\\Desktop\\HtmlViewDemo\\HtmlViewDemo\\file\\test.mht");//html mht
m_sAdviceFilePathName = sCurrentPath;
}
void CHtmlViewDemoDlg::InitMhtFile(void)
{
RECT rect;
m_stcAdviceArea.GetWindowRect(&rect);
ScreenToClient(&rect);
if(m_pDisplayView)
{}
else
{
CCreateContext pContext;
CWnd* pFrameWnd = this;
pContext.m_pCurrentDoc = NULL;// new CMyDocument;
pContext.m_pNewViewClass = RUNTIME_CLASS(CHtmlView);
m_pDisplayView =(CHtmlView *) ((CFrameWnd*)pFrameWnd)->CreateView(&pContext);
ASSERT(m_pDisplayView);
m_pDisplayView->ShowWindow(SW_NORMAL);
m_pDisplayView->MoveWindow(&rect);
}
if(_T("") == m_sAdviceFilePathName)
{
m_pDisplayView->Navigate(_T("about:blank"));
}
else
{
m_pDisplayView->Navigate(m_sAdviceFilePathName);
}
}
最后,运行效果
如果路径不正确或者相应的文件不存在的显示效果如下