web 2.0

Creating Cool Draggable Window in Silverlight

 
 

Silverlight is often dubbed as the would-be better platform for creating Rich Internet Applications. But to date, I’ve seen only applications sitting duck in the browser. Well, I’m not saying that those are not great RIA, but for goodness sake, I want to see something different like a Silverlight application flying around in the browser among HTML elements, or at least a window in Silverlight that could be dragged around. This motivates me to create a proof of concept.

This tutorial basically demonstrates how to create and overlay a draggable window on HTML body in Silverlight, using managed code and DOM. For simplicity, this sample uses an alpha-blended background PNG image to cast the shadows and make the ‘see-through glass’ effect on the LCD screen.

 

View Online Demo - Silverlight 2.0 Project (209.73 kb)

 

Notes: This tutorial was written for Silverlight 1.1 Alpha; however, you can find a working sample project for Silverlight 2.0 at the end of this article.

Make Background Transparent

The background of Silverlight host element and root canvas is by default in opaque color. To make the whole background transparent, firstly set the the root canvas’s Background to Transparent. (See screenshot below)

 
 

Secondly we need to modify the arguments of the Silverlight.createObjectEx(), setting isWindowless: ’true’ and background: '#00FFFFFF'.  Windowless mode allows host element to overlay on HTML elements, and vice-versa (more on this later). Notice the first two digits ‘00’ in the background property. This is alpha-blend opacity value in HEX format. E.g. ‘00’ makes it completely transparent; ‘7F’ is 50% transparent. And ‘FF’ is completely opaque. See the screenshot after the changes:

 
 

By now Silverlight application is completely transparent. Just a quick note, if you can’t reproduce this transparency, double check the code, look especially for typos and missing quote ‘  or “ sign.

Create a Draggable Window

We want to create a window that could be dragged around in the browser, on top of HTML elements. In this case, we treat the host element as window. Now the very first thing to do is to absolutely position the host element to allow overlaying on other HTML elements. We simply add some CSS (highlighted below):

 
 

Add a ‘Title Bar’

What we need now is the ‘Title Bar’ area  where user can drag the window. In this case, we add a big transparent ellipse as the ‘Title Bar’. Name it “ellipseDrag”. Remember, you’re not limited to ellipse. You can just create ‘Title Bar’ out of any Silverlight element.

<Ellipse x:Name="ellipseDrag" Canvas.Left="0" Canvas.Top="0" Width="500" Height="300" Fill="Transparent"></Ellipse>

Define the Dragging Stages

I view Window dragging as a three-stage process. First, user holds down the mouse left button on the title bar. Second, the pointer moves from point A to point B, then the Window is spontaneously moved along with the mouse pointer. Third, the mouse left button is released. Therefore, we model this in MouseDraggingState enumeration:

Public Enum MouseDraggingState As Integer
    Down = 1
    Dragging
    Normal
End Enum

First Stage of Dragging: Mouse Button is Hold Down

To detect when user holds down mouse button on the ellipse, we handle the MouseLeftButtonDown() event then set the dragging state to ‘Down’.

Dim _draggingState As MouseDraggingState

Private Sub ellipseDrag_MouseLeftButtonDown(…) Handles ellipseDrag.MouseLeftButtonDown
        ' Setdragging state to 'Down'
        _ draggingState = MouseButtonState.Down
End Sub

Second Stage of Dragging: Mouse Pointer is Moving

In second stage, while mouse pointer is moving, the host element is moved along to create dragging effect. But we don’t handle the ellipse MouseMove() event here as you can see in screenshot below, since the mouse pointer is always fixed to the same position on ellipse during dragging.  

 
 

Instead, we handle the document OnMouseMove() event which fires every time mouse pointer moves across the document. To handle it, HtmlPage.Document.AttachEvent() method is called to subscribe “Document_OnMouseMove” as an event handler. You can see we also handle the document OnMouseUp() event which fires when mouse button is released.

Now the Document_OnMouseMove() is fired every time mouse pointer is moved.

 
 

