Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Chapter 17: Track-Based Controls Visual C++ and MFC Fundamentals


}

After creating the control, to indicate what control would display the value of the spin
button, we saw that you can use the Alignment or the Auto Buddy properties. If you did
not do this at design time, and if you want to explicitly specify the name of the control
that would act as the buddy window, you can call the CSpinButtonCtrl::SetBuddy()
method. Its syntax is:

CWnd* SetBuddy(CWnd* pWndBuddy);

The pWndBuddy argument is the new control that would serve as buddy. Here is an
example that sets an existing label on the dialog box (the label was created as a Static
Text control and identified as IDC_SPIN_BUDDY) as the spin button’s buddy window:

BOOL CSpinDlg::OnInitDialog()
{
CDialog::OnInitDialog();

// TODO: Add extra initialization here
CSpinButtonCtrl *SpinCtrl = new CSpinButtonCtrl;
CStatic *SpinBuddy;

SpinCtrl->Create(WS_CHILD | WS_VISIBLE | UDS_SETBUDDYINT,
CRect(60, 10, 80, 35), this, 0x128);

SpinBuddy = reinterpret_cast<CStatic *>(GetDlgItem(IDC_SPIN_BUDDY));
SpinCtrl->SetBuddy(SpinBuddy);

return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}

If the buddy window has already been set and you want to find what control performs
that role, you can call the CSpinButtonCtrl::GetBuddy() method. Its syntax is:

CWnd* GetBuddy( ) const;

This method returns a handle to the control that acts as the buddy window of the spin
button that called it.

One of the most important actions you should perform after creating a spin button is to
specify its lowest and its highest values. The default range is 100 (lowest) to 0 (highest).
This causes the spin button to count in decrement. If you do not want this (bizarre)
behavior, you must explicitly set the lower and higher values.

To set the minimum and maximum values of a spin button, call either the
CSpinButtonCtrl::SetRange() or the CSpinButtonCtrl::SetRange32() methods. Their
syntaxes are:

void SetRange( int nLower, int nUpper );
void SetRange32( int nLower, int nUpper );

In both cases the nLower argument holds the minimum value and the nUpper argument
specifies the maximum value. Here is an example:

BOOL CSpinDlg::OnInitDialog()
{
Free download pdf