In developing WPF, use System.Windows.SystemParameters to get screen resolution
//Get the working area width of the screen
double x = SystemParameters.WorkArea.Width;
//Get screen working area height
double y = SystemParameters.WorkArea.Height;
//Get the overall screen width
double x1= SystemParameters.PrimaryScreenWidth;
//Get the overall height of the screen
double y1 = SystemParameters.PrimaryScreenHeight;
My favorite WPF window size setting:
Height="{Binding Source={x:Static SystemParameters.PrimaryScreenHeight}, Converter={StaticResource RatioConverter}, ConverterParameter='0.8'}"
Width="{Binding RelativeSource={RelativeSource Self}, Path=Height, Converter={StaticResource RatioConverter}, ConverterParameter='1.6'}"
MinHeight="{Binding Source={x:Static SystemParameters.PrimaryScreenHeight}, Converter={StaticResource RatioConverter}, ConverterParameter='0.5'}"
MinWidth="{Binding RelativeSource={RelativeSource Self}, Path=MinHeight, Converter={StaticResource RatioConverter}, ConverterParameter='1.6'}"
The implementation code of RatioConverter is as follows:
[ValueConversion(typeof(string), typeof(string))]
public class RatioConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var size = 0d;
if (value != null)
size = System.Convert.ToDouble(value, CultureInfo.InvariantCulture) *
System.Convert.ToDouble(parameter, CultureInfo.InvariantCulture);
return size;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
- Link : https://www.zdyla.com/en/post/in-developing-wpf-use-system.windows.systemparameters-to-get-screen-resolution.html
- Copyright Notice : Unless otherwise stated, please contact the author for authorization and indicate the source!