Countdown to disaster - Part 7

Last Code

Saturday, November 8, 2008 by RomanDA | Discussion: DesktopX Tutorials

Ok those that are still with me, I have refined the code some, modified the graphics, and i belive the only part left will be to put the graphic in that will display when today is their birthday.  I have also added a "sound" object with a mp3 to play Happy Birthday.  The buttons have been replaced with balloons to fit into the images better.

Here is the code:

Code: vbscript
  1. Dim BDate,BMonth,BDay
  2. '--  for INI FILE
  3. Dim IniName, IniCnt, IniTitle(100), IniDetails(100)
  4. Const ForWriting =2 : Const ForReading = 1 : Const ForAppending = 8
  5. '--- End INI
  6. Dim HappyBirthdayToday
  7. Dim INIPath
  8. Sub Object_OnScriptEnter
  9.      Set objShell = CreateObject("WScript.Shell")
  10.     INIPath = objShell.RegRead("HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Personal")
  11.      Set objShell = nothing
  12.      If right(INIPath,1) <> "\" Then INIPath = INIPath & "\"
  13.     HappyBirthdayToday = False
  14.     IniName = "CommunityBday.ini" '---- INI FILE NAME
  15.     Call ReadINI() '-- Read INI File
  16.     BMonth = ReadINIValue("Month") '--Set Initial Values
  17.     BDay = ReadINIValue("Day")
  18.     Call SetMode(ReadINIValue("Mode"))
  19.     Call GetBDay
  20.     Object.SetTimer 1, 1000
  21. End Sub
  22. Sub Object_OnTimer1
  23.     CurTime = formatdatetime(now,3)
  24.      i = DateDiff("s", Now, BDate)
  25.   DaysLeft = i \ 86400
  26.   i = i Mod 86400
  27.   HoursLeft = i \ 3600
  28.   i = i Mod 3600
  29.   MinutesLeft = i \ 60
  30.   i = i Mod 60
  31.   SecondsLeft = i
  32.   desktopx.Object("Birthday-CD-Large-days").text = DaysLeft
  33.   desktopx.Object("Birthday-CD-Large-hrs").text = HoursLeft
  34.   desktopx.Object("Birthday-CD-Large-min").text = MinutesLeft
  35.   desktopx.Object("Birthday-CD-Large-sec").text = SecondsLeft
  36.   Call GetBDay()
  37. End Sub
  38. Sub Object_OnScriptExit
  39.  object.KillTimer 1
  40. End Sub
  41. Function SetMode(Mode)
  42.   desktopx.Object("Birthday-CD-Flame-BG").visible = False
  43.     Select Case Mode
  44.         Case "Mini"
  45.       desktopx.object("Birthday-CD-LargeBG").visible = False
  46.         desktopx.object("Birthday-CD-MiniBG").visible = True
  47.         FT = 48 : FL = 193
  48.         Call SetINIValue("Mode","Mini")
  49.         Case "Large"
  50.         desktopx.object("Birthday-CD-MiniBG").visible = False
  51.       desktopx.object("Birthday-CD-LargeBG").visible = True
  52.         FT = 74 : FL = 204
  53.       Call SetINIValue("Mode","Large")
  54.     End Select
  55.   desktopx.Object("Birthday-CD-Flame-BG").top = FT
  56.   desktopx.Object("Birthday-CD-Flame-BG").left = FL
  57.   desktopx.Object("Birthday-CD-Flame-BG").visible = True
  58. End Function
  59. Function Object_OnLButtonUpEx(obj,x,y,dragged)
  60.     If Not dragged Then
  61.         Select Case obj.name
  62.        
  63.     Case "Birthday-CD-LargeModeBtn"
  64.       Call SetMode("Large")
  65.     Case "Birthday-CD-MiniModeBTN"
  66.             Call SetMode("Mini")
  67.     Case "Birthday-CD-Mini-PrefsBTN"
  68.       Call PrefsMenu()
  69.      
  70.     Case "Birthday-CD-Large-PrefsBTN"
  71.       Call PrefsMenu()
  72.     Case "HappyBirthday"
  73.         Call ResetBirthday
  74.     Case "Birthday-CD-Large-Close-BTN"
  75.       widget.Close
  76.      
  77.     Case "Birthday-CD-Mini-Close-BTN"
  78.         widget.Close
  79.         End Select
  80.   End If
  81. End Function
  82. Function PrefsMenu()
  83.     Set frm = DesktopX.CreateForm
  84.     frm.caption = "Community Birthday Countdown Prefs"
  85.   frm.AddPreference "B-Month"
  86.   frm.Preference("B-Month").Type = "ComboList"
  87.   For x = 1 To 12
  88.       frm.Preference("B-Month").AddValue x
  89.   Next
  90.   frm.Preference("B-Month").DefaultValue = BMonth
  91.   frm.Preference("B-Month").Caption = "Month"
  92.   frm.AddPreference "B-Day"
  93.   frm.Preference("B-Day").Type = "ComboList"
  94.   For x = 1 To 31
  95.       frm.Preference("B-Day").AddValue x
  96.   Next
  97.   frm.Preference("B-Day").DefaultValue = BDay
  98.   frm.Preference("B-Day").Caption = "Day"
  99.     If frm.prompt Then
  100.         BMonth = frm.Preference("B-Month").value
  101.         BDay = frm.Preference("B-Day").value
  102.         Call SetINIValue("Month",BMonth)
  103.         Call SetINIValue("Day",BDay)
  104.     End If
  105.     Call GetBDay()
  106. End Function
  107. Function GetBDay()
  108.   BdayToday = False
  109.   testDate = FormatDate(BMonth &"/" & BDay &"/" & year(now))
  110.   CheckDate = IsDate(testDate)
  111.   If CheckDate <> True Then
  112.    Msgbox "Date: " & testDate & vbnewline & "is Invalid" & vbnewline & "Please select a new date"
  113.    Call PrefsMenu()
  114.   Else
  115.         s = DateDiff("s", Now, testdate)
  116.         If s > 0 Then
  117.             BDate = cdate(testdate)
  118.         Else
  119.           testDate = FormatDate(BMonth &"/" & BDay &"/" & (year(now)+1))
  120.             BDate = cdate(testdate)
  121.       End If
  122.       d = DateDiff("d", Now, testdate)
  123.       If d= 365 Then
  124.           Call HappyBirthday
  125.       Else
  126.           HappyBirthdayToday = False
  127.       End If
  128.       desktopx.Object("Birthday-CD-Mini-BDate").text = BDate
  129.       desktopx.Object("Birthday-CD-Large-BDate").text = BDate
  130.   End If
  131. End Function
  132. '---INI FILE FUNCTIONS ----
  133. Function WriteBlankINI()
  134.     '------------------ INI DEFAUTL VALUES HERE -----------------------
  135.     IniCnt = 0
  136.     IniCnt = IniCnt+ 1 : IniTitle(1) = "Mode" : IniDetails(1) = "Mini"
  137.     IniCnt = IniCnt+ 1 : IniTitle(2) = "Month"         : IniDetails(2) = 11
  138.     IniCnt = IniCnt+ 1 : IniTitle(3) = "Day"     : IniDetails(3) = 20
  139.         ' The INI File is Stored in the Directory where the Program is run
  140.     IniContents = ""
  141.     For x = 1 To INICnt   
  142.    IniContents = IniContents & IniTitle(x) & "|" & IniDetails(x) & vbnewline
  143.     Next
  144.     'fn = desktopx.ExecutableDirectory & IniName
  145.      fn = INIPath & IniName
  146.     System.SimpleWrite fn, IniContents, 1
  147. End Function
  148. Function ReadINI()
  149.     Call CheckForINI()
  150.     fn = INIPath & IniName
  151.     TextList = System.SimpleRead(fn, 1)
  152.   Lines = Split(TextList, Chr(10))
  153.     IniCnt = 0
  154.     For A = UBound(Lines) To LBound(Lines) Step -1
  155.          If Len(Lines(A)) > 0 Then
  156.           t = instr(Lines(A),"|")
  157.           IniCnt = IniCnt + 1
  158.           IniTitle(IniCnt) = left(Lines(A),t-1)
  159.           IniDetails(IniCnt) = mid(Lines(A),t+1)
  160.           l = len(IniDetails(IniCnt))
  161.           IniDetails(IniCnt) = left(IniDetails(IniCnt),l-1)
  162.       End If
  163.     Next
  164. End Function
  165. Function WriteINI()
  166.     ' The INI File is Stored in the Directory where the Program is run
  167.     IniContents = ""
  168.     For x = 1 To INICnt   
  169.    IniContents = IniContents & IniTitle(x) & "|" & IniDetails(x) & vbnewline
  170.     Next
  171.     fn = INIPath & IniName
  172.     System.SimpleWrite fn, IniContents, 1
  173. End Function
  174. Function CheckForINI()
  175.     'fn = desktopx.ExecutableDirectory & IniName
  176.     fn = INIPath & ININame
  177.     Set filesys = CreateObject("Scripting.FileSystemObject")
  178.     temp = filesys.FileExists(fn)
  179.     path = filesys.GetAbsolutePathName(fn)
  180.     If Not filesys.FileExists(fn) Then
  181.        Call WriteBlankINI()
  182.     End If
  183. End Function
  184. Function ReadINIValue(Item)
  185.     ReadINIValue = ""
  186.     For x = 1 To INICnt
  187.         If UCase(IniTitle(x)) = UCase(Item) Then    temp = IniDetails(x)     
  188.     Next
  189.     ReadINIValue = Temp
  190. End Function
  191. Function SetINIValue(Item,value)
  192.     For x = 1 To INICnt
  193.         If UCase(IniTitle(x)) = UCase(Item) Then    IniDetails(x) = Value
  194.     Next
  195.  Call WriteINI()
  196. End Function
  197. '---- FIX DATE FOR ANY LOCATION
  198. Function FormatDate(xdate)
  199.     On Error Resume Next
  200.   Dim ,s1,s2,sx
  201.      If instr(xdate,"/") > 0 Then
  202.       s1 = "/"
  203.      ElseIf instr(xdate,".") > 0 Then
  204.       s1 = "."
  205.      ElseIf instr(xdate,"-") > 0 Then
  206.       s1 = "-"
  207.      End If
  208.      Set objShell = CreateObject("WScript.Shell")
  209.   = objShell.RegRead("HKEY_CURRENT_USER\Control Panel\International\sShortDate")
  210.   s2 = objShell.RegRead("HKEY_CURRENT_USER\Control Panel\International\sDate")
  211.      Set objShell = nothing
  212.   sx = split(xdate,s1)
  213.   If left(LCase( ),1) = "d" Then = sx(1)&s2&sx(0)&s2&sx(2) Else = sx(0)&s2&sx(1)&s2&sx(2)
  214.   FormatDate = FormatDateTime( ,2)
  215.      Set = nothing
  216.      Set s1 = nothing
  217.      Set s2 = nothing
  218.      Set sx = nothing
  219. End Function
  220. Function HappyBirthday()
  221.     If HappyBirthdayToday = False Then
  222.         desktopx.object("Birthday-CD-PlaySong").state = "Play"
  223.       desktopx.Object("Today").text = "WOOT!"
  224.       HappyBirthdayToday = True
  225.       desktopx.object("Birthday-CD-LargeBG").visible = False
  226.       desktopx.object("Birthday-CD-MiniBG").visible = False
  227.         desktopx.Object("Birthday-CD-Flame-BG").visible = False
  228.         desktopx.Object("Birthday-CD-TimeBack").visible = False
  229.         desktopx.object("HappyBirthday").visible = True
  230.         msgbox "HAPPY BIRTHDAY!!" & vbnewline & "Click OK to stop music"
  231.         desktopx.object("Birthday-CD-PlaySong").state = "Stop"
  232.     End If
  233. End Function
  234. Function ResetBirthday()
  235.     desktopx.Object("Today").text = "   "
  236.     desktopx.object("HappyBirthday").visible = False
  237.     Call SetMode(ReadINIValue("Mode"))
  238.     desktopx.Object("Birthday-CD-TimeBack").visible = True
  239. End Function

