Visual C++ and MFC Fundamentals Chapter 17: Track-Based Controls
CDialog::OnInitDialog();
// TODO: Add extra initialization here
CSpinButtonCtrl *SpinCtrl = new CSpinButtonCtrl;
SpinCtrl->Create(WS_CHILD | WS_VISIBLE | UDS_SETBUDDYINT,
CRect(60, 10, 80, 35), this, 0x128);
SpinCtrl->SetRange(-12, 1244);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
If the control exists already and you want to get its minimum and maximum values, call
either the CSpinButtonCtrl::GetRange() or the CSpinButtonCtrl::GetRange32()
methods. The possible syntaxes used are:
DWORD GetRange() const;
void GetRange(int &lower, int& upper) const;
void GetRange32(int &lower, int &upper) const;
Based on the range of values that a spin button can handle, the user can increment and
decrement the control’s value. By default, the values are are incremented by adding 1 and
decrement by adding –1 to the current value. If you want to increment and decrement by
a different value, you have two main options. You can write a routine to take care of this,
or call the CSpinButtonCtrl::SetAccel() method. Its syntax is:
BOOL SetAccel(int nAccel, UDACCEL* pAccel );
The SetAccel() method takes a UDACCEL value and its size as arguments. The
UDACCEL class is defined as follows:
typedef struct {
UINT nSec;
UINT nInc;
}UDACCEL, FAR *LPUDACCEL;
The nSec member variable is a semi-timer that ticks at a specified rate of seconds.
The nInc member variable of this structure defines the incremental value to apply when
the nSec value has passed.
The nAccel argument of the SetAccel() method is the size of the UDACCEL class.
In the following example, a spin button was added to a dialog box and a control variable
named m_Spin was added for it. When the user clicks the arrow buttons or presses the
arrow keys, the value of the spin button is incremented by 5:
BOOL CDlgSpin::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
UDACCEL udAccel;
int SizeOfAccel = sizeof(UDACCEL);
udAccel.nSec = 100;