| or Call: 713-252-0207

Tweet Me!

Make A Digital Clock ψ A Software How-To

How To Make A Custom Digital Clock

I wanted an alarm clock that would play an MP3 of my choice and could be turned up real loud to wake me up. I wanted the ability to have multiple alarms as well. This is a C#, WPF application with quite a few options as well as a seperate clock module that has a ticker tape messaging system.

Quake USA Alarm Clock

The form below will hide the main form and will act like a digital clock. The numbers are custom images I created in Vue (3D Software), and added them as resources to the project. Then it is just a matter of showing the correct digits to match the time. The clock has "Ticker Tape" messages that scroll across from right to left. What makes this object unique is that it is a WPF Custom Control. There is a reference made to it - WpfUserControl.DLL to include the control in the project. So that you can reference it directly you can add using WpfUserControl;

The routine private void ShowJustClock() takes care of showing the control.
var frmJustClock = new JustClock(); Creates an object reference to the control.
frmJustClock.ShowDialog(); Shows the control, at the same time hiding the main form.

Quake USA Alarm Clock

The program has several options as to alarm settings. One time or recurring, time based (i.e. 8:10 AM), or by day (i.e. Sunday, Monday, Tuesday, etc...), or by period (i.e. Week, Month, or Year). When an MP3 is selected, a volume test can be done to adjust speakers. Any type of recurring alarm gets saved to an XML file and each alarm is a line item displayed on the main form under "Active Alarms". To create a new alarm one only has to click on a date in the calendar control, this will initiate the new alarm entry. Double clicking on an alarm entry in the listbox will delete it.

Quake USA Alarm Clock

Inside the code, the first thing to notice is the creation of two timers using System.Windows.Threading.DispatcherTimer(), one for the application and one for the alarms: appTimer and alarmTimer The application timer handles updating the progressbar that is used as a visual aid to denote the seconds past, and the time and date display. appTimer updates every second, and the alarmTimer tick fires every 15 seconds.

Second thing to note is that a reference to Windows Media player is needed to be able to play MP3's from within the program. Add "Interop.WMPLib" to the program references. This is Windows Media Player library. Now a Windows Media player object is created:
WMPLib.WindowsMediaPlayer wmp_AlarmPlayerTest = new WMPLib.WindowsMediaPlayer();

When the program starts, it loads any existing alarms; private void LoadAlarms() takes care of processing the XML storage file utilizing the XmlSerializer to parse the data directly into the programs AlarmList.

public void CreateNewAlarm(string sShortDate = "") takes care of creating a new alarm from the options picked and adding it to the list. The routine public void UpdateListOfAlarms() clears the current list and creates the new list from the internal alarm list derived from public class Alarm in the Alarm.cs file. Of note is the fact that the alarm options window has all the field checking and logic to validate the entry as a new alarm, so the main program does not need any validation for the alarm date; it only needs to add it to the list.

The heart of the program is private void alarmTimer_Tick(object sender, EventArgs e) which processes all existing alarms to see if any need to be triggered. Hitting the "Stop" button will stop the MP3 from playing by calling the routine MediaControl() and the alarm will be updated if it is recurring alarm or deleted from the list if it was a one-time alarm.

public void MediaControl(bool bool_Start, string MediaPath = "") handles playing the selected MP3 for the alarm and handles the stopping of the alarm. Given 'true' to the routine and the path to a valid MP3 file, the player will begin song playback for the alarm. A call to the same routine and passing 'false' will cause the player to stop any playback.

To stop the program, several things need to be done to exit properly. All exits, from the menu or otherwise; will trigger the routine public void CleanUp() which will serialze the alarm data for storage to the XML file, and both timers are stoped as well as closing the media player.

The complete source code can be found here: MyAlarmClock.

End Of Line Man!