Setting the CircleOffset Property

The reason that CircleOffset is defined as a Get/Set property is to provide an entry point where the offset can be validated when the user attempts to change its value. Two rules govern the offset validation:

The Set function for Get/Set properties can be written to ignore requests to set the property value to its current value. Properties implemented using only member variables do not provide this ability. Consequently, there is another optional rule that governs validation of Get/Set properties in general:

If any of the rules are not followed, SetCircleOffset ignores the request.

If the new offset is valid, the value of the CircleOffset property is updated. Because the CircleOffset property is persistent, the modified flag is set. Because CircleOffset affects the visual appearance of the control, the control is invalidated. Invalidating the control is the simplest way to force the control to be redrawn.

Modify SetCircleOffset in CIRCCTL.CPP to validate the new value. Remove the //TODO comment line. Add the if statement and other code below to the SetModifiedFlag function call that ClassWizard added:

void CCircCtrl::SetCircleOffset(short nNewValue)
{
    // Validate the specified offset value
    if ((m_circleOffset != nNewValue) && m_circleShape && InBounds(nNewValue))
    {
        m_circleOffset = nNewValue;
        SetModifiedFlag();
        InvalidateControl();
    }
}

Notice that the function SetCircleOffset calls the InBounds member function. This function returns TRUE if the specified offset does not force the circle outside the bounding rectangle of the control.

To implement the InBounds member function

  1. On WizardBar, select CCircCtl to edit the CCIRCCTL.CPP file.

  2. Click the arrow on the action button, located on the right end of WizardBar, and select Add Member Function.

    The Add Member Function dialog box is displayed.

  3. In the Function Type edit box, type BOOL. This is the type of value the function will return.

  4. In the Function Declaration edit box, type InBounds(short nOffset).

  5. Under Access, click Protected.

  6. Click OK to close the Add Member Function dialog box.

    This will add the following declaration in the protected section of CIRCCTL.H:

    BOOL InBounds(short nOffset);
    

    It will also add this function definition at the end of CIRCCTL.CPP and position the cursor in the function:

    void CCircCtrl::InBounds(short nOffset)
    {
    
    }
    
  7. Add code to the function in CIRCCTL.CPP as shown below:
    BOOL CCircCtrl::InBounds(short nOffset)
    {
        CRect rc;
        int diameter;
        int length;
        int cx,cy;
    
        GetControlSize(&cx, &cy);
    
        if (cx > cy)
        {
            length = cx;
            diameter = cy;
        }
        else
        {
            length = cy;
            diameter = cx;
        }
        if (nOffset < 0)
            nOffset = -nOffset;
        return (diameter / 2 + nOffset) <= (length / 2);
    }