c# 获取电脑 分辨率 及 DPI 设置
using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Runtime.InteropServices; /// /// 这个可以 /// class Program { static void Main() { //设置DPI感知 try { SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); } catch { // 旧版Windows回退方案 try { SetProcessDPIAware(); } catch { } } try { // 获取所有显示器信息 var monitors = GetAllMonitors(); if (monitors.Count == 0) { Console.WriteLine("未检测到显示器"); return; } // 创建或覆盖本地txt文件 string filePath = @"C:\DisplayInfo.txt"; using (StreamWriter writer = new StreamWriter(filePath)) { writer.WriteLine("显示器信息报告"); writer.WriteLine($"获取时间: {DateTime.Now:yyyy-MM-dd HH:mm:ss}"); writer.WriteLine($"检测到 {monitors.Count} 台显示器"); foreach (var monitor in monitors) { writer.WriteLine($"显示器名称: {monitor.DeviceName} {(monitor.IsPrimary ? "(主显示器)" : "")}"); writer.WriteLine($"分辨率: {monitor.Width} × {monitor.Height}"); writer.WriteLine($"DPI缩放百分比: {monitor.DpiScalePercent:F0}%"); } } Console.WriteLine($"信息已写入文件:{filePath}"); Console.WriteLine("按任意键打开文件..."); Console.ReadKey(); Process.Start("notepad.exe", filePath); } catch (Exception ex) { Console.WriteLine($"发生错误:{ex.Message}"); } } //DPI感知声明 [DllImport("shcore.dll")] private static extern int SetProcessDpiAwareness(int value); private const int PROCESS_PER_MONITOR_DPI_AWARE = 2; //显示器API声明 [StructLayout(LayoutKind.Sequential)] public struct RECT { public int Left; public int Top; public int Right; public int Bottom; } [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] public struct MONITORINFOEX { public int Size; public RECT Monitor; public RECT WorkArea; public uint Flags; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] public string DeviceName; } [DllImport("user32.dll")] private static extern IntPtr MonitorFromWindow(IntPtr hwnd, uint dwFlags); [DllImport("user32.dll", CharSet = CharSet.Auto)] private static extern bool EnumDisplayMonitors(IntPtr hdc, IntPtr lprcClip, MonitorEnumDelegate lpfnEnum, IntPtr dwData); [DllImport("user32.dll", CharSet = CharSet.Auto)] private static extern bool GetMonitorInfo(IntPtr hMonitor, ref MONITORINFOEX lpmi); [DllImport("shcore.dll")] private static extern int GetDpiForMonitor(IntPtr hmonitor, int dpiType, out uint dpiX, out uint dpiY); [DllImport("user32.dll")] private static extern IntPtr GetDesktopWindow(); [DllImport("user32.dll")] private static extern bool SetProcessDPIAware(); private delegate bool MonitorEnumDelegate(IntPtr hMonitor, IntPtr hdcMonitor, ref RECT lprcMonitor, IntPtr dwData); // 常量定义 private const int MDT_EFFECTIVE_DPI = 0; private const int MONITOR_DEFAULTTOPRIMARY = 1; // 显示器信息类 public class DisplayInfo { public string DeviceName { get; set; } public int Width { get; set; } public int Height { get; set; } public double DpiScalePercent { get; set; } public bool IsPrimary { get; set; } } // 获取所有显示器信息的方法 private static List GetAllMonitors() { var displayInfos = new List(); MonitorEnumDelegate monitorDelegate = (IntPtr hMonitor, IntPtr hdcMonitor, ref RECT lprcMonitor, IntPtr dwData) => { MONITORINFOEX mi = new MONITORINFOEX(); mi.Size = Marshal.SizeOf(mi); if (GetMonitorInfo(hMonitor, ref mi)) { uint dpiX = 96, dpiY = 96; bool isPrimary = (mi.Flags & 0x1) != 0; // 使用专用API获取DPI if (GetDpiForMonitor(hMonitor, MDT_EFFECTIVE_DPI, out dpiX, out dpiY) != 0) { // API失败时使用主显示器DPI IntPtr primaryMonitor = MonitorFromWindow(GetDesktopWindow(), MONITOR_DEFAULTTOPRIMARY); GetDpiForMonitor(primaryMonitor, MDT_EFFECTIVE_DPI, out dpiX, out dpiY); } // 计算DPI百分比 (基于96 DPI为100%) double dpiScalePercent = Math.Round((dpiX / 96.0) * 100, 0); displayInfos.Add(new DisplayInfo { DeviceName = mi.DeviceName.Trim(), Width = mi.Monitor.Right - mi.Monitor.Left, Height = mi.Monitor.Bottom - mi.Monitor.Top, DpiScalePercent = dpiScalePercent, IsPrimary = isPrimary }); } return true; }; EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, monitorDelegate, IntPtr.Zero); return displayInfos; } }
新建控制台应用程序执行即可
如下,一台显示器:
多台显示器信息
免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们。