home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / SPIN.ZIP / SPIN.C
Text File  |  1991-10-04  |  4KB  |  127 lines

  1. This is code some code for using spinbuttons as requested on Compuserve.
  2. Sorry its not in a state to compile as it comes from a large app that uses
  3. Client-Server architecture. I have extracted the relevant parts.
  4. It is used to get a date from the user where the date is usually Today or very
  5. close. It must deal with the number of days in the month and also leap years.
  6.  
  7. I hope it helps you.
  8. Don't forget to use 1.3 Header files.
  9. Regards
  10.  
  11. Toby
  12.  
  13.  
  14.  
  15.  
  16.       /*.RC file*/
  17.  
  18. BEGIN
  19.     DIALOG "Update", IDLG_UPDATE, 9, 40, 302, 190,
  20.        FS_NOBYTEALIGN | FS_DLGBORDER | WS_VISIBLE | WS_CLIPSIBLINGS | WS_SAVEBITS,
  21.        FCF_TITLEBAR
  22.     BEGIN
  23.     CONTROL "", ID_SB_DAY, 20, 30, 40, 20, WC_SPINBUTTON, WS_TABSTOP | WS_VISIBLE |
  24.         SPBS_MASTER | SPBS_READONLY | SPBS_JUSTCENTER | SPBS_NUMERICONLY
  25.     CONTROL "", ID_SB_MONTH, 60, 30, 40, 20, WC_SPINBUTTON, WS_VISIBLE |
  26.         SPBS_MASTER | SPBS_READONLY | SPBS_JUSTCENTER
  27.     CONTROL "", ID_SB_YEAR, 100, 30, 40, 20, WC_SPINBUTTON, WS_VISIBLE |
  28.         SPBS_MASTER | SPBS_READONLY | SPBS_JUSTCENTER | SPBS_NUMERICONLY
  29.     CONTROL "Send", DID_OK, 19, 6, 38, 13,
  30.                 WC_BUTTON, BS_PUSHBUTTON | BS_DEFAULT | WS_TABSTOP | WS_VISIBLE
  31.     CONTROL "Cancel", DID_CANCEL, 79, 6, 38, 13,
  32.         WC_BUTTON, BS_PUSHBUTTON | WS_TABSTOP | WS_VISIBLE
  33.     END
  34. END
  35.  
  36.  
  37.  
  38.  
  39.  
  40.     /*Message loop for dialog box with spinbuttons*/
  41.  
  42.  
  43.     case WM_INITDLG:            /*set range and value for spinbuttons*/
  44.     DosGetDateTime(&DateTime);
  45.     WinSendDlgItemMsg(hWndDlg, ID_SB_YEAR, SPBM_SETLIMITS, (MPARAM)1999L, (MPARAM)1900L);    /*This centuary only*/
  46.     WinSendDlgItemMsg(hWndDlg, ID_SB_YEAR, SPBM_SETCURRENTVALUE, (MPARAM)DateTime.year, NULL);
  47.     WinSendDlgItemMsg(hWndDlg, ID_SB_MONTH, SPBM_SETLIMITS, (MPARAM)11L, (MPARAM)0L);
  48.     WinSendDlgItemMsg(hWndDlg, ID_SB_MONTH, SPBM_SETCURRENTVALUE, (MPARAM)(DateTime.month -1), NULL);
  49.     WinSendDlgItemMsg(hWndDlg, ID_SB_MONTH, SPBM_SETARRAY, (MPARAM)pszMonths, (MPARAM)12);
  50.     WinSendDlgItemMsg(hWndDlg, ID_SB_DAY, SPBM_SETLIMITS, (MPARAM)31L, (MPARAM)1L);
  51.     WinSendDlgItemMsg(hWndDlg, ID_SB_DAY, SPBM_SETCURRENTVALUE, (MPARAM)DateTime.day, NULL);
  52.     break;
  53.  
  54.     case WM_CONTROL:  /*Work done here is to control SB's range only*/
  55.     switch(SHORT1FROMMP(mp1))
  56.     {
  57.         case ID_SB_DAY:
  58.         case ID_SB_MONTH:
  59.         switch (SHORT2FROMMP(mp1))
  60.         {
  61.             case SPBN_CHANGE:    /*spin button used*/
  62.             {
  63.             LONG lMonth;
  64.             WinSendDlgItemMsg(hWndDlg,     /*query month*/
  65.                       ID_SB_MONTH,
  66.                       SPBM_QUERYVALUE,
  67.                       &lMonth,         /*Dont allow values out of range*/
  68.                       MPFROM2SHORT(NULL, SPBQ_UPDATEIFVALID));
  69.             switch(lMonth)    /*Set days' spinbutton's limits according to days in month*/
  70.             {        /*Months are zero based*/
  71.                 case 0: case 2: case 4: case 6: case 7: case 9: case 11:
  72.                 WinSendDlgItemMsg(hWndDlg, ID_SB_DAY, SPBM_SETLIMITS, (MPARAM)31L, (MPARAM)1L);
  73.                 break;
  74.                 case 1:
  75.                 WinSendDlgItemMsg(hWndDlg,
  76.                           ID_SB_YEAR,
  77.                           SPBM_QUERYVALUE, /*Get year*/
  78.                           &lYear,
  79.                           MPFROM2SHORT(NULL, SPBQ_UPDATEIFVALID));
  80.                 if ((lYear % 4))  /*leap year*/
  81.                     WinSendDlgItemMsg(hWndDlg, ID_SB_DAY, SPBM_SETLIMITS, (MPARAM)28L, (MPARAM)1L);
  82.                 else
  83.                     WinSendDlgItemMsg(hWndDlg, ID_SB_DAY, SPBM_SETLIMITS, (MPARAM)29L, (MPARAM)1L);
  84.                 break;
  85.                 default:
  86.                 WinSendDlgItemMsg(hWndDlg, ID_SB_DAY, SPBM_SETLIMITS, (MPARAM)30L, (MPARAM)1L);
  87.                 break;
  88.             }
  89.             }
  90.             break;
  91.         }
  92.         case ID_SB_YEAR:
  93.         DosSleep(5L);    /*slow spinbutton down, it's very quick!*/
  94.         break;
  95.     }
  96.     break;
  97.  
  98.  
  99.  
  100.  
  101.        /*Function to get date from spinbuttons*/
  102.  
  103.  
  104.  
  105. USHORT GetDateFromDialogBox(HWND hWnd,
  106.                 PCHAR szDate)
  107. {
  108.     LONG lDay, lMonth, lYear;
  109.     WinSendDlgItemMsg(hWnd,
  110.               ID_SB_DAY,
  111.               SPBM_QUERYVALUE,
  112.               &lDay,
  113.               MPFROM2SHORT(NULL, SPBQ_UPDATEIFVALID));
  114.     WinSendDlgItemMsg(hWnd,
  115.               ID_SB_MONTH,
  116.               SPBM_QUERYVALUE,
  117.               &lMonth,
  118.               MPFROM2SHORT(NULL, SPBQ_UPDATEIFVALID));
  119.     WinSendDlgItemMsg(hWnd,
  120.               ID_SB_YEAR,
  121.               SPBM_QUERYVALUE,
  122.               &lYear,
  123.               MPFROM2SHORT(NULL, SPBQ_UPDATEIFVALID));
  124.     sprintf(szDate, "%lu-%lu-%lu",lDay, lMonth+1,lYear);
  125.     return 0;                /*months zero based*/
  126. }
  127.