How to implrment “Spin control”

Chang Chi 61 Reputation points
2024-03-05T09:13:38.64+00:00

Hi,

 I need help getting “spin control” working in Dialog box.

 I chose to use static box and spin control;

They are a part of modeless dialog box

 

I set SETBUDDY, and AUTOBUDDY to be true

I setBuddy hStatic10 and hSpin2.

 

I entered “ABC1”, “DEF5”, GHL10, QRS15 to Static box

I entered range to be 1 to 3; and initial position to be 2

These commands don’t seem to work.

 

When I press the arrow the static box shows 1,2,3 etc

 

Would someone tell me where I made mistakes and errors.

I am not familiar with how things are done with spin control.

Is there a book or article that shows how to do the spin control, in C language?

 I work with C-language on window 11, using Visual Studio 2022

Thank you very much in advance

 

ChangChiTheGraphics

 

A part of dialogproc, and the dialog code are below.

 

BOOL CALLBACK Ac11ALessProc(HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam) {

     static HWND hCtrlBlock,hButton1,hButton2 ;

     static int  iColor, iFigure,iStart ;

     static HWND hList,hList1, hList2, hList3,hEdit1,hSpin1, hSpin2;

     static HWND hStatic1, hStatic2, hSTATIC10;

     static char szText[MAXPATH+1],szFile[MAXPATH+1];

     static char  bufftmp[50],CurDir[MAXPATH];

     char szSum[256];

     PAINTSTRUCT ps ;

     int I, ncode;

     HDC hdc ;

     HBRUSH hBrush ;

     static HFONT hFont ;

     RECT rect ;

 

     switch(iMsg){

          case WM_INITDIALOG :

                      hList= GetDlgItem (hDlg, IDC_LIST1) ;

                      hList2= GetDlgItem (hDlg, IDC_LIST2) ;

                      hList3= GetDlgItem (hDlg, IDC_LIST3) ;

 

                      hStatic10 = GetDlgItem(hDlg, IDC_STATIC10);

                      hSpin2 = GetDlgItem(hDlg, IDC_SPIN2);

 

                      SendMessage(hSpin2, UDM_SETBUDDY,(WPARAM)hStatic10, 0);

      

                      strcpy(bufftmp, "ABC1\nDEF5\nGHL10\nQRS15\n");

                      SetWindowTextA(hStatic10, bufftmp);

 

                      SendMessage(hSpin2, UDM_SETRANGE, 0, MAKELPARAM(3, 1));

                      SendMessage(hSpin2, UDM_SETPOS, 0, 2);

 

                      InvalidateRect(ViewWindow, NULL, FALSE);

                      break; //return TRUE ;

          case WM_COMMAND :

               switch (LOWORD (wParam)){

               case IDOK : 

                break;  

               case IDC_STATIC10:                             

                      SetWindowTextA(hStatic10, "ABC1\nDEF5\nGHL10\nQRS15\n");

                      break;

               case IDC_SPIN2:

                      ncode = ((LPNMHDR)lParam)->code;

                      break;

               }

          case WM_NOTIFY:

                      ncode = ((LPNMHDR)lParam)->code;

                      switch (ncode) {

                      case UDN_DELTAPOS:                     

                         LRESULT lr = SendMessage(hSpin2, UDM_GETPOS, 0, 0);

                         if (HIWORD(lr) == 0) {

                             WORD pos = LOWORD(lr);

                             return TRUE;

                         }

                      }//switch (ncode)

   }// switch(iMsg)

     return FALSE ;

}//Ac11ALessProc

AC11ALESS DIALOGEX 600, 2, 49, 198

STYLE DS_SETFONT | WS_POPUP | WS_VISIBLE | WS_CAPTION

CAPTION "    Sel Area"

FONT 11, "MS Sans Serif", 0, 0, 0x1

BEGIN

    PUSHBUTTON      "OK",IDOK,8,100,36,14,BS_CENTER | WS_GROUP

    GROUPBOX        "Select Area",IDC_STATIC1,3,60,46,39

    CTEXT           "Static",IDC_TEXT,9,39,29,14,SS_CENTERIMAGE | SS_SUNKEN | WS_BORDER,WS_EX_CLIENTEDGE | WS_EX_STATICEDGE

    PUSHBUTTON      "Up",IDC_BUTTON1,8,70,35,14

    PUSHBUTTON      "Down",IDC_BUTTON2,8,84,35,14

    GROUPBOX        "Area ID",IDC_STATIC2,2,30,44,27

    PUSHBUTTON      "Abort",IDCANCEL,18,115,25,16,BS_CENTER | WS_GROUP,WS_EX_DLGMODALFRAME | WS_EX_CLIENTEDGE | WS_EX_STATICEDGE

    CONTROL         "",IDC_C20,"C20",WS_GROUP | WS_TABSTOP,12,11,24,15

    GROUPBOX        "Tutorial",IDC_C7,6,1,35,27

 

    LTEXT           "Static",IDC_STATIC10,4,162,43,13,WS_BORDER,WS_EX_DLGMODALFRAME | WS_EX_CLIENTEDGE

    CONTROL         "",IDC_SPIN2,"msctls_updown32",UDS_SETBUDDYINT | UDS_AUTOBUDDY | UDS_ARROWKEYS,37,161,10,14

END

Developer technologies | C++
{count} votes

Accepted answer
  1. RLWA32 49,636 Reputation points
    2024-03-06T09:51:02.14+00:00

    @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;
    }
    
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Castorix31 90,686 Reputation points
    2024-03-06T09:22:05.6533333+00:00

    To change the text, you must remove UDS_SETBUDDYINT

    I tested in old SDK sample updown.c with a string array and SetDlgItemText at the end of UDN_DELTAPOS

    0 comments No comments

  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.