Click here to Skip to main content
15,891,903 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 584K   12.9K   194  
Capturing HTML documents as images
//****************************************************************************
// N O L D U S   I N F O R M A T I O N   T E C H N O L O G Y   B . V .
//****************************************************************************
// Filename:      AutoPen.h
// Programmer:    Anneke Sicherer-Roetman
// Version:       1.00
// Revision Date: 17-02-1999
//****************************************************************************
// Description:   Declaration of classes CAutoPen & CAutoBrush
//                (header-only classes)
//****************************************************************************
// Revision history:
//   17-02-1999 - First implementation
//****************************************************************************
// Bugs: ........
//****************************************************************************
// @doc
//****************************************************************************

#if !defined(AFX_AUTOPEN_H__FE7A35F5_C649_11D2_A614_0060085FE616__INCLUDED_)
#define AFX_AUTOPEN_H__FE7A35F5_C649_11D2_A614_0060085FE616__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

//****************************************************************************
// @class         CAutoPen |
//                CPen that IS correctly destroyed<nl>
//                If you create a temporary CPen object on the stack with
//                one of the one-step CPen constructors, you can easily forget 
//                to deselect the pen from the device context before it goes out
//                of scope. In that case, although stepping through the code and 
//                BoundsChecker will make you believe otherwise, this causes a 
//                serious GDI resource leak. The CAutoPen class overcomes this 
//                flaw and also selects the pen into the device context in the 
//                constructors. MFC is a much too thin wrapper around Win32!!!
// @base          public | CPen
//****************************************************************************
// @prog 
// Anneke Sicherer-Roetman
// @revs 
// 17-02-1999 - First implementation
// @ex            Example of the usage of a CAutoPen and a <c CAutoBrush> |
//
// void CMyView::OnDraw(CDC* pDC)
// {
//   // make red pen
//   CAutoPen NewPen(pDC, PS_SOLID, 1, RGB(255,0,0));
//   // make green brush
//   CAutoBrush NewBrush(pDC, RGB(0, 255, 0));
//   // draw rectangle
//   CRect rect(10, 10, 110, 110);
//   pDC->Rectangle(&rect);
// }
//
//****************************************************************************
// @todo 
//****************************************************************************
class CAutoPen : public CPen
{

  // @access Public Member Functions and Variables
public:

  // @cmember,mfunc
  // 1st constructor, see CPen docs
  CAutoPen(CDC *pDC, int nPenStyle, int nWidth, COLORREF crColor) :
  CPen(nPenStyle, nWidth, crColor), m_pDC(pDC), m_pOldGdi(NULL) 
  { 
    Initialize();  
  }

  // @cmember,mfunc
  // 2nd constructor, see CPen docs
  CAutoPen(CDC *pDC, int nPenStyle, int nWidth, const LOGBRUSH *pLogBrush,
           int nStyleCount = 0, const DWORD *lpStyle = NULL) :
  CPen(nPenStyle, nWidth, pLogBrush, nStyleCount, lpStyle), m_pDC(pDC), m_pOldGdi(NULL)
  { 
    Initialize();  
  }

  // @cmember,mfunc
  // destructor, deselects pen from device context
  virtual ~CAutoPen()
  { 
    ASSERT_VALID(m_pOldGdi);
    ASSERT_VALID(m_pDC);
    m_pDC->SelectObject(m_pOldGdi);
  }

  // @access Private Member Functions and Variables
private:

  CDC        *m_pDC;      // @cmember remembers device context
  CGdiObject *m_pOldGdi;  // @cmember remembers previous pen

  CAutoPen() { }          // default constructor cannot be called

  // @cmember,mfunc
  // selects pen into device context
  void Initialize() 
  {
    m_pOldGdi = m_pDC->SelectObject(this); 
    ASSERT_VALID(m_pOldGdi);
  }

};

//****************************************************************************
// @class         CAutoBrush |
//                CBrush that IS correctly destroyed<nl>
//                See documentation of class <c CAutoPen>
// @base          public | CBrush
//****************************************************************************
// @prog 
// Anneke Sicherer-Roetman
// @revs 
// 17-02-1999 - First implementation
//****************************************************************************
// @todo 
//****************************************************************************
class CAutoBrush : public CBrush
{

  // @access Public Member Functions and Variables
public:

  // @cmember,mfunc
  // 1st constructor, see CBrush docs
  CAutoBrush(CDC *pDC, COLORREF crColor) :
  CBrush(crColor), m_pDC(pDC), m_pOldGdi(NULL)
  { 
    Initialize();  
  }

  // @cmember,mfunc
  // 2nd constructor, see CBrush docs
  CAutoBrush(CDC *pDC, int nIndex, COLORREF crColor) :
  CBrush(nIndex, crColor), m_pDC(pDC), m_pOldGdi(NULL)
  { 
    Initialize();  
  }

  // @cmember,mfunc
  // 3rd constructor, see CBrush docs
  CAutoBrush(CDC *pDC, CBitmap* pBitmap) :
  CBrush(pBitmap), m_pDC(pDC), m_pOldGdi(NULL)
  { 
    Initialize();  
  }

  // @cmember,mfunc
  // destructor, deselects brush from device context
  virtual ~CAutoBrush()
  { 
    ASSERT_VALID(m_pOldGdi);
    ASSERT_VALID(m_pDC);
    m_pDC->SelectObject(m_pOldGdi);
  }

  // @access Private Member Functions and Variables
private:

  CDC        *m_pDC;      // @cmember remembers device context
  CGdiObject *m_pOldGdi;  // @cmember remembers previous pen

  CAutoBrush() { }        // default constructor cannot be called

  // @cmember,mfunc
  // selects brush into device context
  void Initialize() 
  {
    m_pOldGdi = m_pDC->SelectObject(this); 
    ASSERT_VALID(m_pOldGdi);
  }

};

#endif // !defined(AFX_AUTOPEN_H__FE7A35F5_C649_11D2_A614_0060085FE616__INCLUDED_)

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