Home > Backend Development > C++ > How to Get the Current Screen's Dimensions in WPF?

How to Get the Current Screen's Dimensions in WPF?

DDD
Release: 2025-01-05 13:11:40
Original
588 people have browsed it

How to Get the Current Screen's Dimensions in WPF?

How to Determine the Current Screen's Dimensions in WPF

In multi-monitor environments, the primary screen may not always be the one in use or the one with the highest resolution. Therefore, it's essential to be able to retrieve the size of the current screen you're interested in.

WPF Method: Using SystemParameters

SystemParameters provides direct access to the dimensions of the primary screen:

double primaryScreenWidth = System.Windows.SystemParameters.PrimaryScreenWidth;
double primaryScreenHeight = System.Windows.SystemParameters.PrimaryScreenHeight;
Copy after login

Multi-Screen Support: Using WpfScreen Wrapper

For scenarios with multiple screens with varying resolutions, the WpfScreen class offers a more flexible approach:

private WpfScreen CurrentScreen
{
    get { return WpfScreen.GetScreenFrom(this); }
}

public double ScreenWidth => CurrentScreen.DeviceBounds.Width;
public double ScreenHeight => CurrentScreen.DeviceBounds.Height;
Copy after login

The WpfScreen class provides methods to:

  • Get the dimensions of all active screens
  • Get the screen associated with a specific window
  • Determine if the current screen is the primary display

Using the Wrapper in XAML

The WpfScreen class can be used in XAML through attached properties:

<Window>
    <Window.AttachedProperties>
        <my:WpfScreenAttachedProperties.WpfScreen>
            <my:WpfScreen DeviceBounds="{Binding ElementName=myCtrl, Path=ScreenWidth, Mode=OneWay}"/>
        </my:WpfScreenAttachedProperties.WpfScreen>
    </Window.AttachedProperties>
    ...
</Window>
Copy after login

The above is the detailed content of How to Get the Current Screen's Dimensions in WPF?. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template