⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chm readerview.cpp

📁 chm文件阅读器.开发工具EVC 支持ARM处理器和 模拟器环境 可直接编译通过 基于PPC的 也可以编译为WINCE下的程序
💻 CPP
字号:
// CHMReaderView.cpp : implementation of the CCHMReaderView class
//

#include "stdafx.h"
#include "CHM Reader.h"
#include "CHM ReaderDoc.h"
#include "CHM ReaderView.h"
#include "HtmlViewer.h"

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

/////////////////////////////////////////////////////////////////////////////
// CCHMReaderView

IMPLEMENT_DYNCREATE(CCHMReaderView, CTreeView)

BEGIN_MESSAGE_MAP(CCHMReaderView, CTreeView)
	//{{AFX_MSG_MAP(CCHMReaderView)
	ON_WM_CREATE()
	ON_WM_SETFOCUS()
	ON_WM_DESTROY()	
	ON_NOTIFY_REFLECT(NM_DBLCLK, OnClick)	
	ON_NOTIFY_REFLECT(NM_RECOGNIZEGESTURE, OnRecognizeGesture)
	ON_WM_LBUTTONDOWN()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCHMReaderView construction/destruction

CCHMReaderView::CCHMReaderView()
{	
}

CCHMReaderView::~CCHMReaderView()
{
}

BOOL CCHMReaderView::PreCreateWindow(CREATESTRUCT& cs)
{
	return CTreeView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CCHMReaderView drawing

void CCHMReaderView::OnDraw(CDC* pDC)
{
	CCHMReaderDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
}

void CCHMReaderView::OnInitialUpdate()
{
	CWaitCursor Wait;
	CTreeView::OnInitialUpdate();
		
	HTREEITEM hRoot[10];
	for(int i=0;i<10;i++)
		hRoot[i]=TVI_ROOT;

	CCHMReaderDoc *pDoc=GetDocument();
	POSITION pos=pDoc->GetCHM()->m_TOC.GetHeadPosition();	
	while (pos!=NULL)
	{
		CCHMToc *pTOCEntry=(CCHMToc*)pDoc->GetCHM()->m_TOC.GetNext(pos);
		if (pTOCEntry)
		{
			CString Title(pTOCEntry->m_Title);
			Title.Replace(_T("&amp;"),_T("&"));

			HTREEITEM hItem=GetTreeCtrl().InsertItem(Title,hRoot[pTOCEntry->m_Level-1]);
			GetTreeCtrl().SetItemData(hItem,(DWORD_PTR)pTOCEntry);			
			hRoot[pTOCEntry->m_Level]=hItem;			
			pTOCEntry->m_hTreeItem=hItem;
		}				
	}
}

/////////////////////////////////////////////////////////////////////////////
// CCHMReaderView diagnostics

#ifdef _DEBUG
void CCHMReaderView::AssertValid() const
{
	CTreeView::AssertValid();
}

void CCHMReaderView::Dump(CDumpContext& dc) const
{
	CTreeView::Dump(dc);
}

CCHMReaderDoc* CCHMReaderView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CCHMReaderDoc)));
	return (CCHMReaderDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CCHMReaderView message handlers
int CCHMReaderView::OnCreate( LPCREATESTRUCT lpCreateStruct )
{
	int iResult = CView::OnCreate(lpCreateStruct);

	if(iResult == 0)
	{
		ShowDoneButton(TRUE);	
		m_wndTitleBar.Create(GetParent());
	}

	return iResult;
}
void CCHMReaderView::OnSetFocus(CWnd* pOldWnd)
{
	CView::OnSetFocus(pOldWnd);
	::SHSipPreference(m_hWnd, SIP_FORCEDOWN);
}

void CCHMReaderView::OnDestroy()
{
	::SHSipPreference(m_hWnd, SIP_FORCEDOWN);

	if(IsWindow(m_wndTitleBar))
    	m_wndTitleBar.DestroyWindow();

	CView::OnDestroy();
}

BOOL CCHMReaderView::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext) 
{
	dwStyle|=TVS_HASLINES|TVS_LINESATROOT|TVS_HASBUTTONS;
	return CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);	
}

