SPIN CONTROL

This article was contributed by: Hartwell Bryan
To create a simple spin button:

1.      Point 0:

    a)  Use AppWizard to create a Dialog based app. Call it: ExoSpinText.
	Click ok. Deselect About Dialog and ActiveX Control. Click Finish.

    b)  Once you are in the Visual Studio, press the following combination:
        Alt -> b -> o to access the Set Active Configuration... dialog box.
        Click ExoSpinText - Win32 Release and press Enter.

2.      Resources:

    a)  Pick up an Edit box from the Control toolbox and drop it in your dialog.
        Change the identifier of that Edit to IDC_EDIT. Change the Style to read
	only.

    b)  get a Spin control from the Control toolbox, drop that Spin control on
        your dialog, preferably next on the right of the Edit box.
        Press Alt+Enter to access the Spin Properties dialog.
        from the General tab, change the Spin's ID to IDC_SPIN. Hit enter, do
	not modify the Styles. Alternatively you may select Wrap. 

    c)  Press Ctrl+W to access the ClassWizard. Click the Member Variables tab.
        Make sure that the Class name: is CExoSpinTextDlg.
        Double-click IDC_SPIN and Add Member Variable as: m_cSpin, press Enter.
	Double-click IDC_EDIT and Add Member Variable as: m_strEdit, press Enter.


3.      More detail on acceleration.

    a)  While you are still in the MFC ClassWizard,
        and the Class name: is CExoSpinTextDlg, click the Message Maps.
        In the Member functions: listbox,
        double-click OnInitDialog to access its implementation.

	// TODO: Add extra initialization here
	m_cSpin.SetRange(1, 1000); //we should all be familiar with this by now
	UDACCEL uda[3];
	uda[0].nSec=0;
	uda[0].nInc=1; //Start incrementing by One
	uda[1].nSec=3;
	uda[2].nInc=5; //After 3 seconds increment by 5
	uda[3].nSec=5;
	uda[3].nInc=20; //5 seconds later (or t+8) increment by 20
	m_cSpin.SetAccel(3,uda); //the first argument is the number
				 //of elements in the array
	m_strEdit="Default"
	UpdateData(FALSE);


    c)  Hit F7. If you run your program and click on the arrows nothing will
	happen, the edit box is stuck on "Default". However if you apply these
	values to your other project you'll notice the numbers begin to leap
	in increasing values if you hold down the mouse button.

4.	Spinning Strings
	What if we don't want to see numbers? We would like to cycle through text.

    a)  Access the Class Wizzard by hitting Ctrl-W. Make sure you have the class
	CExoSpinTextDlg selected. Click on the object ID_SPIN and then Add Function.
	Click Ok, and then click Edit Text. Your function should look like this, I'll
	explain in detail later:

void CExoSpinDlg::OnDeltaposSpin(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_UPDOWN* spin = (NM_UPDOWN*)pNMHDR;

	switch((*spin).iPos+(*spin).iDelta){
	case 1: m_strEdit="This"; break;
	case 2: m_strEdit="is"; break;
	case 3: m_strEdit="fun!"; break;
	}
	
	UpdateData(FALSE);

	*pResult = 0;
}

This function activates when the Spin button is ABOUT to modify it's position,
that is the user clicked on the button but changes haven't been applied yet.
The first line is generated by the Wizard, but I modified it a little because
I didn't like the variable name.

NM_UPDOWN is a structure that contains three elements. We only care about two
of them: iPos and iDelta. The first is the current value, the second indicates
the change (+/- increment value).

The variable is a pointer, so spin.iPos is invalid. However spin->iPos IS valid.
The function call UpdateData(FALSE) causes the Edit Field to display its new value,
otherwise you will not see the change occur when you run the program. Finally the
value *pResult must be set to 0 or the Spin position will not get updated (this is
done by the Wizard so you don't need to be worried about).

 Copyright © 2002 Hartwell Bryan