How to get the size of the current screen in WPF?
Asked Answered
L

16

118

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.

Lachellelaches answered 18/12, 2009 at 11:2 Comment(1)
Define "current". A window can be on more than one screen at once.Verve
Q
17

As far as I know, there is no native WPF method(s) to get the dimensions of a given monitor. Instead, you could PInvoke native Multiple Display Monitors Functions, wrap them in a managed class (or use Microsoft's official CsWin32 Nuget package) and expose all properties you need to consume from XAML.

Quickwitted answered 18/12, 2009 at 11:17 Comment(2)
That is exactly what I feared -- the need to P/Invoke the stuff or accessing System.Windows.Forms.Screen somehow. And when doing so I always need to calculte the "device independent pixels"... Thanks, though.Lachellelaches
Yes... Maybe SystemParameters.ConvertPixel() function will also help you. It's internal, but Reflector doesn't care :)...Quickwitted
L
83

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; }
    }
}
Lachellelaches answered 22/1, 2010 at 17:5 Comment(10)
Thanks for this great little wrapper, note that the global::Rect needed converting to just plain Rect when I used with WPF 3.5.Cosher
SystemParameters.PrimaryScreenHeight gets the "device independent pixels". This unfortunately does not get "device independent pixels", rather the physical pixels. Oh well.Ashjian
@MarkLopez: I did not use PrimaryScreenHeigth because I was searching for the dimensions of the current screen, not primary. See also the other anwsers here regarding the PrimaryScreen* or VirtualScreen* or WorkAreaLachellelaches
I'm saying this post doesn't accurately answer the question. The OP wanted "device independent pixels", hence used SystemParameters.PrimaryScreenHeight. You gave a result as physical pixels using Forms.Screen.FromPoint. This answer will be off on different DPI's and can significantly different on multi-screened devices in Windows 8.1 (which introduced improved DPI scaling for 4K screens).Ashjian
I like this. Sure it needs a bit of work, but I don't expect to find 100% solutions.Adjunction
This answer is correct and would provide a far better answer to the following related questions: #6278407 #2705387 #1928040Abeokuta
Works great. I just extended the GetRect method to return the Rect in device independent pixels: private Rect GetRect(Rectangle value) { var pixelWidthFactor = SystemParameters.WorkArea.Width / this.screen.WorkingArea.Width; var pixelHeightFactor = SystemParameters.WorkArea.Height / this.screen.WorkingArea.Height; return new Rect { X = value.X * pixelWidthFactor, Y = value.Y * pixelHeightFactor, Width = value.Width * pixelWidthFactor, Height = value.Height * pixelHeightFactor }; }Evania
I believe adding the code from @JürgenBayer will improve your answer even more. I had the issue with the device independent pixels and the code from Jürgen solved it. Thank you both.Ulu
@Jürgen: I believe your method only works under very specific circumstances. If "this.screen" has a different aspect ratio than the primary monitor (which your method always uses as a reference instead of the current monitor), you will incorrectly get different scale factors for width and height leading to wrong screen dimensions. If the current screen has a different DPI setting than the primary screen, the bounds will be all wrong. On my system, every single value of the returned Rect is (wildly) incorrect.Briarwood
This does not work for me. I get a screen size of 2400 x 1350 pxl wheras windows says it is 1920 x 1080.Toscano
B
55

I also needed the current screen dimension, specifically the Work-area, which returned the rectangle excluding the Taskbar width.

I used it in order to reposition a window, which is opened to the right and down to where the mouse is positioned. Since the window is fairly large, in many cases it got out of the screen bounds. The following code is based on @e-j answer: This will give you the current screen.... The difference is that I also show my repositioning algorithm, which I assume is actually the point.

The code:

using System.Windows;
using System.Windows.Forms;

namespace MySample
{

