// ---------------------------------------------
0 x; O/ i0 n, ^; ]) R6 A// ListCtrlEx.cpp
8 |' v$ S5 e- P. R v// ---------------------------------------------
$ x3 B7 {. x/ e1 \6 o// ListCtrlEx.cpp : 实现文件: l5 }8 _1 ] T" R
// #include "stdafx.h"' ~6 g& I% r/ T& S; j" Q& |
#include "CreateNewClass.h"6 I+ w% n( F: T6 J, k( f5 ?
#include "ListCtrlEx.h"
+ p- A# p5 M% o' n#include ".\listctrlex.h" + Z' o$ s+ e3 F4 M) I* t$ s
// CListCtrlEx IMPLEMENT_DYNAMIC(CListCtrlEx, CListCtrl)
) k* [9 y1 w* }5 d6 g! u+ ~+ b* H2 HCListCtrlEx::CListCtrlEx()
1 B4 j" Q/ K: ~6 F3 t* z{
$ T- g: o1 x* J' |' f // 用于调用基本的方法7 |1 i; @( Y3 H. J' u! g8 w# B/ f$ C/ ?
pThis = (CListCtrl*)this;
0 S) l5 D A' \$ o# Z // 默认的选中框颜色
$ ]& B7 z* e7 k( s: U) S m_clrHighLight = 0xF1DACC; 9 O' k0 T3 i- ]* c
m_clrHighLightEdge = 0xC56A31;
" C9 l( M* T- B5 g& d} CListCtrlEx::~CListCtrlEx()
% ?7 i* t9 t1 i9 B, e# L{ } x& ~8 X* Z3 Q5 I" U) j5 j
BEGIN_MESSAGE_MAP(CListCtrlEx, CListCtrl); `& o/ @# t6 Z& s8 e% B
ON_WM_DESTROY()
/ Z1 E" [. G" q3 L0 C: a( o ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnNMCustomdraw)% \- O2 @2 B! \8 R% F1 B- ]8 }
END_MESSAGE_MAP() // CListCtrlEx 消息处理程序
8 T" n( }+ o7 T G" W" P& @COLORREF CListCtrlEx::SetItemTextColor(int nItem,COLORREF clrText)
: b: j' X) S; ^) j2 O" ?) ?3 q{6 A9 N$ O+ Y4 U9 V" x, Z8 N
COLORREF clr = GetItemTextColor(nItem);
& b: Y" m5 F2 N' t3 D ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);2 \+ o; |& Q5 T/ r) i
pData->clrItemTextColor = clrText;
/ A3 \0 b0 _- ?+ |! K- N return clr;
. h# T& t4 \0 q m+ }9 Q} COLORREF CListCtrlEx::GetItemTextColor(int nItem) const
4 f' R5 f, _9 H" \- _1 l# Y{
, X4 a+ v' E- h6 D$ ~1 s6 T0 \ ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);5 p) b7 e5 L1 V# G z; B
return pData->clrItemTextColor;
% L$ u5 y' L% L0 Y& L} COLORREF CListCtrlEx::SetItemBackColor(int nItem, COLORREF clrBack)3 m f- c, T( Q3 T/ Q+ A
{5 H: x- X. ~2 G$ M
COLORREF clr = GetItemBackColor(nItem);
i0 q3 D9 G0 Q5 ^, a ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);
4 j( _/ C) c- l2 }9 G. e+ q pData->clrItemBackColor = clrBack;. m q6 c' W3 D4 P3 {
return clr;+ ^7 s; ~% O/ n% f
} COLORREF CListCtrlEx::GetItemBackColor(int nItem) const* \9 q9 X! {# M! b0 N
{9 G9 C2 P: C4 e% {
ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);
( K- f' J! ?1 e! j6 W+ j* D7 | return pData->clrItemBackColor;+ h+ `. \5 u# K
}9 g# r4 r, n! R4 J* J* ?6 n" F
DWORD_PTR CListCtrlEx::GetItemData (int nItem) const
7 Y0 ~) T& V9 _( p# w- n{" I r: U n) }& s7 l
if (nItem >= GetItemCount())
' k6 M6 j& o# { l {
1 D3 U% {; \* ?3 C6 ^) t return 0;
: ?- V8 `$ l( W% s }
4 b4 _: G) [* K- u8 h2 ] CListCtrl* pThis = (CListCtrl*)this;1 V- }# P% ?# e% p5 b5 k* [
DWORD_PTR Data = pThis->GetItemData(nItem);
- b# z3 W' [' ~- q# M return ((ItemDataEx*)Data)->SrclParam;8 I2 u& N) L; L6 }2 S( B
}; w1 S2 ~. e6 O2 o. @- Q
BOOL CListCtrlEx::SetItemData (int nItem, DWORD_PTR dwData)0 O# S+ D- K0 O1 k) |
{7 }! ]! O G, w: @
if (nItem >= GetItemCount())
- n9 d9 p" K2 H# w# e' ^) k {/ m+ L4 u. A, v
return 0;; {, H, h' ~3 {* T: s! {. H0 M
}, m$ P2 l+ _- O: U: X( Y( T
CListCtrl* pThis = (CListCtrl*)this;& v1 L5 R9 l$ m( p5 R# p0 D
DWORD_PTR Data = pThis->GetItemData(nItem);
; u2 [4 i3 U: }4 a ASSERT(Data);
( |1 b) T+ n) O d( w ((ItemDataEx*)Data)->SrclParam = dwData;
L# [ ?4 r- a5 F) @& k return 1;
9 ^2 M2 o! m' R" _}' B4 p ^! y1 \: X( B" q
int CListCtrlEx::InsertItem (const LVITEM* pItem)) h# m7 p, j: ?3 x
{. r$ b/ X2 i) i7 ]% z
int nReturn = pThis->InsertItem(pItem);3 [# @1 Z; \7 {9 a4 O+ f- @
ItemDataEx *pData = new ItemDataEx;
2 s0 l. \) p' m" E3 M! u // 设置默认颜色
5 b8 n3 k% o, L+ M* q7 p pData->clrItemBackColor = 0xFFFFFF;//pThis->GetTextBkColor();
: C) T- |" R' ?. e* v, `" J pData->clrItemTextColor = 0x000000;//pThis->GetTextColor(); W; c L/ ^, z8 y8 q
pData->SrclParam = pItem->lParam;4 j, P' a+ n# u
pThis->SetItemData(pItem->iItem,(DWORD_PTR)pData);; g9 h3 q5 J O7 f& c1 C( q
return nReturn;3 |$ |6 c8 U, T8 y. F
}
& r- U0 f9 _4 W& }6 D0 E1 vint CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem) I+ }: R8 t9 g$ s) p
{$ W2 l; q6 f! {: h0 g. F# M
LVITEM Item;0 e" @6 ?+ g+ j. n
memset(&Item,0,sizeof(LVITEM));
( _9 @' R$ B- c Item.pszText = new TCHAR[strlen(lpszItem)+1];% M& Q7 T* o+ ~4 @# \/ l. ?/ Y
strcpy(Item.pszText,lpszItem);; O3 ^- K2 I2 v" Z1 N
Item.iItem = nItem;! S, f& f) G' a4 v. m* a5 e
Item.lParam = 0;
e" @/ a2 W: U/ J2 A( _( J2 d Item.mask = LVIF_TEXT |LVIF_PARAM;0 X( S' z/ p- `" |5 y% w6 r
BOOL bReturn = InsertItem(&Item);6 Q( e. B l- ^+ k# Z$ \4 I/ f
delete Item.pszText;9 g/ N {" a9 ^9 X% a. A2 J
return bReturn;
6 Z& k" Y: Q$ c- S9 h}
& B2 l+ S, E5 n) j) c- Z+ Nint CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem, int nImage)( ?% X+ f' T6 _9 x0 s/ K
{
; ]& J; }+ P1 E0 E LVITEM Item;- G2 @+ f" N5 p7 b, C6 G; U
memset(&Item,0,sizeof(LVITEM));
3 K3 }& s$ s- I, | Item.pszText = new TCHAR[strlen(lpszItem)+1];9 o/ x: E. ]( r2 i! ]
strcpy(Item.pszText,lpszItem);, \" U( i, w* T9 c0 K
Item.iItem = nItem;
+ G6 q* E8 `$ z& F Item.lParam = 0;
: y% U4 p6 y U, n+ A& ~ O( @- Q Item.iImage = nImage;
4 T6 k; o: b/ S8 ~/ M( r {4 I3 o Item.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE;- N3 d: ^* q+ J$ B+ r
BOOL bReturn = InsertItem(&Item);' [% P* i% ]' A0 M# Q: O, k
delete Item.pszText;' F0 W$ _$ D9 k; R
return bReturn;: g1 ~3 c7 f/ D) ]+ |& h0 C8 X4 L& {
}- l" p$ Y& x9 R6 ^- @/ y! t) c
int CListCtrlEx::InsertItem (UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam)3 ?" _& h+ Y0 X: y
{& Q; B2 Y& p9 ]; ]/ n
LVITEM Item;4 I8 ]9 m0 k5 M4 r
memset(&Item,0,sizeof(LVITEM));0 C' J3 k( i. M0 }% {9 g4 ?$ `
Item.pszText = new TCHAR[strlen(lpszItem)+1];4 ^1 b4 S4 l- u, D
strcpy(Item.pszText,lpszItem);3 r5 @1 W4 k {, J
Item.state = nState;
# h+ e1 k! A7 l' |! Z3 e2 k Item.stateMask = nStateMask;
3 \0 t, l9 q6 ?) u1 o [ Item.iItem = nItem;2 _+ \; T) E1 j0 m4 {
Item.lParam = lParam;
. q: h! A" Z: c4 h& Z Item.iImage = nImage;/ B7 C S6 X$ f3 S+ w/ p- C
Item.mask = nMask;/ }' K& e( }7 _. w* y
BOOL bReturn = InsertItem(&Item);# f6 m) v! q, V# u5 Q6 r
delete Item.pszText;
' z/ }. p3 J8 G3 ?7 t- p+ q return bReturn;
* G! G! U1 H8 N5 |1 F+ M}4 |, v) u6 N, _ a' j; f
BOOL CListCtrlEx:eleteItem (int nItem)
{+ s: ]2 y- e% y* n1 Y( x8 L{! |; `& ]2 z* I5 O. t$ G# d. _
void* pDataEx = (void*)pThis->GetItemData(nItem);9 w* \) a6 c$ \0 {
if(pDataEx != NULL); Y' D0 o( V- J$ D+ F
{- X% m' l$ _# k2 t* C$ Y8 x
delete pDataEx;
8 u+ a1 ^6 W6 t0 g$ I }
) v2 R9 |: I& ~% L; t. i+ ~5 r return pThis->DeleteItem(nItem);
5 I* Y0 h$ G; K& {' R- s- L}
! }, q* F3 S" ?BOOL CListCtrlEx:eleteAllItems ()/ H% [: F, e( C0 x, ]2 Z
{* W) T! C- ~1 R, \
int nCount = GetItemCount();2 J; P* F5 I; t1 O
for (int i = 0;i<nCount;i++)
" p* i& S% |3 o7 c! Z- s5 `3 x {
W7 y/ Y# C% a if(!DeleteItem(0))3 y# d! Z# R- B; x; {
{
p( Q# ~' U+ q, r6 Y% t- G return 0;
4 H4 t7 \6 `/ m0 v7 S }
! X9 |. _2 m5 u7 o1 J) \& J }
- E9 a, G$ n0 i, p6 t return 1;9 e9 u# J3 r2 Q' V e2 u0 Q
}
' M$ l- }( K- c, |- ]: ZBOOL CListCtrlEx::GetItem(LVITEM* pItem)4 ~% R% a4 q. M' \/ g# i' I! ?
{
) n# n6 x4 ~# r9 s D BOOL bReturn = pThis->GetItem(pItem);
; t; R# Z8 h# n' J) \; ] if (pItem->lParam)
3 @9 @3 I/ n( y& z {) c- {1 m w' u$ C+ M
pItem->lParam = ((ItemDataEx*)pItem->lParam)->SrclParam;; r {7 E4 e5 c4 E! w
} return bReturn;
2 \+ p- M' H9 N8 H}5 w( Y3 ~/ s, Q3 W' o6 r4 ?
void CListCtrlEx::OnDestroy()
6 h% R% R Z# M" j3 h. O9 i{
, i5 T! |3 b# |+ b8 T* s // 关键是清除申请的内存空间* X" b. l n/ G
DeleteAllItems();: g; t/ E" v, X4 K
CListCtrl::OnDestroy();3 v0 ^/ ]. I. p; k
} BOOL CListCtrlEx::SwitchItem(int nItem1, int nItem2)
5 \3 X. B5 ~( |7 c" Y{0 n+ q2 r% m t6 U* U
if (nItem1 == nItem2 || nItem1 >= GetItemCount() || nItem2 >= GetItemCount() || nItem1 < 0 || nItem2 < 0)
" |. k# U1 ~5 J/ P {
, [$ B# o9 W* z7 L; E4 u; W5 N return 0;
/ T# Z8 |8 y( E0 L9 B+ w# z3 W! R }" c% Y" a* w& @# x5 V
LVITEM ItemData1,ItemData2;- a: C& `. d, g9 j& I/ P/ d
int ItemLength1 = pThis->GetItemText(nItem1,0).GetLength();* a( b2 Q" z4 w2 q2 C2 G4 d! V8 t8 u
int ItemLength2 = pThis->GetItemText(nItem2,0).GetLength();% N0 N* K, b) N
ItemData1.pszText = new TCHAR[ItemLength1+1];
; V3 y9 z' m' _% {) f1 N& e8 A ItemData2.pszText = new TCHAR[ItemLength2+1];
G# o' R* x0 Q/ c ItemData1.cchTextMax = ItemLength1+1;3 Z7 R2 ?4 X. I! K+ K6 S' c
ItemData2.cchTextMax = ItemLength2+1;- C; K; X9 Z/ [: y. Z& R
ItemData1.iItem = nItem1;- X; N; [ ^9 d$ l; t+ I
ItemData2.iItem = nItem2;
! ^$ j! S8 h+ m* f( {6 x: y! n ItemData1.iSubItem = ItemData2.iSubItem = 0;
3 t6 r3 i, {& W D) C ItemData1.mask = ItemData2.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE|LVIF_STATE; pThis->GetItem(&ItemData1);4 G- o Y8 r- T; s0 @1 Z2 C
pThis->GetItem(&ItemData2);5 t" I4 n( A( A2 f$ ?
// 交换数据
- H6 X7 \! v( S) \ b ItemData1.iItem = nItem2;- c( \6 G% U* Z, L# v9 W) o
ItemData2.iItem = nItem1;
& v9 Y' t0 m& `8 _ pThis->SetItem(&ItemData1);
- q3 |( ~0 k* t2 K pThis->SetItem(&ItemData2); delete ItemData1.pszText;
% V6 r5 m) J" b2 p0 R' h delete ItemData2.pszText;2 V1 P1 r) U8 ~/ U- \0 a
// 得到报表模式的列数
. v# b3 i0 E/ ]- w9 _& U. r2 K7 X int nColCount = pThis->GetHeaderCtrl()->GetItemCount();
. x( x! A1 I; V! I- } CString strBuffer;1 E# I8 I8 G- w7 G% s8 C: c: g8 j2 l
for (int iSubItem = 1;iSubItem < nColCount;iSubItem++)
2 |# F2 H: K$ V: ~6 V0 q {
/ H9 a# A9 U* w strBuffer = pThis->GetItemText(nItem1,iSubItem);
# o) l8 G* P, I6 O& @; T pThis->SetItemText(nItem1,iSubItem,pThis->GetItemText(nItem2,iSubItem));- ~ J# B$ W W! @9 c( ?
pThis->SetItemText(nItem2,iSubItem,strBuffer);4 t- ?. q8 C$ Y" B0 z& m
}* D, m3 \' b) j9 X% H: W
// 把原来的选中状态也转移过去
+ F1 M& C$ I# b) P& A( v4 o // 不知道为什么这个属性没有体现在LVITEM结构中……
& X8 o. ]* ~; X- ?$ ~0 w1 u, I% O/ C bool bItemSelected1 = GetItemState(nItem1,LVIS_SELECTED) == LVIS_SELECTED;
6 l: F# m$ `; z1 S( r9 K bool bItemSelected2 = GetItemState(nItem2,LVIS_SELECTED) == LVIS_SELECTED; SetItemState(nItem1,bItemSelected2?LVIS_SELECTED:0,LVIS_SELECTED);- t/ a# a3 d# {) |5 {! _6 ^
SetItemState(nItem2,bItemSelected1?LVIS_SELECTED:0,LVIS_SELECTED);
( S4 F8 O5 E6 d$ {9 W5 L5 g, [; ? return 1;
' p. L4 c- o! W) o* |} BOOL CListCtrlEx:reCreateWindow(CREATESTRUCT& cs)7 h& j. D; [' W7 {# _) x
{4 A7 U$ |4 }$ |4 ?0 M
// 本类报表模式专用
# n0 k' w1 ~$ {+ d cs.style |= LVS_REPORT ;
5 W- ?$ ?3 w+ r6 z4 i/ J. O/ l! Q // cs.dwExStyle |= LVS_EX_FULLROWSELECT;1 ~( U8 ?' B' Z" I% ?) @
return CListCtrl:reCreateWindow(cs);& l2 B8 d- ]8 ?8 ]
} + R9 ]& p: C" K7 T
COLORREF CListCtrlEx::SetHighLightColor(COLORREF clrHighLight)2 F r+ N" ^& L8 f; P! `: o3 z
{/ D* c6 H' i# X3 ^1 R7 t# x
COLORREF clr = GetHighLightColor();
' p& n( }* B% Y, e9 \ m_clrHighLight = clrHighLight;
9 D+ N* @# u1 O! \. ] return clr;2 i$ n6 A6 b# S
} COLORREF CListCtrlEx::SetHighLightEdgeColor(COLORREF clrHighLightEdge)
" Q3 H+ c/ G% Q+ p1 n: i{
4 v, o$ M" H! y COLORREF clr = GetHighLightEdgeColor();
' \- x! [5 h1 e; N7 @4 \4 h m_clrHighLightEdge = clrHighLightEdge;
2 l- H9 O8 L8 U6 c return clr;
' V, I) a- `: q0 n! {4 I} |