-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathDpiUtil.cs
92 lines (77 loc) · 2.49 KB
/
DpiUtil.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System;
using System.Runtime.InteropServices;
/*
This class taken directly from keepass slimmed downed and slightly modified:
https://github.com/dlech/KeePass2.x/blob/9b57541e6fcb49cb5f12029fb8e553295cf153c4/KeePass/UI/DpiUtil.cs
KeePass Password Safe - The Open-Source Password Manager
Copyright (C) 2003-2019 Dominik Reichl <[email protected]>
*/
namespace FSClient {
public static class DpiUtil {
internal const int LOGPIXELSX = 88;
internal const int LOGPIXELSY = 90;
[DllImport("User32.dll")]
internal static extern IntPtr GetDC(IntPtr hWnd);
[DllImport("Gdi32.dll")]
internal static extern int GetDeviceCaps(IntPtr hdc, int nIndex);
[DllImport("User32.dll")]
internal static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
public static int ScaleIntX(double i) {
EnsureInitialized();
return (int)Math.Round((double)i * m_dScaleX);
}
public static int ScaleIntY(double i) {
EnsureInitialized();
return (int)Math.Round((double)i * m_dScaleY);
}
public static int DeScaleIntX(double i) {
EnsureInitialized();
return (int)Math.Round((double)i / m_dScaleX);
}
public static int DeScaleIntY(double i) {
EnsureInitialized();
return (int)Math.Round((double)i / m_dScaleY);
}
private static void EnsureInitialized() {
if (m_bInitialized) return;
try {
IntPtr hDC = GetDC(IntPtr.Zero);
if (hDC != IntPtr.Zero) {
m_nDpiX = GetDeviceCaps(hDC,
LOGPIXELSX);
m_nDpiY = GetDeviceCaps(hDC,
LOGPIXELSY);
if ((m_nDpiX <= 0) || (m_nDpiY <= 0)) {
System.Diagnostics.Debug.Assert(false);
m_nDpiX = StdDpi;
m_nDpiY = StdDpi;
}
if (ReleaseDC(IntPtr.Zero, hDC) != 1) {
System.Diagnostics.Debug.Assert(false);
}
} else { System.Diagnostics.Debug.Assert(false); }
} catch (Exception) { System.Diagnostics.Debug.Assert(false); }
m_dScaleX = (double)m_nDpiX / (double)StdDpi;
m_dScaleY = (double)m_nDpiY / (double)StdDpi;
m_bInitialized = true;
}
private const int StdDpi = 96;
private static bool m_bInitialized = false;
private static int m_nDpiX = StdDpi;
private static int m_nDpiY = StdDpi;
private static double m_dScaleX = 1.0;
public static double FactorX {
get {
EnsureInitialized();
return m_dScaleX;
}
}
private static double m_dScaleY = 1.0;
public static double FactorY {
get {
EnsureInitialized();
return m_dScaleY;
}
}
}
}