-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui.cpccWinGDIHelper.h
289 lines (222 loc) · 7.02 KB
/
gui.cpccWinGDIHelper.h
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/* *****************************************
* File: gui.cpccWinGDIhelper.h
* Purpose: Portable (cross-platform), light-weight, graphic context drawing objects
* *****************************************
* Library: Cross Platform C++ Classes (cpcc)
* Copyright: 2014 StarMessage software.
* License: Free for opensource projects.
* Commercial license for closed source projects.
* Web: http://www.StarMessageSoftware.com
* Download: http://www.StarMessageSoftware.com/cpcclibrary
* https://github.com/starmessage/cpcc
* email: sales -at- starmessage.info
* *****************************************
*/
#pragma once
#include <windows.h>
//////////////////////////////////////////////
// cpccWinGDISelectedObject
// Windows GDI 32 drawing parameters
//////////////////////////////////////////////
class cpccWinGDISelectedObject
{
protected:
HDC m_dc;
HGDIOBJ m_OldObject, m_object;
int m_width=0, m_height=0;
bool m_sizeNotValid;
public:
cpccWinGDISelectedObject(const HDC hdc, const HGDIOBJ aNewObj): m_dc(hdc), m_object(aNewObj), m_sizeNotValid(true)
{
// change the drawing parameters
m_OldObject = SelectObject(m_dc, m_object);
}
~cpccWinGDISelectedObject()
{
// restore the drawing parameters
SelectObject(m_dc, m_OldObject);
DeleteObject(m_object);
}
// rename to replaceSelectedObject
// and check why it is not equivalent to delete the old cpccWinGDISelectedObject object and create a new
void deleteCurrentAndSelectAnother(const HGDIOBJ aNewObj)
{
DeleteObject( SelectObject(m_dc, aNewObj));
m_object = aNewObj;
m_sizeNotValid = true;
}
};
//////////////////////////////////////////////
// cpccWinGDIBrush
// Windows GDI 32 brush construction, destruction
//////////////////////////////////////////////
class cpccWinGDIBrush: public cpccWinGDISelectedObject
{
public:
cpccWinGDIBrush(const HDC hdc, const COLORREF c): cpccWinGDISelectedObject(hdc, (HGDIOBJ) CreateSolidBrush(c))
{ }
/// convert to HBRUSH
inline operator HBRUSH(void) const { return (HBRUSH) m_object; }
};
//////////////////////////////////////////////
// cpccWinGDIPen
// Windows GDI 32 HPEN construction, destruction
//////////////////////////////////////////////
/* simple example:
HPEN hPenOld;
// Draw a red line
HPEN hLinePen;
COLORREF qLineColor = RGB(255, 0, 0);
hLinePen = CreatePen(PS_SOLID, 7, qLineColor);
hPenOld = (HPEN)SelectObject(hdc, hLinePen);
MoveToEx(hdc, 100, 100, NULL);
LineTo(hdc, 500, 250);
SelectObject(hdc, hPenOld);
DeleteObject(hLinePen);
*/
class cpccWinGDIPen : public cpccWinGDISelectedObject
{
public:
cpccWinGDIPen(const HDC hdc, const int width, const COLORREF c) : cpccWinGDISelectedObject(hdc, CreatePen(PS_SOLID, width, c))
{ }
/// convert to HBRUSH
inline operator HPEN(void) const { return (HPEN)m_object; }
};
//////////////////////////////////////////////
// cpccWinGDIBitmap
// Windows GDI 32 HBITMAP construction, destruction
//////////////////////////////////////////////
class cpccWinGDIBitmap: public cpccWinGDISelectedObject
{
protected:
void recalculateSize(void)
{
BITMAP bm;
::GetObject((HBITMAP)m_object, sizeof(bm), &bm);
m_width = bm.bmWidth;
m_height = bm.bmHeight;
m_sizeNotValid = false;
}
public:
// constructor by a ready HBITMAP
cpccWinGDIBitmap(const HDC hdc, const HBITMAP aHBitmap):
cpccWinGDISelectedObject(hdc, (HGDIOBJ) aHBitmap)
{ }
// constructor by width and height
cpccWinGDIBitmap(const HDC hdc, const HDC hCompatDC, const int width, const int height):
cpccWinGDISelectedObject(hdc, (HGDIOBJ) CreateCompatibleBitmap(hCompatDC, width, height))
{ }
/// convert to HBITMAP
inline operator HBITMAP(void) const { return (HBITMAP) m_object; }
const int getWidth(void)
{
if (m_sizeNotValid)
recalculateSize();
return m_width;
}
const int getHeight(void)
{
if (m_sizeNotValid)
recalculateSize();
return m_height;
}
};
//////////////////////////////////////////////
// cpccWinGDIFont
// Windows GDI 32 HFONT construction, destruction
//////////////////////////////////////////////
class cpccWinGDIFont
{
private:
cpccWinGDISelectedObject *m_GDIObjectPtr;
public:
// todo: remove depedencies to eFontWeight, eFontQuality
cpccWinGDIFont(HDC ahDC, const cpcc_char *aFontName, const float *aFontSize, const eFontWeight aWeight, const eFontQuality aQuality): m_GDIObjectPtr(NULL)
{
int fWeight = FW_NORMAL;
switch (aWeight)
{
case fwThin: fWeight= FW_THIN; break;
}
int fQuality = DEFAULT_QUALITY;
switch (aQuality)
{
case fqNonAntiAliased: fQuality=NONANTIALIASED_QUALITY; break;
}
int size= (int) (aFontSize ? *aFontSize : 10.0f);
cpcc_char *fontname = aFontName ? aFontName : _T("Arial");
HFONT hFont = CreateFont(
-MulDiv(size, GetDeviceCaps(ahDC, LOGPIXELSY), 72 ),
0,0,0,
fWeight,
0,0,0, DEFAULT_CHARSET, 0,0,
fQuality,
FF_DONTCARE,
fontname);
m_GDIObjectPtr = new cpccWinGDISelectedObject(ahDC, hFont);
}
~cpccWinGDIFont()
{
if (m_GDIObjectPtr)
delete m_GDIObjectPtr;
m_GDIObjectPtr = NULL;
}
};
//////////////////////////////////////////////
// cpccWinGDIMemoryDC
//////////////////////////////////////////////
class cpccWinGDIMemoryDC
{
private:
cpccWinGDIBitmap *m_bitmapPtr;
HDC m_hDCmemoryBuffer;
int w,h;
public:
HDC dc(void) { return m_hDCmemoryBuffer; };
cpccWinGDIMemoryDC(const HDC hDC, const int aWidth, const int aHeight, const bool copyContents=false):
w(aWidth), h(aHeight),
m_hDCmemoryBuffer(CreateCompatibleDC(hDC)),
m_bitmapPtr(NULL)
{
SetBkMode(m_hDCmemoryBuffer, TRANSPARENT);
m_bitmapPtr = new cpccWinGDIBitmap(m_hDCmemoryBuffer,hDC , aWidth, aHeight);
if (copyContents)
blitFrom(hDC);
}
~cpccWinGDIMemoryDC()
{
delete m_bitmapPtr;
DeleteDC(m_hDCmemoryBuffer);
m_hDCmemoryBuffer=NULL;
}
private:
void blitFrom(const HDC sourceDC /* , const int topLeft_x, const int topLeft_y */)
{
//infoLog().addf("cpccWinGDIMemoryDC.blitFrom w:%i, h:%i", w, h);
::BitBlt(m_hDCmemoryBuffer, 0, 0, w, h, sourceDC, 0,0, /* topLeft_x, topLeft_y, */ SRCCOPY /* |CAPTUREBLT */ );
}
public:
void blitTo(const HDC destDC /* , const int topLeft_x, const int topLeft_y */ )
{
::BitBlt(destDC, /* topLeft_x, topLeft_y, */ 0, 0, w, h, m_hDCmemoryBuffer, 0, 0, SRCCOPY);
}
};
//////////////////////////////////////////////
// cpccFontKerningWin
//////////////////////////////////////////////
class cpccFontKerningWin
{
private:
int m_oldSpacing;
HDC m_dc;
public:
cpccFontKerningWin(HDC hdc, int aKerning): m_dc(hdc), m_oldSpacing(GetTextCharacterExtra(hdc))
{
SetTextCharacterExtra(hdc, aKerning); // change the spacing of the characters
}
~cpccFontKerningWin()
{
SetTextCharacterExtra(m_dc, m_oldSpacing); // restore the drawing tools
}
};