@@ -617,11 +617,93 @@ function info_uptime {
617
617
# ===== RESOLUTION =====
618
618
function info_resolution {
619
619
Add-Type - AssemblyName System.Windows.Forms
620
- $monitors = [System.Windows.Forms.Screen ]::AllScreens
621
- $scale = (Get-CimInstance - Namespace root\wmi - ClassName WmiMonitorBasicDisplayParams - CimSession $cimSession ).DisplayTransferCharacteristic
620
+
621
+ # Checks if running as admin
622
+ if ((New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity ]::GetCurrent())).IsInRole([Security.Principal.WindowsBuiltInRole ]::Administrator)){
623
+ [System.Collections.ArrayList ]$displays = @ ()
624
+ # Gets the current screen layout from the registry
625
+ Get-ChildItem - path HKLM:\SYSTEM\CurrentControlSet\Control\GraphicsDrivers\Configuration\ | Get-ItemProperty | ForEach-Object {
626
+ if ($_.Timestamp -gt $timestamp ){
627
+ $layout = $_
628
+ $timestamp = $_.timestamp
629
+ }
630
+ }
622
631
623
- $displays = for ($i = 0 ;$i -lt $monitors.Length ;$i ++ ){
624
- " $ ( $monitors [$i ].Bounds.Size.Width * ($scale [$i ] / 96 )) x$ ( $monitors [$i ].Bounds.Size.Height * ($scale [$i ] / 96 )) "
632
+ # Gets resolution and refresh rate for each display
633
+ $displays = $layout | Get-ChildItem | Get-ChildItem | Get-ItemProperty | ForEach-Object {
634
+ " $ ( $_ ." ActiveSize.cx" ) x$ ( $_ ." ActiveSize.cy" ) @$ ( $_ ." VSyncFreq.Numerator" / $_ ." VSyncFreq.Denominator" ) Hz"
635
+ }
636
+ }else {
637
+ # Use Add-Type Method (at least 4x slower)
638
+ Add-Type @"
639
+ using System.Runtime.InteropServices;
640
+ using System.Collections.Generic;
641
+ using System;
642
+ using System.Linq;
643
+ namespace WinAPI
644
+ {
645
+ public class MonitorMethods
646
+ {
647
+ [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
648
+ struct MONITORINFOEX
649
+ {
650
+ public int Size;
651
+ public Rect Monitor;
652
+ public Rect WorkArea;
653
+ public uint Flags;
654
+ [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
655
+ public string DeviceName;
656
+ }
657
+
658
+ [DllImport("Shcore.dll")]
659
+ static public extern bool SetProcessDpiAwareness(int value);
660
+ [DllImport("user32.dll")]
661
+ static extern bool EnumDisplayMonitors(
662
+ IntPtr hdc,
663
+ IntPtr lprcClip,
664
+ EnumMonitorsDelegate lpfnEnum,
665
+ IntPtr dwData
666
+ );
667
+ [DllImport("user32.dll", CharSet = CharSet.Auto)]
668
+ static extern bool GetMonitorInfo(IntPtr hMonitor, ref MONITORINFOEX lpmi);
669
+ [StructLayout(LayoutKind.Sequential)]
670
+ public struct Rect
671
+ {
672
+ public int left;
673
+ public int top;
674
+ public int right;
675
+ public int bottom;
676
+ }
677
+ delegate bool EnumMonitorsDelegate(IntPtr hMonitor, IntPtr hdcMonitor, ref Rect lprcMonitor, IntPtr dwData);
678
+ static private List<DisplayInfo> m_displays = new List<DisplayInfo>();
679
+
680
+ static bool EnumProc(IntPtr hMonitor, IntPtr hdcMonitor, ref Rect lprcMonitor, IntPtr dwData)
681
+ {
682
+ MONITORINFOEX mONITORINFOEX = new MONITORINFOEX();
683
+ mONITORINFOEX.Size = Marshal.SizeOf(typeof(MONITORINFOEX));
684
+ GetMonitorInfo(hMonitor, ref mONITORINFOEX);
685
+ var displayInfo = new DisplayInfo();
686
+ displayInfo.X = (int)((mONITORINFOEX.Monitor.right - mONITORINFOEX.Monitor.left));
687
+ displayInfo.Y = (int)((mONITORINFOEX.Monitor.bottom - mONITORINFOEX.Monitor.top));
688
+ m_displays.Add(displayInfo);
689
+ return true;
690
+ }
691
+ public class DisplayInfo
692
+ {
693
+ public int X;
694
+ public int Y;
695
+ }
696
+ public static string GetResolution()
697
+ {
698
+ SetProcessDpiAwareness(2);
699
+ m_displays = new List<DisplayInfo>();
700
+ EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, EnumProc, IntPtr.Zero);
701
+ return String.Join(", ", m_displays.Select(displayInfo => displayInfo.X.ToString() + "x" + displayInfo.Y.ToString()));
702
+ }
703
+ }
704
+ }
705
+ "@
706
+ $displays = [WinAPI.MonitorMethods ]::GetResolution()
625
707
}
626
708
627
709
return @ {
0 commit comments