CODEDIGEST
Home » Articles
Search
 

Technologies
 

Sponsored links
 

CodeDigest Navigation
 

Technology News
No News Feeds available at this time.
 

Community News
No News Feeds available at this time.
 
WPFMystify3D – A Screen Saver using WPF 3D Geometry

By BalaMurali Balaji
Posted On Dec 26,2009
Article Rating:
Be first to rate
this article.
No of Comments: 0
Category: WPF
Print this article.

WPFMystify3D – A Screen Saver using WPF 3D Geometry

 

This article illustrates the creation of a screen saver using WPF(Windows Presentation Foundation) 3D Geometry model.

 

Background

 

To set a .Net application as a screen saver, you get the assembly built, change the .EXE file into .SCR file and store it in the windows/system32 folder. Ensure that you have resources available (like picture files) in the right path. To do so, you may have to explicitly provide the complete paths to the folder where resources are kept.

 

 

In Windows Vista, under Control Panel à Personalize menu, you see screen saver option, which opens up a dialog “Screen Saver Settings”

The calling of the screen saver program from this dialog involves three command line options. /S is passed to your program Main method to start the screen saver; /P hwnd is passed to preview the screen saver on top of the current dialog with the handle hwnd; /C hwnd is passed to show configuration window for the screen saver on top of the current dialog with the handle hwnd;

 

Introduction

 

WpfMystify3D is a screen saver application that is currently available in both the Desktop Mode and Windows User mode. Users may directly run the application upon downloading the zip files that comes along with this.

 

The Main method:

 

[STAThread]

        static public int Main(string[] args)

        {

            App a = new App();

            if (args[0].ToUpper().StartsWith("/S"))

            {

                a.Run(new Mystify());

            }

            else if (args[0].ToUpper().StartsWith("/C"))

            {

                return a.Run(new Settings());

            }

            else if (args[0].ToUpper().StartsWith("/P"))

            {

                var parentHandle = args[1];

                a.Run(new Mystify());

            }

            return 0;

        }

 

As you notice, the screen saver window and the configuration window gets opened as per the command line argument. The preview mode currently does not involve checking of any parent window handles. The WPF window comfortable sits on top of all the other active windows using the attribute TopMost=true.

 

Application features:

 

This application displays 3D graphical elements mystified based on the configurations set in the Xml file named “WpfMystifySettings.xml”.  This Xml file is stored in the applications folder and is read every time the screen saver is activated or the settings been changed by the user.

 

While running the application, certain function keys are enabled to perform certain actions.

 

F2 – This shows the Screen Saver settings dialog where in you may change the attributes of the graphical elements.

 

Esc – This exit the application.

 

If any key is pressed or mouse moved or clicked, the application exits.

 

Reading the Screen Saver Settings

 

        private void GetScreenSaverSettings()

        {

            XmlDocument xData = new XmlDocument();

            xData.Load(@"..\..\WpfMystifysettings.xml");

            XmlElement root = xData.DocumentElement;

            XmlNode node = root.SelectSingleNode("Settings/MeshType");

            meshtype = node.InnerText;

            node = root.SelectSingleNode("Settings/Rotation");

            rotation = node.InnerText;

            node = root.SelectSingleNode("Settings/Design");

            design = node.InnerText;

            node = root.SelectSingleNode("Settings/Background/Color1");

            color1 =Color.FromRgb(byte.Parse(node.InnerText), 0,0);

            node = root.SelectSingleNode("Settings/Background/Color2");

            color2 = Color.FromRgb(0, byte.Parse(node.InnerText), 0);

            node = root.SelectSingleNode("Settings/Background/Color3");

            color3 = Color.FromRgb(0,0,byte.Parse(node.InnerText));

        }

 

The above code gets the values for the attributes MeshType, GradientColors, Design and Rotation from the xml file. Here, MeshType refers to the GeometryModel3D that could be a Plane, Cube or Sphere. GradientColors are used to set the RadialGradientBrush for the background application of the 3D model. Design takes the values Floral, Ocean and Grid and these refers to the picture files that are used to apply as material for the foreground and background surface of GeometryModel3D.

 

The sample XML file for the settings is given below:

 

<?xml version="1.0" encoding="utf-8"?>

<WpfMystify>

  <Settings>

    <MeshType>Cube</MeshType>

    <Rotation>Both</Rotation>

    <Design>Ocean</Design>

    <Background>

      <Color1>123</Color1>

      <Color2>80</Color2>

      <Color3>147</Color3>

    </Background>

  </Settings>

</WpfMystify>

 

 

