Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect screen resolution when dpi is set #131

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 82 additions & 4 deletions winfetch.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
HO-COOH marked this conversation as resolved.
Show resolved Hide resolved
$id = Get-Random
HO-COOH marked this conversation as resolved.
Show resolved Hide resolved
$code = @"
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System;
using System.Linq;
public class Pinvoke$id
HO-COOH marked this conversation as resolved.
Show resolved Hide resolved
{
[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<DisplayInfo> m_displays = new List<DisplayInfo>();

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()
HO-COOH marked this conversation as resolved.
Show resolved Hide resolved
{
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
}
}

Expand Down