Spinner Control Example

To create a Tab control specify UPDOWN_CLASSW as the window class and register the class by specifying the ICC_UPDOWN_CLASS bit flag in the accompanying INITCOMMONCONTROLSEX structure.

When one of the up-down arrows is pressed the UDN_DELTAPOS message is sent in the form of a WM_NOTIFY message. The UDM_SETBUDDY message sets the edit control to be the buddy window for the UpDown control and The UDS_SETBUDDYINT flag causes the UpDown control to send a message (WM_SETTEXT) to its buddy when its position changes. The UDM_SETRANGE message sets the minimum and maximum positions for the UpDown control..

//Spinner control demo
#include <windows.h>
#include <commctrl.h>
#pragma comment(lib, "comctl32.lib")
#define ID_UPDOWN 1
#define ID_EDIT 2
#define ID_STATIC 3
#define UD_MAX_POS 30
#define UD_MIN_POS 0
HWND hUpDown, hEdit,hStatic;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void CreateControls(HWND);

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,PWSTR lpCmdLine, int nCmdShow) {

MSG msg;
WNDCLASS wc = {0};
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpszClassName = TEXT("Updown control");
wc.hInstance = hInstance;
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
wc.lpfnWndProc = WndProc;
wc.hCursor = LoadCursor(0, IDC_ARROW);

RegisterClass(&wc);
CreateWindow(wc.lpszClassName, TEXT("Updown control"), WS_OVERLAPPEDWINDOW | WS_VISIBLE,100, 100, 280, 200, NULL, NULL, hInstance, NULL);

while (GetMessage(&msg, NULL, 0, 0))
{
DispatchMessage(&msg);
}

return (int) msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {

LPNMUPDOWN lpnmud;
UINT code;

switch(msg) {

case WM_CREATE:
CreateControls(hwnd);
break;
//respond to spinner control button clicks
case WM_NOTIFY:
code = ((LPNMHDR) lParam)->code;
if (code == UDN_DELTAPOS) {
lpnmud = (NMUPDOWN *) lParam;
int value = lpnmud->iPos + lpnmud->iDelta;
if (value < UD_MIN_POS)
{
value = UD_MIN_POS;
}

if (value > UD_MAX_POS)
{
value = UD_MAX_POS;
}
const int asize = 4;
size_t cbDest = asize * sizeof(wchar_t);
TCHAR buf[32];
wsprintf(buf,TEXT("%i"), value);
SetWindowText(hStatic, buf);
}

break;

case WM_DESTROY:
PostQuitMessage(0);
break;
}

return DefWindowProc(hwnd, msg, wParam, lParam);
}

void CreateControls(HWND hwnd)
{
// load common control class ICC_UPDOWN_CLASS from the dynamic-link library (DLL).
INITCOMMONCONTROLSEX icex;
icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
icex.dwICC = ICC_UPDOWN_CLASS;
InitCommonControlsEx(&icex);

//create spinner window
hUpDown = CreateWindowW(UPDOWN_CLASSW, NULL, WS_CHILD | WS_VISIBLE | UDS_SETBUDDYINT | UDS_ALIGNRIGHT, 0, 0, 120, 120, hwnd, (HMENU) ID_UPDOWN, NULL, NULL);
//create buddy edit window
hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("edit"), NULL, WS_CHILD | WS_VISIBLE | ES_RIGHT, 15, 15, 70, 25, hwnd, (HMENU) ID_EDIT, NULL, NULL);
//create static window
hStatic = CreateWindow(TEXT("static"), NULL,WS_BORDER | WS_CHILD | WS_VISIBLE | SS_LEFT,90, 16, 30, 23,hwnd, (HMENU) ID_STATIC, NULL, NULL);
//set up child window parameters
SendMessageW(hUpDown, UDM_SETBUDDY, (WPARAM) hEdit, 0);
SendMessageW(hUpDown, UDM_SETRANGE, 0, MAKELPARAM(UD_MAX_POS, UD_MIN_POS));
SendMessageW(hUpDown, UDM_SETPOS, 0, 200);
}