Click here to Skip to main content
15,126,670 members
Articles / Desktop Programming / WPF
Posted 20 Sep 2006


50 bookmarked

3D gadgets for your WPF Desktop

Rate me:
Please Sign up or sign in to vote.
3.32/5 (16 votes)
20 Sep 20064 min read
Have you ever imagined having 3D objects float on your desktop? Lets see how we can achieve such a result using only C# and WPF.

Sample image


This article describes how you can create 3D gadgets for your desktop, using only C# and WPF. A gadget should integrate with the desktop, always staying on top of all open windows so that the user can use the shortcuts it offers. Let's see how we can achieve such results. The demo project contains the iPod model seen in this picture. If you first want to learn how to build something simpler, read on and you won't be disappointed.

Splitting the problem

First, lets split this seemingly impossible problem into bits:

  1. Creating a 3D environment
  2. Adding functionality to the 3D objects we create
  3. Transforming the window so that it is transparent and always stays on top

1. Populating a Viewport

The WPF control that hosts 3D objects is called a Viewport. We will create a Viewport using XAML markup, the preferred way in .NET 3.0. The code is given below:

<Viewport3D Name="mainViewport" ClipToBounds="True">
<PerspectiveCamera Position ="0,500,500" LookDirection ="0,-100,-100">

The camera was added because, with no light, we cannot see any of the objects that we create. To fully describe the camera we must set its Position (where the camera is placed in the 3D environment) and the LookDirection (the direction where the camera is looking )

Now let's add a simple object to the Viewport:

<GeometryModel3D x:Name ="pyramid">
Positions="0,0,37.5 -50,-50,-37.5 50,-50,-37.5 50,50,-37.5
-50,50,-37.5 0,0,-37.5 50,-50,-37.5 50,50,-37.5 -50,50,-37.5
-50,-50,-37.5 -50,-50,-37.5 50,-50,-37.5 50,-50,-37.5 -50,50,-37.5
-50,50,-37.5 -50,-50,-37.5 "
</GeometryModel3D.Geometry >
<GeometryModel3D.Material >
<DiffuseMaterial Brush ="White"/>

The actual 3D object is composed of triangles. Each triangle is specified as a succession of Points; to create another object starting from this one, you have to:

  • replace the points in the Positions collection
  • specify triangles in the TriangleIndices collection.

Sample Image - GADGET.jpg

But don't worry, there are easier ways to do it. The simplest way, for us graphically challenged developers is importing an existing model; the iPod model in the first screenshot is imported from a 3D Max model.

To import a 3D Studio Max model,

(*.3DS), of which there is an abundance on the internet, you can use ZAM 3D which is free to try and located here, or you can use an online free converter and its associated *.dll from here. This free online converter might not be updated for .NET 3.0 RC1 so you might have to replace some spaces with commas, but the rest works OK. In case you're wondering, the iPod model comes from the sample models that ZAM 3D provides.

After we import our model, we find that if we import several, all the models are on top of each other, so we must move each model to another place in the Viewport. We do this by using a Transformation. We want to translate the object so we write:

<TranslateTransform3D OffsetX ="-200"/>

Adding animation

To make things more interesting, we will add a simple animation; don't overdo it in your commercial app as animation distracts the user from his work and he also gets tired more quickly.

To animate elements in XAML we use Storyboards; so that we can use the animation multiple times. We add it to the Windows Resources, like this:

<Storyboard x:Key="RotateStoryboard">
<ParallelTimeline RepeatBehavior="Forever" Storyboard.TargetName="myRotate" 
                    Storyboard.TargetProperty="Angle" >
<DoubleAnimation From="0" To="360" Duration="0:0:30"/>

The animation must start when the window is loaded, so we use a OnLoad handler to start it, like this:

void Window1_Loaded(object sender, RoutedEventArgs e)
    Storyboard s;
    s = (Storyboard)this.FindResource("RotateStoryboard");

2. Adding mouse handlers for the 3D objects

To add mouse handlers for 3D objects we first have to find out which object in the ViewPort was clicked (or any other mouse action). To do this, we use the built-in hittesting engine of the ViewPort, like this:

