Guest User

chatgpt unity monitor management

a guest
Nov 19th, 2024
43
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.97 KB | None | 1 0
  1. using UnityEngine;
  2. using System;
  3. using System.Runtime.InteropServices;
  4.  
  5. public class MonitorInfoExample : MonoBehaviour
  6. {
  7.     // Define the RECT structure
  8.     [StructLayout(LayoutKind.Sequential)]
  9.     public struct RECT
  10.     {
  11.         public int left, top, right, bottom;
  12.     }
  13.  
  14.     // Define the MONITORINFO structure
  15.     [StructLayout(LayoutKind.Sequential)]
  16.     public struct MONITORINFO
  17.     {
  18.         public uint cbSize;
  19.         public RECT rcMonitor;
  20.         public RECT rcWork;
  21.         public uint dwFlags;
  22.     }
  23.  
  24.     // Delegate for the monitor enumeration callback function
  25.     public delegate bool MonitorEnumProc(IntPtr hMonitor, IntPtr hdcMonitor, ref RECT lprcMonitor, IntPtr dwData);
  26.  
  27.     // Import necessary functions from user32.dll
  28.     [DllImport("user32.dll")]
  29.     static extern bool EnumDisplayMonitors(IntPtr hdc, IntPtr lprcClip,
  30.         MonitorEnumProc lpfnEnum, IntPtr dwData);
  31.  
  32.     [DllImport("user32.dll")]
  33.     static extern bool GetMonitorInfo(IntPtr hMonitor, ref MONITORINFO lpmi);
  34.  
  35.     void Start()
  36.     {
  37.         // Start enumerating monitors
  38.         EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, MonitorEnum, IntPtr.Zero);
  39.     }
  40.  
  41.     // Callback function called for each monitor
  42.     bool MonitorEnum(IntPtr hMonitor, IntPtr hdcMonitor, ref RECT lprcMonitor, IntPtr dwData)
  43.     {
  44.         MONITORINFO mi = new MONITORINFO();
  45.         mi.cbSize = (uint)Marshal.SizeOf(typeof(MONITORINFO));
  46.  
  47.         if (GetMonitorInfo(hMonitor, ref mi))
  48.         {
  49.             // Calculate monitor dimensions
  50.             int width = mi.rcMonitor.right - mi.rcMonitor.left;
  51.             int height = mi.rcMonitor.bottom - mi.rcMonitor.top;
  52.  
  53.             // Output monitor information
  54.             Debug.Log("Monitor Handle: " + hMonitor);
  55.             Debug.Log("Position: (" + mi.rcMonitor.left + ", " + mi.rcMonitor.top + ")");
  56.             Debug.Log("Resolution: " + width + "x" + height);
  57.         }
  58.  
  59.         return true; // Continue enumeration
  60.     }
  61. }
  62.  
Advertisement
Add Comment
Please, Sign In to add comment