This section of code controls what happens when TODAY is the birthday.

Code: vbscript
  1. Function HappyBirthday()
  2.     If HappyBirthdayToday = False Then
  3.         desktopx.object("Birthday-CD-PlaySong").state = "Play"
  4.       HappyBirthdayToday = True
  5.       desktopx.object("Birthday-CD-LargeBG").visible = False
  6.       desktopx.object("Birthday-CD-MiniBG").visible = False
  7.         desktopx.Object("Birthday-CD-Flame-BG").visible = False
  8.         desktopx.Object("Birthday-CD-TimeBack").visible = False
  9.         desktopx.object("HappyBirthday").visible = True
  10.         msgbox "HAPPY BIRTHDAY!!" & vbnewline & "Click OK to stop music"
  11.         desktopx.object("Birthday-CD-PlaySong").state = "Stop"
  12.     End If
  13. End Function

You can see in this line:
        desktopx.object("Birthday-CD-PlaySong").state = "Play"

That it simply changes the STATE of the object that I assigned the MP3 to.  This allows you to play the song "on command".
If left alone it will continue to play, so I added a MSGBOX to tell the person Happy Birthday and to STOP the playback of the song.

I also use the IF at the begining to stop this from showing every time the timer ticks off.  You can see that inside the code it adds HappyBirthdayToday = True so that the next time it will not show.  This is RESET back in the CheckBDay Code here:

Code: vbscript
  1.       If d= 365 Then
  2.           Call HappyBirthday
  3.       Else
  4.           HappyBirthdayToday = False
  5.       End If

This allows this to be reset the next day so that it will work again in 365 days (as if anyone's pc will be working no-stop for 1 year.. LOL)

The gadget is about done, I will post a comment with the link when its 100% done.  I will upload a WIDGET and a GADGET version so that if you want to take it apart and see how it works in more detail.

I hope that you have enjoyed this step into how a gadget is created, i know its not 100% perfect but it does allow you to get an idea on how this all works, and how to go about building your own. 

If anyone has any other ones they would like me to create, please let me know.  clock? calendar? weather? (ROFL).

RomanDA

RomanDA
Reply #1 Saturday, November 8, 2008 1:56 PM

oops forgot:

I added this line of code so that it always puts the INI file in the "My Documents" folder, this is important with Vista's security.

Code: vbscript
  1. INIPath = objShell.RegRead(" HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Personal ")
RomanDA
Reply #2 Thursday, November 13, 2008 8:41 PM

If anyone has an interest, I have finished this widget/gadget and uploaded it here:

https://www.wincustomize.com/skins.aspx?skinid=1434&libid=34

If you want the latest code, download the zip, extract the widget and IMPORT it into DX.

I really hope someone got something out of all this.. it was a lot of work.

Please login to comment and/or vote for this skin.

Welcome Guest! Please take the time to register with us.
There are many great features available to you once you register, including:

  • Richer content, access to many features that are disabled for guests like commenting on the forums and downloading skins.
  • Access to a great community, with a massive database of many, many areas of interest.
  • Access to contests & subscription offers like exclusive emails.
  • It's simple, and FREE!



web-wc01