TNB Library
TnbTextDrawer.h
[詳解]
1#pragma once
11#include "TnbDrawable.h"
13#ifndef _TnbDOXYGEN //Document作成用シンボル
14 #ifndef BS_TYPEMASK
15 #define BS_TYPEMASK 0x0000000F
16 #endif
17 #ifndef SS_TYPEMASK
18 #define SS_TYPEMASK 0x0000001FL
19 #endif
20#endif
21
22
23
24//TNB Library
25namespace TNB
26{
27
28
29
52{
53 DEFSUPER(CNullDrawer);
54public:
55
65 CTextDrawer(const SIZE& size, HFONT font, COLORREF textColor, LPCTSTR str, DWORD drawStyle = DT_CENTER | DT_VCENTER, int margin = 0)
66 : _super(size), m_font(font), m_textColor(textColor), m_str(str), m_drawStyle(drawStyle), m_margin(margin)
67 {
68 }
69
79 CTextDrawer(HFONT font, COLORREF textColor, LPCTSTR str, DWORD drawStyle = DT_CENTER | DT_VCENTER, int margin = 0)
80 : _super(), m_font(font), m_textColor(textColor), m_str(str), m_drawStyle(drawStyle), m_margin(margin)
81 {
82 AdjustSize(0);
83 }
84
90 virtual IDrawable* Clone(void) const
91 {
93 }
94
102 virtual void Draw(HDC dc, int x = 0, int y = 0) const
103 {
104 RECT rc = { x + m_margin, y, x + m_size.cx, y + m_size.cy };
105 CDcSelectAssistant dca(dc);
106// int oldBkMode = ::SetBkMode(dc, TRANSPARENT);
107 dca.SetBkMode(TRANSPARENT);
108 if ( m_font != NULL )
109 {
110// HGDIOBJ oldObj = ::SelectObject(dc, m_font);
111 dca.SelectFont(m_font);
113// ::SelectObject(dc, oldObj);
114 }
115 else
116 {
118 }
119// ::SetBkMode(dc, oldBkMode);
120 }
121
126 void SetTextColor(COLORREF color)
127 {
128 m_textColor = color;
129 }
130
135 COLORREF GetTextColor(void) const
136 {
137 return m_textColor;
138 }
139
145 void SetString(LPCTSTR str)
146 {
147 m_str = str;
148 }
149
154 LPCTSTR GetString(void) const
155 {
156 return m_str;
157 }
158
164 void SetDrawStyle(DWORD drawStyle = DT_CENTER | DT_VCENTER)
165 {
166 m_drawStyle = drawStyle;
167 }
168
173 DWORD GetDrawStyle(void) const
174 {
175 return m_drawStyle;
176 }
177
186 bool AdjustSize(int margin = 1, HWND hWnd = NULL)
187 {
188 HDC dc = ::GetDC(hWnd);
189 CDcSelectAssistant dca(dc);
190// HGDIOBJ oldObj = ::SelectObject(dc, m_font);
191 dca.SelectFont(m_font);
192 SIZE s = m_size;
193 bool r = CalcTextSize(s, dc, m_drawStyle, m_str);
194// if ( oldObj != NULL )
195// {
196// ::SelectObject(dc, oldObj);
197// }
198 dca.Restore();
199 ::ReleaseDC(hWnd, dc);
200 if ( r )
201 {
202 m_size.cx = s.cx + margin + m_margin;
203 m_size.cy = s.cy + margin;
204 }
205 return r;
206 }
207
208
209 // -----------------
210
211
217 static DWORD StaticToDrawStyle(DWORD staticStyle)
218 {
219 UINT r = 0;
220 switch ( staticStyle & SS_TYPEMASK )
221 {
222 case SS_CENTER: r = DT_CENTER; break;
223 case SS_LEFT: r = DT_LEFT; break;
224 case SS_RIGHT: r = DT_RIGHT; break;
225 case SS_SIMPLE: r = DT_SINGLELINE; break;
226 };
227 if ( (staticStyle & SS_TYPEMASK) != SS_LEFTNOWORDWRAP )
228 {
229 r |= DT_WORDBREAK;
230 }
231 if ( (staticStyle & SS_CENTERIMAGE) != 0 )
232 {
233 r |= DT_VCENTER | DT_SINGLELINE;
234 }
235 if ( (staticStyle & SS_NOPREFIX) != 0 )
236 {
237 r |= DT_NOPREFIX;
238 }
239 #ifndef _WIN32_WCE
240 if ( (staticStyle & SS_WORDELLIPSIS) == SS_WORDELLIPSIS )
241 {
242 r |= DT_WORD_ELLIPSIS;
243 }
244 else if ( (staticStyle & SS_ENDELLIPSIS) != 0 )
245 {
246 r |= DT_END_ELLIPSIS;
247 }
248 else if ( (staticStyle & SS_PATHELLIPSIS) != 0 )
249 {
250 r |= DT_PATH_ELLIPSIS;
251 }
252 #endif
253 return r;
254 }
255
261 static DWORD ButtonToDrawStyle(DWORD buttonStyle)
262 {
263 DWORD r = 0;
264 bool isPushButton = false;
265 if ( (buttonStyle & BS_TYPEMASK) <= BS_DEFPUSHBUTTON || (buttonStyle & BS_PUSHLIKE) != 0 )
266 {
267 isPushButton = true;
268 }
269 switch ( buttonStyle & 0x0300 )
270 {
271 case BS_LEFT: /*0x00000100L*/ r |= DT_LEFT; break;
272 case BS_RIGHT: /*0x00000200L*/ r |= DT_RIGHT; break;
273 case BS_CENTER: /*0x00000300L*/ r |= DT_CENTER; break;
274 default:
275 r |= (isPushButton ? DT_CENTER : DT_LEFT);
276 break;
277 }
278
279 switch ( buttonStyle & 0x0C00 )
280 {
281 case BS_TOP: /*0x00000400L*/ r |= DT_TOP; break;
282 case BS_BOTTOM: /*0x00000800L*/ r |= DT_BOTTOM; break;
283 default:
284 case BS_VCENTER:/*0x00000C00L*/ r |= DT_VCENTER; break;
285 }
286 if ( (buttonStyle & BS_MULTILINE) == 0 )
287 {
288 r |= DT_SINGLELINE;
289 }
290 else
291 {
292 r |= DT_WORDBREAK;
293 }
294 return r;
295 }
296
307 static bool CalcTextSize(SIZE& _size, HDC dc, UINT drawStyle, LPCTSTR str)
308 {
309 RECT rc = { 0, 0, _size.cx, _size.cy };
310 int r = ::DrawText(dc, str, -1, &rc, (drawStyle & 0xFFFFFFF0) | DT_CALCRECT);
311 _size.cx = rc.right;
312 _size.cy = rc.bottom;
313 return r != 0;
314 }
315
325 static bool CalcTextRect(RECT& _rect, HDC dc, DWORD drawStyle, LPCTSTR str)
326 {
327 SIZE sz = { _rect.right - _rect.left, _rect.bottom - _rect.top };
328 if ( ! CalcTextSize(sz, dc, drawStyle, str) ) { return false; }
329 RECT rc = { 0, 0, sz.cx, sz.cy };
330 int cx = (_rect.right - _rect.left) - sz.cx;
331 int cy = (_rect.bottom - _rect.top) - sz.cy;
332 if ( (drawStyle & DT_CENTER) != 0 )
333 {
334 cx /= 2;
335 }
336 else if ( (drawStyle & DT_RIGHT) == 0 )
337 {
338 cx = 0;
339 }
340 if ( (drawStyle & DT_VCENTER) != 0 )
341 {
342 cy /= 2;
343 }
344 else if ( (drawStyle & DT_BOTTOM) == 0 )
345 {
346 cy = 0;
347 }
348 ::OffsetRect(&rc, cx + _rect.left, cy + _rect.top);
349 _rect = rc;
350 return true;
351 }
352
366 static bool DrawTextRect(RECT& _rect, HDC dc, DWORD drawStyle, const POINT& offset, COLORREF color1, COLORREF color2, LPCTSTR str)
367 {
368 if ( ! CalcTextRect(_rect, dc, drawStyle, str) )
369 {
370 return false;
371 }
372 drawStyle &= ~DT_VCENTER;
373 RECT rc = _rect;
374 if ( color1 == CLR_INVALID )
375 {
376 #ifndef _WIN32_WCE
377 ::DrawState(dc, NULL, &ms_DrawStateProc,
378 reinterpret_cast<LPARAM>(str),
379 static_cast<WPARAM>(drawStyle),
380 rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, DST_COMPLEX | DSS_DISABLED);
381 return true;
382 #else
383 POINT po = { 1, 1 };
384 return DrawTextRect(dc, _rect, drawStyle, po, ::GetSysColor(COLOR_GRAYTEXT), ::GetSysColor(COLOR_3DHILIGHT), str);
385 #endif
386 }
387 if ( color1 == CLR_AUTOSELECT )
388 {
389 color1 = ::GetTextColor(dc);
390 }
391 if ( color2 == CLR_INVALID )
392 {
393 COLORREF oldColor = ::SetTextColor(dc, color1);
394 ::DrawText(dc, str, -1, &rc, drawStyle);
395 ::SetTextColor(dc, oldColor);
396 }
397 else
398 {
399 COLORREF oldColor = ::SetTextColor(dc, color2);
400 ::OffsetRect(&rc, offset.x, offset.y);
401 ::DrawText(dc, str, -1, &rc, drawStyle);
402 ::SetTextColor(dc, color1);
403 rc = _rect;
404 ::DrawText(dc, str, -1, &rc, drawStyle);
405 ::SetTextColor(dc, oldColor);
406 }
407 return true;
408 }
409
423 static bool DrawTextRect(HDC dc, const RECT& rect, DWORD drawStyle, const POINT& offset, COLORREF color1, COLORREF color2, LPCTSTR str)
424 {
425 RECT rc = rect;
426 return DrawTextRect(rc, dc, drawStyle, offset, color1, color2, str);
427 }
428
440 static bool DrawTextRect(RECT& _rect, HDC dc, DWORD drawStyle, COLORREF color, LPCTSTR str)
441 {
442 POINT po = { 0, 0 };
443 return DrawTextRect(_rect, dc, drawStyle, po, color, CLR_INVALID, str);
444 }
445
457 static bool DrawTextRect(HDC dc, const RECT& rect, DWORD drawStyle, COLORREF color, LPCTSTR str)
458 {
459 POINT po = { 0, 0 };
460 RECT rc = rect;
461 return DrawTextRect(rc, dc, drawStyle, po, color, CLR_INVALID, str);
462 }
463
464 #ifndef _WIN32_WCE
465
477 static bool ModifyText(CSimpleStr& _str, const SIZE& size, HDC dc, LPCTSTR lpsz, UINT drawStyle = DT_END_ELLIPSIS | DT_NOPREFIX)
478 {
479 size_t len = STRLIB::GetLen(lpsz);
480 CWorkMemT<TCHAR> work(len + 10);
481 STRLIB::Copy(work, lpsz);
482 RECT rc = { 0, 0, size.cx, size.cy };
483 int r = ::DrawTextEx(dc, work, -1, &rc, (drawStyle & 0xFFFFFFF0) | DT_MODIFYSTRING | DT_CALCRECT, NULL);
484 if ( r != 0 )
485 {
486 _str = work;
487 }
488 return r != 0;
489 }
490
501 static bool ModifyText(CSimpleStr& _str, HWND hWnd, LPCTSTR lpsz, UINT drawStyle = DT_END_ELLIPSIS | DT_NOPREFIX)
502 {
503 HDC dc = ::GetDC(hWnd);
504 if ( dc != NULL )
505 {
506// HGDIOBJ hFont = reinterpret_cast<HGDIOBJ>(::SendMessage(hWnd, WM_GETFONT, 0, 0));
507 HFONT hFont = reinterpret_cast<HFONT>(::SendMessage(hWnd, WM_GETFONT, 0, 0));
508 CDcSelectAssistant dca(dc);
509// HGDIOBJ hOld = ::SelectObject(dc, hFont);
510 dca.SelectFont(hFont);
511 RECT rc;
512 ::GetClientRect(hWnd, &rc);
513 SIZE size = { rc.right - rc.left, rc.bottom - rc.top };
514 bool r = ModifyText(_str, size, dc, lpsz, drawStyle);
515// ::SelectObject(dc, hOld);
516 dca.Restore();
517 ::ReleaseDC(hWnd, dc);
518 return r;
519 }
520 return false;
521 }
522
523 #endif // _WIN32_WCE
524
525protected:
526 HFONT m_font;
527 COLORREF m_textColor;
531
532private:
534 static BOOL CALLBACK ms_DrawStateProc(HDC hDC, LPARAM lParam, WPARAM wParam, int cx, int cy)
535 {
536 LPCTSTR str = reinterpret_cast<LPCTSTR>(lParam);
537 DWORD drawStyle = static_cast<DWORD>(wParam);
538 RECT rc = { 0, 0, cx, cy };
539 ::DrawText(hDC, str, -1, &rc, drawStyle);
540 return TRUE;
541 }
542};
543
544
545
559{
560 DEFSUPER(CTextDrawer);
561 COLORREF m_backColor;
562public:
573 CTextBkDrawer(const SIZE& size, HFONT font, COLORREF textColor, COLORREF backColor, LPCTSTR str, DWORD style = DT_CENTER | DT_VCENTER, int margin = 0)
574 : _super(size, font, textColor, str, style, margin), m_backColor(backColor)
575 {
576 }
577
587 CTextBkDrawer(HFONT font, COLORREF textColor, COLORREF backColor, LPCTSTR str, DWORD style = DT_CENTER | DT_VCENTER, int margin = 0)
588 : _super(font, textColor, str, style, margin), m_backColor(backColor)
589 {
590 }
591
597 virtual IDrawable* Clone(void) const
598 {
599 return new CTextBkDrawer(m_size, m_font, m_textColor, m_backColor, m_str, m_drawStyle, m_margin);
600 }
601
609 virtual void Draw(HDC dc, int x = 0, int y = 0) const
610 {
611 RECT rc = { x + m_margin, y, x + m_size.cx, y + m_size.cy };
612 CDcSelectAssistant dca(dc);
613// COLORREF oldBackColor = ::SetBkColor(dc, m_backColor);
614 dca.SetBkColor(m_backColor);
615 ::ExtTextOut(dc, 0, 0, ETO_OPAQUE, &rc, NULL, 0, NULL);
616 if ( m_font != NULL )
617 {
618// HGDIOBJ oldObj = ::SelectObject(dc, m_font);
619 dca.SelectFont(m_font);
621// ::SelectObject(dc, oldObj);
622 }
623 else
624 {
626 }
627// ::SetBkColor(dc, oldBackColor);
628 }
629
634 void SetBackColor(COLORREF color)
635 {
636 m_backColor = color;
637 }
638};
639
640
641
642}; //TNB
643
644
デバイステキストセレクトアシスタント関係のヘッダ
描画情報関係のヘッダ
デバイステキストセレクトアシスタント
void Restore(void)
[設定] リストア.
void SetBkColor(COLORREF c)
[設定] 背景色設定.
void SetBkMode(int mode)
[設定] 背景モード設定.
void SelectFont(HFONT font)
[選択] フォント選択.
NULL描画クラス
Definition: TnbDrawable.h:106
SIZE m_size
サイズ
Definition: TnbDrawable.h:109
簡易文字列管理クラス.
Definition: TnbDef.h:772
テキスト描画クラス
virtual void Draw(HDC dc, int x=0, int y=0) const
[描画] 描画.
void SetBackColor(COLORREF color)
[設定] 背景色設定.
CTextBkDrawer(HFONT font, COLORREF textColor, COLORREF backColor, LPCTSTR str, DWORD style=DT_CENTER|DT_VCENTER, int margin=0)
コンストラクタ
CTextBkDrawer(const SIZE &size, HFONT font, COLORREF textColor, COLORREF backColor, LPCTSTR str, DWORD style=DT_CENTER|DT_VCENTER, int margin=0)
コンストラクタ
virtual IDrawable * Clone(void) const
[作成] クローン作成.
テキスト描画クラス
Definition: TnbTextDrawer.h:52
HFONT m_font
フォント
DWORD GetDrawStyle(void) const
[取得] スタイル取得.
COLORREF GetTextColor(void) const
[取得] 文字色取得.
COLORREF m_textColor
テキストカラー
static bool ModifyText(CSimpleStr &_str, HWND hWnd, LPCTSTR lpsz, UINT drawStyle=DT_END_ELLIPSIS|DT_NOPREFIX)
[計算] 文字列編集.
static bool DrawTextRect(HDC dc, const RECT &rect, DWORD drawStyle, const POINT &offset, COLORREF color1, COLORREF color2, LPCTSTR str)
[表示] 範囲文字表示.
DWORD m_drawStyle
DrawTextスタイル
CTextDrawer(HFONT font, COLORREF textColor, LPCTSTR str, DWORD drawStyle=DT_CENTER|DT_VCENTER, int margin=0)
コンストラクタ
Definition: TnbTextDrawer.h:79
virtual void Draw(HDC dc, int x=0, int y=0) const
[描画] 描画.
bool AdjustSize(int margin=1, HWND hWnd=NULL)
[設定] サイズ調整.
CSimpleStr m_str
文字列
static bool CalcTextSize(SIZE &_size, HDC dc, UINT drawStyle, LPCTSTR str)
[計算] 文字表示大きさ計算.
static bool CalcTextRect(RECT &_rect, HDC dc, DWORD drawStyle, LPCTSTR str)
[計算] 文字表示範囲計算.
void SetDrawStyle(DWORD drawStyle=DT_CENTER|DT_VCENTER)
[設定] スタイル設定.
static bool DrawTextRect(HDC dc, const RECT &rect, DWORD drawStyle, COLORREF color, LPCTSTR str)
[表示] 範囲文字表示.
CTextDrawer(const SIZE &size, HFONT font, COLORREF textColor, LPCTSTR str, DWORD drawStyle=DT_CENTER|DT_VCENTER, int margin=0)
コンストラクタ
Definition: TnbTextDrawer.h:65
static DWORD ButtonToDrawStyle(DWORD buttonStyle)
[変換] ButtonスタイルをDrawTextスタイルに変換
static bool ModifyText(CSimpleStr &_str, const SIZE &size, HDC dc, LPCTSTR lpsz, UINT drawStyle=DT_END_ELLIPSIS|DT_NOPREFIX)
[計算] 文字列編集.
static bool DrawTextRect(RECT &_rect, HDC dc, DWORD drawStyle, COLORREF color, LPCTSTR str)
[表示] 範囲文字表示.
static bool DrawTextRect(RECT &_rect, HDC dc, DWORD drawStyle, const POINT &offset, COLORREF color1, COLORREF color2, LPCTSTR str)
[表示] 範囲文字表示.
int m_margin
左マージン
static DWORD StaticToDrawStyle(DWORD staticStyle)
[変換] StaticスタイルをDrawTextスタイルに変換
void SetTextColor(COLORREF color)
[設定] 文字色設定.
void SetString(LPCTSTR str)
[設定] 文字列設定.
LPCTSTR GetString(void) const
[取得] 文字列取得.
virtual IDrawable * Clone(void) const
[作成] クローン作成.
Definition: TnbTextDrawer.h:90
size_t GetLen(LPCSTR lpsz)
[計算] 文字列長計算(ASCII/SJIS用)
Definition: TnbStrLib.h:44
void Copy(LPSTR _dst, LPCSTR src)
[複製] 文字列コピー(ASCII/SJIS用)
Definition: TnbStrLib.h:89
TNB Library
Definition: TnbDoxyTitle.txt:2
描画情報インターフェース
Definition: TnbDrawable.h:37