Skip to content

Commit

Permalink
Support Win7
Browse files Browse the repository at this point in the history
  • Loading branch information
1357310795 committed Feb 21, 2023
1 parent 3072274 commit f14ffe2
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion MyQrCodeScanner/Modules/ScreenShot/Helpers/MonitorHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ namespace HandyScreenshot.Helpers
{
public static class MonitorHelper
{
[DllImport("user32.dll")]
static extern IntPtr GetDC(IntPtr ptr);

private const double DefaultDpi = 96.0;
private const uint MonitorDefaultToNull = 0;

Expand Down Expand Up @@ -37,7 +40,20 @@ public static IReadOnlyCollection<MonitorInfo> GetMonitorInfos()

public static (double scaleX, double scaleY) GetScaleFactorFromMonitor(IntPtr hMonitor)
{
if (!DpiApiLevel3) throw new NotSupportedException();
if (!DpiApiLevel3)
{
var windowDc = GetDC(IntPtr.Zero);
if (windowDc == IntPtr.Zero)
throw new Win32Exception("Getting window device context failed");

var dpiX1 = GetDeviceCaps(windowDc, DeviceCap.Logpixelsx);
var dpiY1 = GetDeviceCaps(windowDc, DeviceCap.Logpixelsy);

if (ReleaseDC(IntPtr.Zero, windowDc) == 0)
throw new Win32Exception("Releasing window device context failed");

return (DefaultDpi / dpiX1, DefaultDpi / dpiY1);
}

var ret = GetDpiForMonitor(hMonitor, MonitorDpiType.MdtEffectiveDpi, out var dpiX, out var dpiY);
if (ret != 0)
Expand Down

0 comments on commit f14ffe2

Please sign in to comment.