Click here to Skip to main content
15,884,176 members
Articles / Desktop Programming / MFC

Capture an HTML document as an image

,
Rate me:
Please Sign up or sign in to vote.
4.88/5 (62 votes)
19 May 2004CPOL15 min read 579.4K   12.9K   194  
Capturing HTML documents as images
// VTHtmlThumbnailDlg.cpp : implementation file
//

#include "stdafx.h"
#include "VTHtmlThumbnail.h"
#include "VTHtmlThumbnailDlg.h"
#include ".\vthtmlthumbnaildlg.h"
#include "VTCreateHTMLImage.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CVTHtmlThumbnailDlg dialog

CVTHtmlThumbnailDlg::CVTHtmlThumbnailDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CVTHtmlThumbnailDlg::IDD, pParent)
	, m_csStatus(_T(""))
{
	//{{AFX_DATA_INIT(CVTHtmlThumbnailDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

	m_iDocumentsComplete = 0;
	m_pHTMLImage = NULL;
}

void CVTHtmlThumbnailDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CVTHtmlThumbnailDlg)
	// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
	DDX_Control(pDX, IDC_STATIC_THUMBNAIL, m_StaticThumbnail1);
	DDX_Control(pDX, IDC_STATIC_THUMBNAIL2, m_StaticThumbnail2);
	DDX_Control(pDX, IDC_STATIC_THUMBNAIL3, m_StaticThumbnail3);
	DDX_Control(pDX, IDC_STATIC_THUMBNAIL4, m_StaticThumbnail4);
	DDX_Control(pDX, IDC_STATIC_THUMBNAIL5, m_StaticThumbnail5);
	DDX_Control(pDX, IDC_STATIC_THUMBNAIL6, m_StaticThumbnail6);
	DDX_Text(pDX, IDC_STATIC_STATUS, m_csStatus);
}

BEGIN_MESSAGE_MAP(CVTHtmlThumbnailDlg, CDialog)
	//{{AFX_MSG_MAP(CVTHtmlThumbnailDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_BUTTON1, OnBnClickedButton1)
	ON_MESSAGE(UM_DOCUMENT_COMPLETE, OnDocumentComplete)
	ON_WM_DESTROY()
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CVTHtmlThumbnailDlg message handlers

BOOL CVTHtmlThumbnailDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	m_StaticThumbnail1.Create();
	m_StaticThumbnail2.Create();
	m_StaticThumbnail3.Create();
	m_StaticThumbnail4.Create();
	m_StaticThumbnail5.Create();
	m_StaticThumbnail6.Create();

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CVTHtmlThumbnailDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CVTHtmlThumbnailDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CVTHtmlThumbnailDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CVTHtmlThumbnailDlg::OnBnClickedButton1()
{
	m_iDocumentsComplete = 0;
	m_csStatus.Format("Obtaining Thumbnails");
	UpdateData(FALSE);

	EnableButtons(FALSE);

	if(m_pHTMLImage)
		delete m_pHTMLImage;
	m_pHTMLImage = new CVTCreateHTMLImage();
	m_pHTMLImage->Create(this);
	if(m_pHTMLImage->CreateImage("http://www.codeproject.com", "c:\\codeproject.jpg", CSize(800, 600), CSize(120, 100)))
	{
		m_StaticThumbnail1.SetURL("http://www.codeproject.com");
		m_StaticThumbnail1.SetFilename("c:\\codeproject.jpg");
	}
	if(m_pHTMLImage->CreateImage("http://www.intel.com", "c:\\intel.jpg", CSize(800, 600), CSize(120, 100)))
	{
		m_StaticThumbnail2.SetURL("http://www.intel.com");
		m_StaticThumbnail2.SetFilename("c:\\intel.jpg");
	}
	if(m_pHTMLImage->CreateImage("http://www.dundas.com", "c:\\dundas.jpg", CSize(800, 600), CSize(120, 100)))
	{
		m_StaticThumbnail3.SetURL("http://www.dundas.com");
		m_StaticThumbnail3.SetFilename("c:\\dundas.jpg");
	}
	if(m_pHTMLImage->CreateImage("http://www.cnn.com", "c:\\cnn.jpg", CSize(800, 600), CSize(120, 100)))
	{
		m_StaticThumbnail4.SetURL("http://www.cnn.com");
		m_StaticThumbnail4.SetFilename("c:\\cnn.jpg");
	}
	if(m_pHTMLImage->CreateImage("http://www.apple.com", "c:\\apple.jpg", CSize(800, 600), CSize(120, 100)))
	{
		m_StaticThumbnail5.SetURL("http://www.apple.com");
		m_StaticThumbnail5.SetFilename("c:\\apple.jpg");
	}
	if(m_pHTMLImage->CreateImage("http://welcome.hp.com/country/us/en/prodserv/handheld.html", "c:\\ipaq.jpg", CSize(800, 600), CSize(320,240)))
	{
		m_StaticThumbnail6.SetURL("http://welcome.hp.com/country/us/en/prodserv/handheld.html");
		m_StaticThumbnail6.SetFilename("c:\\ipaq.jpg");
	}
}

