Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Updated Calendar Control

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Updated Calendar Control

    Borje You did a great job with this!
    I just added a bit more flexibility for formatting the output,
    and added the verification step that the Win32api suggests on the systemtime.
    This is a very neat control!

    Code:
    '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    ' A small DDT example of how to create and use Commctrl's Calendar.
    ' Written by Borje Hagsten. Updated by Scott Turchin for easier formatting of output
    ' Public Domain = free To use And modify.
    ' Ideas for usage: Databases of all kinds, "Insert date" functions
    ' in text editors, diaries, PIMs, etc..  :-) Or setting the expiration date on an application
    '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    #Compile Exe
    #Include "WIN32API.INC"
    #Include "COMMCTRL.INC"
    
    %ID_LABEL    = 10
    %ID_CALENDAR = 20
    
    Declare CallBack Function DlgCallback()
    Declare Function GetTimeandDate(sTime As SYSTEMTIME) As String
    
    
    '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    ' PBMAIN - build dialog and controls
    '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    Function PbMain
        Dim hDlg As Long
    
        Local iccex As INIT_COMMON_CONTROLSEX  'Initialize the calendar
        iccex.dwSize = SizeOf(iccex)
        iccex.dwICC = %ICC_DATE_CLASSES
        Call InitCommonControlsEx(iccex)
    
        Dialog New 0, "Calendar demo",,, 196, 60, %WS_SYSMENU To hDlg
        Control Add Label, hDlg, %ID_LABEL, "Click at the Combo's down arrow.", 6, 30, 180, 14
        Control Add Button, hDlg, %IDCANCEL, "&Close", 134, 6, 50, 14
    
        Control Add "SysDateTimePick32", hDlg, %ID_CALENDAR, "", 6, 6, 120, 14, %WS_CHILD Or _
            %WS_TABSTOP Or %WS_VISIBLE Or %DTS_LONGDATEFORMAT, , Call DlgCallback
        'Note: You can also use "SysMonthCal32", to get the calendar only,
        '      but then you also have to set the height to e.g. 100 ..  :-)
        'See Commctrl.inc for other useful Constants and Type structures
    
        Dialog Show Modal hDlg Call DlgCallback
    End Function
    
    '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    'Format your output as you like
    '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    Function GetTimeandDate(sTime As SYSTEMTIME) Export As String
    Local tDay              As Asciiz * 64
    Local tTime             As Asciiz * 64
    GetDateFormat %LOCALE_USER_DEFAULT, %NULL, sTime, "MMM dd',' yyyy", tDay, 64       'Use this to format the date
    GetTimeFormat %LOCALE_SYSTEM_DEFAULT, %TIME_NOSECONDS,sTime, ByVal %NULL, tTime, 64 'Use this to format the time
    Function = tDay + " " + tTime
    End Function
    
    
    '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    ' The main callback function for all controls
    '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    CallBack Function DlgCallback()
    Local hdrX As NMDATETIMECHANGE Ptr
    
    
       Select Case CbMsg
            Case %WM_COMMAND
             If CbCtl = %IDCANCEL Then Dialog End CbHndl, (CbCtl = %IDOK)
    
            Case %WM_NOTIFY
             'Use this to detect changes in the calendar
             hdrX = CbLparam
             Select Case @hdrX.hdr.code
                    Case %DTN_CLOSEUP
                    Case %DTN_DROPDOWN
                    Case %DTN_DATETIMECHANGE  'Get selected date/time
                        If @hdrx.dwFlags <> %GDT_VALID Then Exit Function 'Trust but verify SYSTEMTIME STRUCTURE
                        Control Set Text CbHndl, %ID_LABEL, "Selected Date: " & GetTimeandDate(@hdrX.st)
             End Select
             
       End Select
    End Function
    '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    ' The End
    '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    ------------------
    Scott
    mailto:[email protected][email protected]</A>
    Scott Turchin
    MCSE, MCP+I
    http://www.tngbbs.com
    ----------------------
    True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi

  • #2
    I can't compile, i'm getting this:
    Error 460: Undefined equate
    Line 22: %WS_TABSTOP Or %WS_VISIBLE Or %DTS_LONGDATEFORMAT, , Call DlgCallback


    ------------------
    -

    Comment


    • #3
      Check your commctrl.inc, it should contain these plus a few functions etc:

      %DTS_UPDOWN = &H0001 ' use UPDOWN instead of MONTHCAL
      %DTS_SHOWNONE = &H0002 ' allow a NONE selection
      %DTS_SHORTDATEFORMAT = &H0000 ' use the short date format (app must forward WM_WININICHANGE messages)
      %DTS_LONGDATEFORMAT = &H0004 ' use the long date format (app must forward WM_WININICHANGE messages)
      %DTS_TIMEFORMAT = &H0009 ' use the time format (app must forward WM_WININICHANGE messages)
      %DTS_APPCANPARSE = &H0010 ' allow user entered strings (app MUST respond to DTN_USERSTRING)
      %DTS_RIGHTALIGN = &H0020 ' right-align popup instead of left-align it


      ------------------
      Scott
      mailto:[email protected][email protected]</A>
      Scott Turchin
      MCSE, MCP+I
      http://www.tngbbs.com
      ----------------------
      True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi

      Comment


      • #4
        What is the structure of NMDATETIMECHANGE please, we can find no reference to it in any include files?

        ------------------

        Comment


        • #5
          It's in the commctrl.inc file:

          Code:
          TYPE NMDATETIMECHANGE
              hdr AS NMHDR
              dwFlags AS DWORD        ' GDT_VALID or GDT_NONE
              st AS SYSTEMTIME        ' valid iff dwFlags==GDT_VALID
          END TYPE
          ------------------
          Scott
          Scott Turchin
          MCSE, MCP+I
          http://www.tngbbs.com
          ----------------------
          True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi

          Comment


          • #6
            I've set up the variables as such:

            TYPE NMDATETIMECHANGE
            hdr AS NMHDR
            dwFlags AS DWORD
            st AS SYSTEMTIME
            END TYPE

            TYPE NMHDR
            hwndFrom AS LONG
            idfrom AS LONG
            code AS LONG
            END TYPE

            TYPE SYSTEMTIME
            wYear AS INTEGER
            wDay AS INTEGER
            wDayOfWeek AS INTEGER
            wMonth AS INTEGER
            wHour AS INTEGER
            wMinute AS INTEGER
            wSecond AS INTEGER
            wMilliseconds AS INTEGER
            END TYPE

            and DIMmed hdrX as NMDATETIMECHANGE PTR

            but after hdrX = CBLPARAM, @hdrX.st.wYear (and everything else
            under st, except for wMonth which gives the day) is equal to 0.
            What's happening?

            I've defined the id for the Month Calendar control as 1002
            because I couldn't find the equate %ID_CALENDAR

            ------------------

            Comment


            • #7
              Hi Scott,

              I am experimenting with the Calendar control you posted

              I tried adding these controls :

              %DTS_UPDOWN = &H0001 ' use UPDOWN instead of MONTHCAL
              %DTS_SHOWNONE = &H0002 ' allow a NONE selection
              %DTS_SHORTDATEFORMAT = &H0000 ' use the short date format (app must forward WM_WININICHANGE messages)
              %DTS_LONGDATEFORMAT = &H0004 ' use the long date format (app must forward WM_WININICHANGE messages)
              %DTS_TIMEFORMAT = &H0009 ' use the time format (app must forward WM_WININICHANGE messages)
              %DTS_APPCANPARSE = &H0010 ' allow user entered strings (app MUST respond to DTN_USERSTRING)
              %DTS_RIGHTALIGN = &H0020 ' right-align popup instead of left-align it

              after the DTS_first in the commctrl.inc file but it still wouldn't compile.

              Any tips ?

              Thanks,

              Daniel


              ------------------
              mailto:[email protected][email protected]</A>
              mailto:[email protected][email protected]</A>

              Comment


              • #8
                Make sure you are using the latest WIN32API.INC file set - you can get it from http://www.powerbasic.com/files/pub/pbwin/win32api.zip

                BTW, these kind of questions are off-topic for this forum - please create a new thread in the appropriate forum and include a link to this thread. Thanks!


                ------------------
                Lance
                PowerBASIC Support
                mailto:[email protected][email protected]</A>
                Lance
                mailto:[email protected]

                Comment


                • #9
                  Code:
                  '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
                  ' A small DDT example of how to create and use Commctrl's Calendar.
                  ' Written by Borje Hagsten. Updated by Scott Turchin for easier formatting of output
                  ' [b]Julian code added by Jim Hobbs[/b]
                  ' Public Domain = free To use And modify.
                  ' Ideas for usage: Databases of all kinds, "Insert date" functions
                  ' in text editors, diaries, PIMs, etc.. :-) Or setting the expiration date on an application
                  '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
                  #COMPILE EXE
                  #INCLUDE "WIN32API.INC"
                  #INCLUDE "COMMCTRL.INC"
                  %ID_LABEL = 10
                  %ID_LABEL1 = 15
                  %ID_CALENDAR = 20
                  %ID_TextBox  = 25
                  
                  
                  DECLARE CALLBACK FUNCTION DlgCallback()
                  DECLARE FUNCTION GetTimeandDate(sTime AS SYSTEMTIME) AS STRING
                  DECLARE FUNCTION leapyear( year AS INTEGER) AS INTEGER
                  DECLARE FUNCTION  julian(DAY AS INTEGER,  MONTH AS INTEGER, YEAR AS INTEGER) AS INTEGER
                  DECLARE SUB  GetJul()
                  DECLARE SUB  JulCal()
                  GLOBAL  TheDate     AS STRING, _
                          YEAR        AS INTEGER, _
                          MONTH       AS INTEGER,_
                          DAY         AS INTEGER, _
                          julday      AS INTEGER, _
                          daysleft    AS INTEGER, _
                          total       AS INTEGER, _
                           wDate      AS ASCIIZ * 64, _
                           szJul      AS ASCIIZ * 12, _
                         CENTURY      AS ASCIIZ * 5,  _
                         tDay         AS ASCIIZ * 64,  _
                         tTime        AS ASCIIZ * 64
                  
                  '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
                  ' PBMAIN - build dialog and controls
                  '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
                  FUNCTION PBMAIN
                  DIM hDlg AS LONG
                  LOCAL iccex AS INIT_COMMON_CONTROLSEX 'Initialize the calendar
                  iccex.dwSize = SIZEOF(iccex)
                  iccex.dwICC = %ICC_DATE_CLASSES
                  CALL InitCommonControlsEx(iccex)
                  DIALOG NEW 0, "Calendar Julian demo ",,, 236, 60, %WS_SYSMENU TO hDlg
                  CONTROL ADD LABEL, hDlg, %ID_LABEL,  "Click at the Combo's down arrow.", 6, 30, 180, 14
                  CONTROL ADD BUTTON, hDlg, %IDCANCEL, "&Close" ,185, 30, 42, 14
                  CONTROL ADD LABEL, hDlg, %ID_LABEL1, "Jul    Days Left", 179, 0, 60, 14
                  CONTROL ADD TEXTBOX, hDlg,%ID_TextBox," Julian " ,176, 8, 50,14, %ES_LEFT
                  CONTROL ADD "SysDateTimePick32", hDlg, %ID_CALENDAR, "", 6, 8, 164, 14, %WS_CHILD OR _
                  %WS_TABSTOP OR %WS_VISIBLE OR %DTS_LONGDATEFORMAT, , CALL DlgCallback
                  'Note: You can also use "SysMonthCal32", to get the calendar only,
                  ' but then you also have to set the height to e.g. 100 .. :-)
                  'See Commctrl.inc for other useful Constants and Type structures
                  DIALOG SHOW MODAL hDlg CALL DlgCallback
                  
                  END FUNCTION
                  '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
                  'Format your output as you like
                  '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
                  FUNCTION GetTimeandDate(sTime AS SYSTEMTIME) EXPORT AS STRING
                  
                  GetDateFormat %LOCALE_USER_DEFAULT, %NULL, sTime, "MMM dd',' yyyy", tDay, 64 'Use this to format the date
                  GetTimeFormat %LOCALE_SYSTEM_DEFAULT, %TIME_NOSECONDS,sTime, BYVAL %NULL, tTime, 64 'Use this to format the time
                  GetJul    ' claculate Julian date
                  
                  
                  FUNCTION = tDay + " " + tTime + " " + szJul
                  END FUNCTION
                  
                  '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
                  ' The main callback function for all controls
                  '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
                  CALLBACK FUNCTION DlgCallback()
                  LOCAL hdrX AS NMDATETIMECHANGE PTR
                  
                  SELECT CASE CBMSG
                  CASE %WM_COMMAND
                  IF CBCTL = %IDCANCEL THEN DIALOG END CBHNDL, (CBCTL = %IDOK)
                  CASE %WM_NOTIFY
                  'Use this to detect changes in the calendar
                  hdrX = CBLPARAM
                  SELECT CASE @hdrX.hdr.code
                  CASE %DTN_CLOSEUP
                  CASE %DTN_DROPDOWN
                  CASE %DTN_DATETIMECHANGE 'Get selected date/time
                  IF @hdrx.dwFlags <> %GDT_VALID THEN EXIT FUNCTION 'Trust but verify SYSTEMTIME STRUCTURE
                   CONTROL SET TEXT CBHNDL, %ID_LABEL, "Selected Date: " & GetTimeandDate(@hdrX.st)
                   CONTROL SET TEXT CBHNDL, %ID_TextBox,   szJul
                  END SELECT
                  
                  END SELECT
                  END FUNCTION
                  
                  SUB GetJul()
                                 wDate = TRIM$(tDay)
                                 YEAR     = VAL(RIGHT$(wDate,2))
                                 DAY      = VAL(MID$(wDate,5,2))
                                 CENTURY  = MID$(wDate,9,2)
                       SELECT CASE LEFT$(wDate,3)
                  
                                  CASE  = "Jan"
                                      Month = 01
                                  CASE  = "Feb"
                                     Month  = 02
                                  CASE  = "Mar"
                                      Month = 03
                                  CASE  = "Apr"
                                     Month  = 04
                  
                                  CASE  = "May"
                                      Month = 05
                                  CASE  = "Jun"
                                     Month  = 06
                                  CASE  = "Jul"
                                      Month = 07
                                  CASE  = "Aug"
                                     Month  = 08
                  
                                  CASE  = "Sep"
                                      Month = 09
                                  CASE  = "Oct"
                                     Month  = 10
                                  CASE  = "Nov"
                                      Month = 11
                                  CASE  = "Dec"
                                     Month  = 12
                                  CASE ELSE
                  
                              Month = 00
                              MSGBOX "month is invalid"
                  
                      END SELECT
                  
                  
                    LOCAL cc$,yy$,mm$,dd$
                    cc$ =  TRIM$(CENTURY)
                  
                    IF  YEAR < 10 THEN
                         yy$ = TRIM$("0") &  TRIM$(STR$(YEAR))
                       ELSE
                         yy$ = TRIM$(STR$(YEAR))
                     END IF
                  
                     IF Month < 10 THEN
                        mm$ =  TRIM$("0") &  TRIM$(STR$(Month))
                      ELSE
                        mm$ =    TRIM$(STR$(Month))
                      END IF
                  
                      dd$ = TRIM$(STR$(DAY))
                  
                   TheDate = TRIM$(mm$) & "-" & TRIM$(dd$)& "-" & TRIM$(cc$) & TRIM$(yy$)
                   JulCal
                   szJul = STR$(JulDay)& "/" & TRIM$(STR$(daysleft) )
                  END SUB
                  
                  SUB julCal()
                  
                       'mm-dd-ccyy'
                  
                      YEAR     = VAL(RIGHT$(TheDate,2))
                      MONTH    = VAL(LEFT$(TheDate,2))
                      DAY      = VAL(MID$(TheDate,4,2))
                      julday   = julian(DAY,MONTH,YEAR)
                      daysleft = 365 - julday
                  END SUB
                  
                  FUNCTION leapyear( year AS INTEGER) AS INTEGER
                  
                     IF (year / 4 = 0 AND year / 100 <> 0 OR year / 400 = 0) THEN
                        FUNCTION = 1
                     ELSE
                        FUNCTION = 0
                     END IF
                  
                  END FUNCTION
                  
                   FUNCTION  julian(day AS INTEGER,  month AS INTEGER, year AS INTEGER) AS INTEGER
                  
                    DIM runsum(12) AS INTEGER
                    runsum(0) = 000
                    runsum(1) = 031
                    runsum(2) = 059
                    runsum(3) = 090
                    runsum(4) = 120
                    runsum(5) = 151
                    runsum(6) = 181
                    runsum(7) = 212
                    runsum(8) = 243
                    runsum(9) = 273
                    runsum(10) = 304
                    runsum(11) = 334
                    runsum(12) = 365
                  
                   'STATIC INT runsum[] = {0, 31, 59, 90, 120, 151, 181, 212,
                   '                         243, 273, 304, 334, 365};
                  
                   '   INT total;
                  
                     total = runsum(month - 1) + day
                     IF month > 2 THEN
                        total = total +  leapyear(year)
                      END IF
                     FUNCTION = total
                  
                  END FUNCTION
                  
                  '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
                  ' The End
                  '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
                  ------------------

                  Comment

                  Working...
                  X