SystemTrayMenu/Helper/Scaling.cs

51 lines
1.3 KiB
C#
Raw Normal View History

2020-03-17 02:45:19 +13:00
using System;
using System.Drawing;
using System.Runtime.InteropServices;
namespace SystemTrayMenu.Helper
{
internal static class Scaling
{
internal static float Factor = 1;
internal static void Initialize()
{
CalculateScalingFactor();
SetProcessDPIAwareWhenNecessary();
}
[DllImport("gdi32.dll")]
private static extern int GetDeviceCaps(IntPtr hdc, int nIndex);
private enum DeviceCap
2020-03-17 02:45:19 +13:00
{
VERTRES = 10,
DESKTOPVERTRES = 117,
// http://pinvoke.net/default.aspx/gdi32/GetDeviceCaps.html
}
private static void CalculateScalingFactor()
2020-03-17 02:45:19 +13:00
{
Graphics g = Graphics.FromHwnd(IntPtr.Zero);
IntPtr desktop = g.GetHdc();
int LogicalScreenHeight = GetDeviceCaps(desktop, (int)DeviceCap.VERTRES);
int PhysicalScreenHeight = GetDeviceCaps(desktop, (int)DeviceCap.DESKTOPVERTRES);
Factor = PhysicalScreenHeight / (float)LogicalScreenHeight; // 1.25 = 125%
2020-03-17 02:45:19 +13:00
}
[DllImport("user32.dll")]
private static extern bool SetProcessDPIAware();
private static void SetProcessDPIAwareWhenNecessary()
2020-03-17 02:45:19 +13:00
{
if (Environment.OSVersion.Version.Major >= 6)
{
SetProcessDPIAware();
}
}
}
}