From c126b831d01f781d7ffd7f216c0314ba91fcdf1c Mon Sep 17 00:00:00 2001 From: szpeter Date: Tue, 22 Feb 2022 00:04:56 +0800 Subject: [PATCH 1/3] Fix incorrect screen resolution when dpi is set --- winfetch.ps1 | 86 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 82 insertions(+), 4 deletions(-) diff --git a/winfetch.ps1 b/winfetch.ps1 index 0ec2663..30a51c2 100644 --- a/winfetch.ps1 +++ b/winfetch.ps1 @@ -572,14 +572,92 @@ function info_uptime { # ===== RESOLUTION ===== function info_resolution { - Add-Type -AssemblyName System.Windows.Forms - $displays = foreach ($monitor in [System.Windows.Forms.Screen]::AllScreens) { - "$($monitor.Bounds.Size.Width)x$($monitor.Bounds.Size.Height)" + Add-Type -AssemblyName System.Core + Add-Type -AssemblyName System.Runtime.InteropServices + $id = Get-Random + $code = @" +using System.Runtime.InteropServices; +using System.Collections.Generic; +using System; +using System.Linq; +public class Pinvoke$id +{ + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] + struct MONITORINFOEX + { + public int Size; + public Rect Monitor; + public Rect WorkArea; + public uint Flags; + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] + public string DeviceName; + } + + [DllImport("Shcore.dll")] + static public extern bool SetProcessDpiAwareness(int value); + + [DllImport("user32.dll")] + static extern bool EnumDisplayMonitors( + IntPtr hdc, + IntPtr lprcClip, + EnumMonitorsDelegate lpfnEnum, + IntPtr dwData + ); + + [DllImport("user32.dll", CharSet = CharSet.Auto)] + static extern bool GetMonitorInfo(IntPtr hMonitor, ref MONITORINFOEX lpmi); + + [StructLayout(LayoutKind.Sequential)] + public struct Rect + { + public int left; + public int top; + public int right; + public int bottom; + } + + delegate bool EnumMonitorsDelegate(IntPtr hMonitor, IntPtr hdcMonitor, ref Rect lprcMonitor, IntPtr dwData); + + static private List m_displays = new List(); + + static bool EnumProc(IntPtr hMonitor, IntPtr hdcMonitor, ref Rect lprcMonitor, IntPtr dwData) + { + MONITORINFOEX mONITORINFOEX = new MONITORINFOEX(); + mONITORINFOEX.Size = Marshal.SizeOf(typeof(MONITORINFOEX)); + GetMonitorInfo(hMonitor, ref mONITORINFOEX); + + var displayInfo = new DisplayInfo(); + displayInfo.X = (int)((mONITORINFOEX.Monitor.right - mONITORINFOEX.Monitor.left)); + displayInfo.Y = (int)((mONITORINFOEX.Monitor.bottom - mONITORINFOEX.Monitor.top)); + + m_displays.Add(displayInfo); + return true; + } + + public class DisplayInfo + { + public int X; + public int Y; } + public static string GetResult() + { + SetProcessDpiAwareness(2); + EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, EnumProc, IntPtr.Zero); + return String.Join(", ", m_displays.Select(displayInfo => displayInfo.X.ToString() + "x" + displayInfo.Y.ToString())); + } + + public static void Main() + { + GetResult(); + } +} +"@ + Add-Type -TypeDefinition $code + $result = iex "[Pinvoke$id]::GetResult()" return @{ title = "Resolution" - content = $displays -join ', ' + content = $result } } From a9e8eef9d6645b1ddc3a1c065b48a56b5481b579 Mon Sep 17 00:00:00 2001 From: szpeter Date: Thu, 24 Feb 2022 22:51:06 +0800 Subject: [PATCH 2/3] Refactor monitor methods --- winfetch.ps1 | 131 ++++++++++++++++++++++++--------------------------- 1 file changed, 62 insertions(+), 69 deletions(-) diff --git a/winfetch.ps1 b/winfetch.ps1 index 30a51c2..e2749ad 100644 --- a/winfetch.ps1 +++ b/winfetch.ps1 @@ -572,92 +572,85 @@ function info_uptime { # ===== RESOLUTION ===== function info_resolution { - Add-Type -AssemblyName System.Core - Add-Type -AssemblyName System.Runtime.InteropServices - $id = Get-Random - $code = @" + Add-Type @" using System.Runtime.InteropServices; using System.Collections.Generic; using System; using System.Linq; -public class Pinvoke$id +namespace WinAPI { - [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] - struct MONITORINFOEX - { - public int Size; - public Rect Monitor; - public Rect WorkArea; - public uint Flags; - [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] - public string DeviceName; - } - - [DllImport("Shcore.dll")] - static public extern bool SetProcessDpiAwareness(int value); - - [DllImport("user32.dll")] - static extern bool EnumDisplayMonitors( - IntPtr hdc, - IntPtr lprcClip, - EnumMonitorsDelegate lpfnEnum, - IntPtr dwData - ); - - [DllImport("user32.dll", CharSet = CharSet.Auto)] - static extern bool GetMonitorInfo(IntPtr hMonitor, ref MONITORINFOEX lpmi); - - [StructLayout(LayoutKind.Sequential)] - public struct Rect + public class MonitorMethods { - public int left; - public int top; - public int right; - public int bottom; - } - - delegate bool EnumMonitorsDelegate(IntPtr hMonitor, IntPtr hdcMonitor, ref Rect lprcMonitor, IntPtr dwData); + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] + struct MONITORINFOEX + { + public int Size; + public Rect Monitor; + public Rect WorkArea; + public uint Flags; + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] + public string DeviceName; + } + + [DllImport("Shcore.dll")] + static public extern bool SetProcessDpiAwareness(int value); + + [DllImport("user32.dll")] + static extern bool EnumDisplayMonitors( + IntPtr hdc, + IntPtr lprcClip, + EnumMonitorsDelegate lpfnEnum, + IntPtr dwData + ); + + [DllImport("user32.dll", CharSet = CharSet.Auto)] + static extern bool GetMonitorInfo(IntPtr hMonitor, ref MONITORINFOEX lpmi); + + [StructLayout(LayoutKind.Sequential)] + public struct Rect + { + public int left; + public int top; + public int right; + public int bottom; + } - static private List m_displays = new List(); - - static bool EnumProc(IntPtr hMonitor, IntPtr hdcMonitor, ref Rect lprcMonitor, IntPtr dwData) - { - MONITORINFOEX mONITORINFOEX = new MONITORINFOEX(); - mONITORINFOEX.Size = Marshal.SizeOf(typeof(MONITORINFOEX)); - GetMonitorInfo(hMonitor, ref mONITORINFOEX); + delegate bool EnumMonitorsDelegate(IntPtr hMonitor, IntPtr hdcMonitor, ref Rect lprcMonitor, IntPtr dwData); - var displayInfo = new DisplayInfo(); - displayInfo.X = (int)((mONITORINFOEX.Monitor.right - mONITORINFOEX.Monitor.left)); - displayInfo.Y = (int)((mONITORINFOEX.Monitor.bottom - mONITORINFOEX.Monitor.top)); + static private List m_displays = new List(); + + static bool EnumProc(IntPtr hMonitor, IntPtr hdcMonitor, ref Rect lprcMonitor, IntPtr dwData) + { + MONITORINFOEX mONITORINFOEX = new MONITORINFOEX(); + mONITORINFOEX.Size = Marshal.SizeOf(typeof(MONITORINFOEX)); + GetMonitorInfo(hMonitor, ref mONITORINFOEX); - m_displays.Add(displayInfo); - return true; - } + var displayInfo = new DisplayInfo(); + displayInfo.X = (int)((mONITORINFOEX.Monitor.right - mONITORINFOEX.Monitor.left)); + displayInfo.Y = (int)((mONITORINFOEX.Monitor.bottom - mONITORINFOEX.Monitor.top)); - public class DisplayInfo - { - public int X; - public int Y; - } + m_displays.Add(displayInfo); + return true; + } - public static string GetResult() - { - SetProcessDpiAwareness(2); - EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, EnumProc, IntPtr.Zero); - return String.Join(", ", m_displays.Select(displayInfo => displayInfo.X.ToString() + "x" + displayInfo.Y.ToString())); - } + public class DisplayInfo + { + public int X; + public int Y; + } - public static void Main() - { - GetResult(); + public static string GetResolution() + { + SetProcessDpiAwareness(2); + EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, EnumProc, IntPtr.Zero); + return String.Join(", ", m_displays.Select(displayInfo => displayInfo.X.ToString() + "x" + displayInfo.Y.ToString())); + } } } "@ - Add-Type -TypeDefinition $code - $result = iex "[Pinvoke$id]::GetResult()" return @{ title = "Resolution" - content = $result + content = [WinAPI.MonitorMethods]::GetResolution() } } From e2b4cb8ff28eb0ca502e09acfcf12dda7c19c7da Mon Sep 17 00:00:00 2001 From: szpeter Date: Tue, 12 Apr 2022 23:54:08 +0800 Subject: [PATCH 3/3] Fix duplicated monitor resolution after first invoke --- winfetch.ps1 | 1 + 1 file changed, 1 insertion(+) diff --git a/winfetch.ps1 b/winfetch.ps1 index e2749ad..98427e3 100644 --- a/winfetch.ps1 +++ b/winfetch.ps1 @@ -642,6 +642,7 @@ namespace WinAPI public static string GetResolution() { SetProcessDpiAwareness(2); + m_displays = new List(); EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, EnumProc, IntPtr.Zero); return String.Join(", ", m_displays.Select(displayInfo => displayInfo.X.ToString() + "x" + displayInfo.Y.ToString())); }