TNB Library
TnbMultiFontTextDrawer.h
[詳解]
1#pragma once
11#include "TnbTextDrawer.h"
12#include "TnbVector.h"
13#include "TnbStrLib.h"
14
15
16
17//TNB Library
18namespace TNB
19{
20
21
22
40{
41 DEFSUPER(CNullDrawer);
42protected:
44 struct TPart
45 {
47 HFONT font;
48 COLORREF textColor;
49 };
51 COLORREF m_backColor;
53 //
54 mutable bool m_boIsCalced;
56 mutable SIZE m_partsSize;
58 bool m_calc(HDC dc, DWORD style) const
59 {
60 if ( m_boIsCalced ) { return true; }
61 UINT un = DT_PATH_ELLIPSIS | DT_END_ELLIPSIS | DT_WORD_ELLIPSIS;
62 DWORD st = style & (0xFFFFFFF0 - un);
64 m_partsSize.cx = 0;
65 m_partsSize.cy = 0;
66 int h = 0;
67 HGDIOBJ oldFont = ::GetCurrentObject(dc, OBJ_FONT);
68 loop ( i, m_aParts.GetSize() )
69 {
70 const TPart& pa = m_aParts.At(i);
71 ::SelectObject(dc, pa.font);
72 TEXTMETRIC tTextMetric;
73 if ( ! ::GetTextMetrics(dc, &tTextMetric) )
74 {
75 return false;
76 }
77 SIZE sz = { 0, 0 };
78 if ( ! CTextDrawer::CalcTextSize(sz, dc, st, pa.str.ReferBuffer()) )
79 {
80 return false;
81 }
82 ASSERT( sz.cy == tTextMetric.tmHeight );
83 int l = tTextMetric.tmAscent;//アンダーのサイズ
84 h = max(h, l);
85 m_aOffsets.Add(CPoint(m_partsSize.cx, -l));
86 m_partsSize.cx += sz.cx;
87 m_partsSize.cy = max(m_partsSize.cy, tTextMetric.tmHeight);
88 }
89 ::SelectObject(dc, oldFont);
90 int x = 0;
91 int y = 0;
92 switch( style & 0x03 )
93 {
94 case DT_CENTER:
95 x = (m_size.cx - m_partsSize.cx) / 2;
96 break;
97 case DT_RIGHT:
98 x = m_size.cx - m_partsSize.cx;
99 break;
100 }
101 if ( x < 0 )
102 {
103 x = 0;
104 }
105 switch( style & 0x0C )
106 {
107 case DT_VCENTER:
108 y = (m_size.cy - m_partsSize.cy) / 2;
109 break;
110 case DT_BOTTOM:
111 y = m_size.cy - m_partsSize.cy;
112 break;
113 }
114 if ( y < 0 )
115 {
116 y = 0;
117 }
118 loop ( i, m_aOffsets.GetSize() )
119 {
120 m_aOffsets[i].x += x;
121 m_aOffsets[i].y += y + h;
122 }
123 m_boIsCalced = true;
124 return true;
125 }
126public:
127
134 CMultiFontTextDrawer(const SIZE& size, COLORREF backColor = CLR_INVALID, DWORD drawStyle = DT_CENTER | DT_VCENTER)
135 : _super(size), m_backColor(backColor), m_drawStyle(drawStyle | DT_SINGLELINE), m_boIsCalced(false)
136 {
137 }
138
145 CMultiFontTextDrawer(COLORREF backColor = CLR_INVALID, DWORD drawStyle = DT_CENTER | DT_VCENTER)
146 : _super(), m_backColor(backColor), m_drawStyle(drawStyle | DT_SINGLELINE), m_boIsCalced(false)
147 {
148 }
149
155 virtual IDrawable* Clone(void) const
156 {
158 P->m_aParts = m_aParts;
159 return P;
160 }
161
169 virtual void Draw(HDC dc, int x = 0, int y = 0) const
170 {
171 if ( m_calc(dc, m_drawStyle) )
172 {
173 RECT rc = { x, y, x + m_size.cx, y + m_size.cy };
174 COLORREF oldColor = ::GetBkColor(dc);
175 int oldBkMode = ::SetBkMode(dc, TRANSPARENT);
176 if ( IS_RGBVALUE(m_backColor) )
177 {
178 ::SetBkColor(dc, m_backColor);
179 ::SetBkMode(dc, OPAQUE);
180 }
181 HGDIOBJ oldFont = ::GetCurrentObject(dc, OBJ_FONT);
182 loop ( i, m_aParts.GetSize() )
183 {
184 const TPart& pa = m_aParts.At(i);
185 ::SelectObject(dc, pa.font);
186 const POINT& offset = m_aOffsets[i];
187 RECT r = rc;
188 r.left += offset.x;
189 r.top += offset.y;
190 if ( r.right > r.left && r.bottom > r.top )
191 {
192 CTextDrawer::DrawTextRect(dc, r, m_drawStyle & 0xFFFFFFF0, pa.textColor, pa.str.ReferBuffer());
193 }
194 }
195 ::SelectObject(dc, oldFont);
196 ::SetBkMode(dc, oldBkMode);
197 ::SetBkColor(dc, oldColor);
198 }
199 }
200
209 INDEX Add(LPCTSTR str, HFONT font, COLORREF color)
210 {
211 m_boIsCalced = false;
212 TPart p;
213 p.font = font;
214 p.textColor = color;
215 LPTSTR P = p.str.GetBuffer(_tcslen(str) + 1);
216 STRLIB::Copy(P, str);
217 p.str.ReleaseBuffer();
218 return m_aParts.Add(p);
219 }
220
229 bool AdjustSize(HDC dc, int margin = 1)
230 {
231 m_boIsCalced = false;
232 if ( ! m_calc(dc, m_drawStyle & 0xFFFFFFF0) )
233 {
234 return false;
235 }
236 ASSERTLIB( m_boIsCalced );
237 m_size.cx = m_partsSize.cx + margin * 2;
238 m_size.cy = m_partsSize.cy + margin * 2;
239 m_boIsCalced = false; //計算フラグOFFにする
240 return true;
241 }
242};
243
244
245
246}; //TNB
247
248
#define loop(VAR, CNT)
loop構文.
Definition: TnbDef.h:343
文字列操作ライブラリ関係のヘッダ
テキスト描画関係のヘッダ
配列型情報管理関係のヘッダ
マルチフォントテキスト描画クラス
CVectorT< TPart > m_aParts
情報
DWORD m_drawStyle
DrawTextスタイル
CVectorT< POINT > m_aOffsets
各PartのOFFSET
CMultiFontTextDrawer(COLORREF backColor=CLR_INVALID, DWORD drawStyle=DT_CENTER|DT_VCENTER)
コンストラクタ
virtual void Draw(HDC dc, int x=0, int y=0) const
[描画] 描画.
bool m_calc(HDC dc, DWORD style) const
計算
CMultiFontTextDrawer(const SIZE &size, COLORREF backColor=CLR_INVALID, DWORD drawStyle=DT_CENTER|DT_VCENTER)
コンストラクタ
INDEX Add(LPCTSTR str, HFONT font, COLORREF color)
[追加] 情報追加.
SIZE m_partsSize
全Partの大きさ
COLORREF m_backColor
背景カラー
bool AdjustSize(HDC dc, int margin=1)
[設定] サイズ調整.
virtual IDrawable * Clone(void) const
[作成] クローン作成.
NULL描画クラス
Definition: TnbDrawable.h:106
SIZE m_size
サイズ
Definition: TnbDrawable.h:109
static bool CalcTextSize(SIZE &_size, HDC dc, UINT drawStyle, LPCTSTR str)
[計算] 文字表示大きさ計算.
static bool DrawTextRect(RECT &_rect, HDC dc, DWORD drawStyle, const POINT &offset, COLORREF color1, COLORREF color2, LPCTSTR str)
[表示] 範囲文字表示.
virtual size_t GetSize(void) const
[取得] サイズ取得
Definition: TnbVector.h:368
TYP * GetBuffer(size_t size=0)
[操作] データアドレス取得
Definition: TnbVector.h:745
void ReleaseBuffer(void)
[操作] データの管理を元に戻す.
Definition: TnbVector.h:805
virtual bool RemoveAll(void)
[削除] 空化
Definition: TnbVector.h:565
virtual const TYP * ReferBuffer(void) const
[取得] データアドレス取得
Definition: TnbVector.h:664
virtual INDEX Add(const TYP &t)
[追加] 要素一つ追加.
Definition: TnbVector.h:383
void Copy(LPSTR _dst, LPCSTR src)
[複製] 文字列コピー(ASCII/SJIS用)
Definition: TnbStrLib.h:89
TNB Library
Definition: TnbDoxyTitle.txt:2
COLORREF textColor
テキストカラー
描画情報インターフェース
Definition: TnbDrawable.h:37