Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System;
- using System.Runtime.InteropServices;
- public class MonitorInfoExample : MonoBehaviour
- {
- // Define the RECT structure
- [StructLayout(LayoutKind.Sequential)]
- public struct RECT
- {
- public int left, top, right, bottom;
- }
- // Define the MONITORINFO structure
- [StructLayout(LayoutKind.Sequential)]
- public struct MONITORINFO
- {
- public uint cbSize;
- public RECT rcMonitor;
- public RECT rcWork;
- public uint dwFlags;
- }
- // Delegate for the monitor enumeration callback function
- public delegate bool MonitorEnumProc(IntPtr hMonitor, IntPtr hdcMonitor, ref RECT lprcMonitor, IntPtr dwData);
- // Import necessary functions from user32.dll
- [DllImport("user32.dll")]
- static extern bool EnumDisplayMonitors(IntPtr hdc, IntPtr lprcClip,
- MonitorEnumProc lpfnEnum, IntPtr dwData);
- [DllImport("user32.dll")]
- static extern bool GetMonitorInfo(IntPtr hMonitor, ref MONITORINFO lpmi);
- void Start()
- {
- // Start enumerating monitors
- EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, MonitorEnum, IntPtr.Zero);
- }
- // Callback function called for each monitor
- bool MonitorEnum(IntPtr hMonitor, IntPtr hdcMonitor, ref RECT lprcMonitor, IntPtr dwData)
- {
- MONITORINFO mi = new MONITORINFO();
- mi.cbSize = (uint)Marshal.SizeOf(typeof(MONITORINFO));
- if (GetMonitorInfo(hMonitor, ref mi))
- {
- // Calculate monitor dimensions
- int width = mi.rcMonitor.right - mi.rcMonitor.left;
- int height = mi.rcMonitor.bottom - mi.rcMonitor.top;
- // Output monitor information
- Debug.Log("Monitor Handle: " + hMonitor);
- Debug.Log("Position: (" + mi.rcMonitor.left + ", " + mi.rcMonitor.top + ")");
- Debug.Log("Resolution: " + width + "x" + height);
- }
- return true; // Continue enumeration
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment