Microsoft Visual C# Starter Kit

Starter Kit: Screen Saver

Contents:

Introduction
Goals
Getting Started
How the Screen Saver Works
Concepts
Extending the Screen Saver
For More Information

Introduction

Screen Saver

This Visual C# Starter Kit is a ready-to-run Windows screen saver application. The screen saver displays a sequence of images — either default images, or images from a directory you specify — overlaid with text from a live internet RSS (Really Simple Syndication) feed. The project comes ready to compile and run, and you can customize it with only a little extra C# programming. We’ve included a list of suggestions in the section Extending the Screen Saver. You are also free to use the source code as the basis for your own projects, and share your work with others or upload it to the Internet.


Note: This documentation assumes that you have a basic knowledge of programming concepts and the Visual C# environment. You can learn more about these topics in the product documentation by clicking Help on the Main Menu, and selecting Contents, Index, or Search. You can also access Help by positioning the mouse cursor on language keywords or user interface elements such as windows or dialog boxes, and pressing F1.

Goals

After reading through this starter kit documentation you will understand how to install and configure the Screen Saver application, how the source code is organized, how the application works, and how you can customize it using Visual C#. This starter kit demonstrates the following technologies:

Getting Started

Building and running the Screen Saver

Once your project is loaded into the Visual C# environment, you can compile and run the program in one step.

To build and run the Screen Saver application

The screen saver launches and starts displaying the most recent articles from the RSS feed. Pressing a key or moving the mouse causes the screen saver to exit.

Making a Quick Change to the Source Code

Editing the source code to change the way the program works is easy. Here’s an example that adds your own text string to the screen saver display.

To display an extra line of text:

  1. Find the Solution Explorer window; its default position is in the upper right corner of the Visual C# environment. Right-click ScreenSaverForm.cs in the Solution Explorer window and select View Code to open ScreenSaverForm.cs in Code view.
  2. Scroll through the source code until you see the OnPaint method, or select it from the drop-down list in the upper right of Code View.
  3. Type the following code at the end of the method:

    e.Graphics.DrawString("<Your name>C# Screen Saver", rssView.TitleFont, Brushes.Orange, new PointF(0, 0));
  4. Press F5 to run the application again.

A title is now displayed on the screen saver.

Accessing the Options Dialog Box

The Screen Saver Starter Kit also includes an Options dialog where users can change the settings for the Screen Saver. To run the Options dialog inside Visual Studio, you will need to send a command-line argument directly to the executable as it opens. If you are using Windows 98, be aware that the data you enter in the Options dialog box will be visible to other users of your computer.

To force the Options dialog box to appear:

  1. Open Solution Explorer, and double-click the Properties node under the Screen Saver project.
    This shows you options for configuring your project.
  2. Click the Debug tab, and find the Command Line Arguments text box.
  3. Type /c into the text box.
    This tells Visual C# to run the screen saver with the /c command-line argument.
  4. Press F5 to launch the application.
    The Options dialog box appears.
  5. Experiment with the options by selecting a different background image directory or entering a new RSS feed.
  6. Click OK to close the dialog box.

After changing the settings in the options mode, you can run the screen saver again by entering /s as the command line argument instead of /c.

Installing the Screen Saver on Your Computer

The following steps describe how to install the screen saver application on your computer.

To install the screen saver:

  1. If you haven't already done so, load the Starter Kit into Visual C#.

  2. On the File menu, click Save All.

  3. If you are requested to enter a location to save the project, select a directory or accept the default location. Make a note of where the project is saved - by default it will be in a directory under \My Documents\Visual Studio 2005\Projects.

  4. On the Build menu, click Build Solution.
    This creates an executable file for the screen saver.

  5. Open up the directory specified in step 3 containing the saved project.

  6. Open the bin\Debug subdirectory. This is where the debug version of your executable file is located. Its default name is ScreenSaver1.exe but you may have given it a different name when you created the project the first time. Change the ".exe" extension on this file to ".scr".

    Note: If you do not see the file extensions in Windows Explorer, then click on Folder Options under the Tools menu, click on the View tab, and uncheck the checkbox labeled "Hide extensions for known file types".

  7. Right-click on the .scr file and select "Install".
    This opens up the Windows Screen Saver Dialog Box.

  8. Click the Preview button in the Windows Screen Saver Dialog Box.
    The screen saver launches. Press any key to stop it.

  9. Click the Settings button in the Windows Screen Saver Dialog Box to see the screen saver options. Try changing the background image or the RSS feed.

  10. Click OK.
     

You have now installed your own Windows screen saver.

Sharing Your Version of the Screen Saver with Friends

Now that you have a customized version of the screen saver, you can share it with your friends and family. Please note that the screen saver application uses version 2.0 of the .NET Framework, so any computer running it needs to have this version of the Framework installed. The .NET Framework is installed as part of Visual Studio, but it can also be installed directly from the Microsoft .NET Framework Developer Center. If you plan on sharing your screen saver with friends, you should let them know that they need to install the new .NET Framework as well.

How the Screen Saver Works

When Windows activates the screen saver due to a period of inactivity it launches the executable, which is named "ScreenSaver1.scr" unless you have given it a different name.