    public class WindowPostion
    {
        /// <summary>
        /// This method adjust the window position to avoid from it going 
        /// out of screen bounds.
        /// </summary>
        /// <param name="topLeft">The requiered possition without its offset</param>
        /// <param name="maxSize">The max possible size of the window</param>
        /// <param name="offset">The offset of the topLeft postion</param>
        /// <param name="margin">The margin from the screen</param>
        /// <returns>The adjusted position of the window</returns>
        System.Drawing.Point Adjust(System.Drawing.Point topLeft, System.Drawing.Point maxSize, int offset, int margin)
        {
            Screen currentScreen = Screen.FromPoint(topLeft);
            System.Drawing.Rectangle rect = currentScreen.WorkingArea;

            // Set an offset from mouse position.
            topLeft.Offset(offset, offset);

            // Check if the window needs to go above the task bar, 
            // when the task bar shadows the HUD window.
            int totalHight = topLeft.Y + maxSize.Y + margin;

            if (totalHight > rect.Bottom)
            {
                topLeft.Y -= (totalHight - rect.Bottom);

                // If the screen dimensions exceed the hight of the window
                // set it just bellow the top bound.
                if (topLeft.Y < rect.Top)
                {
                    topLeft.Y = rect.Top + margin;
                }
            }

            int totalWidth = topLeft.X + maxSize.X + margin;
            // Check if the window needs to move to the left of the mouse, 
            // when the HUD exceeds the right window bounds.
            if (totalWidth > rect.Right)
            {
                // Since we already set an offset remove it and add the offset 
                // to the other side of the mouse (2x) in addition include the 
                // margin.
                topLeft.X -= (maxSize.X + (2 * offset + margin));

                // If the screen dimensions exceed the width of the window
                // don't exceed the left bound.
                if (topLeft.X < rect.Left)
                {
                    topLeft.X = rect.Left + margin;
                }
            }

            return topLeft;
        }
    }
}

Some explanations:

1) topLeft - position of the top left at the desktop (works                     
   for multi screens - with different aspect ratio).                            
            Screen1              Screen2                                        
        ─  ┌───────────────────┐┌───────────────────┐ Screen3                   
        ▲  │                   ││                   │┌─────────────────┐  ─     
        │  │                   ││                   ││   ▼-            │  ▲     
   1080 │  │                   ││                   ││                 │  │     
        │  │                   ││                   ││                 │  │ 900 
        ▼  │                   ││                   ││                 │  ▼     
        ─  └──────┬─────┬──────┘└──────┬─────┬──────┘└──────┬────┬─────┘  ─     
                 ─┴─────┴─            ─┴─────┴─            ─┴────┴─             
           │◄─────────────────►││◄─────────────────►││◄───────────────►│        
                   1920                 1920                1440                
   If the mouse is in Screen3 a possible value might be:                        
   topLeft.X=4140 topLeft.Y=195                                                 
2) offset - the offset from the top left, one value for both                    
   X and Y directions.                                                          
3) maxSize - the maximal size of the window - including its                     
   size when it is expanded - from the following example                        
   we need maxSize.X = 200, maxSize.Y = 150 - To avoid the expansion            
   being out of bound.                                                          

   Non expanded window:                                                         
   ┌──────────────────────────────┐ ─                                           
   │ Window Name               [X]│ ▲                                           
   ├──────────────────────────────┤ │                                           
   │         ┌─────────────────┐  │ │ 100                                       
   │  Text1: │                 │  │ │                                           
   │         └─────────────────┘  │ │                                           
   │                         [▼]  │ ▼                                           
   └──────────────────────────────┘ ─                                           
   │◄────────────────────────────►│                                             
                 200                                                            

   Expanded window:                                                             
   ┌──────────────────────────────┐ ─                                           
   │ Window Name               [X]│ ▲                                           
   ├──────────────────────────────┤ │                                           
   │         ┌─────────────────┐  │ │                                           
   │  Text1: │                 │  │ │                                           
   │         └─────────────────┘  │ │ 150                                       
   │                         [▲]  │ │                                           
   │         ┌─────────────────┐  │ │                                           
   │  Text2: │                 │  │ │                                           
   │         └─────────────────┘  │ ▼                                           
   └──────────────────────────────┘ ─                                           
   │◄────────────────────────────►│                                             
                 200                                                            
4) margin - The distance the window should be from the screen                   
   work-area - Example:                                                          
   ┌─────────────────────────────────────────────────────────────┐ ─            
   │                                                             │ ↕ Margin     
   │                                                             │ ─            
   │                                                             │              
   │                                                             │              
   │                                                             │              
   │                          ┌──────────────────────────────┐   │              
   │                          │ Window Name               [X]│   │              
   │                          ├──────────────────────────────┤   │              
   │                          │         ┌─────────────────┐  │   │              
   │                          │  Text1: │                 │  │   │              
   │                          │         └─────────────────┘  │   │              
   │                          │                         [▲]  │   │              
   │                          │         ┌─────────────────┐  │   │              
   │                          │  Text2: │                 │  │   │              
   │                          │         └─────────────────┘  │   │              
   │                          └──────────────────────────────┘   │ ─            
   │                                                             │ ↕ Margin     
   ├──────────────────────────────────────────────────┬──────────┤ ─            
   │[start] [♠][♦][♣][♥]                              │en│ 12:00 │              
   └──────────────────────────────────────────────────┴──────────┘              
   │◄─►│                                                     │◄─►│              
    Margin                                                    Margin            

