CGDI - Simple GDI Set And Reset

In MFC, GDI Objects need to be selected and then destroyed. CGDI and its derived classes take care of this by selecting the object in the constructor and deselecting it in the destructor. CGDIObject is a template class which takes as its parameter the CGdiObject-derived class. This class library only accounts for a few of the set/get function pairs. This could be extended by creating classes for each of the CDC Set/Get functions. Examples:

// Example to draw a line using the BLACK_PEN stock object
void DrawLine(CDC* pDC)
{
    CStockObject theObject(pDC, BLACK_PEN);

    pDC->LineTo(100, 100);
}

// Example to draw a line using a custom pen
void DrawLinesWithPen(CDC* pDC)
{                                                         // Current Pen Selected
    CPen thePen(PS_SOLID, 3, RGB(100, 200, 2));

    CGDIObject<CPen> theObject(pDC, &thePen);                  // thePen Selected

    pDC->LineTo(100, 100);

    // Use brackets to nest calls
    {
        CPen theOtherPen(PS_SOLID, 3, RGB(200, 10, 2));         

        CGDIObject<CPen> theOtherObject(pDC, &theOtherPen);    // theOtherPen Selected

        pDC->LineTo(200, 200);
    }                                                     // thePen Selected

    pDC->LineTo(300, 300);
}                                                         // Current Pen Selected




// helper template function for unused parameters
template <class T>
void Unused(T rT)
{
    static_cast<void>(rT);
};


class CGDI
{
protected:
    CDC* m_pDC;

    CGDI(CDC* pDC) : 
      m_pDC(pDC)
    {
    }

    virtual ~CGDI()
    {
        m_pDC = NULL;
    }
};

// Class to select a stock object
class CGDIStockObject : public CGDI
{
    CGdiObject* m_pLastObject;

public:
    CGDIStockObject(CDC* pDC, int nIndex) : CGDI(pDC),
      m_pLastObject(NULL)
    {
        m_pLastObject = m_pDC->SelectStockObject(nIndex);
    }

    virtual ~CGDIStockObject()
    {
        if (m_pLastObject)
        {
            Unused(m_pDC->SelectObject(m_pLastObject));

            m_pLastObject = NULL;
        }
    }
};


class CGDITextColor : public CGDI
{
    COLORREF m_LastColor;

public:
    CGDITextColor(CDC* pDC, COLORREF theColor) : CGDI(pDC),
      m_LastColor(RGB(0,0,0))
    {
        m_LastColor = m_pDC->SetTextColor(theColor);
    }

    virtual ~CGDITextColor()
    {
        Unused(m_pDC->SetTextColor(m_LastColor));
    }

};

class CGDIBackColor : public CGDI
{
    COLORREF m_LastColor;

public:
    CGDIBackColor(CDC* pDC, COLORREF theColor) : CGDI(pDC),
      m_LastColor(RGB(0,0,0))
    {
        m_LastColor = m_pDC->SetBkColor(theColor);
    }

    virtual ~CGDIBackColor()
    {
        Unused(m_pDC->SetBkColor(m_LastColor));
    }

};

class CGDIBackMode : public CGDI
{
    int m_LastMode;

public:
    CGDIBackMode(CDC* pDC, int theMode) : CGDI(pDC),
	  m_LastMode(0)
    {
        m_LastMode = m_pDC->SetBkMode(theMode);
    }

    virtual ~CGDIBackMode()
    {
        Unused(m_pDC->SetBkMode(m_LastMode));
    }

};

// This class can be used like this:
// CGDIObject<CFont>(pDC, pFont);
// CGDIObject<CPen>(pDC, pPen);

template <class T>
class CGDIObject : public CGDI
{
T* m_pLast;

public:
    CGDIObject(CDC* pDC, T* pObject) : CGDI(pDC), 
	  m_pLast(NULL)
    {
        if (pObject)
        {
            m_pLast = pDC->SelectObject(pObject);
        }
    }

    virtual ~CGDIObject()
    {
        if (m_pLast)
        {
            Unused(m_pDC->SelectObject(m_pLast));

            m_pLast = NULL;
        }
    }
};

class CGDITextAlign : public CGDI
{
UINT m_Last;

public:
    CGDITextAlign(CDC* pDC, UINT nTextAlign) : CGDI(pDC),
	  m_Last(0)
    {
        m_Last = pDC->SetTextAlign(nTextAlign);
    }

    virtual ~CGDITextAlign()
    {
        Unused(m_pDC->SetTextAlign(m_Last));
    }
};


class CGDIROP : public CGDI
{
int m_Last;

public:
    CGDIROP(CDC* pDC, int nROP) : CGDI(pDC),
	  m_Last(0)
    {
        m_Last = pDC->SetROP2(nROP);
    }

    virtual ~CGDIROP()
    {
        Unused(m_pDC->SetROP2(m_Last));
    }
};

class CGDIMapMode : public CGDI
{
int m_Last;

public:
    CGDIMapMode(CDC* pDC, int nMapMode) : CGDI(pDC),
	  m_Last(0)
    {
        m_Last = pDC->SetMapMode(nMapMode);
    }

    virtual ~CGDIMapMode()
    {
        Unused(m_pDC->SetMapMode(m_Last));
    }
};

Last updated: 8 June 1998



Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • You probably have several goals for your patient portal of choice. Is "community" one of them? With a bevy of vendors offering portal solutions, it can be challenging for a hospital to know where to start. Fortunately, YourCareCommunity helps ease the decision-making process. Read this white paper to learn more. "3 Ways Clinicians can Leverage a Patient Portal to Craft a Healthcare Community" is a published document owned by www.medhost.com

  • Live Event Date: May 7, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT This eSeminar will explore three popular games engines and how they empower developers to create exciting, graphically rich, and high-performance games for Android® on Intel® Architecture. Join us for a deep dive as experts describe the features, tools, and common challenges using Marmalade, App Game Kit, and Havok game engines, as well as a discussion of the pros and cons of each engine and how they fit into your development …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds