Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

C# Trying to get for each Display.displays[] the FriendlyName and VendorsName

Discussion in 'Scripting' started by BrotenStudios, Apr 17, 2016.

  1. BrotenStudios

    BrotenStudios

    Joined:
    Feb 13, 2014
    Posts:
    155
    I am trying to collect information on each Display.displays[].
    Current the code gets for each Display.displays[]
    • public string Availability { get; set; }
    • public int ScreenHeight { get; set; }
    • public int ScreenWidth { get; set; }
    • public Rect MonitorArea { get; set; }
    • public int MonitorTop { get; set; }
    • public int MonitorLeft { get; set; }
    • public string DeviceName { get; set; }
    But I need to also get for each Display.displays[] connected to the system:
    • public string FriendlyName { get; set; }
    • public string VendorsName { get; set; }

    Code (CSharp):
    1. using UnityEngine;
    2. using System;
    3. using System.Collections.Generic;
    4. using System.Runtime.InteropServices;
    5.  
    6. namespace BrotenTech
    7. {
    8.     /// <summary>
    9.     /// Find out about which monitors are made available by the system.
    10.     /// </summary>
    11.     public abstract class WindowsMultiDisplayTools
    12.     {
    13.         #region Multi-Display Detection
    14.         private delegate bool MonitorEnumDelegate(IntPtr hMonitor, IntPtr hdcMonitor, ref Rect lprcMonitor, IntPtr dwData);
    15.  
    16.         [DllImport("user32.dll")]
    17.         private static extern bool EnumDisplayMonitors(IntPtr hdc, IntPtr lprcClip, MonitorEnumDelegate lpfnEnum, IntPtr dwData);
    18.  
    19.         [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    20.         private struct MonitorInfo
    21.         {
    22.             public uint Size;
    23.             public RectNative Monitor;
    24.             public RectNative WorkArea;
    25.             public uint Flags;
    26.             [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
    27.             public string DeviceName;
    28.         }
    29.  
    30.         [StructLayout(LayoutKind.Sequential)]
    31.         private struct RectNative
    32.         {
    33.             public int Left;
    34.             public int Top;
    35.             public int Right;
    36.             public int Bottom;
    37.         }
    38.  
    39.         [DllImport("user32.dll", CharSet = CharSet.Auto)]
    40.         private static extern bool GetMonitorInfo(IntPtr hmon, ref MonitorInfo monitorinfo);
    41.         #endregion
    42.  
    43.         /// <summary>
    44.         /// The struct that contains the display information
    45.         /// </summary>
    46.         public class DisplayInfo
    47.         {
    48.             public string Availability { get; set; }
    49.             public int ScreenHeight { get; set; }
    50.             public int ScreenWidth { get; set; }
    51.             public Rect MonitorArea { get; set; }
    52.             public int MonitorTop { get; set; }
    53.             public int MonitorLeft { get; set; }
    54.             public string DeviceName { get; set; }
    55.             public string FriendlyName { get; set; }
    56.             public string VendorsName { get; set; }
    57.         }
    58.  
    59.         /// <summary>
    60.         /// Returns the number of Displays using the Win32 functions.
    61.         /// </summary>
    62.         /// <returns>A collection of DisplayInfo with information about each monitor.</returns>
    63.         public static List<DisplayInfo> QueryDisplays()
    64.         {
    65.             var Monitors = new List<DisplayInfo>();
    66.  
    67.             // Get the all Display Monitors.
    68.             EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero,
    69.                 delegate (IntPtr hMonitor, IntPtr hdcMonitor, ref Rect lprcMonitor, IntPtr dwData)
    70.                 {
    71.                     MonitorInfo monitor = new MonitorInfo();
    72.                     monitor.Size = (uint)Marshal.SizeOf(monitor);
    73.                     monitor.DeviceName = null;
    74.                     bool Success = GetMonitorInfo(hMonitor, ref monitor);
    75.                     if (Success)
    76.                     {
    77.                         DisplayInfo displayinfo = new DisplayInfo();
    78.                         displayinfo.ScreenWidth = monitor.Monitor.Right - monitor.Monitor.Left;
    79.                         displayinfo.ScreenHeight = monitor.Monitor.Bottom - monitor.Monitor.Top;
    80.                         displayinfo.MonitorArea = new Rect(monitor.Monitor.Left, monitor.Monitor.Top, displayinfo.ScreenWidth, displayinfo.ScreenHeight);
    81.                         displayinfo.MonitorTop = monitor.Monitor.Top;
    82.                         displayinfo.MonitorLeft = monitor.Monitor.Left;
    83.                         displayinfo.Availability = monitor.Flags.ToString();
    84.                         displayinfo.DeviceName = monitor.DeviceName;
    85.                         displayinfo.FriendlyName = QueryDisplaysFriendlyName(monitor.DeviceName);
    86.                         displayinfo.VendorsName = QueryDisplaysVendorName(monitor.DeviceName);
    87.                         Monitors.Add(displayinfo);
    88.                     }
    89.                     return true;
    90.                 }, IntPtr.Zero);
    91.             return Monitors;
    92.         }
    93.         /// <summary>
    94.         /// Returns the Friendly Name of a target Display using the Win32 functions.
    95.         /// </summary>
    96.         /// <returns>A string of with FriendlyName from DeviceName.</returns>
    97.         private static string QueryDisplaysFriendlyName(string DeviceName)
    98.         {
    99.             string FriendlyName = null;
    100.            
    101.             // Get Friendly Name for the Device code goes here.
    102.  
    103.  
    104.             return FriendlyName;
    105.         }
    106.         /// <summary>
    107.         /// Returns the Vendors Name of a target Display using the Win32 functions.
    108.         /// </summary>
    109.         /// <returns>A string of with VendorName from DeviceName.</returns>
    110.         private static string QueryDisplaysVendorName(string DeviceName)
    111.         {
    112.             string VendorName = null;
    113.  
    114.             // Get Vendors Name for the Device code goes here.
    115.  
    116.  
    117.             return VendorName;
    118.         }
    119.     }
    120. }
     
    fffMalzbier likes this.
  2. GiuPor

    GiuPor

    Joined:
    May 15, 2013
    Posts:
    7
    Please give me an example!
    How can i get DeviceName of each monitor?
    Thanks
     
  3. waltran

    waltran

    Joined:
    Feb 23, 2017
    Posts:
    36