Showing a notification Window(like msn) in WPF

I was playing around creating a notification window just like the one of MSN with WPF and I found a problem. In order the place the window in the Bottom, Right corner of the screen I tried to do the following

   1: Left = Screen.PrimaryScreen.WorkingArea.Width - this.Width
   2: Top = Screen.PrimaryScreen.WorkingArea.Height - this.Height

 

The window was not showing up on screen and I got confused. After some thinking I realized that the problem was that WPF uses DPI while the other measurement was in Pixels. In order to set the correct location we must convert the Screen.Primary.WorkingArea.Width and Height which is in Pixels to DPI. The question is HOW?

I found a good post on the forums that helped me a lot here. In order to convert the Screen.Primary.WorkingArea.Width and Height and fix the Top and Left of the window to be the Bottom, Right corner we have to do the following

   1: Loaded +=
   2:         delegate
   3:         {
   4:             PresentationSource source = PresentationSource.FromVisual(this);
   5:  
   6:             if (source != null)
   7:             {
   8:                 Left = (Screen.PrimaryScreen.WorkingArea.Width * source.CompositionTarget.TransformFromDevice.M11) - Width;
   9:                 Top = (Screen.PrimaryScreen.WorkingArea.Height * source.CompositionTarget.TransformFromDevice.M11) - Height;
  10:             }
  11:         };

 

one of the questions you might be asking yourself would be;

Why is he doing it in the Loaded Event of the Window?
Basically PresentationSource.FromVisual will return null if the Window is not shown yet.

 

I hope this helps ….

2 thoughts on “Showing a notification Window(like msn) in WPF

  1. Pingback: 2008 September 11 - Links for today « My (almost) Daily Links

  2. Pingback: Dew Drop - September 11, 2008 | Alvin Ashcraft's Morning Dew

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s