Click here to Skip to main content
15,892,804 members
Articles / Mobile Apps / Windows Mobile

CHM Reader for Pocket PC 2003

Rate me:
Please Sign up or sign in to vote.
4.91/5 (65 votes)
29 Feb 2004CPOL3 min read 477.2K   960   87  
Allows the reading of CHM files on a Pocket PC2003.
// STHtmlDialog.cpp : implementation file
//
#include "stdafx.h"
#include "resource.h"
#include "STHtmlDialog.h"
#include <Htmlctrl.h>
#include "VOImage.h"
#include "CHMFile.h"

#define LPINLINEIMAGEINFO (INLINEIMAGEINFO*)

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

/////////////////////////////////////////////////////////////////////////////
// CSTHtmlDialog dialog

HINSTANCE CSTHtmlDialog::m_HtmlViewInstance = 0;

CSTHtmlDialog::CSTHtmlDialog(UINT nIDTemplate, CWnd* pParentWnd)
	: CDialog(nIDTemplate, pParentWnd)
{
	//{{AFX_DATA_INIT(CSTHtmlDialog)
	//}}AFX_DATA_INIT
	m_pCHMFile=NULL;
}


BEGIN_MESSAGE_MAP(CSTHtmlDialog, CDialog)
	//{{AFX_MSG_MAP(CSTHtmlDialog)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSTHtmlDialog message handlers


void CSTHtmlDialog::SetCHMFile(CCHMFile *pCHMFile)
{
	m_pCHMFile=pCHMFile;
}

BOOL CSTHtmlDialog::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	CreateHtmlWindow();
	
	return TRUE;
}

void CSTHtmlDialog::CreateHtmlWindow()
{
	CWaitCursor Wait;
	if (m_HtmlViewInstance == 0) {
		m_HtmlViewInstance = ::LoadLibrary(L"htmlview.dll");
	}

	VERIFY(InitHTMLControl(AfxGetInstanceHandle()));
	
	DWORD dwStyle = WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS;

	RECT rect;
	GetClientRect(&rect);	
	rect.top+=17;
	rect.bottom-=17;
	m_hwndHtml = ::CreateWindow (DISPLAYCLASS, 
                                 NULL,
                                 dwStyle,
                                 rect.left, 
                                 rect.top, 
                                 rect.right, 
                                 rect.bottom,
                                 m_hWnd, 
                                 0, 
                                 m_HtmlViewInstance, 
                                 NULL);

	::SetWindowLong(m_hwndHtml, GWL_ID, 12321);
	::SetFocus (m_hwndHtml);
	::SendMessage(m_hwndHtml, WM_SETTEXT, 0, (LPARAM)(LPCTSTR)_T("")); 
}

void CSTHtmlDialog::SetHtml(const CString &strHtml)
{
	CWaitCursor Wait;
	::SendMessage(m_hwndHtml, WM_SETTEXT, 0, (LPARAM)(LPCTSTR)_T(""));
	::SendMessage(m_hwndHtml, DTM_ADDTEXTW, FALSE, (LPARAM)(LPCTSTR)strHtml);
	::SendMessage(m_hwndHtml, DTM_ENDOFSOURCE, 0, 0);
}

LRESULT CSTHtmlDialog::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
{
	switch(message)
	{	
	case WM_NOTIFY:
			NM_HTMLVIEW * pnmHTML = (NM_HTMLVIEW *) lParam;
			LPNMHDR pnmh = (LPNMHDR) &(pnmHTML->hdr);
			CString strReg = _T("register");

			switch(pnmh->code)
      
			{
			case NM_HOTSPOT:
				OnLink(pnmHTML->szTarget);
				return 1;
				break;

			case NM_INLINE_IMAGE:
				{
					CWaitCursor Wait;					
					CVOImage imgToLoad;
					CDC* pDC = GetDC();
					CStringEx FN(pnmHTML->szTarget);					
					CCHMFile ExternalCHM;

					BYTE *pImage=NULL;									
					CCHMFile *pCHMFile=m_pCHMFile;
					
					CCHMFileInfo *pInfo=m_pCHMFile->CheckFileExists(FN);
					DWORD ImageSize=0;
					if (pInfo==NULL)
					{
						CSplitPath Split(m_CHMFileName,_T('/'));
						CString Name;						
						Name=Split.GetPath()+FN;						
						pInfo=m_pCHMFile->CheckFileExists(Name);
					}
					if ((pInfo==NULL) && (FN.FindNoCase(_T("MS-ITS:"))==0))
					{
						CString ExternalCHMName;				
						int idx=FN.Find(_T("::"));
						ExternalCHMName=FN.Mid(7,idx-7);

						CStringEx Text;			
						int idx2=FN.ReverseFind(_T('/'));
						Text=FN.Right(FN.GetLength()-idx2-1);

						// Get file from external source.						
						if (ExternalCHM.Open(m_pCHMFile->GetSplit().GetPath()+ExternalCHMName))
						{	
							pInfo=ExternalCHM.CheckFileExists(Text);
							pCHMFile=&ExternalCHM;
						}		
					}

					if (pInfo==NULL)
						return 0;
					pImage=pCHMFile->GetFile(pInfo);
					ImageSize=pInfo->m_Length;

					if(imgToLoad.Load(pDC->GetSafeHdc(), pImage, ImageSize))
					{
						INLINEIMAGEINFO	imgInfo;
						imgInfo.dwCookie = pnmHTML->dwCookie;
						imgInfo.iOrigHeight = imgToLoad.GetHeight();
						imgInfo.iOrigWidth = imgToLoad.GetWidth();
						imgInfo.hbm = (HBITMAP)imgToLoad;
						imgInfo.bOwnBitmap = true;
						::SendMessage(m_hwndHtml, DTM_SETIMAGE, 0, (LPARAM)&imgInfo);						
					}
					else
					{
						TRACE(_T("Failed to load %s \n"),FN);
						::SendMessage(m_hwndHtml, DTM_IMAGEFAIL, 0, pnmHTML->dwCookie);
					}

					if (pImage) delete pImage; pImage=NULL;
					return 1;
				}
				break;
			}
	}
	
	return CDialog::WindowProc(message, wParam, lParam);
}

void CSTHtmlDialog::RegisterHtmlImage(int nResourceId, const CString &strHtmlImageName)
{
	CBitmap* pBitmap = new CBitmap();
	pBitmap->LoadBitmap(nResourceId);
	m_imagesCache.SetAt(strHtmlImageName, (CObject*)pBitmap);
}

void CSTHtmlDialog::RegisterHtmlImage(CBitmap *pBitmap, const CString &strHtmlImageName)
{
	m_imagesCache.SetAt(strHtmlImageName, (CObject*)pBitmap);
}

void CSTHtmlDialog::DeleteHtmlImages()
{
	CWaitCursor Wait;
	POSITION pos = m_imagesCache.GetStartPosition();
	CString key;
	CBitmap* bmp;
	while (pos != NULL) 
	{
		m_imagesCache.GetNextAssoc(pos, key, (CObject*&)bmp);
		delete bmp;
		m_imagesCache.RemoveKey(key);
	}
}


void CSTHtmlDialog::SetCHMFileName(CString CHMFileName)
{
	m_CHMFileName=CHMFileName;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions