TNB Library
TnbMfcAbstractCustomListCtrl.h
[詳解]
1#pragma once
11#include "TnbMfcCommon.h"
12
13
14
15//TNB Library
16namespace TNB {
17namespace MFC {
18
19
20
38{
39 DEFSUPER(CListCtrl);
40public:
41
43 CAbstractCustomListCtrl(void) : _super(), m_isReportView(false)
44 , m_countOfSelected(0), m_itemOfSelected(-1), m_eraseBkGndOffset(2)
45 {
46 }
47
54 int GetSelectedItem(void) const
55 {
56 return _super::GetNextItem(-1, LVNI_ALL | LVNI_SELECTED);
57 }
58
64 void SetSelectedItem(int item)
65 {
66 if ( item >= 0 )
67 {
68 _super::SetItemState(item, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
69 _super::EnsureVisible(item, false);
70 }
71 else
72 {
73 int i = GetSelectedItem();
74 if ( i >= 0 )
75 {
76 _super::SetItemState(i, 0, LVIS_SELECTED | LVIS_FOCUSED);
77 }
78 }
79 }
80
85 void SetEraseBkGndOffset(int f = 2)
86 {
87 m_eraseBkGndOffset = f;
88 }
89
90protected:
91
94 {
95 HDC dc;
96 CRect rect;
97 DWORD itemNo;
98 DWORD subItemNo;
99 UINT state;
122 COLORREF textColor;
123 COLORREF backColor;
124 };
125
135 virtual void OnBeginItemPaint(HDC dc) = 0;
136
143 virtual void OnEndItemPaint(HDC dc) = 0;
144
153 virtual LRESULT OnItemPainting(TDrawParam* pParam) = 0;
154
160 virtual void OnItemPainted(const TDrawParam* pParam) = 0;
161
167 virtual void OnSelectItemChanged(int item) { }
168
175 virtual void OnDrawBackground(HDC dc, const RECT& rect)
176 {
177 ::FillRect(dc, &rect, ::GetSysColorBrush(COLOR_WINDOW));
178 }
179
192 virtual BOOL OnChildNotify(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* _pResult)
193 {
194 if ( message == WM_NOTIFY )
195 {
196 LPNMHDR lpNmHdr = reinterpret_cast<LPNMHDR>(lParam);
197 if ( lpNmHdr->hwndFrom == m_hWnd)
198 {
199 LPNMLISTVIEW lpLv = reinterpret_cast<LPNMLISTVIEW>(lParam);
200 if ( lpLv->hdr.code == NM_CUSTOMDRAW )
201 {
202 LPNMLVCUSTOMDRAW lpLvCd = reinterpret_cast<LPNMLVCUSTOMDRAW>(lParam);
203 *_pResult = m_OnCustomDraw(lpLvCd);
204 return TRUE;
205 }
206 else if ( lpLv->hdr.code == LVN_ITEMCHANGED )
207 {
208 SetTimer(TIMERID_CHANGED, 50, NULL);
209 }
210 }
211 }
212 return _super::OnChildNotify(message, wParam, lParam, _pResult);
213 }
214
225 virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
226 {
227 switch ( message )
228 {
229 case WM_ERASEBKGND:
230 {
231 CHeaderCtrl* H = GetHeaderCtrl();
232 if ( H !=NULL )
233 {
234 HDC hdc = reinterpret_cast<HDC>(wParam);
235 CRect clientRect;
236 _super::GetClientRect(clientRect);
237 CRect rect = clientRect;
238 int headerHeight = 0;
239 CRect rc;
240 H->GetClientRect(&rc);
241 headerHeight = rc.Height();
242 rect.top += headerHeight;
243 if ( _super::GetItemCount() != 0 )
244 {
245 CRect rc;
246 _super::GetItemRect(_super::GetTopIndex(), &rc, LVIR_BOUNDS);
247 OnDrawBackground(hdc, CRect(0, headerHeight, rect.Width(), rc.top));
248 int y = _super::GetItemCount() - _super::GetTopIndex();
249// rect.top += y * rc.Height() + 1 + 1;
250 rect.top += y * rc.Height() + m_eraseBkGndOffset;
251 //
252 if ( _super::GetItemRect (0, rc, LVIR_BOUNDS) )
253 {
254 if ( rc.right < clientRect.right)
255 {
256 rc.left = rc.right;
257 rc.right = clientRect.right;
258 rc.top = clientRect.top;
259 rc.bottom = rect.top;
260 OnDrawBackground(hdc, rc);
261 }
262 }
263 }
264 OnDrawBackground(hdc, rect);
265 }
266 return 0;
267 }
268 break;
269 case WM_SETFOCUS:
270 case WM_KILLFOCUS:
271 Invalidate();
272 break;
273 case WM_VSCROLL:
274 case WM_MOUSEWHEEL:
275 {
276 SetRedraw(FALSE);
277 LRESULT r = _super::WindowProc(message, wParam, lParam);
278 SetRedraw(TRUE);
279 return r;
280 }
281 break;
282 case WM_TIMER:
283 if ( wParam == TIMERID_CHANGED )
284 {
285 KillTimer(TIMERID_CHANGED);
286 int count = _super::GetSelectedCount();
287 int item = _super::GetNextItem(-1, LVNI_ALL | LVNI_SELECTED);
288 if ( m_countOfSelected != count || m_itemOfSelected != item )
289 {
290 m_countOfSelected = count;
291 m_itemOfSelected = item;
293 }
294 }
295 break;
296 case WM_DESTROY:
297 KillTimer(TIMERID_CHANGED);
298 break;
299 }
300 return _super::WindowProc(message, wParam, lParam);
301 }
302
308 virtual void PreSubclassWindow(void)
309 {
310 ASSERT0( (_super::GetStyle() & LVS_OWNERDRAWFIXED) == 0, "AbstractCustomListCtrl", "Ownerデータは OFFにしてください。" );
311 _super::PreSubclassWindow();
312 _super::SetExtendedStyle(LVS_EX_FULLROWSELECT);
313 }
314
315private:
316 enum { TIMERID_CHANGED = 100 };
318 LRESULT m_OnItemPaintSub(LPNMLVCUSTOMDRAW P, int subItemNo)
319 {
320 TDrawParam pa;
321 pa.dc = P->nmcd.hdc;
322 pa.itemNo = ToDword(P->nmcd.dwItemSpec);
323 pa.subItemNo = subItemNo;
324 pa.state = P->nmcd.uItemState;
325 pa.textColor = P->clrText;
326 pa.backColor = P->clrTextBk;
327 _super::GetSubItemRect(pa.itemNo, subItemNo, LVIR_BOUNDS, pa.rect);
328 if ( m_isReportView && subItemNo == 0 )
329 {
330 pa.rect.right = pa.rect.left + _super::GetColumnWidth(subItemNo);
331 }
332 if ( m_isReportView && (pa.rect.right < 0 || pa.rect.left > m_clientWidth) )
333 {
334 // クライアント内にかかっていない
335 return CDRF_SKIPDEFAULT;
336 }
337 if ( (_super::GetItemState(pa.itemNo, LVIS_SELECTED) & LVIS_SELECTED) != 0 )
338 {
339 pa.state |= CDIS_SELECTED;
340 }
341 else
342 {
343 pa.state &= ~CDIS_SELECTED;
344 }
345 LRESULT r = OnItemPainting(&pa);
346 P->clrText = pa.textColor;
347 P->clrTextBk = pa.backColor;
348 return r;
349 }
351 LRESULT m_OnCustomDraw(LPNMLVCUSTOMDRAW P)
352 {
353 switch ( P->nmcd.dwDrawStage )
354 {
355 case CDDS_PREPAINT:
356 m_isReportView = (GetStyle() & LVS_TYPEMASK) == LVS_REPORT;
357 m_clientWidth = 0x8000;
358 if ( m_isReportView )
359 {
360 CRect rc;
361 GetClientRect(rc);
362 m_clientWidth = rc.right;
363 }
364 OnBeginItemPaint(P->nmcd.hdc);
365 return CDRF_NOTIFYITEMDRAW | CDRF_NOTIFYPOSTPAINT;
366 case CDDS_POSTPAINT:
367 OnEndItemPaint(P->nmcd.hdc) ;
368 return CDRF_DODEFAULT;
369 case CDDS_ITEMPOSTPAINT:
370 {
371 TDrawParam pa;
372 pa.dc = P->nmcd.hdc;
373 pa.itemNo = ToDword(P->nmcd.dwItemSpec);
374 pa.subItemNo = 0;
375 pa.state = P->nmcd.uItemState;
376 pa.textColor = P->clrText;
377 pa.backColor = P->clrTextBk;
378 _super::GetItemRect(pa.itemNo, pa.rect, LVIR_BOUNDS);
379 OnItemPainted(&pa);
380 }
381 break;
382 case CDDS_ITEMPREPAINT:
383 if ( ! m_isReportView )
384 {
385 return m_OnItemPaintSub(P, 0) | CDRF_NOTIFYPOSTPAINT;
386 }
387 return CDRF_NOTIFYSUBITEMDRAW | CDRF_NOTIFYPOSTPAINT;
388 case CDDS_ITEMPREPAINT | CDDS_SUBITEM:
389 return m_OnItemPaintSub(P, P->iSubItem);
390 }
391 return CDRF_DODEFAULT;
392 }
393
394 bool m_isReportView;
395 int m_countOfSelected;
396 int m_itemOfSelected;
397 LONG m_clientWidth;
398 int m_eraseBkGndOffset;
399
400#ifdef _TnbDOXYGEN //Document作成用シンボル
401 public:
406 int GetItemCount() const;
447 int GetNextItem(int nItem, int nFlags) const;
463 int GetNextSelectedItem(POSITION& _pos) const;
468 int GetTopIndex() const;
473 int GetCountPerPage() const;
482 BOOL SetItemState(int nItem, UINT nState, UINT nMask);
489 UINT GetItemState(int nItem, UINT nMask) const;
494 UINT GetSelectedCount() const;
495// CString GetItemText(int nItem, int nSubItem) const;
496// int GetItemText(int nItem, int nSubItem, LPTSTR lpszText, int nLen) const;
497// BOOL SetItemText(int nItem, int nSubItem, LPCTSTR lpszText);
498// BOOL SetItemData(int nItem, DWORD dwData);
499// DWORD GetItemData(int nItem) const;
505 {
513 };
514#endif
515};
516
517
518
519}; // MFC
520}; // TNB
MFCコントロール共通のヘッダ
リストコントロール.
DWORD GetStyle(void) const
[取得] ウィンドウスタイル取得.
カスタム描画抽象ListCtrlコントロール
virtual void OnItemPainted(const TDrawParam *pParam)=0
[通知] アイテム描画終わり.
int GetItemCount() const
[取得] アイテム数取得
int GetTopIndex() const
[取得] 一番上表示のアイテム
int GetCountPerPage() const
[取得] 垂直方向に表示されるアイテム数
UINT GetSelectedCount() const
[取得] 選択アイテム数取得.
POSITION GetFirstSelectedItemPosition() const
[取得] 選択アイテム検索.
int GetNextItem(int nItem, int nFlags) const
[取得] アイテム検索
virtual void OnEndItemPaint(HDC dc)=0
[通知] 描画終了.
BOOL SetItemState(int nItem, UINT nState, UINT nMask)
[設定] アイテム状態設定
void SetEraseBkGndOffset(int f=2)
[設定] BGクリア調整値設定.
virtual BOOL OnChildNotify(UINT message, WPARAM wParam, LPARAM lParam, LRESULT *_pResult)
[通知] for notifications from parent
CAbstractCustomListCtrl(void)
コンストラクタ
void SetSelectedItem(int item)
[設定] 選択アイテム設定.
virtual void PreSubclassWindow(void)
[通知] subclassing/unsubclassing functions.
virtual void OnSelectItemChanged(int item)
[通知] 選択変更通知.
virtual LRESULT OnItemPainting(TDrawParam *pParam)=0
[通知] アイテム描画.
virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
[通知] for processing Windows messages.
int GetSelectedItem(void) const
[取得] 選択アイテム取得.
int GetNextSelectedItem(POSITION &_pos) const
[取得] 選択アイテム検索.
UINT GetItemState(int nItem, UINT nMask) const
[取得] アイテム状態取得
virtual void OnBeginItemPaint(HDC dc)=0
[通知] 描画開始.
@ LVIS_STATEIMAGEMASK
The item's state image index is retrieved by a mask.
@ LVIS_CUT
The item is marked for a cut-and-paste operation.
@ LVIS_FOCUSED
The item has the focus, so it is surrounded by a standard focus rectangle. Although more than one ite...
@ LVIS_SELECTED
The item is selected. The appearance of a selected item depends on whether it has the focus and also ...
@ LVIS_OVERLAYMASK
The item's overlay image index is retrieved by a mask.
@ LVIS_DROPHILITED
The item is highlighted as a drag-and-drop target.
virtual void OnDrawBackground(HDC dc, const RECT &rect)
[通知] 背景描画通知.
DWORD ToDword(LPCSTR lpsz, int iBase=10)
[変換] INT変換(ASCII/SJIS用).
Definition: TnbStrLib.h:395
TNB Library
Definition: TnbDoxyTitle.txt:2