// ---------------------------------------------" E- Z7 m7 H; ^: ?5 m! w( v
// ListCtrlEx.cpp0 z3 r* y0 B7 o# Q
// ---------------------------------------------
3 C6 d2 _5 |. D) g0 J/ Q// ListCtrlEx.cpp : 实现文件: a1 c/ d7 K0 z. |3 ?8 R# V: e
// #include "stdafx.h"/ @9 y4 J4 I" b) R. u7 Q8 j
#include "CreateNewClass.h"9 U" k: j% x+ x# k$ A+ P: r
#include "ListCtrlEx.h"3 I( f3 `6 N% j! @& b% y. [
#include ".\listctrlex.h" 5 g2 W; u) m# u& ~9 n
// CListCtrlEx IMPLEMENT_DYNAMIC(CListCtrlEx, CListCtrl)$ u3 i; }' A% \: t4 A
CListCtrlEx::CListCtrlEx()
$ [( Q& w7 z! A# J# k{4 ^ I- {; y1 R. C' g( Q. k: H: b
// 用于调用基本的方法
2 d8 v* \9 o, b3 I$ l7 {' |3 h pThis = (CListCtrl*)this;% s: T, Y! y4 L% g8 W) d0 E' a
// 默认的选中框颜色
% n; ~! `3 c$ `( S, ^6 V" R m_clrHighLight = 0xF1DACC; ! G% \5 O: u2 _
m_clrHighLightEdge = 0xC56A31;
, p! |/ O5 J2 L) k5 r} CListCtrlEx::~CListCtrlEx()
, n6 P9 s& V6 O8 X# C: j4 [ l/ T( d# |$ y{ } ' A6 d, S6 Z8 t$ s: ?
BEGIN_MESSAGE_MAP(CListCtrlEx, CListCtrl)
/ l' w3 {3 P% N& E0 i$ @ ON_WM_DESTROY()
: O1 [" i. o5 V ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnNMCustomdraw)* ]7 F7 z0 X/ k6 [; e0 G" e
END_MESSAGE_MAP() // CListCtrlEx 消息处理程序
/ O/ d( x, e5 @( v7 m2 TCOLORREF CListCtrlEx::SetItemTextColor(int nItem,COLORREF clrText)
2 U- W! y; w5 f8 k; K; H0 H{
1 l5 w' E( ^" y# S; a$ O0 Z1 w COLORREF clr = GetItemTextColor(nItem);
! [( K% c/ ?: S2 f ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);
4 S2 F6 m! p+ ]8 W pData->clrItemTextColor = clrText;
; X0 I0 f. D$ [. V9 {9 t. Y return clr;1 Z' \" E# Q/ |3 }& c& |
} COLORREF CListCtrlEx::GetItemTextColor(int nItem) const. q& D, H, y7 K: e' ]; S
{
( [% R# w1 E' d0 D3 | ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);
: T( o- y2 ^. A, s; x9 q8 e0 T return pData->clrItemTextColor;
* ~( o0 K. q! \! ~. D$ x g* Q} COLORREF CListCtrlEx::SetItemBackColor(int nItem, COLORREF clrBack)* M$ ~' E7 b3 W6 P
{$ {0 g- G( F* D2 w+ b! k' e7 U
COLORREF clr = GetItemBackColor(nItem);
4 \- t' H- A" B. V! m+ t ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);7 N& F" ~) z ?. m2 }8 F2 h
pData->clrItemBackColor = clrBack;* p2 }7 e! N$ u4 r
return clr;$ \: n4 M" ], r$ H: m
} COLORREF CListCtrlEx::GetItemBackColor(int nItem) const
5 t; J! B6 O" m; S! {{
" X D/ u! r% S9 Y/ E( `* b1 n ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);0 |/ M& p2 A8 {' M1 O( t/ w
return pData->clrItemBackColor;% }( p- u# j: |0 c% B7 N
}, Z- V* i4 o }$ G" l8 y
DWORD_PTR CListCtrlEx::GetItemData (int nItem) const
" {" F- R) _8 D, c& A# l{
4 t$ h2 ~ s0 Z$ |2 G if (nItem >= GetItemCount())$ ]: j# E$ l% c1 }) K
{
; k/ r# a; x0 b- D7 O return 0;
6 I5 z7 |8 ?& x }5 q4 N& c) m+ \7 @
CListCtrl* pThis = (CListCtrl*)this;
, j! I8 ~3 z' o, N9 e DWORD_PTR Data = pThis->GetItemData(nItem);
- z6 F' l( X- _! P, U return ((ItemDataEx*)Data)->SrclParam;7 g6 _5 ~4 D) f4 ?; z% G
}. f$ K5 R; H, |9 e2 ?5 I2 w% q( n/ q
BOOL CListCtrlEx::SetItemData (int nItem, DWORD_PTR dwData)
5 c& u3 e9 Z! y ~! o{3 \+ z' X6 }; L7 a6 p
if (nItem >= GetItemCount())
- `- C2 H3 A4 A) A0 O2 ^7 R8 ~ {: f* y6 l# I% x
return 0;+ y2 J" Z4 h+ N/ s i
}% D4 E: }3 W) g R% J5 L% I3 K
CListCtrl* pThis = (CListCtrl*)this;4 F+ [2 F+ I5 }$ l% m0 T0 b1 r) P
DWORD_PTR Data = pThis->GetItemData(nItem); U% l1 b% O2 e, O$ I3 W
ASSERT(Data);
6 M/ ]& o: ~+ v1 a ((ItemDataEx*)Data)->SrclParam = dwData;
/ |+ Q, M: l' z; d0 s! r0 N return 1;: R3 K9 E% i4 F c8 Q
}
$ [' b& m1 z8 s3 p0 Eint CListCtrlEx::InsertItem (const LVITEM* pItem)
2 y0 e: {- z) r- _" p{
- ~/ f* y% |/ {1 {; _! L int nReturn = pThis->InsertItem(pItem);
! t9 b7 U2 h- c5 c0 c9 C+ e ItemDataEx *pData = new ItemDataEx;
) O: M7 T2 W( P: b2 C // 设置默认颜色
1 l6 \5 d/ \, \3 | J pData->clrItemBackColor = 0xFFFFFF;//pThis->GetTextBkColor();
; h* V+ A; ^" e pData->clrItemTextColor = 0x000000;//pThis->GetTextColor();" w8 |( c3 e% L* r& b5 Q5 v
pData->SrclParam = pItem->lParam;
' R7 V. I6 x8 R3 C8 h pThis->SetItemData(pItem->iItem,(DWORD_PTR)pData);0 E8 H2 B, i q& Q, \
return nReturn;
" t& e. E+ u6 l# M3 {}
! A y' `/ n8 q5 q6 e" p0 Zint CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem)
! a9 }8 ^9 {7 C& n% w1 m1 o- O{6 o: S8 K4 |/ k% l* b' m% w$ U
LVITEM Item;6 b1 y; t# e: b# h. ^$ U
memset(&Item,0,sizeof(LVITEM));% c# G% N9 T7 j* ]( N
Item.pszText = new TCHAR[strlen(lpszItem)+1];, f2 |2 w7 Q5 t9 S6 o' ^
strcpy(Item.pszText,lpszItem);
3 ]2 g; z; D9 i- k: C0 W Item.iItem = nItem;
4 Q) j( W1 x- X2 h o Item.lParam = 0;- F6 {) q' m1 x; i& V3 W$ ^# S
Item.mask = LVIF_TEXT |LVIF_PARAM;
8 F6 J* G1 T1 U+ J$ B* M BOOL bReturn = InsertItem(&Item);
. J- e: x! M w/ I1 e- r3 B! F delete Item.pszText;
' @! r$ e) a; d: R return bReturn;
7 j3 l5 Y. Y. Y7 B7 N}( T3 i: i+ J0 }% O0 X
int CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem, int nImage). ~, y) t) o! O
{
, {# k0 H7 ^; h+ c7 |9 q) g LVITEM Item;
0 g3 o0 G% J9 ]" F memset(&Item,0,sizeof(LVITEM));
1 N; d4 v' d$ s6 t- Y Item.pszText = new TCHAR[strlen(lpszItem)+1];
, ~4 P( e) [: ^' K7 n* ] strcpy(Item.pszText,lpszItem);
9 W8 W! j. i; }$ Z Item.iItem = nItem;* j8 e/ n! T" q1 u! Z( j8 ^
Item.lParam = 0;
6 r0 P9 @% W- P- x8 n; I Item.iImage = nImage;6 l8 Z! d2 U# h# i: d; k) M3 l
Item.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE;
+ t. v, o O; Q. Z1 ~ BOOL bReturn = InsertItem(&Item);7 S+ M5 U2 \3 c9 Z- N1 ~
delete Item.pszText;
: s5 w3 ~7 O# o; T4 X return bReturn;1 o7 y7 v# J5 M5 ?" f! o
}0 Y9 i& e; }, U% K' @- A* p3 e
int CListCtrlEx::InsertItem (UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam)) X7 r5 P6 H" Y. r) A. I! W9 r
{
8 ]* m V; S( X2 A LVITEM Item;
; a0 K' X! k: \5 L1 s3 R) | memset(&Item,0,sizeof(LVITEM));: \- C7 F: O" q6 o: z# r7 X4 n
Item.pszText = new TCHAR[strlen(lpszItem)+1];
4 d3 I2 b' b- a& c1 Q strcpy(Item.pszText,lpszItem);( |# f; L) M, D6 L! O% s
Item.state = nState;
* A( F8 ^- M0 ~7 R Item.stateMask = nStateMask;
! r! [6 H2 m' |5 c, | Item.iItem = nItem;* B1 D3 A( L: l9 a% y6 ^
Item.lParam = lParam;& H$ h7 l4 |' y' l& n5 ~
Item.iImage = nImage;1 r ^- e& b- [" P) \# p# Q& ?2 D# m
Item.mask = nMask;) a$ k7 e# p h& A& h3 Y$ Y
BOOL bReturn = InsertItem(&Item);& F: L, i- K! Y8 A7 F$ H) E
delete Item.pszText;+ ^% ~' H+ O1 ~) ~
return bReturn;) ~+ u: F% R5 M( l
}
3 u+ t% M+ N& r- m/ Q& |BOOL CListCtrlEx:eleteItem (int nItem)/ M+ r6 Z D, v7 K! H W h
{+ A- o2 O- y7 |1 t) K# w
void* pDataEx = (void*)pThis->GetItemData(nItem);7 b& [! I0 W8 ~: S4 r/ v9 b
if(pDataEx != NULL)
4 ^4 y. J/ e! L! D+ X {* \- J$ | H. p8 K1 I+ b
delete pDataEx;
3 y& p5 E! ^ p+ j2 {1 T }
. R4 J z2 M# J$ S K return pThis->DeleteItem(nItem);
% |6 ~7 n7 v4 U9 h$ L}9 }3 l! f! P2 \( r
BOOL CListCtrlEx:eleteAllItems ()
+ L& R4 C7 [& |{8 {/ y/ N; Y" X& {) h' k
int nCount = GetItemCount();( v7 a8 {) ` m* s
for (int i = 0;i<nCount;i++). o$ I' K1 R, s; W7 Z
{
0 R1 X: l7 S9 { if(!DeleteItem(0))
; Q. V9 `# y& } n3 [) w+ | {2 `. d7 I+ f ~0 P5 n! B
return 0;
. ` O! u2 @1 Z }2 @8 F; A9 l1 q, B0 J! i4 m
}
. h& g0 ]5 k0 |- f4 C' c; L return 1;
2 r7 Z6 S: S8 Z! _$ C}
9 \( |1 E4 m0 a/ A: vBOOL CListCtrlEx::GetItem(LVITEM* pItem); C8 S* H2 G7 G/ r3 `
{
; x0 d" u- }, J! u- S2 s' G BOOL bReturn = pThis->GetItem(pItem);
& X/ a7 h: {4 @0 x! P5 \& z if (pItem->lParam); _6 M5 P3 H+ b. L+ h# S) C
{5 S: F& H2 W, K+ Z
pItem->lParam = ((ItemDataEx*)pItem->lParam)->SrclParam;
: Q. Y6 o" z$ K } return bReturn;& |8 K" J! E$ r* I5 K Q
}( ]* K: j1 l+ O. E9 g6 e% L4 R
void CListCtrlEx::OnDestroy()
3 p# ^, m& c3 u{
3 b, Z. t$ f, {1 Z( l }: l0 W // 关键是清除申请的内存空间' \! I/ w: W d& u4 u( h2 X
DeleteAllItems();5 h" x: c* ~+ d9 H8 D" n" h9 P
CListCtrl::OnDestroy();
, b1 d6 h: I- \- N} BOOL CListCtrlEx::SwitchItem(int nItem1, int nItem2)# c7 c- ~& i7 q1 [& u. O F
{
/ e; S( Q- S3 S- l8 U3 v if (nItem1 == nItem2 || nItem1 >= GetItemCount() || nItem2 >= GetItemCount() || nItem1 < 0 || nItem2 < 0)( y& C" c5 i. H+ k2 ?
{
8 k1 h2 q4 m2 t return 0;
/ D) t/ x# I! U3 W }" H0 _& A& L. n) @& `6 q7 ^
LVITEM ItemData1,ItemData2;
; \/ u; }& l. q; W& O( J4 ~ int ItemLength1 = pThis->GetItemText(nItem1,0).GetLength();" @, ^6 I% Z0 p; y, q' Y% s2 R
int ItemLength2 = pThis->GetItemText(nItem2,0).GetLength();
' O( v& w6 j; B( t" G3 b! ` ItemData1.pszText = new TCHAR[ItemLength1+1];
; P# p% t j% T4 i ItemData2.pszText = new TCHAR[ItemLength2+1];' A5 \8 M& p. d( F/ m ?+ v9 Q
ItemData1.cchTextMax = ItemLength1+1;
! N' F( I2 _! L" m/ |+ v# P9 a ItemData2.cchTextMax = ItemLength2+1;
3 a' o7 N4 h. _; v7 D9 s# }9 u3 T, L ItemData1.iItem = nItem1;6 z2 E4 a2 p# H1 A; X
ItemData2.iItem = nItem2;
+ A2 D; z8 L* C5 Q$ I1 d5 i' @4 d5 ] ItemData1.iSubItem = ItemData2.iSubItem = 0;) X5 b: [3 K7 O. s& K& _3 L/ E
ItemData1.mask = ItemData2.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE|LVIF_STATE; pThis->GetItem(&ItemData1);
' ^: I" c6 o% i: l; W j6 O pThis->GetItem(&ItemData2);% Y: U; j/ }3 _ T) ]
// 交换数据$ S5 C7 {( S& i0 l, K" t- ]# w' u9 N
ItemData1.iItem = nItem2;
6 C) P6 _2 N, ~0 ?% e6 [( S ItemData2.iItem = nItem1;
0 V3 a8 Z" a* d' \- G pThis->SetItem(&ItemData1);
2 K( V1 K8 [' T/ b3 c2 ` pThis->SetItem(&ItemData2); delete ItemData1.pszText;
& o* `9 A/ K3 t, Z9 @. I delete ItemData2.pszText;
6 H& h9 e6 O- F1 `! ^ p // 得到报表模式的列数
/ Q6 b. a9 i4 h int nColCount = pThis->GetHeaderCtrl()->GetItemCount();- w; R% E! }, ?, l7 p/ X. a
CString strBuffer;
- U D" n) d* P4 p* U for (int iSubItem = 1;iSubItem < nColCount;iSubItem++)
, E' f2 Y$ N7 u8 K) o, G1 G# L {
5 n: N& }+ E1 I% r- z; Q1 O2 r strBuffer = pThis->GetItemText(nItem1,iSubItem);
* v/ Y" H8 X% F- U! n pThis->SetItemText(nItem1,iSubItem,pThis->GetItemText(nItem2,iSubItem));, G; B! _" D: H7 }/ ~
pThis->SetItemText(nItem2,iSubItem,strBuffer);
+ e/ g, R2 N* J. k* Q* n }3 Q. B0 _8 f K+ Q+ ^
// 把原来的选中状态也转移过去7 A7 Z+ }7 T' X" ^: w; p
// 不知道为什么这个属性没有体现在LVITEM结构中……
5 y/ F( b3 F3 E bool bItemSelected1 = GetItemState(nItem1,LVIS_SELECTED) == LVIS_SELECTED;
- F, k- ]! j: n. h bool bItemSelected2 = GetItemState(nItem2,LVIS_SELECTED) == LVIS_SELECTED; SetItemState(nItem1,bItemSelected2?LVIS_SELECTED:0,LVIS_SELECTED);7 W# v) G ^. b6 G/ J. g- [# ^
SetItemState(nItem2,bItemSelected1?LVIS_SELECTED:0,LVIS_SELECTED); K! c5 d- E3 k0 N
return 1;& z. B8 U# h1 G3 f3 j/ ^
} BOOL CListCtrlEx:reCreateWindow(CREATESTRUCT& cs)2 W7 Y0 ^3 l8 I: o3 A% x
{
" w; m9 p( K# V4 p // 本类报表模式专用4 x: \3 u' n( D/ G$ Y8 J
cs.style |= LVS_REPORT ;+ |% m: y3 q4 {3 i8 A
// cs.dwExStyle |= LVS_EX_FULLROWSELECT;
F; l: r0 p! E* M6 ]# V, t return CListCtrl:reCreateWindow(cs);
' g8 u7 H3 d; K- y} ' E. P. _# L }: [( O0 P
COLORREF CListCtrlEx::SetHighLightColor(COLORREF clrHighLight)1 C$ j# C& _' {/ P, u; s3 i
{4 q( T+ L0 f6 ]6 c6 _9 n, T
COLORREF clr = GetHighLightColor();1 \+ T' \/ X' [
m_clrHighLight = clrHighLight;# U7 q& ` F, h0 X, @2 S$ p
return clr;
& F( x R4 x' X+ F' t- `5 V& V v} COLORREF CListCtrlEx::SetHighLightEdgeColor(COLORREF clrHighLightEdge)
8 {9 k3 F6 p6 w1 S{& I0 q+ k" w; f7 [: M$ ^! j
COLORREF clr = GetHighLightEdgeColor();* L( g4 A" u& z c/ N
m_clrHighLightEdge = clrHighLightEdge;# @& ]3 b- P' X% ?
return clr;8 m* R, J8 c6 Z/ s, V
} |