Serializing the CRgn object in VC++ MFC

The CRgn class in provided in VC++ MFC is not serializable, but what if you have the need to serialize this object and store it in some file.
We can serialize the data back and forth, in this way:

/file CSerializeRegion.h
 
class CSerializeRegion: public CObject
{
 
private:
   CRgn mRgn;
 
public :
   BOOL                    SerializeRgn(CArchive& ar);
 
protected:
    DECLARE_SERIAL(CSerializeRegion)

};
 
//file CSerializeRegion.cpp
/////////////////////////////////////////////////////////////////////////////
// CSerializeRegion

IMPLEMENT_SERIAL(CSerializeRegion, CObject, 2)
CSerializeRegion::CSerializeRegion()
{
}

CSerializeRegion::~CSerializeRegion()
{
    m_rgn.DeleteObject();
}

#define CURRENT_VERSION 2.5
 
 
BOOL CSerializeRegion::SerializeRgn(CArchive& ar)
{
 
//main section where region is getting serialized
 
//writing the CRgn object
    if (ar.IsStoring())
    {
        if(m_rgn.m_hObject)
        {
            DWORD w = 1;
            ar  w;
        if(w == 1)
        {
            DWORD size = 0;
            ar >> size;

            if(size > 0)
            {
                RGNDATA *rgndata = NULL;
                rgndata = (RGNDATA *) HeapAlloc( GetProcessHeap(), 0, size );
                if(!rgndata) {
                    return FALSE;
                }
                ar.Read(rgndata, size);

                if(m_rgn.m_hObject)
                {
                    CRgn tRgn;
                    tRgn.CreateFromData(NULL, rgndata->rdh.nCount, rgndata);
                    m_rgn.CopyRgn(&tRgn);
                }
                else
                {
                    m_rgn.CreateFromData(NULL, size, rgndata);
                }

                HeapFree(GetProcessHeap(), 0, rgndata);
            }
        }
    }
    return TRUE;
}

 //It depends on you how you use this class, you can make the CSerializeRegion class object , a member of CDocument class of MFC application. or use it in your way.

150 150 Burnignorance | Where Minds Meet And Sparks Fly!