-
Notifications
You must be signed in to change notification settings - Fork 0
/
GDIHelper.cpp
172 lines (166 loc) · 3.27 KB
/
GDIHelper.cpp
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
#include "GDIHelper.h"
HDCWrapper HDCWrapper::FromHDC(HDC hdc)
{
HDCWrapper wrapper(HDCSourceExternal);
wrapper.hdc = hdc;
return wrapper;
}
HDCWrapper HDCWrapper::BeginPaint(HWND hWnd)
{
HDCWrapper wrapper(HDCSourceBeginPaint);
wrapper.hWnd = hWnd;
wrapper.hdc = ::BeginPaint(hWnd, &wrapper.ps);
return wrapper;
}
HDCWrapper HDCWrapper::GetDC(HWND hWnd)
{
HDCWrapper wrapper(HDCSourceGetDC);
wrapper.hWnd = hWnd;
wrapper.hdc = ::GetDC(hWnd);
return wrapper;
}
HDCWrapper HDCWrapper::CreateCompatibleDC(HDC hdc)
{
HDCWrapper wrapper(HDCSourceCreateCompatibleDC);
wrapper.hdc = ::CreateCompatibleDC(hdc);
return wrapper;
}
HDCWrapper::HDCWrapper(HDCWrapper &&other) : hdc(other.hdc), source(other.source), objects(std::move(other.objects)), hWnd(other.hWnd), ps(other.ps)
{
other.hdc = 0;
}
HDCWrapper::HDCWrapper(HDCSource source) : hdc(0), source(source)
{
}
HDCWrapper::~HDCWrapper()
{
for (auto iter : objects)
{
if (iter.second.second)
iter.second.second->UnregisterFromParent();
}
if (hdc)
{
switch (source)
{
case HDCSourceBeginPaint:
EndPaint(hWnd, &ps);
break;
case HDCSourceGetDC:
ReleaseDC(hWnd, hdc);
break;
case HDCSourceCreateCompatibleDC:
DeleteDC(hdc);
break;
}
}
}
HDCWrapper::operator HDC() const
{
return hdc;
}
void HDCWrapper::Select(GDIObject &obj)
{
obj.UnregisterFromParent();
DWORD type = GetObjectType(obj.Get());
auto &res = objects[type];
if (!res.first)
{
res.first = SelectObject(hdc, obj.Get());
}
else
{
SelectObject(hdc, obj.Get());
}
res.second = &obj;
obj.SetParent(this);
}
void HDCWrapper::Unselect(GDIObject &obj)
{
DWORD type = GetObjectType(obj.Get());
auto &res = objects[type];
if (res.second == &obj)
{
if (res.first)
{
SelectObject(hdc, res.first);
}
res.second = 0;
}
}
void HDCWrapper::UnselectType(GDIObject &obj)
{
DWORD type = GetObjectType(obj.Get());
if (!type)
return;
auto &res = objects[type];
if (res.first)
{
SelectObject(hdc, res.first);
}
res.second = 0;
}
GDIObject::GDIObject(HGDIOBJ obj) : obj(obj), parent(0)
{
}
HGDIOBJ GDIObject::operator = (HGDIOBJ other)
{
UnregisterFromParent();
if (obj)
DeleteObject(obj);
obj = other;
return obj;
}
GDIObject::operator bool() const
{
return obj != 0;
}
GDIObject::~GDIObject()
{
UnregisterFromParent();
if (obj)
DeleteObject(obj);
}
void GDIObject::UnregisterFromParent()
{
if (parent)
{
parent->Unselect(*this);
parent = 0;
}
}
void GDIObject::SetParent(HDCWrapper *parent)
{
this->parent = parent;
}
HGDIOBJ GDIObject::Get() const
{
return obj;
}
template<class T>
T GetGDIObjectAsType(HGDIOBJ obj, DWORD target_type)
{
if (obj && GetObjectType(obj) == target_type)
return static_cast<T>(obj);
return 0;
}
HCURSOR GDIObject::GetCursor() const
{
return GetGDIObjectAsType<HCURSOR>(obj, 0);
}
HBITMAP GDIObject::GetBitmap() const
{
return GetGDIObjectAsType<HBITMAP>(obj, OBJ_BITMAP);
}
HPEN GDIObject::GetPen() const
{
return GetGDIObjectAsType<HPEN>(obj, OBJ_PEN);
}
HBRUSH GDIObject::GetBrush() const
{
return GetGDIObjectAsType<HBRUSH>(obj, OBJ_BRUSH);
}
HFONT GDIObject::GetFont() const
{
return GetGDIObjectAsType<HFONT>(obj, OBJ_FONT);
}