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")]
|
2020-03-24 00:32:47 +13:00
|
|
|
|
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
|
|
|
|
|
}
|
2020-03-24 00:32:47 +13:00
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
2020-03-24 00:38:23 +13:00
|
|
|
|
Factor = PhysicalScreenHeight / (float)LogicalScreenHeight; // 1.25 = 125%
|
2020-03-17 02:45:19 +13:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[DllImport("user32.dll")]
|
2020-03-24 00:32:47 +13:00
|
|
|
|
private static extern bool SetProcessDPIAware();
|
|
|
|
|
|
|
|
|
|
private static void SetProcessDPIAwareWhenNecessary()
|
2020-03-17 02:45:19 +13:00
|
|
|
|
{
|
|
|
|
|
if (Environment.OSVersion.Version.Major >= 6)
|
|
|
|
|
{
|
|
|
|
|
SetProcessDPIAware();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|