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.
SystemParameters provides direct access to the dimensions of the primary screen:
double primaryScreenWidth = System.Windows.SystemParameters.PrimaryScreenWidth; double primaryScreenHeight = System.Windows.SystemParameters.PrimaryScreenHeight;
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;
The WpfScreen class provides methods to:
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>
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!