TNB Library
TnbMfcAttributedTextCtrl.h
[詳解]
1#pragma once
11#include "TnbAttributedStr.h"
12#include "TnbThread.h"
13
14
15
16//TNB Library
17namespace TNB {
18namespace MFC {
19
20
21
39{
40 DEFSUPER(CStatic);
41public:
42
44 enum EAttr
45 {
48 BLINK
49 };
50
52
53private:
54
55 CThread m_thread;
56 bool m_boIsNormalForBlink;
57 CFont* m_pfont;
58 CRect m_rect;
59 CPoint m_pointMargin;
60 COLORREF m_colorBack;
61 COLORREF m_colorNormal;
62 CSyncCounter m_syncDraw;
63 enum {
64 BLINK_TIMING = 500
65 };
66 CAttrStr m_content;
67 CVectorT<INDEX> m_vdwTopPoss;
68
73 void m_ResearchContent(void)
74 {
75 m_vdwTopPoss.RemoveAll();
76 m_vdwTopPoss.Add(0);
77 loop ( i, m_content.GetLength() )
78 {
79 if ( m_content.GetAt(i) == '\r' )
80 {
81 if ( m_content.GetAt(i + 1) == '\n' )
82 {
83 m_vdwTopPoss.Add(i + 2);
84 }
85 else
86 {
87 m_vdwTopPoss.Add(i + 1);
88 }
89 }
90 }
91 }
92
98 void m_Draw(CDC* dc, bool boIsForBlink = false)
99 {
100 dc->SaveDC();
101 CFont* pOldFont = dc->SelectObject(m_pfont);
102
103 dc->IntersectClipRect(m_rect);
104
105 if ( ! boIsForBlink )
106 {
107 dc->FillSolidRect(m_rect, m_colorBack);
108 }
109 LOGFONT tFont;
110 m_pfont->GetLogFont(&tFont);
111 dc->SetBkMode(OPAQUE/*TRANSPARENT*/);
112 loop ( i, m_vdwTopPoss.GetSize() )
113 {
114 CRect r = m_rect;
115 r.OffsetRect(m_pointMargin);
116 r.OffsetRect(0, abs(static_cast<LONG>(tFont.lfHeight * i)));
117 TCHAR c[3] = {0};
118 UINT uChar;
119 INDEX j = m_vdwTopPoss[i];
120 while ( j < m_content.GetLength() )
121 {
122 EAttr a = m_content.GetAttribute(j);
123 c[0] = m_content.GetAt(j++);
124 if ( c[0] == '\n' || c[0] == 0 )
125 {
126 break;
127 }
128 if ( a == BLINK )
129 {
130 a = m_boIsNormalForBlink ? NORMAL : REVERSE;
131 }
132 if ( a == NORMAL )
133 {
134 dc->SetTextColor(m_colorNormal);
135 dc->SetBkColor(m_colorBack);
136 }
137 else
138 {
139 dc->SetTextColor(m_colorBack);
140 dc->SetBkColor(m_colorNormal);
141 }
142 uChar = c[0] & 0xff;
143 if ( STRLIB::GetCharSize(c[0]) == 2 )
144 {
145 //- SJISだ
146 c[1] = m_content.GetAt(j);
147 uChar = (uChar << 8) | (c[1] & 0xFF);
148 j++;
149 dc->DrawText(c, 2, r, 0);
150 }
151 else
152 {
153 //- ASCIIだ
154 dc->DrawText(c, 1, r, 0);
155 }
156 //
157 ABC abc;
158 VERIFY(dc->GetCharABCWidths(uChar, uChar, &abc));
159 r.OffsetRect(abc.abcA + abc.abcB + abc.abcC, 0);
160 }
161 }
162 dc->SelectObject(pOldFont);
163 dc->RestoreDC(-1);
164 }
165
172 DWORD Run(void)
173 {
174 while ( IsRunnable() )
175 {
176 ::Sleep(BLINK_TIMING);
177 m_boIsNormalForBlink = ! m_boIsNormalForBlink;
178 if ( m_syncDraw.Lock(0) )
179 {
180 if ( ::IsWindow(GetSafeHwnd()) )
181 {
182 HDC hdc = ::GetDC(m_hWnd);
183 if ( hdc != NULL )
184 {
185 CDC dc;
186 dc.Attach(hdc);
187 m_Draw(&dc, true);
188 hdc = dc.Detach();
189 ::ReleaseDC(m_hWnd, dc);
190 }
191 }
192 m_syncDraw.Unlock();
193 }
194 }
195 return 0;
196 }
197
207 virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
208 {
209 if ( message == WM_PAINT )
210 {
211 CPaintDC dc(this); // 描画用のデバイス コンテキスト
212 if ( m_syncDraw.Lock(0) )
213 {
214 m_Draw(&dc);
215 m_syncDraw.Unlock();
216 }
217 return 0;
218 }
219 else if ( message == WM_PRINTCLIENT )
220 {
221// if ( (lParam & PRF_CLIENT) != 0 )
222 {
223 CDC* pDc = CDC::FromHandle(reinterpret_cast<HDC>(wParam));
224 if ( m_syncDraw.Lock(0) )
225 {
226 m_Draw(pDc);
227 m_syncDraw.Unlock();
228 }
229 }
230 return 0;
231 }
232 else if ( message == WM_SETTEXT )
233 {
234 CAttrStr s(reinterpret_cast<LPCTSTR>(lParam));
235 SetContent(s);
236 }
237 else if ( message == WM_DESTROY )
238 {
239 EXCLUSIVE(&m_syncDraw);
240 return _super::WindowProc(message, wParam, lParam);
241 }
242 return _super::WindowProc(message, wParam, lParam);
243 }
244
250 virtual void PreSubclassWindow(void)
251 {
252 _super::GetClientRect(m_rect);
253 _super::PreSubclassWindow();
254 CString s;
255 _super::GetWindowText(s);
256 m_content = s.operator LPCTSTR();
257 m_ResearchContent();
258 //
259 CWnd* P = GetParent();
260 if ( P != NULL )
261 {
262 m_pfont = P->GetFont();
263 }
264 m_thread.Start(_T("CAttributeTextCtrl Blink用"));
265 }
266
267public:
268
271 : _super()
272 , m_colorBack(::GetSysColor(COLOR_MENU))
273 , m_colorNormal(::GetSysColor(COLOR_MENUTEXT))
274 , m_boIsNormalForBlink(false)
275 , m_pointMargin(0, 0)
276 {
277 m_thread.SetRunner(this);
278 }
279
282 {
283 m_thread.Stop();
284 }
285
291 void SetMargin(int x, int y)
292 {
293 m_pointMargin.x = x;
294 m_pointMargin.y = y;
295 }
296
304 void SetFont(CFont* pFont, BOOL bRedraw = TRUE)
305 {
306 m_pfont = pFont;
307 _super::SetFont(pFont, bRedraw);
308 }
309
314 const CAttrStr& GetContent(void) const
315 {
316 return m_content;
317 }
318
323 void SetContent(const CAttrStr& as)
324 {
325 m_content = as;
326 m_ResearchContent();
327 RedrawWindow();
328 }
329
334 void AddContent(const CAttrStr& as)
335 {
336 m_content += as;
337 m_ResearchContent();
338 RedrawWindow();
339 }
340
346 void InsertContent(int iIndex, const CAttrStr& as)
347 {
348 m_content.Insert(iIndex, as);
349 m_ResearchContent();
350 RedrawWindow();
351 }
352
359 void SetAt(int iIndex, TCHAR c, EAttr attr = NORMAL)
360 {
361 m_content.SetAt(iIndex, c, attr);
362 }
363};
364
365
366
367}; //MFC
368}; //TNB
369
属性付き文字列管理関係のヘッダ
#define loop(VAR, CNT)
loop構文.
Definition: TnbDef.h:343
スレッド管理関係のヘッダ
スタティックコントロール.
ウィンドウ管理.
HWND GetSafeHwnd(void) const
[取得] ウィンドウハンドル取得.
カウンタ式排他クラス
Definition: TnbSync.h:594
virtual bool Lock(DWORD dwTime=INFINITE) const
[排他] ロック
Definition: TnbSync.h:621
virtual void Unlock(void) const
[排他] アンロック
Definition: TnbSync.h:654
スレッド管理クラス
Definition: TnbThread.h:316
bool SetRunner(IRunner *pRunner)
[設定] ランナー、設定
Definition: TnbThread.h:420
bool Stop(DWORD dwWait=15000)
[設定] スレッド停止 スレッドに対して停止要求します。
Definition: TnbThread.h:505
bool Start(LPCTSTR lpszName=NULL)
[設定] スレッド開始
Definition: TnbThread.h:618
virtual size_t GetSize(void) const
[取得] サイズ取得
Definition: TnbVector.h:368
virtual bool RemoveAll(void)
[削除] 空化
Definition: TnbVector.h:565
virtual INDEX Add(const TYP &t)
[追加] 要素一つ追加.
Definition: TnbVector.h:383
テキストコントロール
~CAttributedTextCtrl(void)
デストラクタ
void SetContent(const CAttrStr &as)
[設定] 内容一括設定
CAttributedStrT< EAttr > CAttrStr
属性付き文字列
void SetAt(int iIndex, TCHAR c, EAttr attr=NORMAL)
[設定] 内容変更
void SetMargin(int x, int y)
[設定] マージン設定
const CAttrStr & GetContent(void) const
[取得] 内容取得
void AddContent(const CAttrStr &as)
[設定] 内容連結
void SetFont(CFont *pFont, BOOL bRedraw=TRUE)
[設定] フォント設定
void InsertContent(int iIndex, const CAttrStr &as)
[設定] 内容挿入
CAttributedTextCtrl(void)
コンストラクタ
int GetCharSize(char c)
[取得] 文字のサイズ(ASCII/SJIS用)
Definition: TnbStrLib.h:341
#define EXCLUSIVE(CLS)
簡易排他制御マクロ.
Definition: TnbSync.h:788
TNB Library
Definition: TnbDoxyTitle.txt:2
スレッド実行管理ランナーインターフェース
Definition: TnbThread.h:341
bool IsRunnable(void) const
[確認] 実行可能か否か
Definition: TnbThread.h:355