-
Notifications
You must be signed in to change notification settings - Fork 81
/
Utility.cs
100 lines (77 loc) · 2.25 KB
/
Utility.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
93
94
95
96
97
98
99
100
/*
* Copyright (c) 2013-present, The Eye Tribe.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the LICENSE file in the root directory of this source tree.
*
*/
using System;
using System.Drawing;
using System.Runtime.InteropServices;
namespace TETControls
{
public class Utility
{
#region Variabels
private const float DPI_DEFAULT = 96f; // default system DIP setting
private static Utility _instance;
private Point sysDpi;
private float scaleDpi;
#endregion
#region Enums
public enum DeviceCap
{
/// <summary>
/// Logical pixels inch in X
/// </summary>
LOGPIXELSX = 88,
/// <summary>
/// Logical pixels inch in Y
/// </summary>
LOGPIXELSY = 90
}
#endregion
#region Constructor
private Utility()
{
sysDpi = GetSystemDpi();
ScaleDpi = DPI_DEFAULT / sysDpi.X;
}
#endregion
#region Get/Set
public static Utility Instance
{
get { return _instance ?? (_instance = new Utility()); }
}
public Point SysDpi
{
get { return sysDpi; }
set { sysDpi = value; }
}
public float ScaleDpi
{
get { return scaleDpi; }
set { scaleDpi = value; }
}
#endregion
#region Public methods
public static Point GetSystemDpi()
{
Point result = new Point();
IntPtr hDc = GetDC(IntPtr.Zero);
result.X = GetDeviceCaps(hDc, (int)DeviceCap.LOGPIXELSX);
result.Y = GetDeviceCaps(hDc, (int)DeviceCap.LOGPIXELSY);
ReleaseDC(IntPtr.Zero, hDc);
return result;
}
#endregion
#region Private methods (DLL Imports)
[DllImport("gdi32.dll")]
private static extern int GetDeviceCaps(IntPtr hdc, int nIndex);
[DllImport("user32.dll")]
private static extern IntPtr GetDC(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern int ReleaseDC(IntPtr hWnd, IntPtr hDc);
#endregion
}
}