// ---------------------------------------------
! ]1 L9 [5 v. r; P S9 Q) f% `// ListCtrlEx.cpp1 j0 p1 v0 m: L V0 C7 ]
// ---------------------------------------------- P( k' T5 z c ~& J6 K
// ListCtrlEx.cpp : 实现文件9 z b( r: r. t. F1 M1 y
// #include "stdafx.h"3 U0 g0 y$ L9 D& k
#include "CreateNewClass.h"
! `4 G: s9 T ]) v% p& L- Y9 W% ~#include "ListCtrlEx.h"
( h! x5 H+ @0 d6 S#include ".\listctrlex.h" 0 M! s% W$ t6 G; o# t8 |
// CListCtrlEx IMPLEMENT_DYNAMIC(CListCtrlEx, CListCtrl), n0 j% F& L* H& K& \2 i
CListCtrlEx::CListCtrlEx()
8 f$ `7 B0 N1 i! z2 e{
5 B7 c/ P; A7 r( R- H+ E // 用于调用基本的方法$ K; _2 X$ z$ P2 N6 L
pThis = (CListCtrl*)this;9 J. k* z5 r8 j4 \4 H8 N: a
// 默认的选中框颜色
1 Q/ K. w. S: f: z" I m_clrHighLight = 0xF1DACC;
3 ^3 B% w/ B p; t, z4 W4 e3 V m_clrHighLightEdge = 0xC56A31;
$ L! J+ T- |+ e$ j N6 b} CListCtrlEx::~CListCtrlEx()
0 _& R+ h* b5 ]7 j2 c% m. [{ } ! p c b" o4 W, }
BEGIN_MESSAGE_MAP(CListCtrlEx, CListCtrl)
0 S) P- N3 l3 O ON_WM_DESTROY()
# H8 e2 W' h. f$ D+ \ ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnNMCustomdraw)
- m2 ^" F9 o) u' F9 `6 B) M+ b3 mEND_MESSAGE_MAP() // CListCtrlEx 消息处理程序 ! `# I' B/ \* ^: ~ F& {1 Q% O
COLORREF CListCtrlEx::SetItemTextColor(int nItem,COLORREF clrText)
/ E: H3 S$ @- f! _! {7 k3 i{
6 p0 k4 o2 t5 {/ p7 ` COLORREF clr = GetItemTextColor(nItem);
9 P( i" v9 M/ y' r) [ ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);
7 _# B+ ?- R; N4 @, a3 a& i' _/ A pData->clrItemTextColor = clrText;# o. [# J% l6 M; a0 D8 }6 S. G6 q
return clr;
6 b. b% {" U$ Q} COLORREF CListCtrlEx::GetItemTextColor(int nItem) const' j4 r, u; |. V7 F. x* q
{
7 V/ i2 ?9 x* X9 n ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);- y# _# e5 C3 |
return pData->clrItemTextColor;
& @6 v, L8 R3 i8 G; m' E} COLORREF CListCtrlEx::SetItemBackColor(int nItem, COLORREF clrBack)# @3 y: _/ V- m" z7 E7 S+ s" @
{) P5 Q; ]! ]* s/ ^
COLORREF clr = GetItemBackColor(nItem);
7 K) s) |* d$ f6 { ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);
' U- ]# a, O: f6 g3 [6 } pData->clrItemBackColor = clrBack;1 C7 \0 u/ o) S
return clr;0 C$ U1 P) c+ X0 I3 Z
} COLORREF CListCtrlEx::GetItemBackColor(int nItem) const8 Z6 _; ]1 y: ^7 F
{
& {+ o6 O/ U% i$ ?, F H( Z ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);
3 S+ G6 p7 |3 \* g return pData->clrItemBackColor;
/ t# ~) O' e8 G0 g0 |}
7 y; ]. P' H8 h4 L! C* W1 NDWORD_PTR CListCtrlEx::GetItemData (int nItem) const
# L) [# h! g1 {( r" \, @{
! c# k; H2 n4 T if (nItem >= GetItemCount())
" `9 L- l, a# W0 Q {
9 W9 d# p8 r! R9 ~* N U return 0;4 w1 U2 A- e. G) X% g. D- T% @+ h3 ]: Q
}
" {8 n1 Y' E+ y9 t* z# k$ f CListCtrl* pThis = (CListCtrl*)this;5 E9 T6 t* g0 X. S2 ~
DWORD_PTR Data = pThis->GetItemData(nItem);
" l' b* b+ f2 M/ H$ F return ((ItemDataEx*)Data)->SrclParam;
3 L, C( V% a+ C}7 b2 d0 e& Z5 U$ x
BOOL CListCtrlEx::SetItemData (int nItem, DWORD_PTR dwData)8 ]" A# z1 k$ X3 j
{3 Z3 c6 w7 U8 P% [- }: K
if (nItem >= GetItemCount())- @9 I+ i/ O6 E! _7 ?% n
{$ _' a% k( P) V' T% g& @2 m F0 ^0 X
return 0;
/ G; ^. h) b8 i+ a6 f: P }$ |' F4 z6 D) z) u
CListCtrl* pThis = (CListCtrl*)this;
( G1 g4 w/ D0 X DWORD_PTR Data = pThis->GetItemData(nItem);
. J$ ?& K9 L9 |7 ]# {5 E ASSERT(Data);
: w, q; m" }$ e. _; | ((ItemDataEx*)Data)->SrclParam = dwData;
: }; P# W( }3 `6 v) O return 1;$ t# _' K a% _& _' _9 G# h
}
/ Q) f" C+ z9 Y& T/ e7 oint CListCtrlEx::InsertItem (const LVITEM* pItem)
' P$ E/ q4 F5 d{, p1 j. o2 v- p; i9 k5 ?
int nReturn = pThis->InsertItem(pItem);
4 _! P9 l5 M! }, k ItemDataEx *pData = new ItemDataEx;, M/ a/ E6 o% }7 ]4 G4 |
// 设置默认颜色
J' P* m5 Y; k. T3 _2 b pData->clrItemBackColor = 0xFFFFFF;//pThis->GetTextBkColor();
8 b) F" @# _9 X) z/ \$ Q pData->clrItemTextColor = 0x000000;//pThis->GetTextColor();
" }1 T# x) j( f% w2 j& ^# J* b pData->SrclParam = pItem->lParam;
& ^% R" D+ i, } pThis->SetItemData(pItem->iItem,(DWORD_PTR)pData);
" y: [1 N( q6 a6 u l return nReturn;
0 K0 |6 R1 \. ~- T# Z}
" Y: Z- M9 i: Y8 Pint CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem)5 |% r0 j1 G O
{
) x9 D9 W7 {5 e( h0 u2 }) F) g+ \ LVITEM Item;. t, d5 M8 ?/ e& L3 O( Y
memset(&Item,0,sizeof(LVITEM));7 x. x% ?3 y) p$ u; M' M% {
Item.pszText = new TCHAR[strlen(lpszItem)+1];
0 }5 }0 A8 B9 R; T2 M" u! K strcpy(Item.pszText,lpszItem);5 j6 y: n! B* K4 n3 Q" |' l$ t
Item.iItem = nItem;
& v- E0 b2 o) p* G# G2 W Y Item.lParam = 0;* C( X1 z r6 p) m
Item.mask = LVIF_TEXT |LVIF_PARAM;7 ?' }0 O8 V5 ?
BOOL bReturn = InsertItem(&Item);! b/ E$ @: f: ~' Y+ `$ Q: w
delete Item.pszText;. \8 q* x+ C2 s2 ]
return bReturn;
. ?/ ~4 P/ n& T$ ]}
$ s n0 k' w+ Y- ], R) `( y! cint CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem, int nImage)2 i. Q( W- v3 U% a) [2 u2 i4 X
{
$ |/ {5 l) _* j2 j8 y LVITEM Item;
# ^/ b2 y" X/ v8 R memset(&Item,0,sizeof(LVITEM));8 e& c) q$ t0 W* @9 g% R
Item.pszText = new TCHAR[strlen(lpszItem)+1];
, ^/ y2 _& v0 N$ v strcpy(Item.pszText,lpszItem);
1 \; ^0 A& e/ y Item.iItem = nItem;
# w/ @; W' w0 K Item.lParam = 0;$ F7 `0 r. t2 o+ s4 z& v
Item.iImage = nImage;
% j) k! U& T3 J# ?1 k% _ n Item.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE;* T$ `& m: m1 t0 l( o
BOOL bReturn = InsertItem(&Item);% u) @% H8 }, }
delete Item.pszText;
4 r% q, R# y8 s3 h$ m7 g5 u return bReturn;
' M. Y Q e' Z) K' @+ W6 {}% d2 y) w3 [+ |5 w; ]$ Z
int CListCtrlEx::InsertItem (UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam)& i) d* z8 b! p
{) F; Q* c/ \" {- c3 b4 d4 F2 G, z" P
LVITEM Item;
4 |1 r# X4 V; `9 K) r9 f: o# @1 p memset(&Item,0,sizeof(LVITEM));
1 s( @% |: V1 R" [# k+ P" H Item.pszText = new TCHAR[strlen(lpszItem)+1];& T! W! h7 X6 S5 s% x* c
strcpy(Item.pszText,lpszItem);9 s" C2 u: X m" X9 p- n3 k" o
Item.state = nState;8 Y- D1 {- U" s
Item.stateMask = nStateMask;& g5 Q# s, T, o3 f# |( E
Item.iItem = nItem;1 [6 {" q4 S) I8 }8 b+ v% Q
Item.lParam = lParam;
2 A/ M1 U% F: v( s& `6 l7 z( C: L Item.iImage = nImage;
8 M8 b0 `$ v9 J, s7 f! l. | Item.mask = nMask;/ o# I- p" ]( U/ e' ^* z# |
BOOL bReturn = InsertItem(&Item);
+ M9 y% f' c2 w* [2 H2 L delete Item.pszText;" k+ R# \ K m; d/ ^/ `
return bReturn;
6 Z5 h- I8 m3 E3 O% ~}
( c- s6 c* k" P: u& W" LBOOL CListCtrlEx:eleteItem (int nItem)% \: d+ J, K( S H- T$ _- N; y
{, y2 n" \8 l# @0 F* O
void* pDataEx = (void*)pThis->GetItemData(nItem);; Q& S* f! X; \
if(pDataEx != NULL), V$ b- W$ `/ K
{5 F1 t/ e/ o7 m* o( l
delete pDataEx;' `' ]$ w1 d5 o' x+ r1 \7 M
}
' y4 n4 _2 o4 x: M2 a0 D return pThis->DeleteItem(nItem);
) g% k. w2 ?4 b9 g6 E) W1 W8 j+ [; N}% p. `% o. N- D6 F* ^3 E. U
BOOL CListCtrlEx:eleteAllItems ()
; @/ G4 z, N2 b3 s& g% b4 P) x L, g{' E9 x5 O& o( a. j( c
int nCount = GetItemCount();. n& u/ J+ f& m4 O: S! w" ~
for (int i = 0;i<nCount;i++)
& p- H, V2 x; Z5 O2 c {6 a8 W% z! B! |- x8 j3 f
if(!DeleteItem(0))3 Z: V: b+ J+ K* V1 O
{
6 B0 H; g$ f: F8 B return 0;, W" \9 y, P5 N
}8 [8 ]- v5 ], m) F( S, n% X% k
}& n4 a. A' H5 u, J) p
return 1;
' _" f, t' N7 m7 s! C [}" v4 J) j- i( a5 `1 t# C
BOOL CListCtrlEx::GetItem(LVITEM* pItem)
+ z- r! q. ~- ~2 x' M& p' B{9 o( e1 R# |8 }7 ?' E3 s e. ^
BOOL bReturn = pThis->GetItem(pItem);, r/ w: o: z5 {! X2 K
if (pItem->lParam), ^% k) i# e3 d H4 P1 v
{# [" W; X: l: X
pItem->lParam = ((ItemDataEx*)pItem->lParam)->SrclParam;
$ B( { \! E" R+ |7 ` } return bReturn;
9 \. v7 Y# Z0 r3 l: i}# c% j- H }( T2 O, S# r
void CListCtrlEx::OnDestroy()/ D8 }! h/ t: g9 }& S
{
/ Z% ?6 S4 B0 n/ R& Z* N! t // 关键是清除申请的内存空间
1 @( O1 ?' ^, |4 o- D DeleteAllItems();; T n X5 s4 p! m* e$ e1 v A8 \
CListCtrl::OnDestroy();
, q, z; p) H: @3 e& ?* h} BOOL CListCtrlEx::SwitchItem(int nItem1, int nItem2), |( R% m- U, B% ~' k
{2 ?( A. H# _" s& R6 U- Q
if (nItem1 == nItem2 || nItem1 >= GetItemCount() || nItem2 >= GetItemCount() || nItem1 < 0 || nItem2 < 0)
! w# m. W; K; T4 j {
% f' _2 N2 ?( n return 0;
! e- n2 o0 E3 ]2 W }
, h) A' S( h& {4 ]& b- y7 k8 } LVITEM ItemData1,ItemData2;6 m# n1 J+ p! M' w2 [
int ItemLength1 = pThis->GetItemText(nItem1,0).GetLength();' S V. r! E- V c3 k& s
int ItemLength2 = pThis->GetItemText(nItem2,0).GetLength();
) x( J1 K, T% N ItemData1.pszText = new TCHAR[ItemLength1+1];
6 J6 k1 x: y' X: l3 Q9 C ItemData2.pszText = new TCHAR[ItemLength2+1];
( j/ N& u" Q/ y$ S* s% d' n: ]1 Y ItemData1.cchTextMax = ItemLength1+1;5 L& U8 d1 H% w7 O! S3 p
ItemData2.cchTextMax = ItemLength2+1;) W9 t A9 Z6 y) I Z4 a
ItemData1.iItem = nItem1;$ p5 Z+ m" A+ y! p% Y- c9 R8 u
ItemData2.iItem = nItem2;; u4 {/ C; M3 N8 [
ItemData1.iSubItem = ItemData2.iSubItem = 0;7 e( O$ Z; e) a& N& w5 F5 y4 ^
ItemData1.mask = ItemData2.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE|LVIF_STATE; pThis->GetItem(&ItemData1);% y/ K+ B! M0 H1 L* l
pThis->GetItem(&ItemData2);
! `8 U& x# R4 d // 交换数据
' a3 |1 R3 _. b2 O ]5 K& m ItemData1.iItem = nItem2;( p1 j, _! b/ b' }" ^5 @
ItemData2.iItem = nItem1;
8 I4 G' d( Q- C/ v1 e pThis->SetItem(&ItemData1);; e h1 y+ F) o+ @
pThis->SetItem(&ItemData2); delete ItemData1.pszText;* M, K$ O0 t! s# e5 @7 S/ k
delete ItemData2.pszText;
$ J3 d1 x# c) f8 ?: u! a+ u // 得到报表模式的列数3 G5 \' _5 z# C9 ?3 @# G4 r
int nColCount = pThis->GetHeaderCtrl()->GetItemCount();
$ d. h9 i$ Z# P4 c" {* {; B0 j CString strBuffer;
" e+ I l9 _6 i$ S2 X1 i# M1 [, m for (int iSubItem = 1;iSubItem < nColCount;iSubItem++)
2 T1 S3 Q3 l( d; f2 ~- t0 @ {0 {' x! G2 u/ s' p6 ^4 a" j
strBuffer = pThis->GetItemText(nItem1,iSubItem);- Y' O1 A. F* a1 h$ `
pThis->SetItemText(nItem1,iSubItem,pThis->GetItemText(nItem2,iSubItem));
& g% ^; X4 O9 U1 i% B" A pThis->SetItemText(nItem2,iSubItem,strBuffer);
3 |% ]( Y8 x* d8 f5 m }
, Q% N, Z* A3 u4 [% [ // 把原来的选中状态也转移过去
3 f! |9 P' I0 M6 j- b3 _ // 不知道为什么这个属性没有体现在LVITEM结构中……
2 @1 W/ G2 U7 C. D bool bItemSelected1 = GetItemState(nItem1,LVIS_SELECTED) == LVIS_SELECTED;/ d/ m3 i7 {4 Z5 n
bool bItemSelected2 = GetItemState(nItem2,LVIS_SELECTED) == LVIS_SELECTED; SetItemState(nItem1,bItemSelected2?LVIS_SELECTED:0,LVIS_SELECTED);) P" f. |4 I8 r* q! F5 x5 p
SetItemState(nItem2,bItemSelected1?LVIS_SELECTED:0,LVIS_SELECTED);
- }; j( J$ `/ ]4 h return 1; w2 x: _. Z. G
} BOOL CListCtrlEx:reCreateWindow(CREATESTRUCT& cs)
( C- s* A' ?$ w{
5 Q) j: q8 G( E; J9 X) I // 本类报表模式专用$ o$ R+ d, d: |
cs.style |= LVS_REPORT ;5 b9 x. [4 x+ }9 O. J- }
// cs.dwExStyle |= LVS_EX_FULLROWSELECT;
1 W+ {% X' C3 n. | return CListCtrl:reCreateWindow(cs);
. r" K5 i2 C" D- N' i}
3 z$ F/ W2 j& e' ]+ g0 b- MCOLORREF CListCtrlEx::SetHighLightColor(COLORREF clrHighLight). @- w, K) @& r' @, J" m: W
{& U) k8 o: n9 C9 h
COLORREF clr = GetHighLightColor();
* L$ A( ^2 q% R ?. e5 F m_clrHighLight = clrHighLight;5 q( ^# k/ i c9 g! _
return clr;. x8 H, [9 K8 I Q6 W
} COLORREF CListCtrlEx::SetHighLightEdgeColor(COLORREF clrHighLightEdge)
0 z1 g; X* x. x/ h! ^{
5 n3 t/ D; `& d+ \ COLORREF clr = GetHighLightEdgeColor();3 Y) z0 Y9 t+ b$ K3 b/ h' `2 p
m_clrHighLightEdge = clrHighLightEdge;
' P) p' C6 b2 j# X, r( x, Z% g+ P+ C5 z return clr;
, k; z, @6 B F" X% D) _} |