Using Controls in the Microsoft Foundation Class Library - Common Controls (Page 2 of 4 )
The Common Controls offer a modern, stylish interface accompanied by some advanced utilities; because of this, they are more complex than the standard controls. Here we can include the evolution bar (the one that appears upon copying a file), the slide button (used for volume control, for example), the spinner (we'll cover this in a moment), image list, list view, tree view, status, and toolbar.
The spinner is used to force the user to choose only from a range of values. This control has the advantage of occupying just a little space and can include a wide range of numbers. The user can modify the value in it with the power of a click on one of the arcs. The creation process starts, as with anything, by declaring a variable that will handle the controller. The class for this button is the CSpinButtonCtrl:
CSpinButtonCtrl _spin;
Just as with standard controls, now we create it in the OnCreate() section. The rest is the same as with standard controls, so I won't cover all that again. Additional information is required. Here we have some different styles that are specific to the spinner. Like the UDS_SETBUDDYINT, this tells the application that we want to create a box near it that contains int values.
Because it isn't all that practical to create a couple of arrows, most of the time you would also show the current value near the arrows. For this we need to assign a standard control to the spinner (we'll use an edit control) in which the application will automatically draw the current value. For this you create a CEdit control and you assign it to the spinner. After this, you set it as a buddy with the SetBuddy function of the spinner.
Build the application, if you want to see the results. A default range value of 100 to 0 will be assigned. For setting other, custom values use the SetRange function and for the start value, the SetPos. Look at the following code snippet and all should be clear.
int CMFC_exampleView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
// the spinner
_edit3.Create(WS_CHILD | WS_VISIBLE | WS_BORDER | ES_READONLY,
CRect(10, 110, 50, 130),this,IDC_SPINNER);
_spin.Create(WS_CHILD | WS_VISIBLE | WS_BORDER |
UDS_SETBUDDYINT | UDS_ALIGNRIGHT ,
CRect(10, 110, 50, 130),this,IDC_SPINNER);
_spin.SetBuddy( &_edit3);
_spin.SetRange(1, 10);
_spin.SetPos( 8);
return 0;
}
Again, you can locate the full code and the application in the attached zip file, located in the final section of this article. This is one of the simpler controls. For more in-depth information turn to a book or an online step-by-step tutorial, because we are moving to the final part of this article, covering image handling.
Next: Image Handling >>
More BrainDump Articles
More By Gabor Bernat