Rotation might be Horizontal, Vertical or Both. I have two story boards, one for the horizontal transformation of the surface and the other for vertical transformation of the surface. User can set both the storyboards running simultaneously. The following XAML code provides the declarative part of these story boards.

 

        <Storyboard x:Key="RotateStoryboardHorizontal">

            <ParallelTimeline RepeatBehavior="Forever"  Storyboard.TargetName="myRotateInner" Storyboard.TargetProperty="Angle" >

                <DoubleAnimation From="0" To="360" Duration="0:0:20"/>

            </ParallelTimeline>

        </Storyboard>

        <Storyboard x:Key="RotateStoryboardVertical">

            <ParallelTimeline  RepeatBehavior="Forever"  Storyboard.TargetName="myRotateOuter" Storyboard.TargetProperty="Angle" >

                <DoubleAnimation From="0" To="360" Duration="0:0:30"/>

            </ParallelTimeline>

        </Storyboard>

 

Output Screen Saver Designs:

 

Ocean:




 

Floral:

 

Grid:

 

 

Rendering 3D graphics

 

The RenderNewGraphics method sets the values for the 3D elements in the GeometryModel3D and Geometry objects.

 

  private void RenderNewGraphics()

        {

            if (meshtype == "Plane")

            {

                MyGeoModel1.Geometry = (MeshGeometry3D)this.FindResource("PlaneMesh");

                MyGeoModel2.Geometry = (MeshGeometry3D)this.FindResource("PlaneMesh");

            }

            else if (meshtype == "Cube")

            {

                MyGeoModel1.Geometry = (MeshGeometry3D)this.FindResource("CubeMesh");

                MyGeoModel2.Geometry = (MeshGeometry3D)this.FindResource("CubeMesh");

            }

            else if (meshtype == "Sphere")

            {

                MyGeoModel1.Geometry = (MeshGeometry3D)this.FindResource("Sphere16x16");

                MyGeoModel2.Geometry = (MeshGeometry3D)this.FindResource("Sphere16x16");

            }

            if (design == "Floral")

            {

                MyGeoModel1.Material = (Material)this.FindResource("FloralDesign");

                MyGeoModel2.Material = (Material)this.FindResource("FloralDesign");

                MyGeoModel1.BackMaterial = (Material)this.FindResource("FloralDesign");

                MyGeoModel2.BackMaterial = (Material)this.FindResource("FloralDesign");

            }

            else if (design == "Grid")

            {

                MyGeoModel1.Material = (Material)this.FindResource("GridDesign");

                MyGeoModel2.Material = (Material)this.FindResource("GridDesign");

                MyGeoModel1.BackMaterial = (Material)this.FindResource("GridDesign");

                MyGeoModel2.BackMaterial = (Material)this.FindResource("GridDesign");

            }

            else if (design == "Ocean")

            {

                MyGeoModel1.Material = (Material)this.FindResource("OceanDesign");

                MyGeoModel2.Material = (Material)this.FindResource("OceanDesign");

                MyGeoModel1.BackMaterial = (Material)this.FindResource("OceanDesign");

                MyGeoModel2.BackMaterial = (Material)this.FindResource("GridDesign");

            }

           

            RadialGradientBrush rgb = (RadialGradientBrush)this.FindResource("GridBackground");

            rgb.GradientStops[0] = new GradientStop(color1, 1);

            rgb.GradientStops[1] = new GradientStop(color2, 0.5);

            rgb.GradientStops[2] = new GradientStop(color3, 0);

            myGrid.Background = rgb;

 

 

 

            if (rotation == "Horizontal")

            {

                Storyboard s;

                s = (Storyboard)this.FindResource("RotateStoryboardVertical");

                s.Stop(this);

                s = (Storyboard)this.FindResource("RotateStoryboardHorizontal");

                s.Begin(this, true);

            }

            else if (rotation == "Vertical")

            {

                Storyboard s;

                s = (Storyboard)this.FindResource("RotateStoryboardHorizontal");

                s.Stop(this);

                s = (Storyboard)this.FindResource("RotateStoryboardVertical");

                s.Begin(this, true);

            }

            else  if (rotation == "Both")

            {

                Storyboard s;

                s = (Storyboard)this.FindResource("RotateStoryboardHorizontal");

                s.Begin(this, true);

                s = (Storyboard)this.FindResource("RotateStoryboardVertical");

                s.Begin(this, true);

            }

            myViewport3D.UpdateLayout();

 

        }

 

Desktop Mode and Windows User Mode

 

The main difference between the Desktop and Windows User modes is that the location of the resource files.

 

In the Desktop mode, the resources including the image files and WpfMystifySettings.xml file in the application folder. The function key F2 and Esc key are functional only in this mode.

 

In the Windows User Mode, the image files must be located in the folder [Drive:]\Windows\System32\Images. The screen saver settings file WpfMystifySettings.xml must be in the root directory, i.e., [Drive:]\

 

Downloads

Download Source

 

Conclusion

 

Typically, the configuration settings for screen saver are done through Windows Registry entries. But, to run this WpfMystify3D screen saver, please remember that the resource files are required in the appropriate folders as required by the Desktop mode and Windows User mode.

 

 

Similar Articles
You can contribute to CodeDiget.Com:
Donate to CodeDigest.com
Article Feedback
Comments