Original: Three ways to get the width and height of the current screen in WPF
The units in a WPF program are device-independent units, each unit is 1/96 inches, and if the computer's DPI is set to 96 (96 pixels per inch), then each WPF unit corresponds to one pixel at a time, but if the computer has a DPI device of 120 (120 pixels per inch), That at this time each WPF unit corresponds to a 120/96=1.25 pixel
Generally in the program we often need to get the current screen width and height, common practices are:
1. These two methods can return the resolution of the current screen selection, which is in pixels, at a dpi of 96 we can use them to do some positioning of the control, because the WPF unit corresponds to a pixel, and when the DPI is not 96, the resolution is used to locate the error will be found, Therefore, each WPF unit does not correspond to one pixel at this time
System.Windows.Forms.Screen.PrimaryScreen.Bounds.WidthSystem.Windows.Forms.Screen.PrimaryScreen.Bounds.Height
2. both methods can return the current screen width and height, it is device-independent units (1/96 inches), so it is used to do the positioning of the control, in the case of DPI change, there will be no positioning error
SystemParameters.PrimaryScreenWidthSystemParameters.PrimaryScreenHeight
3. both methods can return the width and height of the current screen's workspace (except for the taskbar), which is also device-independent units, and usually we can combine 2 and the other to get the height of the taskbar
SystemParameters.WorkArea.Size.WidthSystemParameters.WorkArea.Size.Height
Three methods in WPF get the width and height of the current screen