// ---------------------------------------------# Y0 h v2 u6 g7 L" U5 m7 H7 u
// ListCtrlEx.cpp
% t% H6 ^# u; x5 j( {// ---------------------------------------------
' B" K' g. x7 a: T! \// ListCtrlEx.cpp : 实现文件) f# x4 M/ }# A+ T! @
// #include "stdafx.h"' O5 R9 t( W2 D$ s
#include "CreateNewClass.h"- C; E* u: N6 t( r" f$ G
#include "ListCtrlEx.h"2 u' n7 A# t* u q0 H! u5 P
#include ".\listctrlex.h"
) A! v! q& O3 ]& |0 e// CListCtrlEx IMPLEMENT_DYNAMIC(CListCtrlEx, CListCtrl)3 R# _* H& |; Y/ q
CListCtrlEx::CListCtrlEx() \. d+ J0 s+ w) v) C) P
{1 m3 [4 s, h# r& I- ?3 w: \
// 用于调用基本的方法$ g- d' w T! d7 |# d. n' S2 j
pThis = (CListCtrl*)this;: U' k9 ~: ?! U" Z4 {" u
// 默认的选中框颜色
# ]6 F% J$ T) u( H/ } z m_clrHighLight = 0xF1DACC; 5 x# [5 R8 }# u7 E! `- m! Y: t
m_clrHighLightEdge = 0xC56A31;
: \! y2 K$ J% C1 B} CListCtrlEx::~CListCtrlEx()5 r/ U2 V& ~8 o+ _) o. c: s
{ } 9 l R F" c! A* D
BEGIN_MESSAGE_MAP(CListCtrlEx, CListCtrl)
. `$ A. O$ h8 v9 `- V ON_WM_DESTROY()
* c6 I5 V. V1 V. b: q4 Q ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnNMCustomdraw)
$ p6 H* _5 ~; }" Q0 N% J& r1 jEND_MESSAGE_MAP() // CListCtrlEx 消息处理程序
8 H( [3 x9 q) _, mCOLORREF CListCtrlEx::SetItemTextColor(int nItem,COLORREF clrText)( c3 Z: k+ D. s1 A
{
K; @- m" U. B% A8 k6 V! x9 @, _ COLORREF clr = GetItemTextColor(nItem);
9 r& [, k i" x) X x ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);7 ^6 c+ v+ t/ B1 g9 S6 U5 {$ g4 ~9 n
pData->clrItemTextColor = clrText;
Q3 H) Q @" k return clr;
7 ], e8 M! ^5 N3 O( r4 `( ~} COLORREF CListCtrlEx::GetItemTextColor(int nItem) const' H8 i/ I) L" S
{! `, z; _. h/ w" [
ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);
0 O# T* A9 h X# J' E# V return pData->clrItemTextColor;
1 r! n2 N; i, V4 a} COLORREF CListCtrlEx::SetItemBackColor(int nItem, COLORREF clrBack)
7 H. B) ?1 u# O3 w, L{9 D$ g- X7 o5 E: g
COLORREF clr = GetItemBackColor(nItem);. e& D+ e. O! s0 [
ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);
O3 j$ k' \" R9 o8 _/ J pData->clrItemBackColor = clrBack;
) r# z" R- X# \ return clr;& S7 n; T5 D' V V
} COLORREF CListCtrlEx::GetItemBackColor(int nItem) const$ r* m+ a$ f# \% d7 X8 J+ S
{
; a* L+ S$ f( a# i5 ? ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);$ J. g" X1 w* W# o- I$ V8 f' F' c
return pData->clrItemBackColor;4 B; P1 p5 s2 |& ~
}2 I0 ~5 g& t& P" W
DWORD_PTR CListCtrlEx::GetItemData (int nItem) const
; A1 }6 a8 ^8 Q% a& c" }{
. |3 B5 J" [7 f9 \/ O if (nItem >= GetItemCount())
$ o' K& E8 ^9 D5 X {. B# O% L; N/ a5 p5 @" f) B# T
return 0;; z6 ~$ ]; X, H3 ?1 I
}7 u- f9 D, D3 y- c$ f# V
CListCtrl* pThis = (CListCtrl*)this;
) X6 p1 A: A/ U& Z6 r1 m% g DWORD_PTR Data = pThis->GetItemData(nItem);$ }4 s* K5 |6 ~7 ^
return ((ItemDataEx*)Data)->SrclParam;
3 i; a+ O5 u7 i b$ L}
3 Z6 i: w' b$ k3 Z2 e9 mBOOL CListCtrlEx::SetItemData (int nItem, DWORD_PTR dwData)2 {* k7 X0 q" S- T. B* c- z
{: o/ B. j1 L& A/ E0 X* [
if (nItem >= GetItemCount()) }3 q, {- L- u& W; N- S u
{( X# L1 G* M: Q: X4 ]9 v/ H
return 0;7 t% y0 \ ^( T" N) U* U! A
}
2 T( h" V% _% o6 j CListCtrl* pThis = (CListCtrl*)this;) `, f) J7 l* M. T
DWORD_PTR Data = pThis->GetItemData(nItem);% L- h9 U) ?& A& O. W
ASSERT(Data);
# L* s1 Y& C( u$ x6 J) G$ [4 y ((ItemDataEx*)Data)->SrclParam = dwData;* t: g) Q& Z% c) p% E5 [( S2 b
return 1;$ c8 X& _7 X2 Y, F) K
}' _& @/ b* y7 ]$ Y. j! [' R
int CListCtrlEx::InsertItem (const LVITEM* pItem), M* V2 o2 z9 T1 h9 _
{, ?. K4 [# z' k3 X, _5 a
int nReturn = pThis->InsertItem(pItem);
7 }( P% v" C# F( y) n ItemDataEx *pData = new ItemDataEx;# G# T$ k8 b' x: J+ G
// 设置默认颜色3 U, _9 e# l V2 x1 g x: n
pData->clrItemBackColor = 0xFFFFFF;//pThis->GetTextBkColor();: ^9 D* s# f O' C8 B
pData->clrItemTextColor = 0x000000;//pThis->GetTextColor();9 I6 a' Z* g% G3 Q5 c5 q
pData->SrclParam = pItem->lParam;$ B- F1 k$ `) @ _
pThis->SetItemData(pItem->iItem,(DWORD_PTR)pData);
# a+ L' p' }3 D2 E- v. `" m return nReturn;5 L2 p- z9 w; j
}3 M7 D# k) X2 c7 l
int CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem)1 O; m, {: X' g) U9 s* \
{7 h/ \: \+ [$ U, E: n. ~) K
LVITEM Item;
; y9 m4 u' |! B3 o. |1 T* H4 q* I memset(&Item,0,sizeof(LVITEM));8 T% w8 o9 z6 G+ a) i" r
Item.pszText = new TCHAR[strlen(lpszItem)+1];, [9 \. W0 S- V$ w6 ]2 f: V
strcpy(Item.pszText,lpszItem);
3 G* c: L4 |: z$ \ Item.iItem = nItem;
E' C4 p3 Y/ a" R( F p) d Item.lParam = 0;
4 |$ W4 [/ e: ?7 U7 B( S- g Item.mask = LVIF_TEXT |LVIF_PARAM;# B. j Z5 P& B$ H
BOOL bReturn = InsertItem(&Item);/ t% L( L! B; q* B! }& ^
delete Item.pszText;$ K% ^- ^" z" Q- ?
return bReturn;
3 f; J4 b6 |2 e! ~% P0 a6 O* c$ I}. Z, U' h5 ?/ Z8 I
int CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem, int nImage)$ S: N" W% u& X& \0 i) Q
{
1 f; [5 O" _+ x. n8 B: j LVITEM Item;
' J+ F% f/ c3 W& a6 k# K/ L, K \ memset(&Item,0,sizeof(LVITEM));
# Z r4 I3 J0 |+ h Item.pszText = new TCHAR[strlen(lpszItem)+1];4 U' f) G6 V }! S
strcpy(Item.pszText,lpszItem);
4 O( D2 u. c( ^. p# h) M Item.iItem = nItem;/ j' R: {# j& b& i$ j8 w. x M- a
Item.lParam = 0;5 E7 P$ T) G0 k* g; c- s# k
Item.iImage = nImage;
L4 h1 ~# o; A" |& e: u/ A Item.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE;" W, H: q3 b( U! C3 j
BOOL bReturn = InsertItem(&Item);
, ]; K% z" @1 A Y Z4 { delete Item.pszText;
: ^' [# |, c% H* Z9 u return bReturn;
, a0 q' e) @1 W- t8 v1 G' V}
4 D& z' y9 _: ~* b6 q# `' A/ vint CListCtrlEx::InsertItem (UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam)% G7 f- g8 k$ C6 F6 }
{
) }: C' @& t& R, R' d LVITEM Item;
- c. P+ U/ ?8 }$ c memset(&Item,0,sizeof(LVITEM));- O' b7 x( ^# A: s( }
Item.pszText = new TCHAR[strlen(lpszItem)+1];- U# T3 D* E+ o% V3 D7 s0 d% I
strcpy(Item.pszText,lpszItem);
6 E' Z- C8 ^1 _ Item.state = nState;
3 Q3 A% q$ R9 b Item.stateMask = nStateMask;% i2 `8 k4 K) O" a9 m7 @
Item.iItem = nItem;& g0 {3 h# ~, `! b
Item.lParam = lParam;$ x, i i5 }$ f, @
Item.iImage = nImage;
- g: h. J, n4 f8 Q& q Item.mask = nMask;6 i9 I# |9 m% ~% j) \, W
BOOL bReturn = InsertItem(&Item);
, Y0 f+ h7 k% O; } delete Item.pszText;
* c4 X* b6 M" |- `' i. z return bReturn;0 @) j1 v0 o- l! D- Y5 c
}
; J/ |, s4 _- aBOOL CListCtrlEx:eleteItem (int nItem)& {. d, s: L$ m4 C
{, X8 m6 \6 B ^" l. d, E
void* pDataEx = (void*)pThis->GetItemData(nItem);
; |( T* D7 F/ ~2 r; K+ a if(pDataEx != NULL)
- c, G' v( L' h# `* }& e( o$ b7 @$ R {" \ E0 y# U2 ]" O1 J
delete pDataEx;6 @2 q9 b I a
}" o, ?! X- ^+ Q. ~2 I+ x: t
return pThis->DeleteItem(nItem);& M' b3 V5 A; @" R/ u
}. ~' E4 P" r4 c0 D( V
BOOL CListCtrlEx:eleteAllItems ()% J- j- K: T3 v! U0 s7 A
{3 \9 t- J4 l. }' ]8 Z+ G0 @
int nCount = GetItemCount();( Q F# D( D1 ~' W* D( X
for (int i = 0;i<nCount;i++) {6 X% y7 F2 i" O
{1 n, c$ B" W% @
if(!DeleteItem(0))2 R* y' Z* Y! S
{5 w* G# ^, c# h' L6 Z
return 0;& _0 W: z* ^* S- s
}0 a4 ]' D% ]" `6 [: d' T9 l, Q
}
0 q' h6 L% O) P& b2 { return 1;- I& R! S! O2 f8 z1 O, O
}8 B% a6 P5 o/ a; N( P( F
BOOL CListCtrlEx::GetItem(LVITEM* pItem)
* Y* L3 s& N! y$ s7 V4 T{
) c0 F: B/ x% Q! U& B BOOL bReturn = pThis->GetItem(pItem);6 l8 D F1 d3 q) S; L% F! H( u o
if (pItem->lParam)3 n9 S7 P+ J1 n" A! A6 A! g
{5 [3 r/ P* F/ m8 I
pItem->lParam = ((ItemDataEx*)pItem->lParam)->SrclParam;. K2 [# A) p5 O6 {8 e/ H$ ]' r
} return bReturn;" K; Z+ H6 _& f4 B/ a" b
}
4 c/ c# w! t9 E7 y' q) Wvoid CListCtrlEx::OnDestroy()
2 L- U+ G& I0 M! c1 ~1 U; G( W{
% `: n8 n4 F& U: _ // 关键是清除申请的内存空间4 r1 r1 d7 c/ h. D( e6 N6 ~
DeleteAllItems();! P% i* Z2 Q* U" c( w
CListCtrl::OnDestroy();" ]! w. [5 F+ f$ t. W# E; u7 R
} BOOL CListCtrlEx::SwitchItem(int nItem1, int nItem2)
1 c5 e8 Q+ _: o1 b% \4 A% u+ Y2 |{" z- k8 \1 g4 Y9 b4 G( L
if (nItem1 == nItem2 || nItem1 >= GetItemCount() || nItem2 >= GetItemCount() || nItem1 < 0 || nItem2 < 0)
% Y3 q0 c) i' _( N5 h8 q {7 P* [: F- {* `" H5 u
return 0;4 [# _8 D% }- v$ ]& I5 Q) d! w, Q: {4 T
}
' ` i9 c. Q9 C4 s LVITEM ItemData1,ItemData2;/ C! O' f9 ?0 ^+ y
int ItemLength1 = pThis->GetItemText(nItem1,0).GetLength();( p! i' D' G8 r2 I+ v: k4 l
int ItemLength2 = pThis->GetItemText(nItem2,0).GetLength();
3 b+ i4 p1 F& H8 ? ItemData1.pszText = new TCHAR[ItemLength1+1];
}: d) L* ?3 g" b ItemData2.pszText = new TCHAR[ItemLength2+1];
/ t; q2 P( M8 ?( C9 r ItemData1.cchTextMax = ItemLength1+1;, N6 V# ^ \3 u! I% e5 k
ItemData2.cchTextMax = ItemLength2+1;' e1 _# e! [; v$ h+ k: \
ItemData1.iItem = nItem1; g9 P2 d- x5 s/ K8 K/ K$ D
ItemData2.iItem = nItem2;
0 c& A8 y$ Z1 Q ItemData1.iSubItem = ItemData2.iSubItem = 0;
1 ?8 q( R+ z2 z" f3 G! B0 D2 E5 x ItemData1.mask = ItemData2.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE|LVIF_STATE; pThis->GetItem(&ItemData1);4 B% I/ ~4 z/ K# o' D
pThis->GetItem(&ItemData2);
7 x& L1 d8 Y+ W/ b // 交换数据
) Y9 N$ Z' S$ D5 G' J ItemData1.iItem = nItem2;4 I' n: F: c# P8 q [
ItemData2.iItem = nItem1;9 z9 [8 J1 }3 K1 I! u4 Z
pThis->SetItem(&ItemData1);; n9 ?! U8 I$ t- l2 z9 f: B' @
pThis->SetItem(&ItemData2); delete ItemData1.pszText;" e; W: Z# P }8 f* m6 O" N2 {
delete ItemData2.pszText;
+ ~0 V: o1 U) K( J! m9 p+ }% h // 得到报表模式的列数: `; e; c( [8 Z) C2 @/ X
int nColCount = pThis->GetHeaderCtrl()->GetItemCount();
+ m1 g- g7 U4 u CString strBuffer;7 D! k S9 `' w( D2 V* F
for (int iSubItem = 1;iSubItem < nColCount;iSubItem++)$ b" \+ z) ? N, M) e3 D% l
{
2 b: ]2 M4 @* ~" K) C( Q3 C, \# L strBuffer = pThis->GetItemText(nItem1,iSubItem);
/ N8 P) L! V, P9 V" M) M pThis->SetItemText(nItem1,iSubItem,pThis->GetItemText(nItem2,iSubItem));9 @8 A1 m D# |( G+ G- o1 m
pThis->SetItemText(nItem2,iSubItem,strBuffer);
7 z% F( } |- r2 j5 s, ] }
% I3 l" L. e+ m1 X // 把原来的选中状态也转移过去8 R9 Z9 b3 a2 |0 l* l, D+ e
// 不知道为什么这个属性没有体现在LVITEM结构中……
+ b$ @) N1 w6 B$ F1 F! g4 E bool bItemSelected1 = GetItemState(nItem1,LVIS_SELECTED) == LVIS_SELECTED;
! Q9 \, W; k/ {% O# z0 Z bool bItemSelected2 = GetItemState(nItem2,LVIS_SELECTED) == LVIS_SELECTED; SetItemState(nItem1,bItemSelected2?LVIS_SELECTED:0,LVIS_SELECTED);
4 M+ x% |8 d3 D0 ^* U SetItemState(nItem2,bItemSelected1?LVIS_SELECTED:0,LVIS_SELECTED);
`4 }& n; r& r" A return 1;) l7 D F* ^* H9 V' G2 m
} BOOL CListCtrlEx:reCreateWindow(CREATESTRUCT& cs), Z' A! H: b# G& O7 O/ X; \
{
) y: M1 Y- z4 Q; c* Q // 本类报表模式专用1 F3 P N: e4 d4 @/ P/ Y
cs.style |= LVS_REPORT ;* R Z" F3 A/ t5 u& n: P
// cs.dwExStyle |= LVS_EX_FULLROWSELECT;0 o( J. S# p- v: R1 r: W6 g
return CListCtrl:reCreateWindow(cs);
- Q+ G- K: \8 W' l0 ^; d} % I$ {# Y- ^7 @& t8 i
COLORREF CListCtrlEx::SetHighLightColor(COLORREF clrHighLight)
( m. t" c M5 }" u' Q{
5 Q( ^& Q- a/ T- @. k# ^6 H COLORREF clr = GetHighLightColor();
, Q: F. L$ Q3 q. Y m_clrHighLight = clrHighLight;
; i: y5 J# a+ B! P: t" {2 U, S return clr;3 C9 f- x+ \ z9 Y/ M
} COLORREF CListCtrlEx::SetHighLightEdgeColor(COLORREF clrHighLightEdge)
, w% H9 y+ L1 t{( ?) i6 R, v6 n a
COLORREF clr = GetHighLightEdgeColor();
" i8 e: w$ T, m6 M: `( H6 f; n4 a m_clrHighLightEdge = clrHighLightEdge;9 l: l0 D, ?8 P& S
return clr;
8 @. M) P' A" E1 k# U. g* e6 p0 x} |