Skip to content

Commit 7533641

Browse files
committed
Actually Fix this Time
- Added two methods of getting screen resolution - First one runs if running as admin, and gets the screen information from HKLM:\SYSTEM\CurrentControlSet\Control\GraphicsDrivers\Configuration\ - Location contains refresh rate, so I added that in too - Second method uses implementation in lptstr#131 which is about 4x slower for me, but seems to be the only way to get it working that I can find
1 parent 111cd1c commit 7533641

File tree

1 file changed

+86
-4
lines changed

1 file changed

+86
-4
lines changed

winfetch.ps1

Lines changed: 86 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -617,11 +617,93 @@ function info_uptime {
617617
# ===== RESOLUTION =====
618618
function info_resolution {
619619
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+
}
622631

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()
625707
}
626708

627709
return @{

0 commit comments

Comments
 (0)