* Note that this simple algorithm will always want to leave the cursor          
  out of the window, therefor the window will jumps to its left:                
  ┌─────────────────────────────────┐        ┌─────────────────────────────────┐
  │                  ▼-┌──────────────┐      │  ┌──────────────┐▼-             │
  │                    │ Window    [X]│      │  │ Window    [X]│               │
  │                    ├──────────────┤      │  ├──────────────┤               │
  │                    │       ┌───┐  │      │  │       ┌───┐  │               │
  │                    │  Val: │   │  │ ->   │  │  Val: │   │  │               │
  │                    │       └───┘  │      │  │       └───┘  │               │
  │                    └──────────────┘      │  └──────────────┘               │
  │                                 │        │                                 │
  ├──────────────────────┬──────────┤        ├──────────────────────┬──────────┤
  │[start] [♠][♦][♣]     │en│ 12:00 │        │[start] [♠][♦][♣]     │en│ 12:00 │
  └──────────────────────┴──────────┘        └──────────────────────┴──────────┘
  If this is not a requirement, you can add a parameter to just use             
  the margin:                                                                   
  ┌─────────────────────────────────┐        ┌─────────────────────────────────┐
  │                  ▼-┌──────────────┐      │                ┌─▼-───────────┐ │
  │                    │ Window    [X]│      │                │ Window    [X]│ │
  │                    ├──────────────┤      │                ├──────────────┤ │
  │                    │       ┌───┐  │      │                │       ┌───┐  │ │
  │                    │  Val: │   │  │ ->   │                │  Val: │   │  │ │
  │                    │       └───┘  │      │                │       └───┘  │ │
  │                    └──────────────┘      │                └──────────────┘ │
  │                                 │        │                                 │
  ├──────────────────────┬──────────┤        ├──────────────────────┬──────────┤
  │[start] [♠][♦][♣]     │en│ 12:00 │        │[start] [♠][♦][♣]     │en│ 12:00 │
  └──────────────────────┴──────────┘        └──────────────────────┴──────────┘
* Supports also the following scenarios:
  1) Screen over screen:
       ┌─────────────────┐  
       │                 │
       │                 │
       │                 │
       │                 │
       └─────────────────┘
     ┌───────────────────┐ 
     │                   │ 
     │  ▼-               │ 
     │                   │ 
     │                   │ 
     │                   │ 
     └──────┬─────┬──────┘ 
           ─┴─────┴─       
  2) Window bigger than screen hight or width
     ┌─────────────────────────────────┐        ┌─────────────────────────────────┐ 
     │                                 │        │ ┌──────────────┐                │
     │                                 │        │ │ Window    [X]│                │
     │                  ▼-┌────────────│─┐      │ ├──────────────┤ ▼-             │
     │                    │ Window    [│]│      │ │       ┌───┐  │                │
     │                    ├────────────│─┤ ->   │ │  Val: │   │  │                │ 
     │                    │       ┌───┐│ │      │ │       └───┘  │                │
     │                    │  Val: │   ││ │      │ │       ┌───┐  │                │
     │                    │       └───┘│ │      │ │  Val: │   │  │                │
     ├──────────────────────┬──────────┤ │      ├──────────────────────┬──────────┤
     │[start] [♠][♦][♣]     │en│ 12:00 │ │      │[start] [♠][♦][♣]     │en│ 12:00 │
     └──────────────────────┴──────────┘ │      └──────────────────────┴──────────┘
                          │       ┌───┐  │        │       └───┘  │
                          │  Val: │   │  │        └──────────────┘
                          │       └───┘  │
                          └──────────────┘


     ┌─────────────────────────────────┐             ┌─────────────────────────────────┐     
     │                                 │             │                                 │ 
     │                                 │             │ ┌───────────────────────────────│───┐
     │    ▼-┌──────────────────────────│────────┐    │ │ W▼-dow                        │[X]│
     │      │ Window                   │     [X]│    │ ├───────────────────────────────│───┤
     │      ├──────────────────────────│────────┤    │ │       ┌───┐      ┌───┐      ┌─┤─┐ │
     │      │       ┌───┐      ┌───┐   │  ┌───┐ │ -> │ │  Val: │   │ Val: │   │ Val: │ │ │ │
     │      │  Val: │   │ Val: │   │ Va│: │   │ │    │ │       └───┘      └───┘      └─┤─┘ │
     │      │       └───┘      └───┘   │  └───┘ │    │ └───────────────────────────────│───┘
     ├──────────────────────┬──────────┤────────┘    ├──────────────────────┬──────────┤
     │[start] [♠][♦][♣]     │en│ 12:00 │             │[start] [♠][♦][♣]     │en│ 12:00 │     
     └──────────────────────┴──────────┘             └──────────────────────┴──────────┘     
  • I had no choice but using the code format (otherwise the white spaces would have been lost).
  • Originally this appeared in the code above as a <remark><code>...</code></remark>