You can see that at the very beginning of dragging (see Block #1 in above screenshot), the position of the host element as well as the position of mouse pointer are noted down for later use, and the dragging state is changed from ‘Down’ to ‘Dragging’. While the ellipse is being dragged (see Block #2), the host element is moved along with the mouse pointer. Remember that, position of the host element is relative to the document since we absolutely positioned it. To help you with the positioning calculations, take a look at this model:

 
 

You can see I included an extra condition: e.ClientY >= -1000 AndAlso e.ClientX >= -1000. It’s because in FireFox, Silverlight 1.1 Alpha sometimes return e.ClientY and e.ClientX in weird negative values when mouse pointer is moved out of document to the far left or top.

Third Stage of Dragging: Mouse Pointer is Released

What we need to do now is handle the document OnMouseUp() event in Document_OnMouseUp() method. When mouse button is released, simply change the dragging state to normal to stop moving the host element.

 
 

This is it, folks. We now have a window in Silverlight that could be dragged around.

Spice it up with PNG background image

I just want to demonstrate this sneaky use of alpha-blended PNG image as background. In Page_Load() event, set the “window-back.png” image as the background of the root canvas. In the full source codes, you’ll see I added an animated ellipse just to show off the alpha-blending effect.

 

For Silverlight 2.0 RTW:

Online Demo

Silverlight 2.0 Project (209.73 kb)

Tags: ,

Silverlight 1.1 | Tutorial

Comments

Rui Marinho Portugal, on 1/31/2008 12:34:52 PM Said:

Rui Marinho

hi there, the source files that you have for download, don't work. can you please send me a sample project that works... really nice this idea of yours Smile

Greetings
Rui

Jacek Ciereszko , on 4/14/2008 9:39:42 AM Said:

Jacek Ciereszko

WOW.. impressive (O.o) nice nice nice

Regards,
Jacek

Erno , on 4/28/2008 3:47:30 AM Said:

Erno

Thanks for this information. Could you please confirm that transparancy does NOT work when using the aspx page instead of the html page that is generated by Visual Studio in beta 1 of Silverlight 2? To me it looks like the Silverlight Aspx Control does not propagate the background or is windowless correctly.

Thank you

wes , on 5/2/2008 9:37:19 AM Said:

wes

Hi,
Currently this page doesn't work. It prompts a window error when I load it.
www.silverlightexamples.net/.../TestPage.html

Neo , on 5/7/2008 1:43:56 AM Said:

Neo

@Erno, Silverlight Control for ASP.NET has the properties. <asp:Silverlight ID="Silverlight1" runat="server" Height="100px" Width="100px" Windowless="True" BackColor="Transparent"></asp:Silverlight>

@wes, the link is for Silverlight 1.1. For Silverlight 2 Beta 1, use this link instead:
www.silverlightexamples.net/.../TestPage.html

slhungry , on 6/8/2008 7:49:48 AM Said:

slhungry

very cool stuff, how would we make this draggable window resizable by the user?

Fatih , on 6/29/2008 12:25:05 PM Said:

Fatih

incorrect:   Windowless="True" BackColor="Transparent"

correct (SL2-b2):     windowless="true" background="transparent"

gigabite ethernet , on 1/16/2009 3:41:11 AM Said:

gigabite ethernet

This is a wonderful trick for a webpage. I am going to try this on one of my projects - its sure to make me look brillant - thanks

Inventory Management Software , on 2/3/2009 10:04:57 PM Said:

Inventory Management Software

wow.. what a awesome topic here..
bookmarked !!

Perfect Home Design , on 3/12/2009 5:47:27 AM Said:

Perfect Home Design

this is like a widget for windows media player

Melayu Boleh , on 4/20/2009 9:07:43 PM Said:

Melayu Boleh

This is a wonderful trick for a webpage. I am going to try this on one of my projects - its sure to make me look brillant - thanks

Melayu Boleh Also , on 6/27/2009 12:00:54 AM Said:

Melayu Boleh Also

very cool stuff

ezyshop2u , on 6/28/2009 6:31:34 AM Said:

ezyshop2u

gud info! its really help me regarding this topic!

Air Purifier , on 6/30/2009 4:02:13 AM Said:

Air Purifier

This is very cool. It's look like a new widget

club penguin cheats , on 7/5/2009 8:11:17 PM Said:

club penguin cheats

Could you please confirm that transparancy does NOT work when using the aspx page instead of the html page that is generated by Visual Studio in beta 1 of Silverlight 2?

Ebays Tips , on 7/14/2009 9:36:34 AM Said:

Ebays Tips

hi there, the source files that you have for download, don't work. can you please send me a sample project that works... really nice this idea of yours

SEO , on 7/17/2009 1:11:36 AM Said:

SEO

The ASP.NET MVC Framework couples the models, views, and controllers using interface-based contracts, thereby allowing each component to be easily tested independently. By default, the view engine in the MVC framework uses regular .aspx pages to design the layout of the user interface pages onto which the data is composed.

san antonio movers , on 7/28/2009 9:18:33 AM Said:

san  antonio movers

thank u
nice ppl

dallas movers , on 7/28/2009 9:18:53 AM Said:

dallas  movers

who r u all?

jammer , on 7/28/2009 9:19:04 AM Said:

jammer

thank u all

israel debate , on 7/28/2009 9:22:04 AM Said:

israel debate

how is my love

israel research , on 8/5/2009 6:18:15 AM Said:

israel research

thank u sir

Emo Hair , on 8/9/2009 2:23:59 AM Said:

Emo Hair

The ASP.NET MVC Framework couples the models, views, and controllers using interface-based contracts, thereby allowing each component to be easily tested independently. By default, the view engine in the MVC framework uses regular .aspx pages to design the layout of the user interface pages onto which the data is composed.

Emo Hair

LCD  TV brackets , on 8/14/2009 1:57:34 AM Said:

LCD  TV brackets



Hi,

how can i implement one Draggable window, one that  i can put inside it all the controls i want like a normal page, and then have multiple windows on my application.

SEO Company , on 8/30/2009 8:48:45 PM Said:

SEO Company

What are the system requirments and code for silverlight application in asp.net.?Please help !!!!

Search engine marketing , on 8/31/2009 6:24:03 PM Said:

Search engine marketing

Hi,
   Excellent post.I want to thank you for this informative read, I really appreciate sharing this great post. Keep up your work…

tin whiskers , on 9/1/2009 11:01:00 PM Said:

tin whiskers

Hi,
  Nice design.....I m planning to buy new laptop , I m sure this blog will helpful for me....

Cheap Double Glazing , on 9/9/2009 3:49:31 AM Said:

Cheap Double Glazing

Read it all! Thanks for this information. Could you please confirm that transparancy does NOT work when using the aspx page instead of the html page that is generated by Visual Studio in beta 1 of Silverlight 2?

Web design , on 9/17/2009 7:14:40 PM Said:

Web design

How to install these master pages of the silver light. Need the instructions for the new visitors.

Plastic surgeon Pennsylvania , on 9/23/2009 4:32:40 AM Said:

Plastic surgeon Pennsylvania

The demo video doesn't work; I prefer following instructions through video, but after reading the whole thing, I think I got it. You big help, you! Thanks!

giocare ai casinò , on 9/23/2009 7:41:58 PM Said:

giocare ai casinò

It helped me with ocean of knowledge so I really believe you will do much better in the future I appreciate everything you have added to my knowledge base.Admiring the time and effort you put into your blog and detailed information you offer!

US Gold Coin Auctions , on 9/27/2009 7:58:51 PM Said:

US Gold Coin Auctions

One of the best trick to our window application.

buy laptop , on 9/30/2009 12:15:05 AM Said:

buy laptop

How to use windows silver light and there is no icon that shows that it is installed pls help?

Boiler Manuals , on 9/30/2009 4:09:05 AM Said:

Boiler Manuals

Creating Cool Draggable Window in Silverlight  , grabbed my interest. I have recently set out to develop utilising Silverlight but I am realising it is a real learning curve.  My earlier experience is with mysql, php, most linux based tools and flash. The problems of utilising Silverlight to arrive at a satisfactory page that functions speedily in all the leading web browsers, Internet Explorer, Safari, Firefox and Google Chrome can be a large headache which is taking me numerous hours to overcome.  Engrossing to study your thoughts and the comments in your web site on Silverlight.  I feel the tutorial web sites and Microsofts offering are strict and address the identical points, comments and dialog in blogs frequently references effective ways to beat problems that leads me through the learning curve more quickly.  Thanks for the note, it has helped in a moderate way to take me through the migration.

trading pins , on 10/9/2009 12:24:25 AM Said:

trading pins

What are advanced methods and properties of record set in ASP?

Internet marketing , on 10/10/2009 12:11:23 AM Said:

Internet marketing


How to install these master pages of the silver light. Need the instructions for the new visitors.

los angeles web development , on 10/19/2009 1:58:43 AM Said:

los angeles web development

I have been surfing for about an hour to get some interesting news.  

Online poker , on 10/21/2009 12:21:31 AM Said:

Online poker


How to install these master pages of the silver light. Need the instructions for the new visitors.

childhood autism , on 10/22/2009 8:37:37 PM Said:

childhood autism

I have never thought that the post will be so interesting.  

Add comment


(Will show your Gravatar icon)

biuquote
  • Comment
  • Preview
Loading