Three methods in WPF get the width and height of the current screen

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.