Click here to Skip to main content
12,507,173 members (60,752 online)
Click here to Skip to main content

Stats

375.2K views
806 downloads
87 bookmarked
Posted

CHM Reader for Pocket PC 2003

, 29 Feb 2004 CPOL
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)

Share

About the Author

Peter Tewkesbury
Software Developer (Senior)
United Kingdom United Kingdom
Hi, I am a developer working in Cheshire, England. I started programming on a Vic-20 back in the 80's with only 5K of ram), and I have not looked back since. I currently develop using C#/.NET, SQL, HTML, CSS, JS & ASP.NET. In the past I have developed software using Visual C/C++, MFC, ATL & COM, Crystal Reports.

My favourite language is C# and I am always trying to learn new stuff like LINQ, and mobile development.

You may also be interested in...

Pro
Pro
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160927.1 | Last Updated 1 Mar 2004
Article Copyright 2004 by Peter Tewkesbury
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid