Search code examples
winapimfcgdi

MFC/GDI CRgn CreateEllipticRgn is two pixels short?


If I do CRgn::CreateEllipticRgn(0, 0, 120, 120); the result ends up with two pixels short at the right and bottom. Why is that? I thought maybe because a Region is within the outline rather than on the outline but in that case it should have been 1 pixel border around the entire circle instead of 2 off at right and bottom. Any ideas?

TIA!!


Solution

  • Use a picture to explain the problem:

        //RECT
        hPen = CreatePen(PS_SOLID, 1, RGB(255, 100, 0));
        SelectObject(hdc, hPen);
        Rectangle(hdc, 10, 10, 130, 130);
        //Ellipse
        hBrush = CreateSolidBrush(RGB(255, 0, 0));
        HRGN hTemp = CreateEllipticRgn(10, 10, 130, 130);
        FrameRgn(hdc, hTemp, hBrush, 1, 1);
    

    enter image description here

    From Rectangle function, it points out:

    The rectangle that is drawn excludes the bottom and right edges.

    If a PS_NULL pen is used, the dimensions of the rectangle are 1 pixel less in height and 1 pixel less in width.

    From CreateEllipticRgn, it points out:

    A bounding rectangle defines the size, shape, and orientation of the region: The long sides of the rectangle define the length of the ellipse's major axis; the short sides define the length of the ellipse's minor axis; and the center of the rectangle defines the intersection of the major and minor axes.

    From the test result, the length and width of the ellipse excludes the bottom and right edges.