Click to See Complete Forum and Search --> : How to copy CRgn object


Alan Benett
June 4th, 1999, 09:30 AM
Hello!

I'm writing on a class that mainly encapsulates a CRgn.
As I want to use this class in a container (mainly an array), i added
a copy constructor and assignment operator that should copy the
CRgn encapsulated in my class to the new instance.
However i get an error (access violation) when i try to create a CRgn
using CreateFromData and GetRegionData.
Here's the code:


class CMyRgn : public CObject
{
CRgn m_rgn;

CMyRgn (CMyRgn&);
CMyRgn operator= (CMyRgn&);
...
};




CMyRgn operator= (CMyRgn&)
{
// copy the region data
RGNDATA RgnData;
int nDataSize;

nDataSize = rgnSrc.m_rgn.GetRegionData (NULL, 0);

rgnSrc.m_rgn.GetRegionData (&RgnData, nDataSize); // this one fails
m_rgn.CreateFromData (NULL, nDataSize, &RgnData);
...
}

Safai Ma
June 4th, 1999, 11:10 AM
Well, you see, you need to allocate memory for your region data, specifically, the rectangles pointed by RgnData.Buffer....
// copy the region data
LPRGNDATA lpRgnData;
int nDataSize;
nDataSize = rgnSrc.m_rgn.GetRegionData (NULL, 0);
lpRgnData = (LPRGNDATA) new char[sizeof(RGNDATA)+nDataSize];
rgnSrc.m_rgn.GetRegionData (lpRgnData, nDataSize);
m_rgn.CreateFromData (NULL, nDataSize, &RgnData);
...
// clean up
delete (char*)lpRgnData;




-Safai

Safai Ma
June 4th, 1999, 11:15 AM
The other option is to use CRgn::CopyRgn

-Safai