C# Christmas Lights

Download ChristmasLights.zip (32 KB)

Updated 3/Jan/11: Upgraded project to VS2010 and changed the images to be embedded resources

This Christmas I decided to do a little festive coding. I remembered a program I had as a child that drew flashing Christmas lights across the top of the screen, and decided to recreate it in C#. Full source code is attached to this post.

The program support 3 different flashing sequences, each contained in its own class: FlashSequence, ColorSequence and AlternateSequence. Each sequence inherits from the abstract base class Sequence which makes it easy to add your own.

The only tricky part is knowing how to draw onto the desktop. It requires a couple of calls into the windows API. We start by creating signatures for the API calls:

[DllImport("User32.dll")]
private static extern IntPtr GetDC(IntPtr hwnd);
[DllImport("User32.dll")]
private static extern void ReleaseDC(IntPtr dc);

We then get a pointer to the desktop:

IntPtr desktopDC = GetDC(IntPtr.Zero);

Using this we retrieve a Graphics object:

Graphics g = Graphics.FromHdc(desktopDC);

It’s important to dispose of the pointer when finished to free resources:

ReleaseDC(desktopDC);

The rest of the application is fairly straight-forward. Enjoy!

Thanks goes to Nick La for supplying the graphics:

http://www.webdesignerwall.com/general/free-christmas-icons-for-you/

Download ChristmasLights.zip (32 KB)

Advertisements
This entry was posted in Projects and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s