LRESULT CVTHtmlThumbnailDlg::OnDocumentComplete(WPARAM wParam, LPARAM lParam)
{
	m_iDocumentsComplete++;

	if(m_iDocumentsComplete == 1)
		m_csStatus.Format("%ld Document Complete", m_iDocumentsComplete);
	else
		m_csStatus.Format("%ld Documents Complete", m_iDocumentsComplete);
	UpdateData(FALSE);

	if(m_iDocumentsComplete == 6)
		EnableButtons(TRUE);
	return 0L;
}
void CVTHtmlThumbnailDlg::EnableButtons(bool bEnable)
{
	GetDlgItem(IDOK)->EnableWindow(bEnable);
	GetDlgItem(IDCANCEL)->EnableWindow(bEnable);
	GetDlgItem(IDC_BUTTON1)->EnableWindow(bEnable);
}

void CVTHtmlThumbnailDlg::OnDestroy()
{
	CDialog::OnDestroy();

	if(m_pHTMLImage)
	{
		delete m_pHTMLImage;
		m_pHTMLImage = NULL;
	}
}

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
United States United States
I've been programming for 35 years - started in machine language on the National Semiconductor SC/MP chip, moved via the 8080 to the Z80 - graduated through HP Rocky Mountain Basic and HPL - then to C and C++ and now C#.

I used (30 or so years ago when I worked for Hewlett Packard) to repair HP Oscilloscopes and Spectrum Analysers - for a while there I was the one repairing DC to daylight SpecAns in the Asia Pacific area.

Afterward I was the fourth team member added to the Australia Post EPOS project at Unisys Australia. We grew to become an A$400 million project. I wrote a few device drivers for the project under Microsoft OS/2 v 1.3 - did hardware qualification and was part of the rollout team dealing directly with the customer.

Born and bred in Melbourne Australia, now living in Scottsdale Arizona USA, became a US Citizen on September 29th, 2006.

I work for a medical insurance broker, learning how to create ASP.NET websites in VB.Net and C#. It's all good.

Oh, I'm also a Kentucky Colonel. http://www.kycolonels.org

Written By
Web Developer
United States United States
Technical Evangelist and Computer Programmer. OS of choice is any Win32 OS. Started working in the gaming industry, programming mainframe VOS OS and dealing with Slot machine serials comms protocols, creating test-tools and line monitoring software on W2K.

Moved on to working in a small software company that worked with electronic forms.

Next, worked at a company with Security, Identity and Trust Solutions. Learnt lots about encryption, signing, digital signatures, hashing and all sorts of new buzz words. It might as well be another language.

Now at Nintex. World class workflow software vendor and am a Technical Evangelist. IF you're looking to automate business processes, this is the place to look.

Comments and Discussions