// ---------------------------------------------& O/ I- I0 s5 s. L4 [8 d6 q+ j
// ListCtrlEx.cpp: G$ _& Z x2 d
// ---------------------------------------------' L4 m% y2 t! ^' e4 s
// ListCtrlEx.cpp : 实现文件
: d# T; R$ T: T* ~/ g A// #include "stdafx.h"
1 f' t, X$ k& Y& p9 `0 y9 y/ c7 a#include "CreateNewClass.h"
8 |9 O( v& ~7 p+ V' O9 H3 {#include "ListCtrlEx.h"- G% t. v, P0 v. y
#include ".\listctrlex.h" : c: _( t$ G+ [. L
// CListCtrlEx IMPLEMENT_DYNAMIC(CListCtrlEx, CListCtrl)
6 v/ { ]) C$ M9 nCListCtrlEx::CListCtrlEx()) r5 E4 c/ \7 j7 u7 l8 o- h
{( W/ w% A$ Z [# Z+ m( d$ w/ t) U
// 用于调用基本的方法
5 p$ H7 o# t- {7 ~ pThis = (CListCtrl*)this;
0 _ a7 k( y5 [! b% B k8 c // 默认的选中框颜色0 `4 e( j9 c8 A6 d
m_clrHighLight = 0xF1DACC;
" c' l* [6 u4 u/ i m_clrHighLightEdge = 0xC56A31;
8 G9 f+ L( V. @+ k" }} CListCtrlEx::~CListCtrlEx()+ a& q$ s/ D! J2 K) `& j
{ } 3 ^+ r2 F% x! d( F( ]
BEGIN_MESSAGE_MAP(CListCtrlEx, CListCtrl)' l6 T. ]( ~* I1 @& `3 S7 _
ON_WM_DESTROY()7 F' n& m! A; X7 ^! k
ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnNMCustomdraw)8 D1 h, ]5 r% C- L& D8 c
END_MESSAGE_MAP() // CListCtrlEx 消息处理程序
$ `) u( V, _4 a/ cCOLORREF CListCtrlEx::SetItemTextColor(int nItem,COLORREF clrText)& u0 Z% b( c! D! R0 Q7 o) i2 H! L$ ^
{+ ^+ R$ n# j$ E @$ q) ~( D
COLORREF clr = GetItemTextColor(nItem);
7 s6 k, ?# |0 {3 F% b ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);
/ X! [8 u d/ t2 `. G pData->clrItemTextColor = clrText; F/ `; V% H e0 u6 a2 g7 y
return clr;. _8 I( T# u+ e9 @- B
} COLORREF CListCtrlEx::GetItemTextColor(int nItem) const
) P# r2 s) t# d/ @* t6 a* w{
; b/ L ~% A6 g! Q ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);# l" W+ T& `3 N+ @
return pData->clrItemTextColor;+ Q Z9 x! f+ f1 B
} COLORREF CListCtrlEx::SetItemBackColor(int nItem, COLORREF clrBack)3 H+ G* r: L8 z2 D
{
4 x; Y/ w r% a/ Y1 ~! B1 \/ n* e$ H: W COLORREF clr = GetItemBackColor(nItem);) q$ d3 A) L7 W3 M) a, k
ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);3 [+ M% E+ f- L0 s( E3 t$ S# g
pData->clrItemBackColor = clrBack;
; Q3 p: w6 c, k+ F& S return clr;
8 J) w" {7 l& A1 |' Q} COLORREF CListCtrlEx::GetItemBackColor(int nItem) const
. }- s, N* l# }8 n{" @4 @/ u( x: V: [
ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);) y2 D- F- n+ o1 P* \7 x2 u
return pData->clrItemBackColor;2 h- [/ R6 D9 I2 L
}
4 E% t4 |! P) UDWORD_PTR CListCtrlEx::GetItemData (int nItem) const6 U# ^- f5 G, @' X
{5 I% _/ y. J6 \4 F
if (nItem >= GetItemCount())
4 t3 c3 U, }6 d3 G' t2 D {0 w7 S2 e z3 @& S8 s" [! v
return 0;
" X, Y" }6 u$ I# K" j+ {: f }1 Q- U7 L9 ]+ t5 U5 X
CListCtrl* pThis = (CListCtrl*)this;- X/ O) d1 z0 C
DWORD_PTR Data = pThis->GetItemData(nItem);
5 A# I# }/ R* d' ?; y1 N @ return ((ItemDataEx*)Data)->SrclParam;$ }9 W3 K4 ?( I- C/ e- g e
}
! a& q6 \ z ~BOOL CListCtrlEx::SetItemData (int nItem, DWORD_PTR dwData)5 k4 I8 H r( Y. G) f: g0 _$ x% D3 K
{
2 i, p& j! |' u) e6 s& c6 d if (nItem >= GetItemCount())$ U0 Z" O4 m7 D7 ~
{
! r( a' A+ \' j* f1 z return 0;# e+ U. I, }/ U+ ?) s
}
" C2 D1 g' @6 H1 x w/ J CListCtrl* pThis = (CListCtrl*)this;/ {: F& e6 _( }/ v2 ]" o8 p
DWORD_PTR Data = pThis->GetItemData(nItem);
* M8 H& Q( j$ O5 q" u! C+ K1 X! Y ASSERT(Data);
5 t" `4 M5 Y- Q2 i/ b7 y1 k ((ItemDataEx*)Data)->SrclParam = dwData;
7 G; H/ v2 l$ ~* J" k return 1;
3 |+ c5 U& h$ z5 r+ \}+ u3 |6 U- D, C. \- k8 r2 t
int CListCtrlEx::InsertItem (const LVITEM* pItem)
" g+ C3 b# K' l0 N{
0 I! i0 o+ q: F" x. | int nReturn = pThis->InsertItem(pItem);! l# I+ z$ q; a
ItemDataEx *pData = new ItemDataEx;+ h( A$ g) O; X/ y- o8 N) c
// 设置默认颜色
4 X( f2 n8 `6 Q I pData->clrItemBackColor = 0xFFFFFF;//pThis->GetTextBkColor();
9 P$ _1 L- k" p9 k/ e pData->clrItemTextColor = 0x000000;//pThis->GetTextColor();/ U9 I. K- g9 g2 Z6 p# Q
pData->SrclParam = pItem->lParam;; t( p& y3 r" W+ G, ^( j
pThis->SetItemData(pItem->iItem,(DWORD_PTR)pData);
8 q! Q* u) j( ? return nReturn;1 x" M9 V D/ }/ g9 m
}
. U- I4 U; r9 F9 z9 o4 qint CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem)
2 L% h. ~+ C1 Z" U: R8 o1 }; g$ L{
% G+ ]! }; t7 X& p LVITEM Item;5 X, n1 h) q2 `$ Z. R. g3 i1 R" P
memset(&Item,0,sizeof(LVITEM));
2 l. h! I! R) ^0 a, A" I1 \ Item.pszText = new TCHAR[strlen(lpszItem)+1];
( y; g* |6 r8 W. A strcpy(Item.pszText,lpszItem);
8 _6 d7 R/ s, c+ c5 g! V Item.iItem = nItem;, _8 J$ h& J6 O8 a5 L# J9 ^) g
Item.lParam = 0;8 C0 F( a# t& D& i7 W1 V
Item.mask = LVIF_TEXT |LVIF_PARAM;
- z* e) i" s$ L1 r4 w3 S BOOL bReturn = InsertItem(&Item);5 U% |; E. Y+ M1 w, W
delete Item.pszText;" F* J/ n G7 s# c; {
return bReturn;
. h( q0 K- `. B1 [/ ?7 k8 ?}8 P; w" @0 {4 g/ I- e5 f1 _6 v
int CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem, int nImage)
( k J7 Z4 n# R- k! l n3 m( x{
5 O1 ?4 o& W5 f8 w' E! O$ x LVITEM Item;
# P. {! b! x* f3 I; Y" L6 c9 r! f memset(&Item,0,sizeof(LVITEM));7 V0 `# j. S0 }- o( B
Item.pszText = new TCHAR[strlen(lpszItem)+1];! u$ n+ i5 h; \. G# L. G6 r
strcpy(Item.pszText,lpszItem);/ n. H! T- ]" C: U S
Item.iItem = nItem;1 [$ n1 ?3 G0 u/ ]5 B
Item.lParam = 0;& P' N* u% X" b: f: K
Item.iImage = nImage;/ B5 J& P( c, y2 G" f1 X: z
Item.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE;
* U& t1 G, f$ d* N+ f& [& I/ I BOOL bReturn = InsertItem(&Item);; X. b+ {' r& I5 i8 b
delete Item.pszText;
( y6 y3 y ?: Y+ e: A$ i return bReturn;
1 @3 x( \& F- j# h}
1 H0 _. ~$ S% m( Y" Dint CListCtrlEx::InsertItem (UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam)3 J( q3 L: R! b" G
{
/ j( B% h/ r" S9 a/ L, ? LVITEM Item;
- M9 |( x$ B; s, v5 w) D memset(&Item,0,sizeof(LVITEM));
- E0 }# k% A% N" Z! O( G Item.pszText = new TCHAR[strlen(lpszItem)+1];
. ~6 u+ R- _! \ strcpy(Item.pszText,lpszItem);8 j7 t) ~& }' v- H+ q
Item.state = nState;
9 u8 p, l1 Q$ F# V/ Y9 r Item.stateMask = nStateMask;
' n( P, D0 s0 |6 {3 x Item.iItem = nItem;& f% J) `3 W5 W" ?
Item.lParam = lParam;3 |# [4 _) G3 W
Item.iImage = nImage;4 Y- n+ u* s7 q/ l1 B- }1 @" m
Item.mask = nMask;
- W2 a( B: i! p B( { BOOL bReturn = InsertItem(&Item);0 C$ o" t( S& ]3 P0 G- P8 |
delete Item.pszText;1 B \# u1 x+ t$ l. Z9 M
return bReturn;
9 i/ p8 Z8 b) Q}
- y- F# |8 {+ u/ B# e8 l7 eBOOL CListCtrlEx:eleteItem (int nItem)
3 g2 G3 o5 h, ~! q: \{
1 k3 d# z$ Y6 K D2 F) ]; p2 W void* pDataEx = (void*)pThis->GetItemData(nItem);
* y1 u" z* o7 E7 O" Z$ T# B2 s6 o if(pDataEx != NULL)+ c6 P1 u p; F. s* Y7 p' y8 V! p
{
: l6 o% k# q5 b& }: R5 S, `& J delete pDataEx;1 q8 V) v, h! \7 t9 D' I) f
}/ z7 T. e/ i' I1 y
return pThis->DeleteItem(nItem);( C; d5 ~% v o! S" C+ n" S! h
} D2 v6 P0 V3 c/ Z. u
BOOL CListCtrlEx:eleteAllItems ()
$ c) X& |9 w1 }: K! j$ q3 V( B5 j* B$ N{
3 y! N% B5 T4 B. I' E# O) O1 b9 ] int nCount = GetItemCount();
; j/ i: B# w7 Q8 }! z" {5 q for (int i = 0;i<nCount;i++)7 V! H* |7 S4 }9 G* q/ ~5 H
{6 C1 f( U" g4 v/ A; i
if(!DeleteItem(0))
" E. |: c5 E+ c" P {8 T3 X/ T) L# P- J# P, Y
return 0;" Y' I6 P7 @: p( ~7 ]& ?
}2 A. O( R5 f0 k
}
# J* i5 f# {8 f+ C- L3 N# Z& h return 1;
* J- `+ ~6 T- a}
0 F# ]5 k Q, o: IBOOL CListCtrlEx::GetItem(LVITEM* pItem)
, U# ]9 c7 x3 q* Y{
9 B' P& c% U8 H; d9 C4 `) M8 _" I BOOL bReturn = pThis->GetItem(pItem);
+ G6 b0 i C; r: D# P- N if (pItem->lParam)
: C5 p$ c% B% G7 w7 B {
" U4 ^, O, a( T5 d6 ~; V. [. Y pItem->lParam = ((ItemDataEx*)pItem->lParam)->SrclParam;* d Q- E0 R% w V. s- W- G
} return bReturn;
+ ?) R- ?7 U& O! k3 a7 d6 W6 P- a}
: R% M# P1 q0 ~ B+ _& u9 dvoid CListCtrlEx::OnDestroy()
3 J/ F$ A& p' t{, x6 `5 M, l' M: B# u# J7 \
// 关键是清除申请的内存空间
$ E' C+ s, \2 u2 g) ?$ I! o7 r DeleteAllItems();
. m6 J8 u+ J- f+ m5 V: B# i3 F7 c5 { CListCtrl::OnDestroy();% }% z! @+ N) O L5 b
} BOOL CListCtrlEx::SwitchItem(int nItem1, int nItem2)
- e, o5 N- ~8 j! B2 z; ?{* y# u, X1 u, c( l: _7 v: j' `
if (nItem1 == nItem2 || nItem1 >= GetItemCount() || nItem2 >= GetItemCount() || nItem1 < 0 || nItem2 < 0)
. ^9 u! R) X8 E) F+ t% x5 a {0 K6 T6 j1 O! a
return 0;
2 J0 G9 d: _& s, i* k2 ^2 w }8 a; g$ q+ q, z
LVITEM ItemData1,ItemData2;# t% v4 E9 U8 f6 P& A" \ {. }8 Z0 F
int ItemLength1 = pThis->GetItemText(nItem1,0).GetLength();
& Z& `4 Y* F2 U int ItemLength2 = pThis->GetItemText(nItem2,0).GetLength();8 Y' `; _6 x* F8 T+ A" Z4 X: B
ItemData1.pszText = new TCHAR[ItemLength1+1];# T! y7 \ }4 K8 g6 p+ Y( i
ItemData2.pszText = new TCHAR[ItemLength2+1];+ a# N. C8 ~- y [7 z6 c4 @7 f( _
ItemData1.cchTextMax = ItemLength1+1;7 D' _2 L8 z; S
ItemData2.cchTextMax = ItemLength2+1;
) ?% i+ R4 G: Z. R; ~- c ItemData1.iItem = nItem1;
2 Z8 J4 H- W7 Q8 ~" _ ItemData2.iItem = nItem2;
5 d1 e% ?- P$ a8 f- P ItemData1.iSubItem = ItemData2.iSubItem = 0;4 ]7 x! X0 L7 |! W9 |6 P# o
ItemData1.mask = ItemData2.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE|LVIF_STATE; pThis->GetItem(&ItemData1);
- q4 x' ?2 ^1 r3 z! K& S, v4 _ pThis->GetItem(&ItemData2);
, M2 d% C- I( R6 r h2 K9 K0 a // 交换数据8 h1 [, y" d) [
ItemData1.iItem = nItem2;
# ~& Q7 L- a+ c4 I7 x9 g ItemData2.iItem = nItem1;: y. l) b2 e( k9 }; E+ ~+ t
pThis->SetItem(&ItemData1);: o4 t9 l9 i% d7 ^
pThis->SetItem(&ItemData2); delete ItemData1.pszText;
# X; B* B- J0 K. W' j8 U delete ItemData2.pszText;
7 y3 A1 ?( R# o4 f1 Z0 K. `, s // 得到报表模式的列数) I0 J! P$ e4 r$ F/ P2 h2 K8 G
int nColCount = pThis->GetHeaderCtrl()->GetItemCount();. ]- y8 i1 d( p+ [! w- H: h$ Y. _
CString strBuffer;* D$ f& `3 | N" L
for (int iSubItem = 1;iSubItem < nColCount;iSubItem++); C- ]2 r& e9 J' q$ Q
{
7 b" @! ^- W2 z4 ]5 f" D2 _8 R strBuffer = pThis->GetItemText(nItem1,iSubItem);
: ~: t9 B) {; f' P pThis->SetItemText(nItem1,iSubItem,pThis->GetItemText(nItem2,iSubItem));
+ I) A# {" v( @ Y' B pThis->SetItemText(nItem2,iSubItem,strBuffer);
. P) b: P3 D, c: H y: a }
: z6 @6 O1 t* O- q+ r- \3 ] // 把原来的选中状态也转移过去 t" a N1 ^1 R( h
// 不知道为什么这个属性没有体现在LVITEM结构中……
4 o2 a% a! X( n/ L5 V9 d2 B bool bItemSelected1 = GetItemState(nItem1,LVIS_SELECTED) == LVIS_SELECTED;
% k- Y" {+ H! s9 \: | bool bItemSelected2 = GetItemState(nItem2,LVIS_SELECTED) == LVIS_SELECTED; SetItemState(nItem1,bItemSelected2?LVIS_SELECTED:0,LVIS_SELECTED);8 X/ y! T$ f1 z, I" m' k) t" w
SetItemState(nItem2,bItemSelected1?LVIS_SELECTED:0,LVIS_SELECTED);) k* D- U/ Q3 K2 x, T' W3 G8 \
return 1;
/ d5 y7 F: ^8 i" P4 [# w} BOOL CListCtrlEx:reCreateWindow(CREATESTRUCT& cs)2 F3 @& J+ g8 \+ A9 u$ E
{: I. `& p" T- u3 q3 {* K' }8 J; w" @
// 本类报表模式专用# u1 ~ G9 C( x
cs.style |= LVS_REPORT ;9 \+ v: e/ n4 ~. i* j2 f
// cs.dwExStyle |= LVS_EX_FULLROWSELECT;
S( N$ h( x: E3 d" D: y return CListCtrl:reCreateWindow(cs);
. p* C$ q/ b/ z, {- M4 s/ ]5 P$ i} ( E p2 o7 Z: B8 e+ @6 v
COLORREF CListCtrlEx::SetHighLightColor(COLORREF clrHighLight)7 }$ @- z+ P! z/ C0 m( H; m1 G
{: L' d: H+ S0 U2 X2 G
COLORREF clr = GetHighLightColor();" y; X( |, V! ]7 t
m_clrHighLight = clrHighLight;* O3 T( f6 h5 M
return clr;+ Z8 `! x4 S+ _( W
} COLORREF CListCtrlEx::SetHighLightEdgeColor(COLORREF clrHighLightEdge)
: {9 f. ], W) D) t{. g* E7 |8 m. l* v. B& G
COLORREF clr = GetHighLightEdgeColor();
9 v5 g& g. |3 t5 ~ x* O) a m_clrHighLightEdge = clrHighLightEdge;( H8 i8 @1 q& N6 G& v2 C4 |
return clr;
, R1 F" j- \. O' v+ {' `} |