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

如何在.NET MAUI中获取多显示器的分辨率与位置信息?

在.NET MAUI Windows平台获取多显示器位置与分辨率

目前.NET MAUI框架原生没有提供获取多显示器信息的API,要实现Windows平台的多显示器数据获取,需要通过调用Win32 API来完成,以下是具体实现方案:

实现步骤

1. 定义Win32互操作结构与函数

创建Windows平台专属的辅助类,封装Win32的显示器枚举和信息获取API:

using System.Runtime.InteropServices;

namespace YourAppNamespace.Platforms.Windows;

public static class DisplayHelper
{
    [DllImport("user32.dll")]
    private static extern bool EnumDisplayMonitors(IntPtr hdc, IntPtr lprcClip, MonitorEnumProc lpfnEnum, IntPtr dwData);

    [DllImport("user32.dll", CharSet = CharSet.Unicode)]
    private static extern bool GetMonitorInfoW(IntPtr hMonitor, ref MONITORINFOEX lpmi);

    public delegate bool MonitorEnumProc(IntPtr hMonitor, IntPtr hdcMonitor, ref RECT lprcMonitor, IntPtr dwData);

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public struct MONITORINFOEX
    {
        public int cbSize;
        public RECT rcMonitor;
        public RECT rcWork;
        public uint dwFlags;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
        public string szDevice;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct RECT
    {
        public int Left;
        public int Top;
        public int Right;
        public int Bottom;
    }

    public class DisplayInfo
    {
        public int Left { get; set; }
        public int Top { get; set; }
        public int Width { get; set; }
        public int Height { get; set; }
        public bool IsPrimary { get; set; }
        public string DeviceName { get; set; }
    }

    public static List<DisplayInfo> GetAllDisplays()
    {
        var displays = new List<DisplayInfo>();

        EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero,
            (IntPtr hMonitor, IntPtr hdcMonitor, ref RECT lprcMonitor, IntPtr dwData) =>
            {
                var mi = new MONITORINFOEX();
                mi.cbSize = Marshal.SizeOf(mi);
                if (GetMonitorInfoW(hMonitor, ref mi))
                {
                    displays.Add(new DisplayInfo
                    {
                        Left = mi.rcMonitor.Left,
                        Top = mi.rcMonitor.Top,
                        Width = mi.rcMonitor.Right - mi.rcMonitor.Left,
                        Height = mi.rcMonitor.Bottom - mi.rcMonitor.Top,
                        IsPrimary = (mi.dwFlags & 1) != 0,
                        DeviceName = mi.szDevice
                    });
                }
                return true;
            }, IntPtr.Zero);

        return displays;
    }
}

2. 在MAUI中调用平台特定代码

使用条件编译确保代码仅在Windows平台执行,避免影响其他平台构建:

#if WINDOWS
using YourAppNamespace.Platforms.Windows;
#endif

// 调用示例
private void FetchDisplayData()
{
#if WINDOWS
    var allDisplays = DisplayHelper.GetAllDisplays();
    foreach (var display in allDisplays)
    {
        // 处理显示器数据,例如用于跨屏画布的坐标计算
        Console.WriteLine($"设备: {display.DeviceName} | 位置: ({display.Left}, {display.Top}) | 分辨率: {display.Width}x{display.Height} | 主显示器: {display.IsPrimary}");
    }
#endif
}

说明

  • 该方案通过Win32的EnumDisplayMonitors枚举所有连接的显示器,再通过GetMonitorInfoW获取每个显示器的物理位置、整体分辨率以及主显示器标识。
  • 代码采用平台专属实现,通过条件编译隔离Windows平台代码,符合MAUI的跨平台开发规范。

内容的提问来源于stack exchange,提问作者Johan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 21:30:48