C# Disciples

my life in Avalon ….

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 ….

September 10, 2008 - Posted by marlongrech | WPF, tips and tricks | | 2 Comments

2 Comments »

  1. [...] Grech details Showing a notification Window (like msn) in WPF Jake Ginnivan gives us his take on WPF Dynamic UI using [...]

    Pingback by 2008 September 11 - Links for today « My (almost) Daily Links | September 11, 2008 | Reply

  2. [...] Showing a Notification Window (Like MSN) in WPF (Marlon Grech) [...]

    Pingback by Dew Drop - September 11, 2008 | Alvin Ashcraft's Morning Dew | September 11, 2008 | Reply


Leave a comment