TNB Library
TnbMfcDrawingListBox.h
[詳解]
1#pragma once
11#include "TnbMfcCommon.h"
12#include "TnbDrawingContainer.h"
13#include "TnbVector.h"
14#include "TnbMfcBitmapDC.h"
16
17
18
19//TNB Library
20namespace TNB {
21namespace MFC {
22
23
24
44{
45 DEFSUPER(CListBox);
47public:
48
50 CDrawingListBox(void) : _super(), m_itemsWidth(0)
51 {
52 }
53
56 {
57 m_ResetContent();
58 }
59
66 const IDrawable* GetDrawing(int index) const
67 {
68 if ( ! m_drawersList.IsInRange(index) )
69 {
70 return NULL;
71 }
72 return m_drawersList[index];
73 }
74
82 int AddDrawing(const IDrawable& draw)
83 {
84 int r = _super::AddString(_T(""));
85 if ( r >= 0 )
86 {
87 if ( ! m_InsertDrawing(r, draw) )
88 {
89 r = LB_ERR;
90 }
91 }
92 return r;
93 }
94
102 int AddString(LPCTSTR lpszItem)
103 {
104 return _super::AddString(lpszItem);
105 }
106
115 int InsertDrawing(int index, const IDrawable& draw)
116 {
117 int r = _super::InsertString(index, _T(""));
118 if ( r >= 0 )
119 {
120 if ( ! m_InsertDrawing(r, draw) )
121 {
122 r = LB_ERR;
123 }
124 }
125 return r;
126 }
127
136 int InsertString(int index, LPCTSTR lpszItem)
137 {
138 return _super::InsertString(index, lpszItem);
139 }
140
149 int ResetDrawing(int index, const IDrawable& draw)
150 {
151 return m_ResetDrawing(index, draw);
152 }
153
160 int DeleteDrawing(UINT index)
161 {
162 int r = _super::DeleteString(index);
163 if ( r >= 0 )
164 {
165 m_drawersList.Remove(index);
166 }
167 return r;
168 }
169
176 int DeleteString(UINT index)
177 {
178 return DeleteDrawing(index);
179 }
180
184 void ResetContent(void)
185 {
186 m_ResetContent();
187 if ( ::IsWindow(GetSafeHwnd()) )
188 {
189 _super::SetHorizontalExtent(m_itemsWidth);
190 _super::ResetContent();
191 }
192 }
193
194protected:
195
203 virtual LPARAM CalcDrawParam(DWORD_PTR itemNo) const
204 {
205 return itemNo;
206 }
207
218 virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
219 {
220 switch ( message )
221 {
222 case WM_ERASEBKGND: //背景
223 {
224 HDC hdc = reinterpret_cast<HDC>(wParam);
225 CRect rect;
226 GetClientRect(rect);
227 int y = 0;
228 for ( int i = GetTopIndex(); i < GetCount(); i++ )
229 {
230 y += GetItemHeight(i);
231 }
232 int h = rect.Height();
233 if( h > y )
234 {
235 _mark::DrawBackColor(hdc, CRect(0, y, rect.Width(), h));
236 }
237 return 0;
238 }
239 break;
240 case WM_PAINT:
241 if ( GetCount() == 0 )
242 {
243 CPaintDC dc(this);
244 return 0;
245 }
246 else
247 {
249 LRESULT r = _super::WindowProc(message, wParam, lParam);
251 return r;
252 }
253 break;
254 case WM_SETFOCUS:
255 case WM_KILLFOCUS:
256 Invalidate();
257 break;
258 case WM_DESTROY:
259 m_ResetContent();
261 break;
262 case LB_RESETCONTENT: //全消し
263 m_ResetContent();
264 _super::SetHorizontalExtent(m_itemsWidth);
265 break;
266 }
267 return _super::WindowProc(message, wParam, lParam);
268 }
269
275 virtual void PreSubclassWindow(void)
276 {
277 #ifdef _DEBUG
278 DWORD dwStyle = _super::GetStyle();
279 ASSERT0(dwStyle & (LBS_OWNERDRAWVARIABLE | LBS_OWNERDRAWFIXED), "CDrawingListBox", "[オーナー描画]を[なし]以外にしてください");
280 ASSERT0((dwStyle & LBS_SORT) == 0, "CDrawingListBox", "[ソート]はOFFにしてください");
281 // ASSERT0(dwStyle & LBS_EXTENDEDSEL, "PreSubclassWindow", "");
282 #endif
283 _super::PreSubclassWindow();
284 }
285
291 virtual void MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
292 {
293 if ( lpMeasureItemStruct->CtlType != ODT_LISTBOX || ToInt(lpMeasureItemStruct->CtlID) != _super::GetDlgCtrlID() )
294 {
295 return;
296 }
297 }
298
305 virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
306 {
307 if ( lpDrawItemStruct->CtlType != ODT_LISTBOX || lpDrawItemStruct->hwndItem != _super::GetSafeHwnd() )
308 {
309 return;
310 }
311 UINT index = lpDrawItemStruct->itemID;
312 HDC dc = lpDrawItemStruct->hDC;
313 const CRect& rc = lpDrawItemStruct->rcItem;
314 CSize sz = rc.Size();
315 CBitmapImage bi;
316 if ( bi.Set(sz.cx, sz.cy) )
317 {
318 LPARAM lParam = CalcDrawParam(index);
319 CBitmapDC bmpDC(&bi);
320 _mark::DrawBackground(bmpDC, CRect(CPoint(0, 0), sz), lParam);
321 if ( GetSelectMarkType() == BACKGROUND )
322 {
323 m_DrawSelectMaker(bmpDC, sz, lpDrawItemStruct->itemState, lParam);
324 }
325 const IDrawable* pDraw = GetDrawing(index);
326 if ( pDraw != NULL )
327 {
328 SIZE s;
329 pDraw->GetSize(s);
330 pDraw->DrawEx(bmpDC, 0, (sz.cy - s.cy) / 2, lParam);
331 }
332 else if ( ::IsWindow(m_hWnd) && static_cast<INDEX>(GetCount()) > index )
333 {
334 CString str;
335 GetText(index, str);
337 _mark::DrawText(bmpDC, CRect(0, 0, sz.cx, sz.cy), str, lParam);
338 }
339 if ( GetSelectMarkType() == OVERLAP )
340 {
341 m_DrawSelectMaker(bmpDC, sz, lpDrawItemStruct->itemState, lParam);
342 }
343 if ( (lpDrawItemStruct->itemState & ODS_FOCUS) != 0 )
344 {
345 DrawFocusMark(bmpDC, CRect(CPoint(0, 0), sz), lParam);
346 }
347 if ( ! ::IsWindowEnabled(GetSafeHwnd()) )
348 {
349 DrawDisabledStyle(bmpDC, CRect(CPoint(0, 0), sz), lParam);
350 }
351 bmpDC.Draw(dc, rc.left, rc.top);
352 }
353 }
354
355private:
356
357 // アイテム全けし
358 void m_ResetContent(void)
359 {
360 m_itemsWidth = 0;
361 m_drawersList.RemoveAll();
362 }
363 //アイテムの幅計算(結果は m_iItemWidth に代入)
364 void m_CalcItemWidth(bool withReheight = false)
365 {
366 m_itemsWidth = 0;
367 loop ( i, m_drawersList.GetSize() )
368 {
369 SIZE size;
370 IDrawable::Ptr P = m_drawersList[i];
371 if ( ! P.IsNull() && m_drawersList[i]->GetSize(size) )
372 {
373 if ( m_itemsWidth < static_cast<DWORD>(size.cx) )
374 {
375 m_itemsWidth = size.cx;
376 }
377 if ( withReheight )
378 {
379 _super::SetItemHeight(ToInt(i), size.cy);
380 }
381 }
382 }
383 }
385 bool m_InsertDrawing(int index, const IDrawable& draw)
386 {
387 if ( m_drawersList.Insert(index, draw.Clone()) )
388 {
389 SIZE size;
390 draw.GetSize(size);
391 m_CalcItemWidth(true);
392 _super::SetHorizontalExtent(m_itemsWidth);
393 _super::SetItemHeight(index, size.cy);
394 return true;
395 }
396 _super::DeleteString(index);
397 return false;
398 }
400 bool m_ResetDrawing(int index, const IDrawable& draw)
401 {
402 if ( m_drawersList.Set(index, draw.Clone()) )
403 {
404 SIZE size;
405 draw.GetSize(size);
406 m_CalcItemWidth();
407 _super::SetHorizontalExtent(m_itemsWidth);
408 _super::SetItemHeight(index, size.cy);
409 return true;
410 }
411 return false;
412 }
414 void m_DrawSelectMaker(HDC dc, const SIZE& sz, UINT itemState, LPARAM lParam)
415 {
416 HWND hFocus = ::GetFocus();
417 bool r = (hFocus == m_hWnd);
418 if ( ! r && ::IsWindow(m_hWnd) )
419 {
420 CWnd* P = GetOwner();
421 if ( P != NULL )
422 {
423 r = (hFocus == P->GetSafeHwnd());
424 }
425 }
426 if ( (itemState & CDIS_SELECTED) != 0 )
427 {
428 _mark::DrawSelectMark(dc, CRect(CPoint(0, 0), sz), r, lParam);
429 }
430 }
431
432 DWORD m_itemsWidth;
433 CAutoVectorT<IDrawable::Ptr> m_drawersList;
434};
435
436
437
438}; // MFC
439}; // TNB
#define loop(VAR, CNT)
loop構文.
Definition: TnbDef.h:343
描画コンテナ関係のヘッダ
描画情報コントロール抽象クラス関係のヘッダ
ビットマップDC関係のヘッダ
MFCコントロール共通のヘッダ
配列型情報管理関係のヘッダ
ListBoxコントロール.
ウィンドウ管理.
HWND GetSafeHwnd(void) const
[取得] ウィンドウハンドル取得.
virtual bool Insert(INDEX index, const TYP &t)
[追加] 要素一つ挿入
Definition: TnbVector.h:1070
virtual bool Set(INDEX index, const TYP &t)
[設定] 要素の設定.
Definition: TnbVector.h:1053
ビットマップイメージ管理クラス
bool Set(int cx, int cy, COLORREF color=CLR_INVALID)
[設定] イメージ設定.
bool IsNull(void) const
[確認] NULLチェック
virtual size_t GetSize(void) const
[取得] サイズ取得
Definition: TnbVector.h:368
virtual bool Remove(INDEX index)
[削除] 要素一つ削除.
Definition: TnbVector.h:397
virtual bool RemoveAll(void)
[削除] 空化
Definition: TnbVector.h:565
描画情報コントロール抽象クラス
@ OVERLAP
アイテム描画上に描画
bool EndItemPaint(void)
[処理] 描画終了.
void DrawBackground(HDC dc, const RECT &rect, LPARAM lParam=0)
[描画] 背景表示
void DrawText(HDC dc, const RECT &rect, LPCTSTR lpsz, LPARAM lParam=0)
[描画] 文字列描画
void DrawSelectMark(HDC dc, const RECT &rect, bool isActive, LPARAM lParam=0)
[描画] 選択マーク表示
ESelectMarkType GetSelectMarkType(void) const
[取得] セレクトマークタイプ
void DrawDisabledStyle(HDC dc, const RECT &rect, LPARAM lParam=0)
[描画] 無効状態描画.
void AllReset(void)
[設定] 全設定リセット.
void DrawFocusMark(HDC dc, const RECT &rect, LPARAM lParam=0)
[描画] フォーカスマーク表示
void BeginItemPaint(void)
[処理] 描画開始.
void DrawBackColor(HDC dc, const RECT &rect)
[描画] 背景色描画
void SetDefaultTextDrawer(CWnd *pWnd)
[設定] テキスト描画情報設定.
ビットマップデバイスコンテキストクラス
bool Draw(HDC dc, int x=0, int y=0) const
[処理] イメージ描画.
描画情報ListBoxコントロール
virtual LPARAM CalcDrawParam(DWORD_PTR itemNo) const
[取得] 描画用パラメータ計算.
CDrawingListBox(void)
コンストラクタ
int DeleteString(UINT index)
[削除] 一行削除.
int AddDrawing(const IDrawable &draw)
[追加] 描画情報一行追加.
int InsertString(int index, LPCTSTR lpszItem)
[追加] 文字列一行挿入.
~CDrawingListBox(void)
デストラクタ
int InsertDrawing(int index, const IDrawable &draw)
[追加] 描画情報一行挿入.
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
[通知] オーナードロー処理.
void ResetContent(void)
[削除] 全描画情報削除.
virtual void MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
オーナードロー時.
const IDrawable * GetDrawing(int index) const
[取得] 描画情報取得.
int ResetDrawing(int index, const IDrawable &draw)
[追加] 描画情報変更.
virtual void PreSubclassWindow(void)
[通知] subclassing/unsubclassing functions.
int DeleteDrawing(UINT index)
[削除] 一行削除.
virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
[通知] for processing Windows messages.
int AddString(LPCTSTR lpszItem)
[追加] 文字列一行追加.
int ToInt(LPCSTR lpsz, int iBase=10)
[変換] INT変換(ASCII/SJIS用).
Definition: TnbStrLib.h:367
TNB Library
Definition: TnbDoxyTitle.txt:2
bool IsInRange(INDEX index) const
[確認] INDEXの有効確認.
描画情報インターフェース
Definition: TnbDrawable.h:37
virtual bool GetSize(SIZE &_size) const =0
[取得] サイズ取得.
virtual IDrawable * Clone(void) const =0
[作成] クローン作成.
virtual void DrawEx(HDC dc, int x, int y, LPARAM lParam) const
[描画] 描画.
Definition: TnbDrawable.h:83