Binturong answered 13/5, 2020 at 14:1 Comment(2)
I think you need a gold medal for the explanation and illustrationPesthouse
That illustration is just.. Beautiful!!Minor
B
28

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

System.Windows.SystemParameters.WorkArea.Width
System.Windows.SystemParameters.WorkArea.Height
Bend answered 9/9, 2013 at 18:33 Comment(1)
"Gets the size of the work area on the primary display monitor." - not what I was looking for....Lachellelaches
P
23

This will give you the current screen based on the top left of the window just call this.CurrentScreen() to get info on the current screen.

using System.Windows;
using System.Windows.Forms;

namespace Common.Helpers
{
    public static class WindowHelpers
     {
        public static Screen CurrentScreen(this Window window)
         {
             return Screen.FromPoint(new System.Drawing.Point((int)window.Left,(int)window.Top));
         }
     }
}
Progressionist answered 10/3, 2014 at 19:19 Comment(3)
this returns the current screen, based on the top left position of the window that you call the helper function from. But I must be missing something to this question, based on the score of my answer.Progressionist
@jim-balter Voted Up - Actually this is the best answer here, I needed the Screen in order to get the working area and then make sure my dialog is not exceeding the bound, I will post my solution here. Kudos to E.J. for quick to the point answer.Binturong
^ bizarre comment.Verve
Q
17

As far as I know, there is no native WPF method(s) to get the dimensions of a given monitor. Instead, you could PInvoke native Multiple Display Monitors Functions, wrap them in a managed class (or use Microsoft's official CsWin32 Nuget package) and expose all properties you need to consume from XAML.

Quickwitted answered 18/12, 2009 at 11:17 Comment(2)
That is exactly what I feared -- the need to P/Invoke the stuff or accessing System.Windows.Forms.Screen somehow. And when doing so I always need to calculte the "device independent pixels"... Thanks, though.Lachellelaches
Yes... Maybe SystemParameters.ConvertPixel() function will also help you. It's internal, but Reflector doesn't care :)...Quickwitted
D
12

I appreciate this is an old question, but seeing as WPF still doesn't provide a nice way to do this 'out of the box' and the above answers seem a little overly complicated, hopefully you'll find the solution below a bit easier to digest..

  • WPF friendly, i.e. returns device independant units (not WinForm style pixels)
  • Supports monitors at different DPIs
  • Caters for any size/position task bar
  • Extension method on System.Windows.Window.

Enjoy :)

using System.Windows;
using System.Windows.Forms;
using System.Windows.Media;
using Point = System.Drawing.Point;

namespace ClrVpin.Shared
{
    public static class WindowExtensions
    {
        public static Rect GetCurrentScreenWorkArea(this Window window)
        {
            var screen = Screen.FromPoint(new Point((int) window.Left, (int) window.Top));
            var dpiScale = VisualTreeHelper.GetDpi(window);

            return new Rect {Width = screen.WorkingArea.Width / dpiScale.DpiScaleX, Height = screen.WorkingArea.Height / dpiScale.DpiScaleY};
        }
    }
}
Dustydusza answered 27/6, 2021 at 14:27 Comment(1)
Great solution!Hump
A
5

Why not just use this?