Unless the screen saver has been requested to open the Options dialog box, it then creates a full-screen Windows Form, inserts the default background image, and then accesses the Internet to download and display data from the RSS feeds. Two timers update the currently selected topic and change the background image.

If the mouse is moved or clicked at any point, or if a key is pressed, the program exits immediately.

Screen Saver Project Files

The project contains eight different C# source-code files.

Program.cs Contains the Main() method — the location where the program begins execution — and the code for handling the command line switches
OptionsForm.cs Contains the Windows Form that displays the settings options. From this dialog box, you can select a background image and an RSS feed to use when the screen saver is run.
ScreenSaverForm.cs Contains the main screen saver Windows Form. The background images and RSS feed selected by the user are read and displayed on this full-screen form.
RSSFeed.cs Contains the class representing an RSS 2.0 feed.
RssChannel.cs Contains the class representing an RSS channel. There is one channel in any RSS feed. It represents a collection of articles.
RSSItem.cs Contains the class representing an RSS item. An RSS channel can have any number of RSS items. An RSS item represents an article, blog post, or news story.
Item.cs Contains the class representing an item, an object with a title and a description.
ItemDescriptionView.cs Contains code that displays the description of an item from an RSS feed. It slowly fades in the text of the item, and then fades it out again.
ItemListView.cs Contains code that displays the title of an item from an RSS feed. It keeps track of a currently selected article, which it can scroll forward or backward.

Concepts

How Windows Interacts with Screen Savers

A Windows screen saver is an executable that opens a full-screen window displaying a changing graphical display, such as a star field or selection of photographs. Any executable can be used as a Windows screen saver. To make an executable appear in the screen saver selection dialog box, it simply needs to be placed in the Windows directory (This is \Windows\System32 in the system drive if you’re using Windows 98 or Windows XP, or \WinNT\System32, if you are using Windows NT or Windows 2000).

When Windows runs your screen saver, it launches it with one of three command line options:

This starter kit handles these arguments in the Program.cs source file. When you look at the code in that file, you see that the /p argument is not implemented by this starter kit.

Storing Options Using the Settings and Resource Designers

Since the starter kit can be run in two different modes, it needs to have a way to store its settings between sessions, which it does using the Settings Designer. You can see this designer by right-clicking the ScreenSaver project node in Solution Explorer, selecting Properties, and then clicking the Settings tab. This designer enables you to list all the settings that your program would like to set and to access them from your code as:

Properties.Settings.Default.<SettingName>


Similarly, the Resource Designer is used to store the default background image and RSS feed. You can see it by right-clicking the ScreenSaver project node in Solution Explorer and selecting Properties, then clicking the Resources tab.

XML and RSS

RSS feeds are an XML format for syndicating content on the Web. They are often used by sites for publishing their lists of current news articles or blog posts. An RSS feed has an <rss> node containing one <channel> node. The <channel> node has one or more <item> nodes, one for each of the articles or blog posts in the feed. It also contains a <title> element, that contains the title of the channel. Each <item> can contain a title, a link, and a description. A very simple example is below.

 

<rss version="2.0">

  <channel>

    <title>My Feed</title>

    <description>Some articles.</description>

    <link>http://msdn.microsoft.com/</link>     

    <item>

      <title>Article 1</title>

      <description>The abstract of the article.</description>

      <link>http://msdn.microsoft.com/netframework</link>

    </item>

    <item>

      <title>Article 2</title>

      <description>Some other abstract.</description>

      <link>http://msdn.microsoft.com/vcsharp/</link>

    </item>

  </channel>

</rss>

Extending the Screen Saver

Adding Support for the Keyboard

One way to easily enhance the screen saver is by adding keyboard support. For example, you can allow the user to select the current RSS item with the cursor keys. These steps show you how.

To add keyboard support:

  1. In Solution Explorer, right-click ScreenSaverForm.cs and select View Designer.
  2. If the Properties window is not yet showing, open it by clicking the View menu, and selecting Properties Window.
  3. In the drop-down list at the top of the Properties window select ScreenSaverForm.
  4. Click the Events button (the lightning bolt) to see the events that the form can raise.
  5. Find the KeyDown event. It will have a method, ScreenSaverForm_KeyDown, listed as the handler for the event.
  6. Double-click the KeyDown event.
    This opens Code View, and takes you to the ScreenSaverForm_KeyDown method.
  7. Replace the line Close(); with the following:

    switch (e.KeyCode)

                {

                    case Keys.Down:

                         // Rotate down through the articles.

                         rssView.NextArticle();

                         break;

                     case Keys.Up:

                         // Rotate up through the articles.

                         rssView.PreviousArticle();

                         break;

                     default:

                         Close();

                         break;

                 } 
  8. Save the file, and press F5 to run the project.
  9. While the screen saver is running, press the UP ARROW and DOWN ARROW keys to change the currently selected article.

More Suggestions

The screen saver application is designed to be easily expanded and customized. Some ideas for extending it are:

For More Information

Online Resources

 

© Microsoft Corporation and/or its suppliers. All rights reserved. Terms of Use.