previewview.cpp

来自「深入浅出Visual C++入门进阶与应用实例 随书光盘 作者 何志丹」· C++ 代码 · 共 158 行

CPP
158
字号
// PreViewView.cpp : implementation file
//

#include "stdafx.h"
#include "PreViewView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CPreViewView

IMPLEMENT_DYNCREATE(CPreViewView, CHtmlView)

CPreViewView::CPreViewView()
{
	//{{AFX_DATA_INIT(CPreViewView)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}

CPreViewView::~CPreViewView()
{
}

void CPreViewView::DoDataExchange(CDataExchange* pDX)
{
	CHtmlView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPreViewView)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CPreViewView, CHtmlView)
	//{{AFX_MSG_MAP(CPreViewView)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPreViewView diagnostics

#ifdef _DEBUG
void CPreViewView::AssertValid() const
{
	CHtmlView::AssertValid();
}

void CPreViewView::Dump(CDumpContext& dc) const
{
	CHtmlView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CPreViewView message handlers

CArticleDoc* CPreViewView::GetDocument( ) const
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CArticleDoc))) ;
	return (CArticleDoc*)m_pDocument ;
}



void CPreViewView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
{
	OnPreView();
}

bool CPreViewView::OnPreView()
{
	CWnd * pParentWnd = GetParent();
	ASSERT(NULL != pParentWnd);

	CArticleDoc * pDoc = GetDocument() ;
	if(NULL == pDoc)
	{
		ASSERT(false);
		Navigate("about:blank");
		if(NULL != pParentWnd)
			pParentWnd->SetWindowText("空");
		return false ;
	}

	CArticleData * pData = pDoc->m_mainData.GetCurArticleData();
	if(NULL == pData )
	{		
		Navigate("about:blank");
		if(NULL != pParentWnd)
			pParentWnd->SetWindowText("空");
		return false ;
	}

	
	//从模板中读取内容
	CString strTemContent ;	
	CString strTemFileName ;
	try
	{		
		strTemFileName  = ::GetExePath() + "PreViewTem.htm" ;
		CFile file ;
		file.Open(strTemFileName,CFile::modeRead);
		int nFileLength = file.GetLength();
		file.Read(strTemContent.GetBuffer(nFileLength+1),nFileLength+1);
		strTemContent.ReleaseBuffer();
		file.Close();
	}
	catch(CFileException * e)
	{
		CString strMess ;
		strMess.Format("文件%s操作出错,错误代码%d",(LPCTSTR)strTemFileName,e->m_cause);
		ASSERT(false);
		AfxMessageBox(strMess);
	}

	//生成预览内容
	strTemContent.Replace("#Topic#",pData->m_topic);
	CArticleElements arEles  ;
	arEles = pData->m_content ;
	strTemContent.Replace("#Content#",arEles.MakeTextWithHtmlLink());

	char szTmpPath[MAX_PATH + 1];
	GetTempPath(MAX_PATH,szTmpPath);
	char szTmpFileName[MAX_PATH + 1 ] ;
	GetTempFileName(szTmpPath,"htm",MAX_PATH,szTmpFileName);
	//GetTempPath(255,szTmpPath);		
	CString strTmpFileName = szTmpFileName ;
	//写进文件
	try
	{	
		Navigate("about:blank");
		CFile file ;
		file.Open(strTmpFileName,CFile::modeCreate | CFile::modeWrite );
		file.Write(strTemContent,strTemContent.GetLength());
		file.Close();
	}
	catch(CFileException * e)
	{
		CString strMess ;
		strMess.Format("文件%s操作出错,错误代码%d",(LPCTSTR)strTmpFileName,e->m_cause);
		ASSERT(false);
		AfxMessageBox(strMess);
	}

	strTmpFileName = "\\\\\\" + strTmpFileName ;	
	Navigate(strTmpFileName);	
	if(NULL != pParentWnd)
		pParentWnd->SetWindowText(pData->m_topic);

	return true;
}


⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?