var interopHelper = new WindowInteropHelper(System.Windows.Application.Current.MainWindow);
var activeScreen = Screen.FromHandle(interopHelper.Handle);
Archdiocese answered 15/12, 2016 at 9:14 Comment(1)
Screen is Windows.Forms rather than WPF - but this is a starting point. If you look at the solution I used back then (https://mcmap.net/q/186898/-how-to-get-the-size-of-the-current-screen-in-wpf) this is exactly what I did - however I wrapped System.Windows.Forms.Screen to cope with device independent pixelLachellelaches
T
4

I came across this post and found that none of the answers fully captured what I was trying to do. I have a laptop that has a 3840x2160 resolution and two monitors with 1920x1080 resolution. 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. Then by getting the hwnd of the application's MainWindow (doesn't have to be the main window but my application only has one window) and an IntPtr to the monitor. Then I created a new instance of the MONITORINFOEX struct and called the GetMonitorInfo method.

The MONITORINFOEX struct has both the working area and the full resolution of the screen so you can return whichever one you need. This will also allow you to leave out a reference to System.Windows.Forms (assuming you don't need it something else in your application). I used the .NET Framework Reference Source for System.Windows.Forms.Screen to come up with this solution.

public System.Drawing.Size GetMonitorSize()
{
    var window = System.Windows.Application.Current.MainWindow;
    var hwnd = new WindowInteropHelper(window).EnsureHandle();
    var monitor = NativeMethods.MonitorFromWindow(hwnd, NativeMethods.MONITOR_DEFAULTTONEAREST);
    NativeMethods.MONITORINFO info = new NativeMethods.MONITORINFO();
    NativeMethods.GetMonitorInfo(new HandleRef(null, monitor), info);
    return info.rcMonitor.Size;
}

internal static class NativeMethods
{
    public const Int32 MONITOR_DEFAULTTONEAREST = 0x00000002;

    [DllImport("user32.dll")]
    public static extern IntPtr MonitorFromWindow(IntPtr handle, Int32 flags);

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern bool GetMonitorInfo(HandleRef hmonitor, MONITORINFO info);
    
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto, Pack = 4)]
    public class MONITORINFO
    {
        internal int cbSize = Marshal.SizeOf(typeof(MONITORINFO));
        internal RECT rcMonitor = new RECT();
        internal RECT rcWork = new RECT();
        internal int dwFlags = 0;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct RECT
    {
        public int left;
        public int top;
        public int right;
        public int bottom;

        public RECT(int left, int top, int right, int bottom)
        {
            this.left = left;
            this.top = top;
            this.right = right;
            this.bottom = bottom;
        }

        public RECT(System.Drawing.Rectangle r)
        {
            left = r.Left;
            top = r.Top;
            right = r.Right;
            bottom = r.Bottom;
        }

        public static RECT FromXYWH(int x, int y, int width, int height) => new RECT(x, y, x + width, y + height);

        public System.Drawing.Size Size => new System.Drawing.Size(right - left, bottom - top);
    }
}
Teahouse answered 1/2, 2021 at 20:17 Comment(1)
In the context of trying to get the right rect dimensions to do a screen capture on a monitor that has been scaled to something other than 100%, @tstephansen's solution works perfectly.Respectful
R
3

Take the time to scan through the SystemParameters members.

  • VirtualScreenWidth
  • VirtualScreenHeight

These even take into account the relative positions of the screens.

Only tested with two monitors.

Rockwood answered 18/5, 2010 at 15:52 Comment(3)
dana - I have not tested this, but doesn't VirtualScreen* return the full size of all screens? - I specifically Need the size of one screen (the one in which the current window resides).Lachellelaches
VirtualScreen seems to refer to the size of all screensNathanaelnathanial
One mine this returned the size of all 4 my screens combined.Sardis
C
2

If you are familiar with using System.Windows.Forms class then you can just add a reference of System.Windows.Forms class to your project:

Solution Explorer -> References -> Add References... -> ( Assemblies : Framework ) -> scroll down and check System.Windows.Forms assembly -> OK.

Now you can add using System.Windows.Forms; statement and use screen in your wpf project just like before.

Checkers answered 24/12, 2016 at 4:39 Comment(1)
This is by far the easiest solution. I wonder - apart from adding a rather large assembly, are there any good reasons not to do it this way?Tintoretto
G
1

I understand the demands. The thing is, there are WPF Methods for getting those values - but yes, one of the contributors is right, not directly. The Solution is not to get all those workarounds, but to change the initial approach according to clean Design and Development.

A) Set the initial Main Window to Screen