void mainViewport_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    System.Windows.Point mouseposition = e.GetPosition(mainViewport);
    Point3D testpoint3D = new Point3D(mouseposition.X, mouseposition.Y, 0);
    Vector3D testdirection = new Vector3D(mouseposition.X, 
                        mouseposition.Y, 10);
    PointHitTestParameters pointparams = new PointHitTestParameters
    RayHitTestParameters rayparams = new RayHitTestParameters(testpoint3D, 
    VisualTreeHelper.HitTest(mainViewport, null, HTResult, pointparams);

The code that actually handles the click on the objects follows. Some tips for actions that you probably should offer:

  • possibility to close the gadget, as there will be no Close button on the form - it is invisible. Use the this.Close function to close the window.
  • possibility to drag the gadget to another part of the screen, if it accommodates the user by covering an important part of the screen (the Titlebar of the window is also invisible so standard dragging is impossible). Use the DragMove function for achieving this result quickly.
  • to make a shortcut to a internet site, you can start a new process with System.Diagnostics.Process.Start giving the URL as a parameter.

    private HitTestResultBehavior HTResult
        (System.Windows.Media.HitTestResult rawresult)
        RayHitTestResult rayResult = rawresult as RayHitTestResult;
        if (rayResult != null)
            if (cube == rayResult.ModelHit)
                string targetURL = @"";
            if (cross == rayResult.ModelHit)
            if (pyramid == rayResult.ModelHit)

3. Making the Window invisible

The standard definition of a Window in XAML looks like this:

<Window x:Class="WindowTransparency.Window1"
Title="WindowTransparency" Height="314" Width="813"

To add transparency, just add these XAML tags:

WindowStyle="None" Background="Transparent" AllowsTransparency ="True"

We also want two more things: the form must not be visible in the taskbar and must always be on top. The corresponding tags are:

ShowInTaskbar="False" Topmost="True"

Notes on using the code

The code was built on Vista RC1, using the .NET 3.0 RC1 framework and Orcas development tools (

It should also work under Windows XP, as I did not use any Vista-only feature (that I know of ) but it is not tested yet.


You can use this code to create something simple, like a 3D Mac OS X bar, or add a dancer like the ones that you can download from Microsoft, but this time in 3D. The possibilities are endless.

Notice how easy it is to do these things in XAML, just as simple as in a plain old VB6 application, simpler even than in .NET 2.0. WPF allows developers to achieve great visual effects without hassle, increasing productivity, but at the cost of having to learn how to properly use its power.


  • Update 22/09/2006 : Added iPod demo project and screenshot.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Dragos Sbirlea
Software Developer
Romania Romania
Dragos is currently a student at the Polytechnic University of Bucharest. He likes keeping up to date with new and amazing technologies and he hopes he will one day master the mechanisms behind modern day programming languages so that he can write the best possible code from a performance and maintainability point of view.

He keeps track of the things he learns on a daily basis on his blog, at .

Comments and Discussions

GeneralMy vote of 5! Pin
kanbang21-Aug-09 15:51
Memberkanbang21-Aug-09 15:51 
Generalgood Pin
zhujinlong1984091317-Apr-09 1:10
Memberzhujinlong1984091317-Apr-09 1:10 
QuestionHow to save Model3D object to jpg file? Pin
wmjcomcn24-Oct-08 0:20
Memberwmjcomcn24-Oct-08 0:20 
GeneralAlt tab Pin
hamid_m24-Mar-08 1:57
Memberhamid_m24-Mar-08 1:57 
Generalblocking my application form minimizing (Please help) Pin
dmnreddy30-Oct-07 12:45
Memberdmnreddy30-Oct-07 12:45 
GeneralThanks for saving me basic research time Pin
Gerald Gibson Jr17-Mar-07 15:54
MemberGerald Gibson Jr17-Mar-07 15:54 
GeneralRe: Thanks for saving me basic research time Pin
Dragos Sbirlea17-Mar-07 21:59
MemberDragos Sbirlea17-Mar-07 21:59 
GeneralVS2005 and .Net3 Pin
User 324245822-Sep-06 9:13
professionalUser 324245822-Sep-06 9:13 
GeneralRe: VS2005 and .Net3 Pin
Dragos Sbirlea22-Sep-06 11:40
MemberDragos Sbirlea22-Sep-06 11:40 
GeneralRe: VS2005 and .Net3 Pin
User 324245822-Sep-06 12:13
professionalUser 324245822-Sep-06 12:13 
QuestionSimple? Pin
Marc Clifton20-Sep-06 12:07
mvaMarc Clifton20-Sep-06 12:07 
AnswerRe: Simple? Pin
Dragos Sbirlea20-Sep-06 12:16
MemberDragos Sbirlea20-Sep-06 12:16 
GeneralNope Pin
NormDroid20-Sep-06 10:53
professionalNormDroid20-Sep-06 10:53 
GeneralRe: Nope Pin
Dragos Sbirlea20-Sep-06 11:29
MemberDragos Sbirlea20-Sep-06 11:29 
GeneralRe: Nope Pin
AndrewVos25-Sep-06 19:56
MemberAndrewVos25-Sep-06 19:56 
GeneralRe: Nope Pin
Dragos Sbirlea25-Sep-06 21:51
MemberDragos Sbirlea25-Sep-06 21:51 
GeneralRe: Nope Pin
AndrewVos25-Sep-06 21:59
MemberAndrewVos25-Sep-06 21:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.