@Chang Chi Following is another example of using the spin control to set the text of a static control.
resource.h
//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file.
// Used by SpinSample.rc
//
#define IDD_SPINSAMPLE 101
#define IDC_SPIN1 1001
#define IDC_STATIC1 1002
// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 103
#define _APS_NEXT_COMMAND_VALUE 40001
#define _APS_NEXT_CONTROL_VALUE 1003
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif
SpinSample.rc
// Microsoft Visual C++ generated resource script.
//
#include "resource.h"
#define APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include "winres.h"
/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
// English (United States) resources
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
#pragma code_page(1252)
#ifdef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// TEXTINCLUDE
//
1 TEXTINCLUDE
BEGIN
"resource.h\0"
END
2 TEXTINCLUDE
BEGIN
"#include ""winres.h""\r\n"
"\0"
END
3 TEXTINCLUDE
BEGIN
"\r\n"
"\0"
END
#endif // APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Dialog
//
IDD_SPINSAMPLE DIALOGEX 0, 0, 309, 95
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | DS_CENTER | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Spin Sample"
FONT 10, "MS Shell Dlg", 400, 0, 0x1
BEGIN
LTEXT "Static",IDC_STATIC1,91,31,77,13,0,WS_EX_STATICEDGE
CONTROL "",IDC_SPIN1,"msctls_updown32",UDS_ARROWKEYS | WS_TABSTOP,171,31,11,14
PUSHBUTTON "Close",IDCANCEL,255,76,50,14
END
/////////////////////////////////////////////////////////////////////////////
//
// DESIGNINFO
//
#ifdef APSTUDIO_INVOKED
GUIDELINES DESIGNINFO
BEGIN
IDD_SPINSAMPLE, DIALOG
BEGIN
LEFTMARGIN, 5
RIGHTMARGIN, 304
TOPMARGIN, 5
BOTTOMMARGIN, 90
END
END
#endif // APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// AFX_DIALOG_LAYOUT
//
IDD_SPINSAMPLE AFX_DIALOG_LAYOUT
BEGIN
0
END
#endif // English (United States) resources
/////////////////////////////////////////////////////////////////////////////
#ifndef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 3 resource.
//
/////////////////////////////////////////////////////////////////////////////
#endif // not APSTUDIO_INVOKED
SpinSample.c
#include <Windows.h>
#include <CommCtrl.h>
#include "resource.h"
#include <stdio.h>
#define WM_SPINCHANGE WM_APP + 42
// Uncomment #pragma directive to use Visual Styles
/*
#pragma comment(linker, "/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' \
version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
*/
INT_PTR CALLBACK DlgProc(HWND, UINT, WPARAM, LPARAM);
int APIENTRY WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdline, _In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdline);
HWND hwnd = CreateDialogA(hInstance, MAKEINTRESOURCEA(IDD_SPINSAMPLE), NULL, DlgProc);
if (!hwnd)
{
char szMsg[128];
sprintf_s(szMsg, _countof(szMsg), "CreateDialogA failed with error %d", GetLastError());
MessageBoxA(NULL, szMsg, "Error", MB_ICONERROR);
return 1;
}
ShowWindow(hwnd, nCmdShow);
MSG msg;
while (GetMessageA(&msg, NULL, 0, 0))
{
if (!IsDialogMessageA(hwnd, &msg))
{
TranslateMessage(&msg);
DispatchMessageA(&msg);
}
}
return (int) msg.wParam;
}
INT_PTR CALLBACK DlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
static HWND hStatic = NULL, hSpin = NULL;
const char* aItems[] = {
"Sample string Item 1",
"Sample string Item 2",
"Sample string Item 3",
"Sample string Item 4",
};
static int iLow = 0, iHigh = _countof(aItems) - 1; // Index into array of strings for display
switch (msg)
{
case WM_INITDIALOG:
{
hStatic = GetDlgItem(hDlg, IDC_STATIC1);
hSpin = GetDlgItem(hDlg, IDC_SPIN1);
SendMessageA(hSpin, UDM_SETRANGE, 0, MAKELPARAM(iHigh, iLow));
SendMessageA(hSpin, UDM_SETPOS, 0, iLow);
SendMessageA(hStatic, WM_SETTEXT, 0, (LPARAM)aItems[iLow]);
return (INT_PTR)TRUE;
}
case WM_COMMAND:
{
if (LOWORD(wParam) == IDCANCEL)
DestroyWindow(hDlg);
}
break;
case WM_SPINCHANGE:
{
int newPos = LOWORD(SendMessageA(hSpin, UDM_GETPOS, 0, 0));
SendMessageA(hStatic, WM_SETTEXT, 0, (LPARAM) aItems[newPos]);
}
break;
case WM_NOTIFY:
{
LPNMHDR phdr = (LPNMHDR)lParam;
if (phdr->code == UDN_DELTAPOS)
PostMessageA(hDlg, WM_SPINCHANGE, 0, 0); // WM_NOTIFY received before spin control reflects new position
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return (INT_PTR)FALSE;
}