// ---------------------------------------------! E; A' l6 a# A s/ h' b* C
// ListCtrlEx.cpp- P9 z" N) m7 E k4 ]
// ---------------------------------------------
/ ?2 _3 X$ r, i! l6 q* a6 g3 J// ListCtrlEx.cpp : 实现文件
" h) X! K2 T( I( {3 {: L4 s// #include "stdafx.h"
* I% r- ~' H6 k( s#include "CreateNewClass.h"' U8 P/ [* d3 d5 w. f& r
#include "ListCtrlEx.h"
2 Y' j. F1 F" f+ D' C. j2 r/ n#include ".\listctrlex.h" % J* N' r- ~$ x
// CListCtrlEx IMPLEMENT_DYNAMIC(CListCtrlEx, CListCtrl)1 l: W6 G5 T$ W5 d8 y
CListCtrlEx::CListCtrlEx()1 T4 B5 q8 Z# Y J5 Z
{0 ?+ i+ ]8 |9 P' |, I( I( E
// 用于调用基本的方法 L P a9 `3 K, R& ~* Z
pThis = (CListCtrl*)this;
+ Y; U* l2 U# p0 ` // 默认的选中框颜色2 ~7 U4 V/ r5 m2 n
m_clrHighLight = 0xF1DACC; , {( G5 [. W% @- o+ g/ t6 D
m_clrHighLightEdge = 0xC56A31;/ }, ?# @+ I- a V3 s
} CListCtrlEx::~CListCtrlEx()
& W+ |( E" e: A{ } % B2 x% F1 i( ^2 G* U4 F
BEGIN_MESSAGE_MAP(CListCtrlEx, CListCtrl)
. ~# \8 i0 C) q8 e ON_WM_DESTROY()+ k/ m% w$ d, k4 q0 {; U0 b
ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnNMCustomdraw)
% p5 X6 o. M! z+ A2 cEND_MESSAGE_MAP() // CListCtrlEx 消息处理程序 ! I- Z% p# l6 g
COLORREF CListCtrlEx::SetItemTextColor(int nItem,COLORREF clrText)$ z$ ^% ]' n2 g: {1 s' p
{" u: ?" T) m) p- Z+ e9 Q; T
COLORREF clr = GetItemTextColor(nItem);9 n6 h7 H! z; R6 ]5 K" f* _
ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);4 @/ p. r; Q" Z
pData->clrItemTextColor = clrText;
* ^4 b$ g' R7 N" q8 F7 R. u% @# L8 H return clr;0 F8 m8 \$ I$ q; u6 g! E) J
} COLORREF CListCtrlEx::GetItemTextColor(int nItem) const
& O0 F* }# v5 q$ F& f5 j$ u) D& [. c{' ?4 P- R, d# z7 Y! U( j8 f9 M
ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);9 ]% o; n3 W- }. o
return pData->clrItemTextColor;: B" a* n9 T, x
} COLORREF CListCtrlEx::SetItemBackColor(int nItem, COLORREF clrBack)
# q# X1 j/ [* S. v{
2 P" [/ Y+ a* }4 c, k' K9 I COLORREF clr = GetItemBackColor(nItem);# w7 G. @; a- Q* z# Q- p% @3 \
ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);
! ]7 a7 ]6 I3 K. p) `. P pData->clrItemBackColor = clrBack;
, @8 n j5 u7 b; y# j$ [ return clr;( x1 D& X5 {# y1 y/ G
} COLORREF CListCtrlEx::GetItemBackColor(int nItem) const6 I! ?% F7 e0 c; e7 b
{
+ z1 q+ g# I, F* W& g8 }1 v ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);
) E. I K: V9 F. |- I9 Y return pData->clrItemBackColor; ^* j1 W& i1 ?& y6 R
}
2 S' y f6 u m' O7 r# n e1 b' D; hDWORD_PTR CListCtrlEx::GetItemData (int nItem) const4 f& h6 e$ e* G
{' N3 l, k: \% P% O6 }7 ~; M; I9 @
if (nItem >= GetItemCount())
# e6 m! o$ r* h/ y$ ^) w {
2 [4 X2 z0 d* i4 ?9 D) k$ B6 p+ k return 0;
9 j1 Z5 V# y' a6 C' l }- E& W( ?0 J: Q u: J
CListCtrl* pThis = (CListCtrl*)this;
+ H" \1 J. f w6 o$ f DWORD_PTR Data = pThis->GetItemData(nItem);
& G; g h, [8 v: z) h1 V+ _1 G, E return ((ItemDataEx*)Data)->SrclParam;
' K3 _" i/ {" e. U}
$ i) W6 X Q/ ~BOOL CListCtrlEx::SetItemData (int nItem, DWORD_PTR dwData)) h# a" C% Y9 |' v$ h
{
" v$ E3 G- x; ], j5 m& g* e3 v if (nItem >= GetItemCount())
- P. `0 e' r @/ q5 L5 @7 { {
. r. M* W ~* r8 c$ v return 0;
+ R! v) f1 g* {" M }
$ }7 ^# x4 B1 W CListCtrl* pThis = (CListCtrl*)this;
7 a, g8 C0 Q. _0 m. ?) L! n: k; k DWORD_PTR Data = pThis->GetItemData(nItem);/ j9 G8 ?; c- l z) o: e! B2 N
ASSERT(Data);- B- E" _; k! e7 k7 y, e
((ItemDataEx*)Data)->SrclParam = dwData;$ |& o' _6 V& S6 n
return 1;
5 |; |' j5 G6 B) N}$ H# ~+ Z* M! t, R' P6 H
int CListCtrlEx::InsertItem (const LVITEM* pItem)5 m6 _2 R" ~6 n4 y, l. p
{
# T' z( C" Z, i5 c/ }0 p int nReturn = pThis->InsertItem(pItem);
8 F- o/ Q0 j% j& T. O ItemDataEx *pData = new ItemDataEx;
; ^* a6 p/ b& a" n8 {. ` // 设置默认颜色& p7 r0 e2 l6 Q, H, |( M" L
pData->clrItemBackColor = 0xFFFFFF;//pThis->GetTextBkColor();
4 g2 m) F6 C9 d8 M6 k+ H pData->clrItemTextColor = 0x000000;//pThis->GetTextColor();
. }9 W. e k+ A* [1 f2 X) z pData->SrclParam = pItem->lParam;- r& W, Z& C4 X. Y( J- V; h( E+ p- B
pThis->SetItemData(pItem->iItem,(DWORD_PTR)pData);
4 z8 j2 y& j/ ^/ q/ j3 S$ M return nReturn;$ f8 @6 G; v9 P: g, O# T. r
}
; \$ R* a, l" ?2 r9 T& yint CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem)
8 `" z& l. V0 l* o! d" ?, @{ Q8 P" ]2 E# E* D, n! Z9 F7 w& {
LVITEM Item;- ]8 o3 z% B' U& j1 u2 W. D( ]
memset(&Item,0,sizeof(LVITEM));
9 B9 ]+ u' P/ B' l3 O" N Item.pszText = new TCHAR[strlen(lpszItem)+1];
' O0 [* [6 }8 ]$ ]2 M strcpy(Item.pszText,lpszItem);- P6 l0 W1 M4 Z; m0 t: k N2 K1 s j
Item.iItem = nItem;: F, Z4 r8 F) A0 m
Item.lParam = 0;
! V; P" c/ g- v$ P0 E5 Q Item.mask = LVIF_TEXT |LVIF_PARAM;
5 E, b$ e' v! F BOOL bReturn = InsertItem(&Item);% q2 b8 i9 [1 F# g) p- J
delete Item.pszText;3 T8 R8 y a& @
return bReturn;
" W8 X8 u" D m x7 {}! }0 ]; i {9 N
int CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem, int nImage)8 Z/ s% ]* q, r! l
{
. k5 Q" F. S2 u. i4 F LVITEM Item;
# [2 h3 Q, _6 a9 @ V# n$ U memset(&Item,0,sizeof(LVITEM));
+ x( o0 K9 O' c$ K) A5 s+ q; y! t Item.pszText = new TCHAR[strlen(lpszItem)+1];5 D5 m2 }" P6 v. ]* `
strcpy(Item.pszText,lpszItem);
3 X) Q, a6 B( e+ n* ?( h* l Item.iItem = nItem;/ A% Q1 c6 ]! E/ t5 f5 V
Item.lParam = 0;" N" q! r: w5 K) H
Item.iImage = nImage;
) E; n ?: x' J6 d; J1 ` Item.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE;
. S$ q$ r5 m5 Y7 Z: U' \ BOOL bReturn = InsertItem(&Item);
/ V; G1 Y" a Q1 P" l) m* t& P delete Item.pszText;7 z; p1 S. l. p3 ~4 T
return bReturn;
# ^! x8 T& g' L- P9 C} T* Z( A% G, y& |7 e6 B
int CListCtrlEx::InsertItem (UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam)8 x8 W- ~2 W, N
{$ A1 N j: s& L- r9 W$ E* C
LVITEM Item;
2 r) i3 U! v# ]/ e memset(&Item,0,sizeof(LVITEM));. Y5 ?/ M. O" |0 ^
Item.pszText = new TCHAR[strlen(lpszItem)+1];
9 |8 U1 X; o: r7 r s strcpy(Item.pszText,lpszItem);
& \+ }/ D" G2 |; S" F% g Item.state = nState;6 H% _) }; s, c( p- d% L
Item.stateMask = nStateMask;6 r9 U( t9 p6 d, z
Item.iItem = nItem;
/ l8 }' Z+ k3 J" k" V Item.lParam = lParam;
; I7 i- w( Q: w5 u1 j, i5 b# E- \ Item.iImage = nImage;
( r) p H( ^ j- ^" U. ~1 o Item.mask = nMask;
) s- j% ? ?2 r7 ^3 U% Y4 { BOOL bReturn = InsertItem(&Item);; J! B1 G: N# O Q1 D* w% K- _- ]
delete Item.pszText;
/ l7 H/ g* C( _6 ~- e( T5 p0 C return bReturn;
+ Q" @! K5 ~/ d- L) X! t9 i}
5 D1 r1 S+ U; \% HBOOL CListCtrlEx:eleteItem (int nItem)# g6 S' R; O- l' r1 a
{
$ m* `0 L% G) t# r( u void* pDataEx = (void*)pThis->GetItemData(nItem);+ f L4 N# z4 I8 x, a9 `5 O
if(pDataEx != NULL)' W4 @# \4 m* @9 j
{
$ ?( z& P) `: c Y, Z delete pDataEx;
2 d! ~4 f% f3 C/ B }
5 [/ Q0 @) O- J/ A! [% U return pThis->DeleteItem(nItem);
% S6 n6 }7 v% }; F}1 I* _/ m6 y" p. w$ t8 _9 F
BOOL CListCtrlEx:eleteAllItems ()2 B# a( \3 T) x( S3 J
{
$ B* L/ y; d0 n% q0 h. F( l int nCount = GetItemCount();
5 D" G. T, I$ i2 h% {$ ^ for (int i = 0;i<nCount;i++)# \ k/ ?* B% }5 R& A$ s4 }
{0 R2 U0 w0 B/ \' w: O
if(!DeleteItem(0))
/ L, l* r" w# z: b6 O" A! s2 S( ~ {
( q+ k2 p7 T3 I6 a8 K; {* i return 0;" z8 I* {$ D: P# j2 Q: l
}" J) m# U" u/ m+ }
}
/ t& J- ^- \2 e& W# \0 X. D return 1;
5 b) D+ K: k) y}
4 ~+ e0 w i, ]3 ]) A% {) W' Y3 bBOOL CListCtrlEx::GetItem(LVITEM* pItem)
2 E9 Z7 Y" h9 U' _) B+ N2 R5 }{
, `7 r v2 ^. D- Z% [% M! d BOOL bReturn = pThis->GetItem(pItem);
& W/ E! c: ?7 e* ^. F if (pItem->lParam)
$ t* m3 G! M; \ {
2 @2 c4 G2 G, s8 U. x' K pItem->lParam = ((ItemDataEx*)pItem->lParam)->SrclParam;, j0 z5 x7 B7 {2 Y$ l
} return bReturn;8 J; {9 y7 w& L) g( R# k
}
0 [1 A+ o, m& F! ?0 xvoid CListCtrlEx::OnDestroy()
- r9 _. d' u# r' M1 J8 R{2 T( b' G" j* @
// 关键是清除申请的内存空间
, e2 n5 b" n# _$ y( J P1 Y# S# r) w DeleteAllItems();+ N' G! t3 {9 k1 y
CListCtrl::OnDestroy();
% x# \( N m9 H& K( E} BOOL CListCtrlEx::SwitchItem(int nItem1, int nItem2)/ M+ E1 y- C* H" M3 S
{
) F( T' Z N% x& j- W9 h if (nItem1 == nItem2 || nItem1 >= GetItemCount() || nItem2 >= GetItemCount() || nItem1 < 0 || nItem2 < 0)
$ p9 o/ W1 ^+ Z {
! i& u$ W9 |1 S6 Z6 N; O4 d3 j' ~1 T return 0;
# U; ?+ |5 N# I% m# C; ^ }
5 n& V6 h" _ l: e k* D3 m LVITEM ItemData1,ItemData2;
- ^3 z- O( {1 P int ItemLength1 = pThis->GetItemText(nItem1,0).GetLength();5 @- o+ c u! |+ r2 w- p; K
int ItemLength2 = pThis->GetItemText(nItem2,0).GetLength();7 s# a; V1 i( B* n* Q8 g) U
ItemData1.pszText = new TCHAR[ItemLength1+1];
; U. U# }7 j# f8 T7 ^ ItemData2.pszText = new TCHAR[ItemLength2+1];
9 {9 ^, f: W% c ItemData1.cchTextMax = ItemLength1+1;
6 T5 { S" M" u; a' f3 C$ e ItemData2.cchTextMax = ItemLength2+1;# T( S& M$ P6 l9 _& L3 } t
ItemData1.iItem = nItem1;; {' y( t% j! T7 V
ItemData2.iItem = nItem2;
$ \; \" O9 N# I; {( f# I ItemData1.iSubItem = ItemData2.iSubItem = 0;+ ^! B- [/ _6 A
ItemData1.mask = ItemData2.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE|LVIF_STATE; pThis->GetItem(&ItemData1);6 D+ ~5 _* ?% n) `4 B- B( W% J2 y
pThis->GetItem(&ItemData2);
0 N- m G4 k: n5 G6 v // 交换数据
2 R! v- u8 b. |( K4 R( z ItemData1.iItem = nItem2;
; O# }- j6 I3 J0 h/ A& H9 T ItemData2.iItem = nItem1;. t- J" C$ A/ r* `3 S8 j L' ]
pThis->SetItem(&ItemData1);
+ j6 m* q6 G* A* x, R$ J pThis->SetItem(&ItemData2); delete ItemData1.pszText;
! o, I4 B. `3 T: _8 E3 o+ c delete ItemData2.pszText;
5 {' o9 H* B6 h4 y5 T+ x- R // 得到报表模式的列数3 X; ^0 V6 M7 @- b
int nColCount = pThis->GetHeaderCtrl()->GetItemCount();
6 L! Q" O, e) S5 J9 K CString strBuffer;5 x" n1 u* J% m! B
for (int iSubItem = 1;iSubItem < nColCount;iSubItem++)' h O! C6 i' A3 N8 Q0 Q& o4 N, o
{
, g( f% |# g" k) j strBuffer = pThis->GetItemText(nItem1,iSubItem);
: {) N t$ j2 o pThis->SetItemText(nItem1,iSubItem,pThis->GetItemText(nItem2,iSubItem));
# u* {% ]0 Z8 Y. g* C. H pThis->SetItemText(nItem2,iSubItem,strBuffer);
9 d7 w3 H* h: u: p/ o }
3 m' w1 P0 {$ }$ H // 把原来的选中状态也转移过去
! Y$ {" l, ?# z8 e1 ?. e // 不知道为什么这个属性没有体现在LVITEM结构中……
6 ^9 p3 G# m( D N bool bItemSelected1 = GetItemState(nItem1,LVIS_SELECTED) == LVIS_SELECTED;) v1 G# l0 I7 K7 G5 @
bool bItemSelected2 = GetItemState(nItem2,LVIS_SELECTED) == LVIS_SELECTED; SetItemState(nItem1,bItemSelected2?LVIS_SELECTED:0,LVIS_SELECTED);& W4 | o8 ]4 S) P/ E1 o
SetItemState(nItem2,bItemSelected1?LVIS_SELECTED:0,LVIS_SELECTED);
( \6 s2 l% c1 h# K return 1;
2 v+ R( G2 J7 Y% q5 k- r} BOOL CListCtrlEx:reCreateWindow(CREATESTRUCT& cs)( [% H& M. w u
{' m, `0 D0 Y: T
// 本类报表模式专用
7 i/ L, R, s x ?! C cs.style |= LVS_REPORT ;
2 y% v% }, I, L+ B9 G: i* e // cs.dwExStyle |= LVS_EX_FULLROWSELECT;
0 y! H0 Z |7 n return CListCtrl:reCreateWindow(cs);7 `6 i8 _1 X* P9 N8 f
} ; r. `% r+ ]2 a5 o% G
COLORREF CListCtrlEx::SetHighLightColor(COLORREF clrHighLight)
5 Z: @, ]* I( d# n/ i3 R( o* v{
1 Q/ Z. }9 Q1 B+ g# g COLORREF clr = GetHighLightColor();
5 a- a0 H3 ^ ? m_clrHighLight = clrHighLight;4 m2 c; K: ^( m3 w2 x( \$ M
return clr;& @( p( r1 w$ D: y
} COLORREF CListCtrlEx::SetHighLightEdgeColor(COLORREF clrHighLightEdge)2 T8 z: E" S. p8 Z# T4 @
{
# Z" x( I$ \% O% r1 @/ w COLORREF clr = GetHighLightEdgeColor();
( W% N! j. n# V4 a. ? S* ~: h5 s2 H m_clrHighLightEdge = clrHighLightEdge;9 _0 |8 u' K k- o( t& Z
return clr;
; J$ L1 ^! B/ o} |