You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WinUI3:如何从系统显示设置获取分辨率与方向并添加至ComboBox

WinUI3 动态获取系统显示分辨率与方向并填充ComboBox

一、核心思路

通过WinRT原生API获取系统显示设备的参数,包括各监视器支持的分辨率、当前显示方向及支持的方向集合,再将数据填充到对应的ComboBox控件中。无需额外权限,直接调用Windows SDK提供的API即可实现。

二、具体实现步骤

1. 引用必要命名空间

在代码文件顶部添加以下命名空间:

using Windows.Devices.Display.Core;
using Windows.Graphics.Display;
using System.Linq;
using System.Collections.Generic;

2. 获取并填充分辨率数据

编写方法获取所有显示监视器支持的分辨率(自动去重),并填充到分辨率ComboBox:

private void LoadResolutions()
{
    // 获取当前视图的显示管理器及监视器列表
    var displayManager = DisplayManager.GetForCurrentView();
    var monitors = displayManager.GetCurrentDisplayState().DisplayMonitors;

    // 用HashSet去重存储分辨率字符串(格式:宽度x高度)
    var resolutionSet = new HashSet<string>();
    foreach (var monitor in monitors)
    {
        foreach (var size in monitor.SupportedViewSizes)
        {
            resolutionSet.Add($"{size.Width}x{size.Height}");
        }
    }

    // 清空原有项并添加新数据,按宽度降序排序(可选)
    ResolutionComboBox.Items.Clear();
    foreach (var res in resolutionSet.OrderByDescending(r => int.Parse(r.Split('x')[0])))
    {
        ResolutionComboBox.Items.Add(res);
    }

    // 自动选中当前系统分辨率(可选)
    var currentRes = $"{DisplayInformation.GetForCurrentView().ScreenWidthInRawPixels}x{DisplayInformation.GetForCurrentView().ScreenHeightInRawPixels}";
    var currentItem = ResolutionComboBox.Items.FirstOrDefault(i => i.ToString() == currentRes);
    if (currentItem != null)
    {
        ResolutionComboBox.SelectedItem = currentItem;
    }
}

3. 获取并填充显示方向数据

编写方法获取系统支持的显示方向,转换为友好名称后填充到方向ComboBox:

private void LoadOrientations()
{
    var displayInfo = DisplayInformation.GetForCurrentView();
    var supportedOrientations = displayInfo.SupportedOrientations;

    // 转换枚举值为用户友好名称
    var orientationList = new List<string>();
    foreach (var orientation in supportedOrientations)
    {
        orientationList.Add(orientation switch
        {
            DisplayOrientation.Landscape => "横向",
            DisplayOrientation.Portrait => "纵向",
            DisplayOrientation.LandscapeFlipped => "横向翻转",
            DisplayOrientation.PortraitFlipped => "纵向翻转",
            _ => ""
        });
    }

    // 清空原有项并添加新数据
    OrientationComboBox.Items.Clear();
    foreach (var item in orientationList)
    {
        OrientationComboBox.Items.Add(item);
    }

    // 自动选中当前显示方向(可选)
    var currentOrientationStr = displayInfo.CurrentOrientation switch
    {
        DisplayOrientation.Landscape => "横向",
        DisplayOrientation.Portrait => "纵向",
        DisplayOrientation.LandscapeFlipped => "横向翻转",
        DisplayOrientation.PortraitFlipped => "纵向翻转",
        _ => ""
    };
    var currentOrientationItem = OrientationComboBox.Items.FirstOrDefault(i => i.ToString() == currentOrientationStr);
    if (currentOrientationItem != null)
    {
        OrientationComboBox.SelectedItem = currentOrientationItem;
    }
}

4. 初始化与监听设置变更

  • 在页面加载时调用上述方法,确保控件初始化完成后填充数据:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);
    LoadResolutions();
    LoadOrientations();

    // 监听显示设置变更,实时更新ComboBox内容
    var displayInfo = DisplayInformation.GetForCurrentView();
    displayInfo.DisplayContentsChanged += (sender, args) =>
    {
        LoadResolutions();
        LoadOrientations();
    };
}

5. MVVM模式下的绑定(可选)

若采用MVVM架构,可将数据集合放在ViewModel中,通过数据绑定填充ComboBox:

// ViewModel代码
public ObservableCollection<string> Resolutions { get; set; } = new ObservableCollection<string>();
public ObservableCollection<string> Orientations { get; set; } = new ObservableCollection<string>();
public string SelectedResolution { get; set; }
public string SelectedOrientation { get; set; }

public void LoadDisplayData()
{
    // 复用前面的逻辑,将数据添加到Resolutions和Orientations集合中
    // 并设置SelectedResolution和SelectedOrientation
}

对应的XAML绑定代码:

<ComboBox ItemsSource="{x:Bind ViewModel.Resolutions}" 
          SelectedItem="{x:Bind ViewModel.SelectedResolution, Mode=TwoWay}"/>
<ComboBox ItemsSource="{x:Bind ViewModel.Orientations}" 
          SelectedItem="{x:Bind ViewModel.SelectedOrientation, Mode=TwoWay}"/>

内容的提问来源于stack exchange,提问作者Mohan Raj B

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 15:13:16