Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the size of the current screen in WPF?

I know I can get the size of the primary screen by using

System.Windows.SystemParameters.PrimaryScreenWidth; System.Windows.SystemParameters.PrimaryScreenHeight; 

But how do I get the size of the current screen? (Multi-Screen users do not always use the primary screen and not all screens are using the same resolution, right?)

It would be nice to be able to acces the size from XAML, but doing so from code (C#) would suffice.

like image 942
Nils Avatar asked Dec 18 '09 11:12

Nils


People also ask

How to get current Screen size in WPF?

In order to get the correct monitor size in my WPF application I had to make the application DPI aware. Then I used the Win32 API to get the monitor size. I did this by first moving the window to the monitor that I wanted to get the size from.


Video Answer


2 Answers

I created a little wrapper around the Screen from System.Windows.Forms, currently everything works... Not sure about the "device independent pixels", though.

public class WpfScreen {     public static IEnumerable<WpfScreen> AllScreens()     {         foreach (Screen screen in System.Windows.Forms.Screen.AllScreens)         {             yield return new WpfScreen(screen);         }     }      public static WpfScreen GetScreenFrom(Window window)     {         WindowInteropHelper windowInteropHelper = new WindowInteropHelper(window);         Screen screen = System.Windows.Forms.Screen.FromHandle(windowInteropHelper.Handle);         WpfScreen wpfScreen = new WpfScreen(screen);         return wpfScreen;     }      public static WpfScreen GetScreenFrom(Point point)     {         int x = (int) Math.Round(point.X);         int y = (int) Math.Round(point.Y);          // are x,y device-independent-pixels ??         System.Drawing.Point drawingPoint = new System.Drawing.Point(x, y);         Screen screen = System.Windows.Forms.Screen.FromPoint(drawingPoint);         WpfScreen wpfScreen = new WpfScreen(screen);          return wpfScreen;     }      public static WpfScreen Primary     {         get { return new WpfScreen(System.Windows.Forms.Screen.PrimaryScreen); }     }      private readonly Screen screen;      internal WpfScreen(System.Windows.Forms.Screen screen)     {         this.screen = screen;     }      public Rect DeviceBounds     {         get { return this.GetRect(this.screen.Bounds); }     }      public Rect WorkingArea     {         get { return this.GetRect(this.screen.WorkingArea); }     }      private Rect GetRect(Rectangle value)     {         // should x, y, width, height be device-independent-pixels ??         return new Rect                    {                        X = value.X,                        Y = value.Y,                        Width = value.Width,                        Height = value.Height                    };     }      public bool IsPrimary     {         get { return this.screen.Primary; }     }      public string DeviceName     {         get { return this.screen.DeviceName; }     } } 
like image 88
Nils Avatar answered Oct 12 '22 22:10

Nils


Here budy. This will give you only the width and height of the workarea

System.Windows.SystemParameters.WorkArea.Width System.Windows.SystemParameters.WorkArea.Height 
like image 29
Guilherme Ferreira Avatar answered Oct 12 '22 22:10

Guilherme Ferreira