// ---------------------------------------------
2 K( N3 z% h5 x+ ~// ListCtrlEx.cpp
6 j' L+ o% k, L$ ]$ n& e6 N// ---------------------------------------------
$ D" u! C0 `- P' b// ListCtrlEx.cpp : 实现文件
4 G3 `3 t( x% U% F// #include "stdafx.h") r3 a3 z. U8 a2 d' z! r7 R% b
#include "CreateNewClass.h"/ }1 q2 p) x; K- l5 V: h
#include "ListCtrlEx.h"
& W" R/ E2 I% R; {: ^#include ".\listctrlex.h"
0 B7 c0 ]* ]5 x5 M4 P( N1 G1 [// CListCtrlEx IMPLEMENT_DYNAMIC(CListCtrlEx, CListCtrl)
: N( M' ^5 J+ i0 m: m7 f* iCListCtrlEx::CListCtrlEx()
: j7 ]2 C3 h0 a6 O. Y/ o{2 X% |. r0 v g5 f$ y( @6 M
// 用于调用基本的方法
) y: @+ Z4 R$ B$ w, R& J pThis = (CListCtrl*)this;
0 k# r6 [" \* i. w" V4 o+ J // 默认的选中框颜色& F0 m( C! x u% K1 ?% y
m_clrHighLight = 0xF1DACC;
- [" ] k4 c5 r; _' h( E m_clrHighLightEdge = 0xC56A31;
3 g9 [9 K1 g3 k/ p$ _% z} CListCtrlEx::~CListCtrlEx()
' I2 ~3 P0 P+ M, q{ } 2 m) y3 B/ ^1 t; o* I
BEGIN_MESSAGE_MAP(CListCtrlEx, CListCtrl)# R1 l. C- J' h; E. M& I3 u: w4 T3 C- F
ON_WM_DESTROY(), I: P. q9 v" M3 n5 J$ f
ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnNMCustomdraw); M" Q3 L% m/ k, [$ k5 p' }
END_MESSAGE_MAP() // CListCtrlEx 消息处理程序
9 i% _6 z3 M+ t3 M8 W) NCOLORREF CListCtrlEx::SetItemTextColor(int nItem,COLORREF clrText) M6 E6 s; h& J% m I/ E
{
/ e' K4 z2 E9 `3 k COLORREF clr = GetItemTextColor(nItem);1 o! B G0 f( Q2 a/ v1 \& c
ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);* O, B2 M- r- I* p$ ?
pData->clrItemTextColor = clrText;
# e9 M, F Y8 D: u; x5 y return clr;
& _) z. ?, G* s8 p& X. Z} COLORREF CListCtrlEx::GetItemTextColor(int nItem) const. {/ c# n8 g }% q
{
$ Q' l& ^! L/ d5 x/ a ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);
+ T% }, m$ U0 P" r6 {3 B$ x return pData->clrItemTextColor;, b6 h$ |- m4 t+ e& A
} COLORREF CListCtrlEx::SetItemBackColor(int nItem, COLORREF clrBack)
0 P$ Q1 a7 |% Y7 f{4 Z# [9 v3 n& Z* k, ]
COLORREF clr = GetItemBackColor(nItem);
. h( w: c; Y1 x& O5 y0 f ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);
+ _+ B k: a7 t# J) h& V# [ pData->clrItemBackColor = clrBack;! ~: `% W, R& W) M; a) c/ W
return clr;
) f' n0 V. r/ m U} COLORREF CListCtrlEx::GetItemBackColor(int nItem) const
9 N/ ]" u3 f, B0 S: K4 e$ r{
; a" p" P) q& ~) O6 ~ ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);
* Y* Y6 s& R& y& |: x, j9 O return pData->clrItemBackColor;
2 M/ J' P, h3 n2 D7 H! N% K' z ?}; t5 d% h$ |! W: S2 Y; F
DWORD_PTR CListCtrlEx::GetItemData (int nItem) const% d9 u7 C7 |' D$ T6 _7 d/ m
{; P7 U+ y0 g; e2 Q- I
if (nItem >= GetItemCount())
; v2 w( W4 ~2 S {6 G: `1 E2 }" Q1 A' Y7 w
return 0;
9 L) `7 T3 Y" k) K+ _ }
8 O: S/ u: v+ v7 @/ K8 X3 P CListCtrl* pThis = (CListCtrl*)this;
6 W7 Z2 ~/ G, ]- a DWORD_PTR Data = pThis->GetItemData(nItem);
" a$ ^; h) C$ b& s8 C& K4 y5 _ return ((ItemDataEx*)Data)->SrclParam;2 V9 g# ]; O% I5 s' e% n
}
% p2 _# r3 t1 e& G- yBOOL CListCtrlEx::SetItemData (int nItem, DWORD_PTR dwData)
" C: }5 J* f: n3 e{
3 o2 p/ @' }4 D- e3 B1 {% Y if (nItem >= GetItemCount())
! T" {' G/ c5 i9 S {
# P1 I+ Y# v! v! ^2 ?' d1 l return 0;5 S4 N! E6 A/ N( Y! E& s
}
+ _- O+ V r) w! |2 d) } CListCtrl* pThis = (CListCtrl*)this;* V0 F1 F! a4 q# C L# I
DWORD_PTR Data = pThis->GetItemData(nItem);
: F6 z2 F. N! ]" w8 L9 p ASSERT(Data);( l+ M! M! ?+ N( I
((ItemDataEx*)Data)->SrclParam = dwData;& N4 e# B o5 D: I" m
return 1;/ I2 M/ `. M8 w# {8 A
}
5 V1 x0 ^, ]: S2 \7 `int CListCtrlEx::InsertItem (const LVITEM* pItem)/ i/ o' K1 H5 W4 x- d
{
7 U8 f9 q& k( t" P4 e# C int nReturn = pThis->InsertItem(pItem);6 s. H. N1 Z; e4 m- p
ItemDataEx *pData = new ItemDataEx;' k8 X+ v1 l7 K
// 设置默认颜色% ?# Z7 H% u# M
pData->clrItemBackColor = 0xFFFFFF;//pThis->GetTextBkColor();
4 p8 W+ ?. \- x1 D2 ]4 z; k! Q pData->clrItemTextColor = 0x000000;//pThis->GetTextColor();
`5 `5 a( @$ u% e' Y5 e& j pData->SrclParam = pItem->lParam;& p: G0 S# F& P' d, h) }
pThis->SetItemData(pItem->iItem,(DWORD_PTR)pData);
! b8 A6 E# B+ L3 Y return nReturn;
5 r% S a A4 D( X3 @: k6 {% n- f0 Y, f}
7 O& E: u! g& S+ I8 l, r9 \8 a; C- T, aint CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem), ~( D8 @5 q2 m+ b! p% E! p
{
" m0 O5 G7 d- T& m7 J LVITEM Item;
( \( o6 [- v P6 Z) E. \5 \ memset(&Item,0,sizeof(LVITEM));0 [% }# s( s6 e+ {2 c% t1 ~8 j
Item.pszText = new TCHAR[strlen(lpszItem)+1];
; U- L% @: v! d ] strcpy(Item.pszText,lpszItem);
! E4 d P8 ~' V Item.iItem = nItem;
* j! W; _2 _9 F0 }! W5 q Item.lParam = 0;! i! ]: Y# b0 U. `
Item.mask = LVIF_TEXT |LVIF_PARAM;' X; z0 O7 U# U4 V0 ^3 K3 k
BOOL bReturn = InsertItem(&Item);) {6 {5 `3 J" `* w5 B$ D4 G
delete Item.pszText;
& ~! W! m. @; ~9 a# ~( c return bReturn;
- d2 f- U) R/ y% Q! @* A+ Z3 R}
7 n& Z7 Q. W* @+ Fint CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem, int nImage)
( O, y1 V) v0 v, h{
& P! Q2 u" E2 O) p: e( M LVITEM Item;
0 T b5 }0 p9 {$ o memset(&Item,0,sizeof(LVITEM));
0 p+ M: I) Y# @ Item.pszText = new TCHAR[strlen(lpszItem)+1];- I/ y9 z5 f* h/ \" f
strcpy(Item.pszText,lpszItem);& i$ @% y; e$ x0 {
Item.iItem = nItem; O# v' q( u( P E
Item.lParam = 0;
2 K* _% E2 q5 R' M& H Item.iImage = nImage;
1 C6 x4 B# _6 `9 F; N7 m1 T Item.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE;8 l/ z- y1 P$ E7 O x1 O7 G
BOOL bReturn = InsertItem(&Item);: o3 O8 {! J1 F9 j9 c0 E1 h/ A
delete Item.pszText;
. A( X% p0 f6 b return bReturn;; q7 k; o* Y0 p. \+ V
}1 S9 {$ K- }- A) n
int CListCtrlEx::InsertItem (UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam)
! L7 y! e, k/ n X2 e) |{" @& x3 }" d' Z. k( x
LVITEM Item;
0 O! O# r5 T6 y) B, h memset(&Item,0,sizeof(LVITEM));# E& ]8 C$ Z# A
Item.pszText = new TCHAR[strlen(lpszItem)+1];
8 m8 H; }* F% s+ J strcpy(Item.pszText,lpszItem);
" V |0 z' d3 Z/ ^7 _+ S Item.state = nState;) H( I) u4 K3 Q: H0 Z) q+ J; V
Item.stateMask = nStateMask;
& |% E1 j- R' p- U# f& L Item.iItem = nItem;1 }6 V& W6 g0 W3 e1 a: f/ {/ r( h
Item.lParam = lParam;$ o+ D4 @7 ?2 Z! D
Item.iImage = nImage;% j( v* \. ~0 S. L
Item.mask = nMask;$ M& B$ x' _0 |- I9 _! I# ~
BOOL bReturn = InsertItem(&Item);7 m8 k* l( G: s$ S3 X
delete Item.pszText;% I( @# z+ K& L4 A( r, d5 J8 K
return bReturn;& W4 w) T: j r8 X
}
+ o4 A* Q: }0 qBOOL CListCtrlEx:eleteItem (int nItem)
5 U5 S4 M$ } F3 D V" f" }{& a" j4 z" F x m7 W' v
void* pDataEx = (void*)pThis->GetItemData(nItem);
9 \: d8 e7 A8 G% ?" I/ O7 ? if(pDataEx != NULL) Z' m1 F& C1 f+ v9 Y& ^3 e" L, J
{" D+ ` j+ w& `( d8 o! L- O# v
delete pDataEx;7 {4 D* C$ s! g( e* x
}
7 P) { O; F2 U5 _ return pThis->DeleteItem(nItem);% U! B# ]1 O$ } E8 V
}5 a) w$ Y$ u( ?8 \" C
BOOL CListCtrlEx:eleteAllItems ()
2 D( p3 {/ t2 L+ F% e{
J. \# I& }2 {+ M; g1 A! @( X int nCount = GetItemCount();' G- Q! ]7 U7 T* j7 m' T$ d
for (int i = 0;i<nCount;i++)+ t, m0 _( I; V4 F
{
2 w& x1 D& d4 f" P9 O if(!DeleteItem(0))! Z! s# x5 p9 x# r3 @& C# i! m
{8 V$ N3 i. M/ L7 k+ p5 Z& `
return 0;5 t3 v! E( Z$ y" ]3 G
}
8 _9 W7 i9 K3 E" T$ ^ }
1 F9 B m% M9 B3 v* \6 _( t return 1;' t6 [3 t; a( ^* _
}
6 l% Q) r6 H, n2 q4 N, r5 pBOOL CListCtrlEx::GetItem(LVITEM* pItem)
0 u9 m4 M0 G) o2 V7 d4 [+ M% j0 `6 _{
4 C* y! J5 H) {" }5 k- k. b# j BOOL bReturn = pThis->GetItem(pItem);& E) A; k4 K; u, V
if (pItem->lParam)$ K1 H+ j+ @' F
{
# \3 Y, \/ t/ h4 o f* b pItem->lParam = ((ItemDataEx*)pItem->lParam)->SrclParam;8 ^8 R T. O7 l8 V; a
} return bReturn;
/ Q% z% X7 Z1 e}2 j: r; V% Q4 D
void CListCtrlEx::OnDestroy()
4 p& i3 O/ M0 P, y, A6 J{
; ^9 n$ B v2 N6 z& G // 关键是清除申请的内存空间& d" ^+ p/ P1 Y4 S1 e1 T- b/ @; B& N
DeleteAllItems();
, J/ R: W6 B' h* R) c7 |0 `* z, M CListCtrl::OnDestroy();; L5 ^# _5 T* @$ C: |
} BOOL CListCtrlEx::SwitchItem(int nItem1, int nItem2)
0 p6 P6 e& Z" g+ \* [{
7 V, P3 e% c/ ^5 @7 Z2 P if (nItem1 == nItem2 || nItem1 >= GetItemCount() || nItem2 >= GetItemCount() || nItem1 < 0 || nItem2 < 0)
- @: J% m" n$ _/ Z- O; j& b; H {
. E4 _* L+ v; r6 B) ~ d return 0;8 o2 m0 c( }( [: n: f2 }
}
0 L r0 t- Y# z$ J6 ] LVITEM ItemData1,ItemData2;% A; C/ Z- ~; c! c" K2 K* z7 T$ I
int ItemLength1 = pThis->GetItemText(nItem1,0).GetLength();6 J0 O4 H# Y" P* ]
int ItemLength2 = pThis->GetItemText(nItem2,0).GetLength();. ?/ O t2 J/ K+ O: F( S- S# |
ItemData1.pszText = new TCHAR[ItemLength1+1];
+ B. ~" }" h) A: G F ItemData2.pszText = new TCHAR[ItemLength2+1];
u0 e8 d3 O2 v8 J: S- ~ ItemData1.cchTextMax = ItemLength1+1;4 O- c% Y. r1 r" \. Z* J( M- s& P1 I
ItemData2.cchTextMax = ItemLength2+1;" e! d. E6 H5 R8 I6 B1 S4 I
ItemData1.iItem = nItem1;
' V: N! P3 P' z I( x4 h* U ItemData2.iItem = nItem2;7 F: z2 F0 I E% M
ItemData1.iSubItem = ItemData2.iSubItem = 0;% `9 b$ b8 c1 ?2 C$ C3 b: x* D
ItemData1.mask = ItemData2.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE|LVIF_STATE; pThis->GetItem(&ItemData1);4 R/ C6 t+ c. R- i/ h k) `
pThis->GetItem(&ItemData2);) P; M3 F- i9 m" L+ o- v
// 交换数据
' ?: g! E0 K7 w" o b ItemData1.iItem = nItem2;
5 B+ j3 q- Z4 Q7 m ItemData2.iItem = nItem1;. Z0 X) Q5 q& K0 B; ?( r
pThis->SetItem(&ItemData1);" l* ^+ ]" \- C/ q/ c* d2 \/ a
pThis->SetItem(&ItemData2); delete ItemData1.pszText;$ C( k7 U* y1 L9 p* f( N- j
delete ItemData2.pszText;. P8 w+ V# }3 X& z$ I' {9 q9 V
// 得到报表模式的列数
6 ^1 _( i7 O% i2 m; i int nColCount = pThis->GetHeaderCtrl()->GetItemCount();' R6 d2 d0 v/ e( v* h; m) p
CString strBuffer;0 ~4 y# L' s9 c1 m
for (int iSubItem = 1;iSubItem < nColCount;iSubItem++)
5 v: A& G' l# |) \+ {- c N3 {% H1 P { K# M( D# R& H
strBuffer = pThis->GetItemText(nItem1,iSubItem);
1 s( v0 W7 T# {% d pThis->SetItemText(nItem1,iSubItem,pThis->GetItemText(nItem2,iSubItem));7 R5 P! a" c. h' j
pThis->SetItemText(nItem2,iSubItem,strBuffer);
# H+ w R+ k# c }
& T# @5 |( F1 L! A7 K/ H$ z // 把原来的选中状态也转移过去: s3 _& v+ A( x e% _
// 不知道为什么这个属性没有体现在LVITEM结构中……
+ r6 F1 `% l* R3 G bool bItemSelected1 = GetItemState(nItem1,LVIS_SELECTED) == LVIS_SELECTED;
7 [& G3 p6 v% t3 A; f2 p$ O2 y0 U bool bItemSelected2 = GetItemState(nItem2,LVIS_SELECTED) == LVIS_SELECTED; SetItemState(nItem1,bItemSelected2?LVIS_SELECTED:0,LVIS_SELECTED);
! u# P6 F( k, h0 m: F# ^ SetItemState(nItem2,bItemSelected1?LVIS_SELECTED:0,LVIS_SELECTED);' y: e2 ?) m/ G1 m
return 1;
: z( n8 g4 n" U& k9 Z0 F/ f} BOOL CListCtrlEx:reCreateWindow(CREATESTRUCT& cs)
! O5 D( O: D& A: d, }- ?9 U+ [{
5 A. p( a7 @! @, | // 本类报表模式专用
: p, v1 W7 h5 c P { cs.style |= LVS_REPORT ;
# M% B* [) [ `2 P& Y // cs.dwExStyle |= LVS_EX_FULLROWSELECT;7 k6 j" s7 Z* c! v; r; l
return CListCtrl:reCreateWindow(cs);
1 C7 o2 \# V7 C. B' c! z}
6 ~1 }# `& X& \COLORREF CListCtrlEx::SetHighLightColor(COLORREF clrHighLight)) |7 N: i9 N: l1 [& C
{; z( \ X! P% t7 U& ?1 a
COLORREF clr = GetHighLightColor();
& a/ ~' d H2 d' q% J; ] m_clrHighLight = clrHighLight;4 D& w5 r0 d; l+ m& x
return clr;
3 U3 \ {2 J( d3 g H% P: L} COLORREF CListCtrlEx::SetHighLightEdgeColor(COLORREF clrHighLightEdge)
/ ^- m8 B7 D3 k+ w) ]{
/ X6 o/ O5 }" Y' d0 B COLORREF clr = GetHighLightEdgeColor();
! [; T% r# F+ E2 L7 a- s m_clrHighLightEdge = clrHighLightEdge;0 A$ G) p8 T' L$ i; g
return clr;
( |; C" P* L3 ~0 h9 T) v. f6 s% v: `} |