void CCHMReaderView::OnClick(NMHDR* pNMHDR, LRESULT* pResult) 
{
	*pResult = 0;	
	
	HTREEITEM hItem=GetTreeCtrl().GetSelectedItem();
	if (hItem==NULL) return;
	CCHMReaderDoc *pDoc=GetDocument();
	CString HTMLText;
	CCHMToc* pTocEntry=NULL;
	CCHMFileInfo* pInfo=NULL;
	HTREEITEM hNewItem=NULL;
	int rc=0;
	CString Dir(_T("\\Temp\\"));

	// Get first page
	pTocEntry=(CCHMToc *)GetTreeCtrl().GetItemData(hItem);
	do
	{	
		CHtmlViewer HtmlViewer;
		{
			CWaitCursor Wait;
			HtmlViewer.SetCHMFile(pDoc->GetCHM());
			CString Title;

			if (pTocEntry!=NULL)
			{
				HtmlViewer.SetCHMFileName(pTocEntry->m_FileName);
				HTMLText=pDoc->GetCHM()->GetHTMLFile(pTocEntry->m_FileName,Dir);
				Title.Format(_T("%s:%s"),pDoc->GetCHM()->GetSplit().GetFilename()+_T(":")+pTocEntry->m_Title);
			}
			else if (pInfo!=NULL)
			{
				HtmlViewer.SetCHMFileName(pInfo->m_FileName);
				HTMLText=pDoc->GetCHM()->GetHTMLFile(pInfo->m_FileName,Dir);
				Title.Format(_T("%s:%s"),pDoc->GetCHM()->GetSplit().GetFilename()+_T(":")+pInfo->m_FileName);
			}

			HtmlViewer.SetHtmlText(HTMLText);			
			HtmlViewer.m_Title=Title;
		}		
		rc=HtmlViewer.DoModal();
		{
			CWaitCursor Wait;			
			pDoc->GetCHM()->DeleteFiles(Dir);	
		}

		POSITION pos=NULL;
		if (rc==100)
		{
			// Move to next
			pos=pDoc->GetCHM()->m_TOC.Find(pTocEntry);
			if (pos!=NULL)
				pDoc->GetCHM()->m_TOC.GetNext(pos);			
		}
		if (rc==101)
		{
			// Move Prev
			pos=pDoc->GetCHM()->m_TOC.Find(pTocEntry);
			if (pos!=NULL)
				pDoc->GetCHM()->m_TOC.GetPrev(pos);			
		}	
		if (rc==102)
		{
			// Navigate to new page
			// Check we are in the same file
			if (HtmlViewer.m_NewCHMFile.CompareNoCase(pDoc->GetCHM()->GetFileName())==0)
			{	
				POSITION pos3=NULL;
				POSITION pos2=pDoc->GetCHM()->m_TOC.GetHeadPosition();
				CCHMToc *pTocEntry2=NULL;
				while(pos2)
				{
					pTocEntry2=(CCHMToc*)pDoc->GetCHM()->m_TOC.GetAt(pos2);
					if (pTocEntry2)
					{
						if (pTocEntry2->m_FileName.FindNoCase(HtmlViewer.m_strHref)!=-1)
						{		
							pos3=pos2;
							break;
						}
					}
					pDoc->GetCHM()->m_TOC.GetNext(pos2);
				}

				// if item not found then do nothing.
				if (pos3!=NULL)
				{
					pos=pos2;
					pInfo=NULL;
				}
				else
				{	
					pos=NULL;
					pTocEntry=NULL;
					pInfo=pDoc->GetCHM()->CheckFileExists(HtmlViewer.m_strHref);
				}
			}
		}

		if (pos)
		{
			pTocEntry=(CCHMToc*)pDoc->GetCHM()->m_TOC.GetAt(pos);
			if (pTocEntry==NULL)
				break;
		
			hNewItem=pTocEntry->m_hTreeItem;
			if (hNewItem==NULL)
				break;
			
			CWaitCursor Wait;

			hItem=hNewItem;

			// Make sure item in tree is visible
			GetTreeCtrl().Expand(hItem,TVE_EXPAND);
			GetTreeCtrl().EnsureVisible(hItem);
			GetTreeCtrl().Select(hItem,TVGN_CARET);
		}
	}
	while (rc>=100);
}

void CCHMReaderView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
{
	InvalidateRect(NULL, FALSE);

	CCHMReaderDoc *pDoc=GetDocument();	
	m_wndTitleBar.SetTitle(pDoc->GetCHM()->GetSplit().GetFilename());
}

void CCHMReaderView::OnLButtonDown(UINT nFlags, CPoint point) 
{
   Default(); 
}

void CCHMReaderView::OnRecognizeGesture(NMHDR* pNMHDR, LRESULT* pResult) 
{
   *pResult = TRUE; 
} 

⌨️ 快捷键说明

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