CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 1999
    Posts
    14

    How to copy CRgn object

    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);
    ...
    }





  2. #2
    Join Date
    May 1999
    Location
    Toronto, Ontario, Canada
    Posts
    155

    Re: How to copy CRgn object

    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

  3. #3
    Join Date
    May 1999
    Location
    Toronto, Ontario, Canada
    Posts
    155

    Re: How to copy CRgn object

    The other option is to use CRgn::CopyRgn

    -Safai

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured