Home >Backend Development >C++ >How to Get the Active Screen's Dimensions in WPF?
How to Obtain the Dimensions of the Currently Active Screen in WPF
In the realm of multi-screen setups, it becomes crucial to accurately determine the size of the screen on which an application is displayed. While WPF provides access to the primary screen's dimensions via SystemParameters.PrimaryScreenWidth and SystemParameters.PrimaryScreenHeight, this may not always suffice.
To address this limitation, a custom solution can be implemented using the Screen class from the System.Windows.Forms namespace. This approach offers the ability to retrieve the screen size from XAML or C# code.
The code snippet below encapsulates the functionality in a C# class:
public class WpfScreen { // ... (class definition and methods) }
Usage:
C#: Use the following methods to obtain the screen size:
Example:
WpfScreen currentScreen = WpfScreen.GetScreenFrom(Window.GetWindow(this)); double screenWidth = currentScreen.DeviceBounds.Width; double screenHeight = currentScreen.DeviceBounds.Height;
This class provides a convenient way to determine the dimensions of the active screen, enabling accurate application placement and size adjustments in multi-monitor environments.
The above is the detailed content of How to Get the Active Screen's Dimensions in WPF?. For more information, please follow other related articles on the PHP Chinese website!