Home > Backend Development > C++ > How to Get the Current Screen Size in WPF on a Multi-Screen Setup?

How to Get the Current Screen Size in WPF on a Multi-Screen Setup?

Susan Sarandon
Release: 2025-01-05 16:22:40
Original
250 people have browsed it

How to Get the Current Screen Size in WPF on a Multi-Screen Setup?

Getting Current Screen Size in WPF

Your question focuses on determining the size of the current screen in a multi-screen setup, where all screens may not use the same resolution. While you've mentioned using SystemParameters.PrimaryScreenWidth and SystemParameters.PrimaryScreenHeight for the primary screen, it's indeed essential to obtain the size of the current screen.

To address this, you can utilize WpfScreen, a wrapper class that encapsulates the Screen class from System.Windows.Forms. This class offers several methods to retrieve screen-related information:

  • AllScreens(): Returns an enumeration of WpfScreen objects representing all screens.
  • GetScreenFrom(Window window): Retrieves the WpfScreen for a specified window.
  • GetScreenFrom(Point point): Obtains the WpfScreen for the screen that contains a given point.
  • Primary: Returns the primary screen.

Additionally, WpfScreen provides the following properties:

  • DeviceBounds: The screen's boundaries in device-independent pixels.
  • WorkingArea: The usable work area on the screen in device-independent pixels.
  • IsPrimary: Indicates whether the screen is the primary display.
  • DeviceName: The display's device name.

Example usage:

// Get the primary screen's size
var primaryScreen = WpfScreen.Primary;
Console.WriteLine("Primary Screen: {0} x {1}", primaryScreen.DeviceBounds.Width, primaryScreen.DeviceBounds.Height);

// Get the current window's screen size
var currentWindow = new Window();  // Replace this with the actual window object
var currentScreen = WpfScreen.GetScreenFrom(currentWindow);
Console.WriteLine("Current Window's Screen: {0} x {1}", currentScreen.DeviceBounds.Width, currentScreen.DeviceBounds.Height);

// Get the screen containing a specified point
var point = new Point(500, 300);
var containingScreen = WpfScreen.GetScreenFrom(point);
Console.WriteLine("Screen Containing Point: {0} x {1}", containingScreen.DeviceBounds.Width, containingScreen.DeviceBounds.Height);
Copy after login

The above is the detailed content of How to Get the Current Screen Size in WPF on a Multi-Screen Setup?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template