Windows10中多屏显示器型号获取并与Screen对应
需求:标识某块屏,不参与窗口快速移动
@@@code@csharp@
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | public class Monitor { /// <summary> /// DeviceID,如: \\.\DISPLAY17 /// </summary> public String DeviceName { get ; set ; } /// <summary> /// 名称,如: Default_Monitor /// </summary> public String ModelName { get ; set ; } /// <summary> /// 如: MONITOR\Default_Monitor\{4d36e96e-e325-11ce-bfc1-08002be10318}\0004 /// </summary> public String Description { get ; set ; } /// <summary> /// DeviceString =Microsoft Remote Display Adapter /// </summary> public String DeviceString { get ; set ; } public String DeviceKey { get ; set ; } public int ScreenID { get ; set ; } public int ID { get { if (String.IsNullOrEmpty(DeviceKey)) return 0; return 1 + int .Parse(DeviceKey.Split( '\\' ).Last()); } } public bool Status { get ; set ; } = true ; public bool IsPrimary { get ; set ; } [DllImport( "User32.dll" )] private static extern bool EnumDisplayDevices( string lpDevice, int iDevNum, ref DISPLAY_DEVICE lpDisplayDevice, int dwFlags); [StructLayout(LayoutKind.Sequential)] public struct DISPLAY_DEVICE { public int cb; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] public string DeviceName; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] public string DeviceString; public int StateFlags; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] public string DeviceID; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] public string DeviceKey; public DISPLAY_DEVICE( int flags) { cb = 0; StateFlags = flags; DeviceName = new string (( char )32, 32); DeviceString = new string (( char )32, 128); DeviceID = new string (( char )32, 128); DeviceKey = new string (( char )32, 128); cb = Marshal.SizeOf( this ); } } public static List<Monitor> All() { DISPLAY_DEVICE lpDisplayDevice = new DISPLAY_DEVICE(0); // OUT DISPLAY_DEVICE monitor_name = new DISPLAY_DEVICE(0); // OUT int devNum = 0; List<Monitor> all = new List<Monitor>(); while (EnumDisplayDevices( null , devNum, ref lpDisplayDevice, 0)) { if (EnumDisplayDevices(lpDisplayDevice.DeviceName, 0, ref monitor_name, 0)) { all.Add( new Monitor() { DeviceName = lpDisplayDevice.DeviceName.Trim(), Description = monitor_name.DeviceID, DeviceString = lpDisplayDevice.DeviceString.Trim(), ModelName = monitor_name.DeviceID.Split( '\\' )[1], DeviceKey = lpDisplayDevice.DeviceKey.Trim() }); } ++devNum; } return all; } } Color[] colors = new Color[] { Color.AliceBlue, Color.LightBlue, Color.LightSeaGreen, Color.LightSkyBlue }; Color getColor(Monitor monitor) { return monitor.Status ? (monitor.IsPrimary ? Color.LightGreen : colors[monitor.ScreenID]) : Color.Gray; } void renderMonitor() { var ss = Properties.Settings.Default.ExcludeScreen.Split( ',' ).ToList(); double p = 0.05; int w = splitContainer1.Panel2.Width / 2; int h = splitContainer1.Panel2.Height / 5; int idx = 0; foreach ( var screen in Screen.AllScreens) { var monitor = monitors.FirstOrDefault(r => r.DeviceName == screen.DeviceName); if (monitor == null ) continue ; if (ss.Count > 0) { monitor.Status = !ss.Any(r => r == (idx + 1).ToString()); } monitor.IsPrimary = screen.Primary; //int i = int.Parse(screen.DeviceName.Split('Y').Last()); monitor.ScreenID = idx; var panel = new Label(); panel.AutoSize = false ; panel.TextAlign = ContentAlignment.MiddleCenter; panel.Text = $ "{monitor.ID}\r\n{monitor.ModelName}" ; panel.Width = ( int )(p * screen.Bounds.Width); panel.Height = ( int )(p * screen.Bounds.Height); panel.Left = ( int )(p * screen.Bounds.Left) + w; panel.Top = ( int )(p * screen.Bounds.Top) + h; panel.BackColor = getColor(monitor); panel.Tag = monitor; panel.DoubleClick += (s, e) => { var m = (s as Label).Tag as Monitor; if (m != null ) { m.Status = !m.Status; (s as Label).BackColor = getColor(m); Properties.Settings.Default.ExcludeScreen = String.Join( "," , monitors.Where(r => !r.Status).Select(r => r.ScreenID + 1).ToList()); Properties.Settings.Default.Save(); } }; //todo:重新编排序号,按指定序号切屏 idx++; splitContainer1.Panel2.Controls.Add(panel); } } |
Qinqoushui@163.com
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