Spinner (Updown) Control Demo

To create a Spinner (Up-Down) control specify UPDOWN_CLASS as the window class and register the class by specifying the ICC_UPDOWN_CLASS bit flag in the accompanying INITCOMMONCONTROLSEX

#define _WIN32_WINNT 0x0501 #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; HWND hEdit; HWND hStatic; LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM); void CreateControls(HWND); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MSG msg; WNDCLASS wc; wc.style=CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc=WndProc; wc.cbClsExtra=0; wc.cbWndExtra=0; wc.hInstance=hInstance; wc.hIcon=LoadIcon(NULL,IDI_APPLICATION); wc.hCursor=LoadCursor(NULL,IDC_ARROW); wc.hbrBackground=GetSysColorBrush(COLOR_3DFACE); wc.lpszMenuName=NULL; wc.lpszClassName=TEXT("Updown control"); RegisterClass(&wc); CreateWindow( TEXT("Updown control"), TEXT("Updown Control"), WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 100, 280, 200, NULL, NULL, hInstance, NULL); while(GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch(msg) { case WM_CREATE: CreateControls(hwnd); break; case WM_NOTIFY: { LPNMUPDOWN nm; nm=(LPNMUPDOWN)lParam; if(nm->hdr.code==UDN_DELTAPOS) { int value; value=nm->iPos + nm->iDelta; if(value < UD_MIN_POS) value=UD_MIN_POS; if(value > UD_MAX_POS) value=UD_MAX_POS; TCHAR buf[32]; wsprintf(buf,TEXT("%d"),value); SetWindowText( hStatic, buf); } } break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd,msg,wParam,lParam); } return 0; } void CreateControls(HWND hwnd) { INITCOMMONCONTROLSEX icex; icex.dwSize=sizeof(INITCOMMONCONTROLSEX); icex.dwICC=ICC_UPDOWN_CLASS; InitCommonControlsEx(&icex); // Buddy edit box hEdit=CreateWindowEx( WS_EX_CLIENTEDGE, TEXT("edit"), TEXT("10"), WS_CHILD | WS_VISIBLE | ES_RIGHT, 15, 15, 70, 25, hwnd, (HMENU)ID_EDIT, GetModuleHandle(NULL), NULL); // Spinner control hUpDown=CreateWindow( UPDOWN_CLASS, NULL, WS_CHILD | WS_VISIBLE | UDS_SETBUDDYINT | UDS_ALIGNRIGHT | UDS_ARROWKEYS, 0, 0, 120, 120, hwnd, (HMENU)ID_UPDOWN, GetModuleHandle(NULL), NULL); // Display value hStatic=CreateWindow( TEXT("static"), TEXT("10"), WS_BORDER | WS_CHILD | WS_VISIBLE | SS_LEFT, 100, 16, 50, 23, hwnd, (HMENU)ID_STATIC, GetModuleHandle(NULL), NULL); // Attach edit box as buddy SendMessage( hUpDown, UDM_SETBUDDY, (WPARAM)hEdit, 0); // Set range SendMessage( hUpDown, UDM_SETRANGE, 0, MAKELPARAM(UD_MAX_POS,UD_MIN_POS)); // Set starting position SendMessage( hUpDown, UDM_SETPOS, 0, MAKELPARAM(10,0)); }