Maison > développement back-end > C++ > Comment obtenir efficacement les dimensions de l'écran actuel dans WPF ?

Comment obtenir efficacement les dimensions de l'écran actuel dans WPF ?

Linda Hamilton
Libérer: 2025-01-04 14:03:39
original
952 Les gens l'ont consulté

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

WPF : acquisition des dimensions de l'écran actuel

Récupérer la taille de l'écran principal est simple à l'aide de SystemParameters, mais déterminer la taille de l'écran actuel L'écran peut être plus complexe, notamment dans les configurations multi-écrans. Pour relever ce défi, envisagez l'approche suivante :

Utilisation d'un wrapper d'écran WinForms

La création d'un wrapper compatible WPF autour de l'objet System.Windows.Forms.Screen fournit un moyen pratique d’accéder aux informations liées à l’écran. Voici un exemple d'implémentation :

public class WpfScreen
{
    public static IEnumerable<WpfScreen> AllScreens()
    {
        foreach (Screen screen in System.Windows.Forms.Screen.AllScreens)
        {
            yield return new WpfScreen(screen);
        }
    }

    public static WpfScreen GetScreenFrom(Window window)
    {
        WindowInteropHelper windowInteropHelper = new WindowInteropHelper(window);
        Screen screen = System.Windows.Forms.Screen.FromHandle(windowInteropHelper.Handle);
        WpfScreen wpfScreen = new WpfScreen(screen);
        return wpfScreen;
    }

    public static WpfScreen GetScreenFrom(Point point)
    {
        int x = (int)Math.Round(point.X);
        int y = (int)Math.Round(point.Y);
        System.Drawing.Point drawingPoint = new System.Drawing.Point(x, y);
        Screen screen = System.Windows.Forms.Screen.FromPoint(drawingPoint);
        WpfScreen wpfScreen = new WpfScreen(screen);
        return wpfScreen;
    }

    public static WpfScreen Primary
    {
        get { return new WpfScreen(System.Windows.Forms.Screen.PrimaryScreen); }
    }

    private readonly Screen screen;

    internal WpfScreen(System.Windows.Forms.Screen screen)
    {
        this.screen = screen;
    }

    public Rect DeviceBounds
    {
        get { return this.GetRect(this.screen.Bounds); }
    }

    public Rect WorkingArea
    {
        get { return this.GetRect(this.screen.WorkingArea); }
    }

    private Rect GetRect(Rectangle value)
    {
        return new Rect
        {
            X = value.X,
            Y = value.Y,
            Width = value.Width,
            Height = value.Height
        };
    }

    public bool IsPrimary
    {
        get { return this.screen.Primary; }
    }

    public string DeviceName
    {
        get { return this.screen.DeviceName; }
    }
}
Copier après la connexion

Accès aux dimensions de l'écran en XAML

Bien que l'accès à la taille de l'écran à partir de XAML ne soit pas directement possible, vous pouvez utiliser les propriétés attachées en combinaison avec la classe wrapper :

<UserControl>
    <UserControl.Resources>
        <local:WpfScreenExtension x:Key="ScreenExtension" />
    </UserControl.Resources>

    <StackPanel>
        <TextBlock Text="Device Bounds Width: {Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=Width, Converter={StaticResource ScreenExtension}}" VerticalAlignment="Center" />
        <TextBlock Text="Working Area Height: {Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=Height, Converter={StaticResource ScreenExtension}}" VerticalAlignment="Center" />
    </StackPanel>
</UserControl>
Copier après la connexion
public class WpfScreenExtension : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        UserControl userControl = value as UserControl;
        if (userControl == null)
            return null;

        Rect bounds = WpfScreen.GetScreenFrom(userControl).DeviceBounds;
        if (bounds.IsEmpty)
            return String.Empty;

        if (parameter?.ToString() == "Width")
            return bounds.Width;
        else if (parameter?.ToString() == "Height")
            return bounds.Height;
        else
            return bounds;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return null;
    }
}
Copier après la connexion

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Derniers articles par auteur
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal