-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDPIScale.h
More file actions
120 lines (100 loc) · 2.72 KB
/
Copy pathDPIScale.h
File metadata and controls
120 lines (100 loc) · 2.72 KB
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
#pragma once
#include "pch.h"
class DPIScale
{
static float scaleX;
static float scaleY;
static float dpiX;
static float dpiY;
// Half / full pixel in DIPs
static float halfPX;
static float halfPY;
static float fullPX;
static float fullPY;
public:
static inline void Initialize(HWND hwnd)
{
dpiX = static_cast<float>(GetDpiForWindow(hwnd));
//dpiX = 96;
dpiY = dpiX;
scaleX = dpiX / 96.0f;
scaleY = dpiY / 96.0f;
halfPX = PixelsToDipsX(0.5);
halfPY = PixelsToDipsY(0.5);
fullPX = PixelsToDipsX(1);
fullPY = PixelsToDipsY(1);
}
static inline float DPIX() { return dpiX; }
static inline float DPIY() { return dpiY; }
static inline D2D1_POINT_2F PixelsToDips(POINT p)
{
return D2D1::Point2F(static_cast<float>(p.x) / scaleX, static_cast<float>(p.y) / scaleY);
}
static inline D2D1_RECT_F PixelsToDips(RECT rc)
{
return D2D1::RectF(
PixelsToDipsX(rc.left),
PixelsToDipsY(rc.top),
PixelsToDipsX(rc.right),
PixelsToDipsY(rc.bottom)
);
}
template <typename T>
static inline float PixelsToDipsX(T x)
{
return static_cast<float>(x) / scaleX;
}
template <typename T>
static inline float PixelsToDipsY(T y)
{
return static_cast<float>(y) / scaleY;
}
static inline POINT DipsToPixels(D2D1_POINT_2F point)
{
POINT out;
out.x = DipsToPixelsX(point.x);
out.y = DipsToPixelsY(point.y);
return out;
}
static inline RECT DipsToPixels(D2D1_RECT_F rect)
{
RECT out;
out.left = DipsToPixelsX(rect.left);
out.top = DipsToPixelsY(rect.top);
out.right = DipsToPixelsX(rect.right);
out.bottom = DipsToPixelsY(rect.bottom);
return out;
}
template <typename T>
static inline int DipsToPixelsX(T x)
{
return static_cast<int>(static_cast<float>(x) * scaleX);
}
template <typename T>
static inline int DipsToPixelsY(T y)
{
return static_cast<int>(static_cast<float>(y) * scaleY);
}
static inline float SnapToPixelX(float x)
{
return roundf(x * scaleX) / scaleX;
}
static inline float SnapToPixelY(float y)
{
return roundf(y * scaleY) / scaleY;
}
static inline D2D1_RECT_F SnapToPixel(D2D1_RECT_F rect, bool inwards = true)
{
rect.left = SnapToPixelX(rect.left) - (1.0f - 2.0f * inwards) * halfPX;
rect.top = SnapToPixelY(rect.top) - (1.0f - 2.0f * inwards) * halfPY;
rect.right = SnapToPixelX(rect.right) + (1.0f - 2.0f * inwards) * halfPX;
rect.bottom = SnapToPixelY(rect.bottom) + (1.0f - 2.0f * inwards) * halfPY;
return rect;
}
// NOTE: when drawing lines, need to align to 0.5 boundaries. Use hp() below.
// Alternatively, use D2D1_STROKE_TRANSFORM_TYPE_HAIRLINE in future
static inline float hpx() { return halfPX; }
static inline float hpy() { return halfPY; }
static inline float px() { return fullPX; }
static inline float py() { return fullPY; }
};