home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / dt01 / dt01.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1995-09-06  |  15.1 KB  |  372 lines

  1. VERSION 2.00
  2. Begin Form frmCalendar 
  3.    AutoRedraw      =   -1  'True
  4.    BackColor       =   &H00C0C0C0&
  5.    BorderStyle     =   0  'None
  6.    ClientHeight    =   1020
  7.    ClientLeft      =   1335
  8.    ClientTop       =   1710
  9.    ClientWidth     =   1950
  10.    ForeColor       =   &H00000000&
  11.    Height          =   1425
  12.    Left            =   1275
  13.    ScaleHeight     =   1020
  14.    ScaleWidth      =   1950
  15.    Top             =   1365
  16.    Width           =   2070
  17.    Begin PictureBox gpMonthSpin 
  18.       BackColor       =   &H00C0C0C0&
  19.       Height          =   252
  20.       Index           =   2
  21.       Left            =   1320
  22.       ScaleHeight     =   225
  23.       ScaleWidth      =   270
  24.       TabIndex        =   2
  25.       Top             =   120
  26.       Width           =   300
  27.    End
  28.    Begin PictureBox gpMonthSpin 
  29.       BackColor       =   &H00C0C0C0&
  30.       Height          =   252
  31.       Index           =   1
  32.       Left            =   360
  33.       ScaleHeight     =   225
  34.       ScaleWidth      =   270
  35.       TabIndex        =   3
  36.       Top             =   120
  37.       Width           =   300
  38.    End
  39.    Begin PictureBox pic 
  40.       AutoRedraw      =   -1  'True
  41.       BackColor       =   &H00C0C0C0&
  42.       BorderStyle     =   0  'None
  43.       FontTransparent =   0   'False
  44.       ForeColor       =   &H00000000&
  45.       Height          =   372
  46.       Left            =   480
  47.       ScaleHeight     =   375
  48.       ScaleWidth      =   375
  49.       TabIndex        =   0
  50.       Top             =   480
  51.       Width           =   372
  52.    End
  53.    Begin Timer TmrMonthSpin 
  54.       Enabled         =   0   'False
  55.       Interval        =   200
  56.       Left            =   1320
  57.       Top             =   480
  58.    End
  59.    Begin Label lblMonthText 
  60.       Alignment       =   2  'Center
  61.       BackColor       =   &H00C0C0C0&
  62.       Caption         =   "lMonth"
  63.       Height          =   192
  64.       Left            =   720
  65.       TabIndex        =   1
  66.       Top             =   120
  67.       Width           =   564
  68.    End
  69. Option Explicit
  70.     ' Create form level globals?
  71.     Dim nCurrentYear As Integer
  72.     Dim nCurrentMonth As Integer
  73.     Dim nCurrentDay As Integer
  74.     Dim nStartDay As Integer
  75.     Dim nTotalDays As Integer
  76.     Dim nBlockNdx As Integer
  77.     Dim nCopyBlockNdx As Integer
  78.     Dim nBlockHeight As Integer
  79.     Dim nWidth As Integer
  80.     Dim nHeight As Integer
  81. Sub Form_Activate ()
  82.     ' Initialize form level date variables.
  83.     ' -------------------------------------
  84.     If IsDate(gDate) Then
  85.         nCurrentYear = Year(gDate)
  86.         nCurrentMonth = Month(gDate)
  87.         nCurrentDay = Day(gDate)
  88.     Else
  89.         nCurrentYear = Year(Now)
  90.         nCurrentMonth = Month(Now)
  91.         nCurrentDay = Day(Now)
  92.     End If
  93.     ' print days of the month.
  94.     ' ------------------------
  95.     PrintMonth
  96. End Sub
  97. '================================================
  98. ' = Get all the static non-moving bits out here =
  99. '================================================
  100. Sub Form_Load ()
  101.     Dim i As Integer
  102.     Dim nOldWidth As Integer
  103.     ' Set width/height of one char.
  104.     ' -----------------------------
  105.     nWidth = TextWidth("M") ' Change this for bigger/smaller calendars.
  106.     nHeight = nWidth * 1.9
  107.     ' resize the form.
  108.     ' ----------------
  109.     Me.Height = (nHeight * 6) + (nHeight * .75)
  110.     Me.Width = ((nWidth * 2) * 7) + (nWidth * 1.25)
  111.     ' position left/right arrows.
  112.     ' ---------------------------
  113.     gpMonthSpin(1).Top = nHeight / 4
  114.     gpMonthSpin(2).Top = nHeight / 4
  115.     gpMonthSpin(1).Left = nWidth / 2
  116.     gpMonthSpin(2).Left = Width - gpMonthSpin(1).Width - (nWidth / 2)
  117.     ' position month label between l/r arrows.
  118.     ' ----------------------------------------
  119.     lblMonthText.Top = nHeight / 4
  120.     lblMonthText.Left = gpMonthSpin(1).Left + gpMonthSpin(1).Width
  121.     lblMonthText.Width = gpMonthSpin(2).Left - lblMonthText.Left
  122.     ' size background panel.
  123.     ' ----------------------
  124.     pic.Top = (nHeight * 2.25)
  125.     pic.Left = (nWidth / 2)
  126.     pic.Width = ((nWidth * 2) * 7) + 20
  127.     pic.Height = (nHeight * 4) + 50
  128.     ' Output Day text.
  129.     ' ----------------
  130.     For i = 1 To 7
  131.         CurrentY = nHeight * 1.25
  132.         CurrentX = (i * (nWidth * 2)) - (nWidth * 1.5)
  133.         Print Mid$("SuMoTuWeThFrSa", i * 2 - 1, 2)
  134.     Next
  135.     ' draw separator line + shadow.
  136.     ' -----------------------------
  137.     Line (0, nHeight * 2)-(Width, nHeight * 2), QBColor(0)
  138.     Line (0, nHeight * 2 + (nHeight / 29))-(Width, nHeight * 2 + (nHeight / 29)), QBColor(15)
  139.     ' Attempt at a 3D border.
  140.     ' -----------------------
  141.     nOldWidth = Me.DrawWidth
  142.     Me.DrawWidth = 10
  143.     Me.Line (-30, -30)-Step(Me.Width + 50, 0), QBColor(15)
  144.     Me.Line -Step(0, Me.Height + 40), QBColor(8)
  145.     Me.Line -Step(-(Me.Width + 50), 0), QBColor(8)
  146.     Me.Line -Step(0, -(Me.Height + 40)), QBColor(15)
  147.     Me.DrawWidth = nOldWidth
  148. End Sub
  149. ' =============================================================
  150. ' Name.........: GetNumDaysInMonth(nYear, nMonth)
  151. ' Description..: Computes the number of days in any given month
  152. ' Parameters...: <nYear>  - needed to check for leap years
  153. '                <nMonth> - the month number (1-12)
  154. ' Returns......: An integer representing the days in the month
  155. ' =============================================================
  156. Function GetNumDaysInMonth (nYear As Integer, nMonth As Integer) As Integer
  157.     Dim cMonth As String, nDays As Integer
  158.     cMonth = "312831303130313130313031"
  159.     ' Set defaults.
  160.     ' -------------
  161.     If nYear < 100 Or nYear > 9999 Then nYear = Year(Now)
  162.     If nMonth < 1 Or nMonth > 12 Then nMonth = Month(Now)
  163.     ' Set the number of days in the requested month.
  164.     ' ----------------------------------------------
  165.     nDays = Val(Mid$(cMonth, nMonth * 2 - 2 + 1, 2))
  166.     ' Compensate if requested year is a leap year, and month is February.
  167.     ' -------------------------------------------------------------------
  168.     If IsLeapYear(nYear) And nMonth = 2 Then nDays = nDays + 1
  169.     GetNumDaysInMonth = nDays
  170. End Function
  171. Sub gpMonthSpin_MouseDown (Index As Integer, Button As Integer, Shift As Integer, x As Single, y As Single)
  172.     gpMonthSpin(Index).PictureDnChange = 2
  173.     TmrMonthSpin.Interval = 500
  174.     TmrMonthSpin.Enabled = True
  175.     TmrMonthSpin.Tag = Choose(Index, -1, 1)
  176.     nCurrentMonth = nCurrentMonth + TmrMonthSpin.Tag
  177.     PrintMonthText
  178. End Sub
  179. Sub gpMonthSpin_MouseUp (Index As Integer, Button As Integer, Shift As Integer, x As Single, y As Single)
  180.     gpMonthSpin(Index).PictureDnChange = 0
  181.     ' turn off timer
  182.     TmrMonthSpin.Enabled = False
  183.     PrintMonth
  184. End Sub
  185. ' =============================================================
  186. ' Name.........: IsLeapYear( nYear )
  187. ' Description..:  Determines if a year is a leap year, or not.
  188. ' Parameters...: <nYear>  -
  189. ' Returns......: An integer (boolean). True = it is a leap year
  190. ' =============================================================
  191. Function IsLeapYear (nYear)
  192.    ' If the year is evenly divisible by 4 and not divisible
  193.    ' by 100, or if the year is evenly divisible by 400, then
  194.    ' it's a leap year.
  195.    IsLeapYear = (nYear Mod 4 = 0 And nYear Mod 100 <> 0) Or (nYear Mod 400 = 0)
  196. End Function
  197. Sub pic_Click ()
  198.     ' Return to 'sub-level' code.
  199.     ' ---------------------------
  200.     If nCurrentDay > 0 Then
  201.         gDate = DateSerial(nCurrentYear, nCurrentMonth, nCurrentDay)
  202.         Me.Hide
  203.     End If
  204. End Sub
  205. Sub pic_MouseDown (Button As Integer, Shift As Integer, x As Single, y As Single)
  206.     ' Just pass it along to "MouseMove".
  207.     ' ----------------------------------
  208.     pic_MouseMove Button, Shift, x, y
  209. End Sub
  210. Sub pic_MouseMove (Button As Integer, Shift As Integer, x As Single, y As Single)
  211.     Dim i  As Integer
  212.     Dim xt As Integer, x1 As Integer, x2 As Integer
  213.     Dim yt As Integer, y1 As Integer, y2 As Integer
  214.     ' OK. The mouse is moving over the picture. Do we care?
  215.     ' Only if the left mouse button is pressed.
  216.     ' We then need to find out which part of the picture,
  217.     ' the mouse is over, and change the shadow state.
  218.     If (Button = 1) Then
  219.         For i = 1 To 42
  220.             
  221.             yt = Int((i - 1) / 7) + 1
  222.             xt = i - (Int((yt - 1) * 7))
  223.             y1 = (yt - 1) * nBlockHeight: y2 = yt * nBlockHeight
  224.             x1 = (xt - 1) * (nWidth * 2): x2 = xt * (nWidth * 2)
  225.             If (x >= x1) And (x <= x2) And (y >= y1) And (y <= y2) Then nBlockNdx = i: Exit For
  226.         Next
  227.         If (nBlockNdx <> nCopyBlockNdx) And (nBlockNdx > 0) And (nBlockNdx - nStartDay <= nTotalDays) And (nBlockNdx - nStartDay > 0) Then
  228.             
  229.             PrintDay nCopyBlockNdx, 0, 0, 0
  230.             nCopyBlockNdx = nBlockNdx
  231.             nCurrentDay = nBlockNdx - nStartDay
  232.             PrintDay nCopyBlockNdx, 1, 0, 0
  233.             
  234.         End If
  235.     End If
  236. End Sub
  237. Sub pic_MouseUp (Button As Integer, Shift As Integer, x As Single, y As Single)
  238.     pic_Click
  239. End Sub
  240. ' ===============================================================
  241. ' Name.........: PrintDay( nDayIndex, lBorder, lBold, nCaption )
  242. ' Description..: Draws / Clears the border around a box
  243. ' Parameters...: <nDayIndex>  - Number of box to deal with (1-42)
  244. '                <lSetBorder> - True  = draw a 3D border
  245. '                               false = clear the border
  246. ' ===============================================================
  247. Sub PrintDay (nCurrBlock, lBorder As Integer, lBold As Integer, nCaption As Integer)
  248.     Dim x As Integer, x1 As Integer, x2 As Integer
  249.     Dim y As Integer, y1 As Integer, y2 As Integer
  250.     Dim cCaption As String
  251.     ReDim aBorderColours(4)
  252.         
  253.     ' Setup colours for border / no border.
  254.     ' -------------------------------------
  255.     If lBorder Then
  256.         aBorderColours(1) = 0
  257.         aBorderColours(2) = 15
  258.         aBorderColours(3) = 15
  259.         aBorderColours(4) = 0
  260.     Else
  261.         aBorderColours(1) = 7
  262.         aBorderColours(2) = 7
  263.         aBorderColours(3) = 7
  264.         aBorderColours(4) = 7
  265.     End If
  266.     y = Int((nCurrBlock - 1) / 7) + 1
  267.     x = nCurrBlock - (Int((y - 1) * 7))
  268.     y1 = (y - 1) * nBlockHeight: y2 = y * nBlockHeight
  269.     x1 = (x - 1) * (nWidth * 2): x2 = x * (nWidth * 2)
  270.         
  271.     pic.Line (x1, y1)-(x2, y1), QBColor(aBorderColours(1))
  272.     pic.Line (x2, y1)-(x2, y2), QBColor(aBorderColours(2))
  273.     pic.Line (x2, y2)-(x1, y2), QBColor(aBorderColours(3))
  274.     pic.Line (x1, y2)-(x1, y1), QBColor(aBorderColours(4))
  275.     ' Set Bold/Unbold attribute (only Bold if it's today)
  276.     ' and print caption (only if there is a caption to print!)
  277.     If nCaption > 0 Then
  278.             
  279.         pic.FontBold = False: pic.ForeColor = QBColor(0)
  280.         If lBold Then pic.FontBold = True: : pic.ForeColor = QBColor(4)
  281.         cCaption = CStr(nCaption)
  282.         pic.CurrentX = x1 + ((x2 - x1) - TextWidth(cCaption)) / 2
  283.         pic.CurrentY = y1 + ((y2 - y1) - TextHeight(cCaption)) / 2
  284.         pic.Print cCaption
  285.     End If
  286. End Sub
  287. ' =============================================================
  288. ' Name.........: PrintMonth()
  289. ' Description..: Output month text & numbers
  290. ' Notes........: This is a 'mega-slow' procedure. It's a pity
  291. '                we can't do without it.
  292. ' =============================================================
  293. Sub PrintMonth ()
  294.     Static nCopyYear As Integer  ' Saved, so we don't needlessly print the same
  295.     Static nCopyMonth As Integer ' month twice.
  296.     Dim nCount  As Integer
  297.     Dim nWeeks As Integer
  298.     Dim nCaption As Integer
  299.     If (nCurrentYear <> nCopyYear Or nCurrentMonth <> nCopyMonth) Then
  300.         pic.Visible = False
  301.         pic.Cls
  302.         nCopyYear = nCurrentYear: nCopyMonth = nCurrentMonth
  303.         
  304.         ' ======================================================
  305.         ' First day in a month.
  306.         ' An integer between 1 (Sunday) and 7 (Saturday)
  307.         ' that represents the day of the week for a date argument.
  308.         ' ======================================================
  309.         nStartDay = Weekday(DateSerial(nCurrentYear, nCurrentMonth, 1)) - 1
  310.         
  311.         ' ======================================================
  312.         ' Total days in a month.
  313.         ' An integer between 1 and ( 28 or 29 or 30 or 31 )
  314.         ' that represents the number of days in a month.
  315.         ' ======================================================
  316.         nTotalDays = GetNumDaysInMonth(nCurrentYear, nCurrentMonth)
  317.         ' ======================================================
  318.         ' Total weeks in a month.
  319.         ' An integer between 4 and 6
  320.         ' that represents the number of weeks in a month.
  321.         ' ======================================================
  322.         nWeeks = Int((nTotalDays + nStartDay) / 7) + Sgn((nTotalDays + nStartDay) Mod 7)
  323.         
  324.         ' ======================================================
  325.         ' Calculate vertical space needed to display the days
  326.         ' ======================================================
  327.         nBlockHeight = (pic.Height - 50) / nWeeks
  328.         PrintMonthText
  329.         ' ======================================================
  330.         ' Adjust 'Current Day' In case it's .GT. 'total days'
  331.         ' ======================================================
  332.         While nCurrentDay > nTotalDays: nCurrentDay = nCurrentDay - 1: Wend
  333.         nBlockNdx = nCurrentDay + nStartDay
  334.         nCopyBlockNdx = nBlockNdx
  335.         ' ==============================================
  336.         '  Output the month 'Captions'
  337.         ' ==============================================
  338.         For nCount = 1 To nWeeks * 7
  339.             
  340.             nCaption = IIf((nCount >= nStartDay + 1) And (nCount < nTotalDays + nStartDay + 1), nCount - nStartDay, 0)
  341.             
  342.             PrintDay nCount, 0, nCurrentYear = Year(Now) And nCurrentMonth = Month(Now) And nCount - nStartDay = Day(Now), nCaption
  343.         Next
  344.         ' ==============================================
  345.         ' Draw the border around selected day.
  346.         ' ==============================================
  347.         PrintDay nCurrentDay + nStartDay, 1, 0, 0
  348.         pic.Visible = True
  349.     End If
  350. End Sub
  351. ' =============================================================
  352. ' Name.........: PrintMonthText()
  353. ' Description..: Output month text
  354. ' =============================================================
  355. Sub PrintMonthText ()
  356.     If nCurrentMonth > 12 Then nCurrentMonth = 1: nCurrentYear = nCurrentYear + 1
  357.     If nCurrentMonth < 1 Then nCurrentMonth = 12: nCurrentYear = nCurrentYear - 1
  358.     nCurrentYear = IIf(nCurrentYear > 9999, 9999, nCurrentYear)
  359.     nCurrentYear = IIf(nCurrentYear < 100, 100, nCurrentYear)
  360.     lblMonthText.Caption = Format$(DateSerial(nCurrentYear, nCurrentMonth, 1), "mmmm yyyy")
  361.     Me.Refresh
  362. End Sub
  363. Sub TmrMonthSpin_Timer ()
  364.     ' Speed up the timer, on each call.
  365.     ' ---------------------------------
  366.     TmrMonthSpin.Interval = TmrMonthSpin.Interval * .8
  367.     ' Update the current month, and print text.
  368.     ' ----------------------------------------
  369.     nCurrentMonth = nCurrentMonth + TmrMonthSpin.Tag
  370.     PrintMonthText
  371. End Sub
  372.