Click here to Skip to main content
6,295,667 members and growing! (12,633 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Windows Presentation Foundation » Annotations     Intermediate License: The Code Project Open License (CPOL)

WPF Masala - The Sliding and Flying Windows

By rawwool

Experiments with WPF animation to recreate some of Microsoft Health Common User Interface magic!
C#, WPF, Dev
Posted:15 Nov 2008
Views:16,410
Bookmarked:69 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
16 votes for this article.
Popularity: 5.44 Rating: 4.52 out of 5

1

2
4 votes, 25.0%
3
1 vote, 6.3%
4
11 votes, 68.8%
5

Introduction and Background

Anyone who has seen the fantastic user interface developed by Microsoft folks - Microsoft Health Common User Interface, can't remain unimpressed to say the least. Hats off to the fantastic work done by the designers and developers there. As part of my WPF learning, I thought why not try to recreate some of the magic. This was looking promising so I thought I would share this with CodeProject friends. Here is the first cut: Youtube video.

Using the Code

Just unzip the file and take a look at the WindowsContainer class. The only logical block worth looking at is the tiling logic:

private void TileWindows(double left, double top, double width,
	double height, double margin, double childAspectRatio)
{
    // Get the no of child windows to tile:
    int windowsCount = _ListWindows.Where(s => s.Mode == WindowLayoutMode.Tiled).Count();
    if (windowsCount == 0) throw new InvalidOperationException
					("No child windows to tile");

    // Now calculate the optimum size for each window:
    double parentAspectRatio = width / height;// this.Width / this.Height;

    if (childAspectRatio <= 0)
    {
        double childHeight = height / windowsCount;
        childAspectRatio = width / childHeight;
    }

    double sqrtN = Math.Sqrt((double)windowsCount);
    double sqrtAspectRatioRatio = Math.Sqrt(parentAspectRatio / childAspectRatio);
    double childWindowHeight = height * sqrtAspectRatioRatio / sqrtN;
    double childWindowWidth = childAspectRatio * childWindowHeight;
    double rowCountDouble = height / childWindowHeight;
    double columnCountDouble = width / childWindowWidth;

    int columnCount = (int)columnCountDouble;
    double colDiscrepancy = (columnCountDouble - columnCount) / columnCountDouble;
    int rowCount = (int)rowCountDouble;
    double rowDiscrepancy = (rowCountDouble - rowCount) / rowCountDouble;

    if (colDiscrepancy > rowDiscrepancy)
    {
        //shrink column width
        columnCount++;
        childWindowWidth = width / columnCount;
        childWindowHeight = childWindowWidth / childAspectRatio;

        if (columnCount * rowCount < windowsCount)
        {
           //shrink row height
           rowCount++;
           childWindowHeight = height / rowCount;
           childWindowWidth = childWindowHeight * childAspectRatio;
        }
    }
    else if (rowDiscrepancy > colDiscrepancy)
    {
        //shrink row height
        rowCount++;
        childWindowHeight = height / rowCount;
        childWindowWidth = childWindowHeight * childAspectRatio;
        if (columnCount * rowCount < windowsCount)
        {
            //shrink column width
            columnCount++;
            childWindowWidth = width / columnCount;
            childWindowHeight = childWindowWidth / childAspectRatio;
        }
    }

    int index = 0;
    int indexRow = 0;
    int indexColumn = 0;
    List listStoryboard = new List();
    foreach (ChildWindow window in _ListWindows)
    {
        if (window.Mode == WindowLayoutMode.Tiled)
        {
            indexColumn = index % columnCount;
            indexRow = (int)(index / columnCount);

            Rect targetRect = new Rect(
            left + indexColumn * childWindowWidth + margin,
            top + indexRow * childWindowHeight + margin,
            childWindowWidth - 2 * margin,
            childWindowHeight - 2 * margin);
            listStoryboard.Add(GetStoryboard(window, targetRect));

            window.SetVerticalTitle(targetRect.Width <= window.Title.Height + 15);

            index++;
        }
        else
        {
            window.SetVerticalTitle(false);
        }
    }
    foreach (Storyboard sb in listStoryboard)
    {
        LayoutRoot.Resources.Add(sb.Name, sb);

        sb.Begin(LayoutRoot);

        LayoutRoot.Resources.Remove(sb.Name);
    }
}

Windows container uses a Canvas to place the child windows on it. I think there may be a possibility of using Grid there. I am not too sure.

I have used multiple borders with varying opacity and margins to create the shade effect on the child windows:

FlyingWindows1.jpg

Most of the code in this project is self explanatory. However, I would be happy to beef up this article with more explanation if required.

Points of Interest

I am planning to add more windowing effects and port it to Silverlight too. I'll keep you posted.

History

  • 15th November, 2008: Initial post

Awaiting feedback!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

rawwool


Member

Occupation: Team Leader
Company: Sage UK Limited
Location: United Kingdom United Kingdom

Other popular Windows Presentation Foundation articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 23 of 23 (Total in Forum: 23) (Refresh)FirstPrevNext
GeneralWhere will the content be Pinmembersinun0:27 8 May '09  
Generalstart in Details mode? PinmemberkmaNick11:21 21 Mar '09  
GeneralChange location of windows... Pinmemberpatota0:46 12 Mar '09  
GeneralResize Problem PinmemberP P Vilsad0:41 22 Jan '09  
Generalalignment problem Pinmemberkishore@infologicsindia7:43 14 Jan '09  
GeneralNice to see this is possible in WPF PinmemberSWT620:46 9 Jan '09  
Generalhi Pinmemberkishore@infologicsindia1:23 1 Dec '08  
GeneralRe: hi Pinmemberrawwool11:31 4 Dec '08  
GeneralUrgent Please Help me!!!!!!!!!!!!!!! Pinmemberkishore@infologicsindia15:49 30 Nov '08  
AnswerRe: Urgent Please Help me!!!!!!!!!!!!!!! Pinmemberrawwool0:52 1 Dec '08  
GeneralRe: Urgent Please Help me!!!!!!!!!!!!!!! Pinmemberkishore@infologicsindia1:34 1 Dec '08  
GeneralHow to Add Grid and listbox in Child window? PinmemberMember 43643730:04 21 Nov '08  
GeneralRe: How to Add Grid and listbox in Child window? Pinmemberrawwool7:02 22 Nov '08  
GeneralRe: How to Add Grid and listbox in Child window? Pinmemberkishore@infologicsindia18:42 23 Nov '08  
GeneralProject build error - help needed Pinmemberdeepak_j619:18 17 Nov '08  
GeneralRe: Project build error - help needed Pinmemberrawwool21:09 17 Nov '08  
GeneralCan't compile source code PinmemberCheloXL12:50 17 Nov '08  
AnswerRe: Can't compile source code Pinmemberrawwool21:08 17 Nov '08  
GeneralRe: Can't compile source code PinmemberCheloXL2:04 18 Nov '08  
AnswerRe: Can't compile source code Pinmemberrawwool6:46 18 Nov '08  
AnswerRe: Can't compile source code PinmemberCheloXL3:36 20 Nov '08  
Generalexcellent, even it can be better Pinmemberchristoph braendle5:06 16 Nov '08  
AnswerRe: excellent, even it can be better Pinmemberrawwool9:25 16 Nov '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 15 Nov 2008
Editor: Sean Ewington
Copyright 2008 by rawwool
Everything else Copyright © CodeProject, 1999-2009
Web15 | Advertise on the Code Project