-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathdpi.c
190 lines (171 loc) · 3.49 KB
/
dpi.c
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
* CLCL
*
* dpi.c
*
* Copyright (C) 1996-2019 by Ohno Tomoaki. All rights reserved.
* https://www.nakka.com/
*/
/* Include Files */
#define _INC_OLE
#include <windows.h>
#undef _INC_OLE
#include "dpi.h"
/* Define */
#ifndef MONITOR_DEFAULTTONEAREST
#define MONITOR_DEFAULTTONEAREST 0x00000002
#endif
/* Global Variables */
static HMODULE hModThemes;
static UINT m_nScaleFactor = 0;
static UINT m_nScaleFactorSDA = 0;
static PROCESS_DPI_AWARENESS m_Awareness = PROCESS_DPI_UNAWARE;
/* Local Function Prototypes */
static FARPROC _GetProcessDpiAwareness;
static FARPROC _SetProcessDpiAwareness;
static FARPROC _GetDpiForMonitor;
/*
* Scale - スケール変換した値の取得
*/
int Scale(int x)
{
if (m_Awareness == PROCESS_DPI_UNAWARE)
{
return x;
}
if (m_Awareness == PROCESS_SYSTEM_DPI_AWARE)
{
return MulDiv(x, m_nScaleFactorSDA, 100);
}
return MulDiv(x, m_nScaleFactor, 100);
}
/*
* UnScale - スケールを戻した値の取得
*/
int UnScale(int x)
{
if (m_Awareness == PROCESS_DPI_UNAWARE)
{
return x;
}
if (m_Awareness == PROCESS_SYSTEM_DPI_AWARE)
{
return MulDiv(x, 100, m_nScaleFactorSDA);
}
return MulDiv(x, 100, m_nScaleFactor);
}
/*
* GetScale - スケールの取得
*/
UINT GetScale()
{
if (m_Awareness == PROCESS_DPI_UNAWARE)
{
return 100;
}
if (m_Awareness == PROCESS_SYSTEM_DPI_AWARE)
{
return m_nScaleFactorSDA;
}
return m_nScaleFactor;
}
/*
* SetScale - スケールの設定
*/
void SetScale(UINT iDPI)
{
m_nScaleFactor = MulDiv(iDPI, 100, 96);
if (m_nScaleFactorSDA == 0)
{
m_nScaleFactorSDA = m_nScaleFactor;
}
}
/*
* GetAwareness - Awarenessの取得
*/
PROCESS_DPI_AWARENESS GetAwareness()
{
if (_GetProcessDpiAwareness == NULL) {
return m_Awareness;
}
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId());
_GetProcessDpiAwareness(hProcess, &m_Awareness);
return m_Awareness;
}
/*
* SetAwareness - Awarenessの設定
*/
void SetAwareness(PROCESS_DPI_AWARENESS awareness)
{
if (_SetProcessDpiAwareness == NULL) {
return;
}
HRESULT hr = _SetProcessDpiAwareness(awareness);
if (hr == S_OK)
{
m_Awareness = awareness;
}
}
/*
* ScaleRect - RECTのスケール変換
*/
void ScaleRect(RECT* pRect)
{
pRect->left = Scale(pRect->left);
pRect->right = Scale(pRect->right);
pRect->top = Scale(pRect->top);
pRect->bottom = Scale(pRect->bottom);
}
/*
* UnScaleRect - RECTのスケール変換
*/
void UnScaleRect(RECT* pRect)
{
pRect->left = UnScale(pRect->left);
pRect->right = UnScale(pRect->right);
pRect->top = UnScale(pRect->top);
pRect->bottom = UnScale(pRect->bottom);
}
/*
* ScalePoint - POINTのスケール変換
*/
void ScalePoint(POINT* pPoint)
{
pPoint->x = Scale(pPoint->x);
pPoint->y = Scale(pPoint->y);
}
/*
* UnScalePoint - POINTのスケール変換
*/
void UnScalePoint(POINT* pPoint)
{
pPoint->x = UnScale(pPoint->x);
pPoint->y = UnScale(pPoint->y);
}
/*
* InitDpi - DPIの初期化
*/
void InitDpi()
{
if ((hModThemes = LoadLibrary(TEXT("shcore.dll"))) == NULL) {
return;
}
if ((_GetDpiForMonitor = GetProcAddress(hModThemes, "GetDpiForMonitor")) == NULL) {
return;
}
if ((_GetProcessDpiAwareness = GetProcAddress(hModThemes, "GetProcessDpiAwareness")) == NULL) {
return;
}
if ((_SetProcessDpiAwareness = GetProcAddress(hModThemes, "SetProcessDpiAwareness")) == NULL) {
return;
}
// DPIの初期化
GetAwareness();
UINT dpix = 0, dpiy = 0;
POINT pt = { 1, 1 };
HMONITOR hMonitor = MonitorFromPoint(pt, MONITOR_DEFAULTTONEAREST);
HRESULT hr = _GetDpiForMonitor(hMonitor, 0, &dpix, &dpiy);
SetScale(dpix);
}
/* End of source */