Search code examples
c++mfcgdi

Query regarding extracting points from a CRgn Object


I have created a CRgn, with CRgn::CreatePolygonRgn(). Now I need to extract these points from that region. I found that the data's can be extrated using the CRgn::GetRegionData(). but unable to get the data's..

Could anyone tell me with an example..


Solution

  • Able to retrieve valid data with OnPaint event.

    CPaintDC dc(this); // device context for painting       
    
    CRgn   rgnA;
    
    CPoint ptVertex[5];
    
    ptVertex[0].x = 180;
    ptVertex[0].y = 80;
    ptVertex[1].x = 100;
    ptVertex[1].y = 160;
    ptVertex[2].x = 120;
    ptVertex[2].y = 260;
    ptVertex[3].x = 240;
    ptVertex[3].y = 260;
    ptVertex[4].x = 260;
    ptVertex[4].y = 160;
    
    VERIFY(rgnA.CreatePolygonRgn(ptVertex, 5, ALTERNATE));
    
    CRect rectRgnBox;
    int nRgnBoxResult = rgnA.GetRgnBox(&rectRgnBox);
    ASSERT(nRgnBoxResult != ERROR && nRgnBoxResult != NULLREGION);
    
    CBrush brA;
    VERIFY(brA.CreateSolidBrush(RGB(255, 0, 0)));  // rgnA Red
    VERIFY(dc.FrameRgn(&rgnA, &brA, 2, 2));
    rectRgnBox.InflateRect(3, 3);
    
    int size = GetRegionData(rgnA, 0, NULL);
    int rectcount = 0;
    
    if (size)
    {
        RGNDATA * pRegion = (RGNDATA *) new char[size];
        GetRegionData(rgnA, size, pRegion);
    
        RECT * pRect = (RECT *)& pRegion->Buffer;
    
        int rectcount = pRegion->rdh.nCount;
    
        if (pRegion)
        {
            int f;
            for (unsigned i = 0; i < pRegion->rdh.nCount; i++)
            {
                f = pRect[i].left;
                f = pRect[i].top;
                f = pRect[i].right;
                f = pRect[i].bottom;
            }                   
        }
    
        delete[](char *) pRegion;
    
    }
    

    Code reference CRgn::CreatePolygonRgn

    EDIT

    Each rectangle with unique color will give idea how rectangles are filling region

    enter image description here