Drawing the Control

The CircleOffset functionality does not require any major drawing modifications. In fact, the OnDraw function need not be changed at all. You will change the GetDrawRect function to offset the circle in the bounding rectangle. The coordinates of the square region returned by GetDrawRect are properly adjusted based on the current value of the CircleOffset property. Add the two sets of lines of code below beginning with:

// offset circle in bounding rect

to the existing GetDrawRect code.

void CCircCtrl::GetDrawRect(CRect* rc)
{
    if (m_circleShape)
    {
        int cx = rc->right - rc->left;
        int cy = rc->bottom - rc->top;

        if (cx > cy)
        {
            rc->left += (cx - cy) / 2;
            rc->right = rc->left + cy;

            // offset circle in bounding rect
            rc->left += m_circleOffset;
            rc->right += m_circleOffset;
        }
        else
        {
            rc->top += (cy - cx) / 2;
            rc->bottom = rc->top + cx;

            // offset circle in bounding rect
            rc->bottom -= m_circleOffset;
            rc->top -= m_circleOffset;
        }
    }
}

The changes to the code for GetDrawRect are adjustments to the square region's left and right coordinates (if the x-extent is greater) or the top and bottom coordinates (if the y-extent is greater).