Embedding Screensaver’s in a Windows Form


Awhile ago I created the screensaver Vista Sidebar gadget and promised I would put some code up. As promised, here it is. It's actually quite easy to create something like this, and truth be told I actually had the code lying around for a few years since I wrote it for my 2nd book. Rather than confuse you with any sidebar code, I just created a simple Windows Form that hosts the screensaver UserControl (full source code can be downloaded below).

The hardest part (which is actually somewhat easy) is telling Windows to launch the screensaver within a certain window rather than fullscreen. You've seen this window in the control panel and may have wondered how it's done. To do this, you simply launch the screensaver (.scr file) and pass a command line parameter (/p for preview) and your window handle. The window handle can be accessed in a Windows Form or UserControl through the Handle property.

Below is the code that embeds a screensaver in my custom UserControl. You can see that I first check to see if a screensaver is already running and if it is, I tell it to stop (Kill). I then simply check to make sure my UserControl ScreensaverPath property is set and if it is I launch the executable using Process.Start with associated command line arguments (/p <window handle>).

private void LaunchScreensaver(string args)

{

    try

    {

        if (screenSaverProc != null)

        {

            screenSaverProc.Kill();

            screenSaverProc = null;

        }

 

        if (!String.IsNullOrEmpty(screenSaverPath))

        {

            this.Visible = true;

            ProcessStartInfo startInfo = new ProcessStartInfo(screenSaverPath + args, "");

 

            startInfo.UseShellExecute = false;

 

            screenSaverProc = Process.Start(startInfo);

        }

        else

        {

            this.Visible = false;

        }

    }

    catch (Exception ex)

    {

        throw new Exception("Could not launch screensaver:" + this.screenSaverPath, ex);

    }

}

To wire this up to the Windows Form, I simply expose a property named ScreensaverPath. Whenever the form sets this property, I call LaunchScreensaver and pass the appropriate command line arguments as shown below.

public string ScreensaverPath

{

    get { return screenSaverPath; }

    set

    {

        this.Visible = true;

        screenSaverPath = value;

        LaunchScreensaver(" \"/p" + this.Handle.ToString() + "\"");

    }

}

Here's a couple screenshots of the Screensaver Form project running various Vista screensavers.

bubbles skyrocket

mystify ribbons

As promised, here's the code. Also, one thing to note. Some screensavers don't play nice with the /p parameter. I haven't look into why this is, but in most cases the default Vista screensavers work just fine.


Comments (4)
  1. Jamie Akers says:

    The reason that some screensavers don’t play nice with /p is this option is used in the Display Properties, Screensaver tab to display a preview of the screensaver.  Windows passes the handle of the preview window to the screensaver and the screensaver draws a preview on that handle.

    Some screensavers, (typically the quickly knocked up Flash screensavers) don’t show this preview window and simply ignore the /p argument altogether.  This lazy man’s approach to coding screensavers stops them working with your application.

  2. Hi,

    I was wondering if you could help a fellow person.  I am looking for a small app that will let me play some Windows screensavers in a window as opposed to full screen (e.g., screensaver.scr /s at the command line only plays full screen and the /p switch doesn’t work).  Ideally, the window would be resizeable to any dimension using the mouse and would prevent the screensaver from disactivating from any keyboard or mouse movements.  I saw your code online, but I am not a programmer with a compiler, so I wouldn’t know how to modify the code to achieve my objectives.

    I’m sure you’re busy, but if you could take a minute to help it would surely be appreciated.  Thanks and have a great day.

Comments are closed.

Skip to main content