// --------------------------------------------- v. ^: A+ h! h1 S
// ListCtrlEx.cpp" }6 w: L' G5 D9 {3 `0 @
// ---------------------------------------------) d& d( k+ b8 f" W7 |- U6 x
// ListCtrlEx.cpp : 实现文件: |1 e3 k/ g" i1 w7 n
// #include "stdafx.h"5 T! T* I- T" c# F% M+ y
#include "CreateNewClass.h": d0 r5 P* G/ Z) b' G7 \
#include "ListCtrlEx.h"9 b! E" {7 w, S3 @, w
#include ".\listctrlex.h" ; T0 _1 q6 w- U8 W* ?, ~, T7 u
// CListCtrlEx IMPLEMENT_DYNAMIC(CListCtrlEx, CListCtrl)- [8 j' K: m% ^5 l& S
CListCtrlEx::CListCtrlEx()+ ~+ V3 H+ c& u: P" O
{& y$ E( O' s" C
// 用于调用基本的方法
" ?, B3 n4 _; h& v( \, m, w" e pThis = (CListCtrl*)this;
1 P1 L+ ~ w3 e // 默认的选中框颜色
$ Y+ \1 }- ^ r, R$ a8 m& W! w m_clrHighLight = 0xF1DACC;
h% K! g5 h! ` m_clrHighLightEdge = 0xC56A31;
. W) a+ d, E! {} CListCtrlEx::~CListCtrlEx()7 {( G$ ~- a7 t# a7 i
{ } 4 c3 B' U, [( `0 Y
BEGIN_MESSAGE_MAP(CListCtrlEx, CListCtrl) L! L: e" p5 _; M/ g( V
ON_WM_DESTROY()6 [+ a6 l- d. I* m. e# D
ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnNMCustomdraw)
9 W6 B* y. r5 `. U3 T, E2 }0 zEND_MESSAGE_MAP() // CListCtrlEx 消息处理程序 + T- }% T6 v0 T1 u/ s# S
COLORREF CListCtrlEx::SetItemTextColor(int nItem,COLORREF clrText)
. n; X @" ^, e, \1 k0 Y# L& _{' o, Y% R+ U5 G6 e
COLORREF clr = GetItemTextColor(nItem);
; y+ Q5 l; O) V# c ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);
8 T# d9 s3 o6 @6 |4 m pData->clrItemTextColor = clrText;& [; k0 [) W. T$ [. }
return clr;' O$ r7 j( N7 c' Y% D; q% {5 ?
} COLORREF CListCtrlEx::GetItemTextColor(int nItem) const- O; b5 b/ B9 l9 x8 [& L
{
# N- J" o: ^" \+ ~6 c# a8 J# Y' ] ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);& Y4 j# ?! h. \4 ~0 m
return pData->clrItemTextColor;( Y$ o) \4 p' L/ D+ B9 e
} COLORREF CListCtrlEx::SetItemBackColor(int nItem, COLORREF clrBack). j0 G( m( n8 [
{ X5 f, f+ ]4 H6 R0 D8 b- O
COLORREF clr = GetItemBackColor(nItem);
3 c- W4 E9 i9 p! Q# n6 Q9 B ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);. L, P- _. `. u. z" T7 X8 A2 |
pData->clrItemBackColor = clrBack;! v1 [$ @- P6 E& m
return clr;
1 I/ N' V6 C% p/ J3 j} COLORREF CListCtrlEx::GetItemBackColor(int nItem) const0 P. a+ Z# V0 X/ ~% s; Z3 _" |8 A
{
+ Q: w+ i I) k2 a3 C ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);
$ L D/ P; ]$ t( V return pData->clrItemBackColor;
! Z# N `) E S, t3 u; J+ M/ a}4 B- \& D: v$ P) V$ r8 Q# b( G2 `
DWORD_PTR CListCtrlEx::GetItemData (int nItem) const. c$ ?5 w0 c% s6 M# V$ b; _
{
5 q; j8 a! v% m- h if (nItem >= GetItemCount())
5 z/ p0 x. C! M% v9 i$ A: N {
- |' O$ _, K$ i% L2 O# ]" B return 0;- ~: l8 |9 J0 I" ?- o
}9 }7 a9 b6 A1 p. L+ H# F$ H
CListCtrl* pThis = (CListCtrl*)this;
2 P7 x+ [: }2 r/ O DWORD_PTR Data = pThis->GetItemData(nItem);; W8 z8 _/ t5 B. ^1 ~
return ((ItemDataEx*)Data)->SrclParam;1 x0 O# J3 c1 V0 O R
}6 ?0 z8 A3 ]# C) e! W0 @
BOOL CListCtrlEx::SetItemData (int nItem, DWORD_PTR dwData)
9 s0 e9 F# @: C$ l* |% [{
4 v# a2 Z, {% q! w$ G' e* W# k if (nItem >= GetItemCount())
" b( I4 B% b' C, d( ~+ u {
, Z0 M' e' z- W7 Y1 S3 R- K return 0;2 O5 B2 o7 P" I
}
4 a5 v) }3 t+ ` CListCtrl* pThis = (CListCtrl*)this;9 x: E0 I T1 h( d8 j
DWORD_PTR Data = pThis->GetItemData(nItem);* S% U) C% {" h$ l: C
ASSERT(Data);
- g# A1 |5 [/ J$ M ((ItemDataEx*)Data)->SrclParam = dwData;# I9 w. S# v# y. U& U3 G
return 1;
) G( ~; |. h2 i+ H7 A5 V5 n}0 T$ @9 ^$ s2 C! G: \& G2 ~. j, y
int CListCtrlEx::InsertItem (const LVITEM* pItem)3 F' _0 C, F5 i+ {$ b, f
{$ g, `( s& D5 r Q( A {
int nReturn = pThis->InsertItem(pItem);
% }1 M; K9 z, {/ j ItemDataEx *pData = new ItemDataEx;1 @+ ?* ?( E0 i% t" x
// 设置默认颜色0 S. I. S4 }1 n2 I- E, A* v) r
pData->clrItemBackColor = 0xFFFFFF;//pThis->GetTextBkColor();6 k- J$ t0 A m6 @
pData->clrItemTextColor = 0x000000;//pThis->GetTextColor();
& l3 F, W, V. F5 f pData->SrclParam = pItem->lParam;
4 u# y' U; ^$ L: @6 M4 S) C pThis->SetItemData(pItem->iItem,(DWORD_PTR)pData);" ]9 p. ~! Z( j& w0 q
return nReturn;
: y# _2 E# m% _$ P}
% m, n9 G. S3 b* U- K ^int CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem)
0 L2 G0 U& ^3 K{
( Y% ]& P9 c/ G LVITEM Item;
$ t! x1 n9 o7 n0 i, x memset(&Item,0,sizeof(LVITEM));8 C. G2 A: b' q, t
Item.pszText = new TCHAR[strlen(lpszItem)+1];
7 l& g8 }7 Z; {- Y5 {* |* N3 W! _ strcpy(Item.pszText,lpszItem);
1 `( t: a. q j Item.iItem = nItem;
6 H/ j8 W# a& m- x1 x Item.lParam = 0;+ d' v2 ^) k0 U' y8 a% B6 R ]) L
Item.mask = LVIF_TEXT |LVIF_PARAM;3 Y$ k& z/ e# W& D0 a! F. E; l
BOOL bReturn = InsertItem(&Item);1 O- e& y% m& y
delete Item.pszText;
% S/ ]( ?, B/ ^4 v5 r return bReturn;; e7 M, a* K: ^( E0 k5 `
}
' V |. X9 n- Kint CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem, int nImage)6 h( l R: F( k
{7 P; C9 k1 n4 E
LVITEM Item;
, r0 J; r0 O# m0 | memset(&Item,0,sizeof(LVITEM));
. l4 [& v/ l$ v' `0 [' e Item.pszText = new TCHAR[strlen(lpszItem)+1];
4 i, K2 O% I \+ { strcpy(Item.pszText,lpszItem);/ E9 ^1 B$ L7 J" u
Item.iItem = nItem;
7 F1 r! k+ @8 N. g Item.lParam = 0;0 z9 |) Q, n/ q
Item.iImage = nImage;
. D3 ^ E+ f( @1 Q' ~ Item.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE;
5 r) m, h3 q" x. f/ {) K BOOL bReturn = InsertItem(&Item);& _- m3 S& i6 C4 _* z! z
delete Item.pszText;
( D3 g1 v7 z1 ~( W& M) P3 X, } return bReturn;
/ d/ l; s3 V9 C2 ~5 F s}& p. o" U9 z. r" i( @* n/ O$ ]
int CListCtrlEx::InsertItem (UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam)% D" l( |; o) l! E
{0 _$ i0 r4 G$ s0 z
LVITEM Item;, B8 w! ~* t8 J' b6 n4 W% _# @. i
memset(&Item,0,sizeof(LVITEM));
- E, ?. w9 b# X+ @; R! K' Y6 { Item.pszText = new TCHAR[strlen(lpszItem)+1];( B0 h8 u) E9 {" n. [! z! l; u, D
strcpy(Item.pszText,lpszItem);* M' v7 q4 @; f0 q- A, @
Item.state = nState;
" n& M; z6 D( p0 F( x+ U, U; K Item.stateMask = nStateMask;: ^+ U$ {3 }1 W3 D& n! n+ y2 Q9 p
Item.iItem = nItem;' k- n# Y/ n. D. `$ K
Item.lParam = lParam;
: O% I1 s! Q0 b; U% h; V& I Item.iImage = nImage;
; b1 t3 |( ?% M1 X( p9 S% B Item.mask = nMask;
9 w$ M& _% c4 d/ i; D BOOL bReturn = InsertItem(&Item);
U6 z* m+ ]# g% U( B2 X! g2 B delete Item.pszText;
$ z: G* C; Z- Z1 }6 L$ H; J return bReturn;+ p3 Q. H% H Q7 h3 ]' n
}: G' @% ?+ V5 k, x% J0 ] U5 \, x
BOOL CListCtrlEx:eleteItem (int nItem)9 M) Y/ { o" V+ R: p$ \7 B J4 t
{; e7 e0 F6 Y. j* X$ q
void* pDataEx = (void*)pThis->GetItemData(nItem);' \; X: Y: _1 W6 \
if(pDataEx != NULL), G8 S# c) i [- e4 T
{
. F4 K. T( @; r, E$ L2 u9 }# T delete pDataEx;
3 a( Y" {& I* N2 F5 \' _* @6 p }$ b1 J8 M7 a' m2 ^
return pThis->DeleteItem(nItem);
+ d- P6 P& v6 b}" S+ C/ I, `0 K
BOOL CListCtrlEx:eleteAllItems ()
8 A- u/ H9 ?" g' R1 l{
* e1 P3 {+ Y$ S' F' J: d int nCount = GetItemCount();
) l% N9 F9 H6 z. R for (int i = 0;i<nCount;i++)2 q& C5 i$ J/ k6 u
{
. u: x0 G) h$ g if(!DeleteItem(0))
3 P( B) M$ q! _4 z: z/ e {
. F( K) W" u$ T9 G2 [ return 0;
- K% w) F, D! H! V }
- D% H) K' t# T% c0 ? [ }
) N& I: j: Y# R/ E return 1;
) s) w9 C* L$ D! ]/ I! n}
! Z/ Q1 v0 ?/ n- |0 h7 DBOOL CListCtrlEx::GetItem(LVITEM* pItem)4 e' h" R3 {% |) i% t. ^1 c
{
+ k6 k+ R) R; h2 B BOOL bReturn = pThis->GetItem(pItem);* V+ k4 s" V z
if (pItem->lParam)5 ]* \3 P J* I
{1 A6 r5 e6 R) d+ X7 X: \/ N
pItem->lParam = ((ItemDataEx*)pItem->lParam)->SrclParam;: W' _2 O0 S& ?1 E) n/ h% ~
} return bReturn;9 Q/ [( p- g: a, s R" H
}
: l2 ?" S; o2 C X' e5 ?void CListCtrlEx::OnDestroy()! t8 i4 d1 N( H4 l8 b) K$ ]1 u
{
. _" b! C. p3 O8 r // 关键是清除申请的内存空间
1 W" B1 e( [4 k' t4 T" u( l DeleteAllItems();) d1 h6 c2 b1 h& T9 C" d% E
CListCtrl::OnDestroy();
9 O4 v$ y- w/ Y: h; a8 i! O} BOOL CListCtrlEx::SwitchItem(int nItem1, int nItem2)
l/ @$ w" R' Y1 @{
0 [& U4 l( j( A! H if (nItem1 == nItem2 || nItem1 >= GetItemCount() || nItem2 >= GetItemCount() || nItem1 < 0 || nItem2 < 0)
" j p5 D! w, F: z+ g5 r {/ d; L9 y0 k g* }. N: A4 z4 A/ s
return 0;
6 f8 U. v4 i4 \# d. s6 `( U }
8 ^* a7 a+ a% L& X, l b v& i; B LVITEM ItemData1,ItemData2;
9 _" t6 S2 U; L6 i; }5 B! t int ItemLength1 = pThis->GetItemText(nItem1,0).GetLength();& \; F4 L8 j% _3 \
int ItemLength2 = pThis->GetItemText(nItem2,0).GetLength();
, ~% v' c% c2 {/ T1 u V ItemData1.pszText = new TCHAR[ItemLength1+1];
) K5 i# v- b( C8 N, M ItemData2.pszText = new TCHAR[ItemLength2+1];
' h. b0 x; N& _6 B ItemData1.cchTextMax = ItemLength1+1;
) ^ I S. V: p) X, v) j, f ItemData2.cchTextMax = ItemLength2+1;
: K- [8 u# e$ u. f& f ItemData1.iItem = nItem1;% T: @# A v. D( f( e) S1 t
ItemData2.iItem = nItem2;
5 e6 z1 r8 [8 d ItemData1.iSubItem = ItemData2.iSubItem = 0;' d& O, }/ A1 B8 ^8 n4 x- z
ItemData1.mask = ItemData2.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE|LVIF_STATE; pThis->GetItem(&ItemData1);: j4 ^8 ?' x, w/ P+ w
pThis->GetItem(&ItemData2);" `' x! j9 s4 _. z/ K
// 交换数据2 x+ ~5 Y1 B. i
ItemData1.iItem = nItem2;! f) Z& _ T" ?. U
ItemData2.iItem = nItem1;- V2 [$ R! ~$ A
pThis->SetItem(&ItemData1);
+ i; C6 L J0 `' i6 b" l- V( A pThis->SetItem(&ItemData2); delete ItemData1.pszText;
2 M+ z$ i! q } delete ItemData2.pszText;
# } D& T* F2 u' E9 K // 得到报表模式的列数, m4 f. n2 E" X* A2 t% j4 H9 h# @$ T
int nColCount = pThis->GetHeaderCtrl()->GetItemCount();
7 r1 y( u3 A3 U" [4 r* l! }& e CString strBuffer;
# J% h* x4 O7 R% n+ p- P for (int iSubItem = 1;iSubItem < nColCount;iSubItem++)6 {4 C6 y2 x0 e/ `, Z
{, A/ @; Y. o [% e' h; W
strBuffer = pThis->GetItemText(nItem1,iSubItem);9 R2 C1 A, C$ o
pThis->SetItemText(nItem1,iSubItem,pThis->GetItemText(nItem2,iSubItem));+ e c% n% a0 h; l. e: y
pThis->SetItemText(nItem2,iSubItem,strBuffer);$ Y9 B. q W- W
}
9 s1 X& w# R$ P% _ c3 N! Q' U8 t // 把原来的选中状态也转移过去
X7 u% q; h) H! M4 n* Q // 不知道为什么这个属性没有体现在LVITEM结构中……
5 c. m' Q0 s9 v- @/ E3 N bool bItemSelected1 = GetItemState(nItem1,LVIS_SELECTED) == LVIS_SELECTED;# S2 k s6 r9 {$ Y6 t8 ^+ N" j E1 J
bool bItemSelected2 = GetItemState(nItem2,LVIS_SELECTED) == LVIS_SELECTED; SetItemState(nItem1,bItemSelected2?LVIS_SELECTED:0,LVIS_SELECTED);( [5 F/ e% O, L( V3 y
SetItemState(nItem2,bItemSelected1?LVIS_SELECTED:0,LVIS_SELECTED);$ G. g( ~, K! Y) l! C
return 1; V5 Z) X# B( o7 ^4 Z5 i- j8 L
} BOOL CListCtrlEx:reCreateWindow(CREATESTRUCT& cs)- M! H5 ~8 c$ a( w0 G
{) F3 f: O% H1 J
// 本类报表模式专用
& G X7 }) n& E/ k6 I& D3 j( P% y cs.style |= LVS_REPORT ;3 W l, u4 R& s, y
// cs.dwExStyle |= LVS_EX_FULLROWSELECT;
: t" w+ S/ {6 w. @ return CListCtrl:reCreateWindow(cs);" J- Z2 V. x9 b5 g
}
& g4 D6 E* P" Z: T3 uCOLORREF CListCtrlEx::SetHighLightColor(COLORREF clrHighLight)# `6 x E& f9 ]
{
8 I: p$ P! B0 ~( x; u" _, R COLORREF clr = GetHighLightColor();
' B/ {0 z3 ]% k) E* m- {4 O9 n( T m_clrHighLight = clrHighLight;
( z+ P# f/ v+ L; r" z return clr;
. L* T- g/ \" p} COLORREF CListCtrlEx::SetHighLightEdgeColor(COLORREF clrHighLightEdge)$ @9 u2 _/ e! h" {3 S. r; V
{
6 m# r9 l; [& W0 [7 v* j6 ` COLORREF clr = GetHighLightEdgeColor();4 d8 m0 B% {& O1 }% H
m_clrHighLightEdge = clrHighLightEdge;
+ [. z- J& ^8 |/ c% @$ ` return clr;
* u* t* ]- F/ Z9 L} |