forked from sakura-editor/sakura
/
CGraphics.h
336 lines (292 loc) · 7.62 KB
/
CGraphics.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*! @file */
/*
Copyright (C) 2008, kobake
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented;
you must not claim that you wrote the original software.
If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but is
not required.
2. Altered source versions must be plainly marked as such,
and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#ifndef SAKURA_CGRAPHICS_BA5156BF_99C6_4854_8131_CE8B091A5EFF9_H_
#define SAKURA_CGRAPHICS_BA5156BF_99C6_4854_8131_CE8B091A5EFF9_H_
/*
2008.05.20 kobake 作成
*/
#include <Windows.h>
#include <cassert>
#include <vector>
/*!
* @brief API関数FillRectの高速版(ブラシ用)
*
* @param [in] hDC デバイスコンテキスト
* @param [in] rc 塗りつぶし対象の矩形
* @param [in] hBrush 塗りつぶしに使うブラシハンドル
*/
inline bool MyFillRect( const HDC hDC, const RECT &rc, const HBRUSH hBrush ) noexcept
{
assert( hDC );
assert( hBrush );
assert( !IS_INTRESOURCE( hBrush ) );
if ( !hDC || IS_INTRESOURCE( hBrush ) ) return false;
HGDIOBJ hBrushOld = ::SelectObject( hDC, hBrush );
if ( hBrushOld == HGDI_ERROR ) return false;
int retPatBlt = ::PatBlt( hDC, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, PATCOPY );
::SelectObject( hDC, hBrushOld );
return retPatBlt != 0;
}
/*!
* @brief API関数FillRectの高速版(カラーインデックス用)
*
* @param [in] hDC デバイスコンテキスト
* @param [in] rc 塗りつぶし対象の矩形
* @param [in] sysColor システムカラーのインデックス
*/
inline bool MyFillRect( const HDC hDC, const RECT &rc, const int sysColor ) noexcept
{
assert( hDC );
assert( sysColor );
assert( IS_INTRESOURCE( sysColor ) );
if ( !hDC || !IS_INTRESOURCE( sysColor ) ) return false;
HBRUSH hBrush = ::GetSysColorBrush( sysColor );
if ( hBrush == NULL ) return false;
bool retMyFillRect = MyFillRect( hDC, rc, hBrush );
return retMyFillRect;
}
/*!
* @brief API関数FillRectの高速版(色指定用)
*
* @param [in] hDC デバイスコンテキスト
* @param [in] rc 塗りつぶし対象の矩形
* @param [in] color 塗りつぶし色
*/
inline bool MyFillRect( const HDC hDC, const RECT &rc, const COLORREF color ) noexcept
{
assert( hDC );
if ( !hDC ) return false;
HBRUSH hBrush = ::CreateSolidBrush( color );
if ( hBrush == NULL ) return false;
bool retMyFillRect = MyFillRect( hDC, rc, hBrush );
::DeleteObject( hBrush );
return retMyFillRect;
}
//! オリジナル値保存クラス
template <class T>
class TOriginalHolder{
public:
TOriginalHolder<T>()
{
m_data = 0;
m_hold = false;
}
void Clear()
{
m_data = 0;
m_hold = false;
}
void AssignOnce(const T& t)
{
if(!m_hold){
m_data = t;
m_hold = true;
}
}
const T& Get() const
{
return m_data;
}
bool HasData() const
{
return m_hold;
}
private:
T m_data;
bool m_hold;
};
//! フォント情報管理
struct SFONT {
SFontAttr m_sFontAttr;
HFONT m_hFont; //!< フォントハンドル
};
//! 描画管理
//最新実装:ブラシ
class CGraphics{
public:
CGraphics(const CGraphics& rhs){ Init(rhs.m_hdc); }
CGraphics(HDC hdc = NULL){ Init(hdc); }
~CGraphics();
void Init(HDC hdc);
operator HDC() const{ return m_hdc; }
//クリッピング
private:
void _InitClipping();
public:
void PushClipping(const RECT& rc);
void PopClipping();
void ClearClipping();
void SetClipping(const RECT& rc)
{
ClearClipping();
PushClipping(rc);
}
//テキスト文字色
public:
void PushTextForeColor(COLORREF color);
void PopTextForeColor();
void ClearTextForeColor();
void SetTextForeColor(COLORREF color)
{
ClearTextForeColor();
PushTextForeColor(color);
}
COLORREF GetCurrentTextForeColor()
{
assert(!m_vTextForeColors.empty());
return m_vTextForeColors.back();
}
//テキスト背景色
public:
void PushTextBackColor(COLORREF color);
void PopTextBackColor();
void ClearTextBackColor();
void SetTextBackColor(COLORREF color)
{
ClearTextBackColor();
PushTextBackColor(color);
}
COLORREF GetTextBackColor()
{
assert(!m_vTextBackColors.empty());
return m_vTextBackColors.back();
}
//テキストモード
public:
void SetTextBackTransparent(bool b)
{
m_nTextModeOrg.AssignOnce( ::SetBkMode(m_hdc,b?TRANSPARENT:OPAQUE) );
}
//テキスト
public:
void RestoreTextColors();
//フォント
public:
void PushMyFont(HFONT hFont)
{
SFONT sFont = { { false, false }, hFont };
PushMyFont(sFont);
}
void PushMyFont(const SFONT& sFont);
void PopMyFont();
void ClearMyFont();
//! フォント設定
void SetMyFont(const SFONT& sFont)
{
ClearMyFont();
PushMyFont(sFont);
}
bool GetCurrentMyFontBold()
{
assert(!m_vFonts.empty());
return m_vFonts.back().m_sFontAttr.m_bBoldFont;
}
bool GetCurrentMyFontUnderline()
{
assert(!m_vFonts.empty());
return m_vFonts.back().m_sFontAttr.m_bUnderLine;
}
const SFontAttr& GetCurrentMyFontAttr()
{
assert(!m_vFonts.empty());
return m_vFonts.back().m_sFontAttr;
}
//ペン
public:
void PushPen(COLORREF color, int nPenWidth, int nStyle = PS_SOLID);
void PopPen();
void SetPen(COLORREF color)
{
ClearPen();
PushPen(color,1);
}
void ClearPen();
COLORREF GetPenColor() const;
//ブラシ
public:
void _InitBrushColor();
void PushBrushColor(
COLORREF color //!< ブラシの色。(COLORREF)-1 にすると、透明ブラシとなる。
);
void PopBrushColor();
void ClearBrush();
void SetBrushColor(COLORREF color)
{
ClearBrush();
PushBrushColor(color);
}
HBRUSH GetCurrentBrush() const{ return m_vBrushes.size()?m_vBrushes.back():NULL; }
//描画
public:
//! 直線
void DrawLine(int x1, int y1, int x2, int y2)
{
::MoveToEx(m_hdc,x1,y1,NULL);
::LineTo(m_hdc,x2,y2);
}
void DrawDotLine(int x1, int y1, int x2, int y2); //点線
//! 矩形塗り潰し
void FillMyRect(const RECT& rc)
{
::MyFillRect( m_hdc, rc, GetCurrentBrush() );
#ifdef _DEBUG
::SetPixel(m_hdc,-1,-1,0); //###########実験
#endif
}
//! 矩形塗り潰し
void FillSolidMyRect(const RECT& rc, COLORREF color)
{
PushTextBackColor(color);
FillMyRectTextBackColor(rc);
PopTextBackColor();
}
//! 矩形塗り潰し
void FillMyRectTextBackColor(const RECT& rc)
{
::ExtTextOut(m_hdc, rc.left, rc.top, ETO_OPAQUE|ETO_CLIPPED, &rc, _T(""), 0, NULL);
}
static void DrawDropRect(LPCRECT lpRectNew, SIZE sizeNew, LPCRECT lpRectLast, SIZE sizeLast); // ドロップ先の矩形を描画する
void DrawRect(int x1, int y1, int x2, int y2);
void DrawRect(const RECT& rc){ DrawRect(rc.left, rc.top, rc.right, rc.bottom); }
private:
//型
typedef TOriginalHolder<COLORREF> COrgColor;
typedef TOriginalHolder<int> COrgInt;
private:
HDC m_hdc;
//クリッピング
std::vector<HRGN> m_vClippingRgns;
//テキスト
std::vector<COLORREF> m_vTextForeColors;
std::vector<COLORREF> m_vTextBackColors;
std::vector<SFONT> m_vFonts;
//テキスト
COrgInt m_nTextModeOrg;
//ペン
HPEN m_hpnOrg;
std::vector<HPEN> m_vPens;
//ブラシ
std::vector<HBRUSH> m_vBrushes;
HBRUSH m_hbrOrg;
HBRUSH m_hbrCurrent;
bool m_bDynamicBrush; //m_hbrCurrentを動的に作成した場合はtrue
};
#endif /* SAKURA_CGRAPHICS_BA5156BF_99C6_4854_8131_CE8B091A5EFF9_H_ */
/*[EOF]*/