B) Get the Values for the ActualWindow including a ton of useful WPF Methods

C) You can add as many Windows as you like for the behaviour you want to have, like resizeable, minimized whatever… but now you always can access the Loaded and Rendered Screen

Please be careful with the following example, there is some Code around that makes it necessary to use that kind of approach, however it should work (It would give you the Points for each of the Corners of your Screen): Working Example on Single, Dual Monitor and different Resolutions (Within the Primal Main Window Class):

InitializeComponent();
[…]
ActualWindow.AddHandler(Window.LoadedEvent, new RoutedEventHandler(StartUpScreenLoaded));

Routed Event:

private void StartUpScreenLoaded(object sender, RoutedEventArgs e)
    {
        Window StartUpScreen = sender as Window;

        // Dispatcher Format B:
        Dispatcher.Invoke(new Action(() =>
        {
            // Get Actual Window on Loaded
            StartUpScreen.InvalidateVisual();
            System.Windows.Point CoordinatesTopRight = StartUpScreen.TranslatePoint(new System.Windows.Point((StartUpScreen.ActualWidth), (0d)), ActualWindow);
            System.Windows.Point CoordinatesBottomRight = StartUpScreen.TranslatePoint(new System.Windows.Point((StartUpScreen.ActualWidth), (StartUpScreen.ActualHeight)), ActualWindow);
            System.Windows.Point CoordinatesBottomLeft = StartUpScreen.TranslatePoint(new System.Windows.Point((0d), (StartUpScreen.ActualHeight)), ActualWindow);

            // Set the Canvas Top Right, Bottom Right, Bottom Left Coordinates
            System.Windows.Application.Current.Resources["StartUpScreenPointTopRight"] = CoordinatesTopRight;
            System.Windows.Application.Current.Resources["StartUpScreenPointBottomRight"] = CoordinatesBottomRight;
            System.Windows.Application.Current.Resources["StartUpScreenPointBottomLeft"] = CoordinatesBottomLeft;
        }), DispatcherPriority.Loaded);
    }
Gallic answered 7/5, 2019 at 10:59 Comment(1)
Screen here means the window, not the physical display the window resides on, right?Lachellelaches
F
1

If you use any full screen window (having its WindowState = WindowState.Maximized, WindowStyle = WindowStyle.None), you can wrap its contents in System.Windows.Controls.Canvas like this:

<Canvas Name="MyCanvas" Width="auto" Height="auto">
...
</Canvas>

Then you can use MyCanvas.ActualWidth and MyCanvas.ActualHeight to get the resolution of the current screen, with DPI settings taken into account and in device independent units. It doesn't add any margins as the maximized window itself does.

(Canvas accepts UIElements as children, so you should be able to use it with any content.)

Flatter answered 18/11, 2019 at 15:40 Comment(0)
E
0

For WPF App(.NET framework) :

 double H;
 double W;

You can declare a Window event called: SizeChanged (tracking the screen size change from the client side)

This way you can get the size of the screen at runtime.

private void WindowSizeChange(object sender, SizeChangedEventArgs e)
{
     double H = this.ActualHeight;
     double W = this.ActualWidth;
}
Erbe answered 19/10, 2022 at 4:20 Comment(1)
Welcome to SO. Does that track the size of the WPF window or the current screen the window resides on?Lachellelaches
A
-2

Center Window on the screen in XAML WindowStartupLocation="CenterOwner" then call in WindowLoaded()

double ScreenHeight = 2 * (Top + 0.5 * Height);

Argus answered 20/8, 2019 at 19:31 Comment(0)
C
-6
double screenWidth = System.Windows.SystemParameters.PrimaryScreenWidth;
double screenhight= System.Windows.SystemParameters.PrimaryScreenHeight;
Countermove answered 14/11, 2013 at 9:41 Comment(1)
Like the previous answer this is only for the primary Screen. I needed the current screen.Lachellelaches
M
-6

It works with

this.Width = System.Windows.SystemParameters.VirtualScreenWidth;
this.Height = System.Windows.SystemParameters.VirtualScreenHeight;

Tested on 2 monitors.

Many answered 21/3, 2016 at 6:57 Comment(1)
if you look at the answer from May 18 '10 at 15:52 - which was exactly the same as yours you'll see that VirtualScreen spans all screens - so this will never work if you have more than one screen!Lachellelaches

© 2022 - 2024 — McMap. All rights reserved.