// ---------------------------------------------
+ p# D' Z e+ O6 \$ g( H% {$ S& _// ListCtrlEx.cpp ]5 C7 V5 t" Q" a# {
// --------------------------------------------- ?& H3 {- K. s) E$ o" O! }, P
// ListCtrlEx.cpp : 实现文件
/ d- i1 X; x9 T8 |// #include "stdafx.h"
& c) H$ K( D3 {2 L6 b9 i& I* w# i/ W( g#include "CreateNewClass.h"1 c0 U) V! U! }6 ~' f) i, l+ _
#include "ListCtrlEx.h"' E& e( b. k6 k' }1 C5 L
#include ".\listctrlex.h" / F7 O0 k: Q8 m$ b* W: f) J
// CListCtrlEx IMPLEMENT_DYNAMIC(CListCtrlEx, CListCtrl)
6 d# i8 J8 `8 i( SCListCtrlEx::CListCtrlEx()
) j4 K4 D0 c6 H2 ~0 s{
! b8 G( i& W' _# B4 ^ // 用于调用基本的方法 {+ G' a& e+ O# _3 A/ M Z
pThis = (CListCtrl*)this;7 P3 ]6 y7 G7 m0 h- _; L
// 默认的选中框颜色
6 D4 Y3 D) ?1 ? m_clrHighLight = 0xF1DACC;
$ _( Z( I, b6 p$ O- J4 D2 v m_clrHighLightEdge = 0xC56A31;7 f9 v/ Y2 | B9 ~+ G
} CListCtrlEx::~CListCtrlEx()
# K% J) Q, g( d$ \ j% ~{ } , ^( i3 }# N4 y& C9 N9 E# T
BEGIN_MESSAGE_MAP(CListCtrlEx, CListCtrl)
" l9 i: U5 t1 t. Q/ U# A ON_WM_DESTROY()
5 S+ m. G5 n9 g* X/ m; A ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnNMCustomdraw)+ j( v. Z. t% g. V7 D% L
END_MESSAGE_MAP() // CListCtrlEx 消息处理程序
) V2 w0 i' u8 lCOLORREF CListCtrlEx::SetItemTextColor(int nItem,COLORREF clrText)3 g5 X- c7 Q. j7 F
{
$ F' N/ V [1 A: Q COLORREF clr = GetItemTextColor(nItem);* ~/ _; f! b# F1 P0 @- V* c
ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);
3 R$ K( t" [* Z$ Q( i pData->clrItemTextColor = clrText;
0 c. u0 @3 Z, v9 Z7 A4 I1 K( N% V return clr;7 j, p& \; X9 v* @0 [5 |8 C
} COLORREF CListCtrlEx::GetItemTextColor(int nItem) const
1 x$ A: r ]1 Z% S{ U9 `9 B) D+ T. Y0 m# _) Z! j2 e
ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);
6 o+ I9 @+ H% @( X& ^ return pData->clrItemTextColor;; @- T' Q$ F, k6 m7 r
} COLORREF CListCtrlEx::SetItemBackColor(int nItem, COLORREF clrBack)+ `: R, g8 t- U9 e; T3 Z9 ]
{
' G% `1 q0 n+ b* T, F( _ COLORREF clr = GetItemBackColor(nItem);
) G: J: b* l5 K$ K% M0 f! w6 E5 C ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);
/ C% J3 \( _* k pData->clrItemBackColor = clrBack;
- U! h- a$ c+ s return clr;3 t" A+ K+ F5 r- c1 n: O+ I
} COLORREF CListCtrlEx::GetItemBackColor(int nItem) const- ^/ O N8 ~7 Q- a7 `
{3 O0 Y3 s6 W5 ?: u
ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);
) r8 q( c" {9 n3 w3 w3 V return pData->clrItemBackColor;
# v/ \ ` N, y, Z! b+ X}
! x9 m+ d/ p* v9 dDWORD_PTR CListCtrlEx::GetItemData (int nItem) const: Z/ p" I t6 {% s
{; e0 Y9 z7 i& j6 X
if (nItem >= GetItemCount())5 t. G0 Q6 z* G. e
{
2 U/ H5 c0 d7 y$ W/ j; O return 0;
. `5 f- t' P {4 k# d }
. c5 ^: L, a& T5 ?5 y- o* v7 Y+ n4 N CListCtrl* pThis = (CListCtrl*)this;
% a" h: _0 b) X2 Q7 @. | DWORD_PTR Data = pThis->GetItemData(nItem);$ J2 q! s2 Y- @6 C. L8 H# ~
return ((ItemDataEx*)Data)->SrclParam;8 ` e1 o3 s! k8 k$ |
}
. P0 z" J+ s9 F# VBOOL CListCtrlEx::SetItemData (int nItem, DWORD_PTR dwData) E v3 Y6 ^# u4 Z
{
- e1 I+ D/ P. Q( b( n0 J! R if (nItem >= GetItemCount())( X8 m( K+ u* m8 e
{/ ~* i9 Y. R9 Q# {6 j: K6 J
return 0;
% h; N ?* Y4 |8 m5 Z }6 O& W4 C0 E) g3 B0 F; j. Y
CListCtrl* pThis = (CListCtrl*)this;9 s9 [5 A; V) X
DWORD_PTR Data = pThis->GetItemData(nItem);
" r$ v1 U0 h0 e" X2 J ASSERT(Data);: f7 g1 b2 o9 O+ {
((ItemDataEx*)Data)->SrclParam = dwData;
5 y$ Q5 W. Q/ j8 C4 _ return 1;5 c0 W& i. g; C m: s
}
2 T2 ]1 T4 E. A" \1 a1 [int CListCtrlEx::InsertItem (const LVITEM* pItem)1 s0 E. S M; c! @" Q5 `
{5 w: Q( z8 _* m2 s8 u& Y
int nReturn = pThis->InsertItem(pItem);
' A1 A4 C4 {$ y2 A( v/ w# ^3 N ItemDataEx *pData = new ItemDataEx;5 A3 U( N: D; |; y }
// 设置默认颜色
+ d1 b: F# }! d1 W$ k2 c2 ^ pData->clrItemBackColor = 0xFFFFFF;//pThis->GetTextBkColor();
/ B1 r! f( \) H' w pData->clrItemTextColor = 0x000000;//pThis->GetTextColor();& C) D0 y: v+ C, a: J
pData->SrclParam = pItem->lParam;
, `% j' Z; k1 D1 b; j pThis->SetItemData(pItem->iItem,(DWORD_PTR)pData);
( ]& [( Y: j+ y) ]* a; P- K I return nReturn;
/ g9 l: M A% r: o3 i, V}
& d9 |% N9 r' ~- z: l! g3 h+ rint CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem)
6 l7 [, [8 E4 [0 s. d! Y, {{
* C. d1 g+ A8 K LVITEM Item;, }/ V0 x; G& l$ H: U* S
memset(&Item,0,sizeof(LVITEM));4 _& F7 I$ R8 B' F) ` [ S
Item.pszText = new TCHAR[strlen(lpszItem)+1];
+ X- f% }5 V* M strcpy(Item.pszText,lpszItem);6 P7 y3 Z5 E, D% ^ @; a
Item.iItem = nItem;6 C7 M1 A5 m3 d3 Z& l
Item.lParam = 0;
5 h( n0 `) o! U7 D, I3 r7 v Item.mask = LVIF_TEXT |LVIF_PARAM;' M) r; F4 C7 P$ l$ V2 P
BOOL bReturn = InsertItem(&Item);
2 t; O; Z' h/ t# W delete Item.pszText;( D2 _7 P. i' K" h
return bReturn;# e1 _ d! t. m. U
}
* b! z% F; t4 R6 |int CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem, int nImage)
+ E4 Q$ N T: d4 O* x{
D: I2 v6 P, f4 y- W5 U LVITEM Item;( N6 b3 g( ]" M! ]: F
memset(&Item,0,sizeof(LVITEM));: _; [2 T# ?& k9 U @7 _' h% A
Item.pszText = new TCHAR[strlen(lpszItem)+1];
" W0 G6 F' B) W# C strcpy(Item.pszText,lpszItem);
/ Y. f& e2 b9 ?4 ?6 _ Item.iItem = nItem;
; c7 h7 ^. k4 i {: K( |, u$ k Item.lParam = 0;
3 x3 ?* C: M" ]/ l Item.iImage = nImage;. A; H$ ^1 s Z2 L0 V) }! L
Item.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE;
% D* Q* y7 i K' E/ j6 W) g8 N BOOL bReturn = InsertItem(&Item);
1 a5 r% m4 ~+ C+ d& p delete Item.pszText;
s$ Y0 e- H* u. W return bReturn;% a! l8 p% t* u" x; @6 I
}9 [ _/ E! ~8 R ?' ?4 j* N4 {7 U4 o4 e
int CListCtrlEx::InsertItem (UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam)( Q0 x0 x- L$ \% u/ F/ I }! y) a; I" n- P
{- G. B* G8 g& B: g( a
LVITEM Item;5 W/ `+ C& C5 m2 a
memset(&Item,0,sizeof(LVITEM));3 o: I# v8 g6 k3 { C+ I
Item.pszText = new TCHAR[strlen(lpszItem)+1];
8 w4 j& f8 `; M strcpy(Item.pszText,lpszItem);
0 c- d# P+ |6 ^, a; E1 r! u" l Item.state = nState;3 x3 c1 w) X6 b% \
Item.stateMask = nStateMask;
6 z' t9 o8 @. v, ^ Item.iItem = nItem;# T' q6 ~; K& a: ~ v* B1 @5 R
Item.lParam = lParam;; ^7 y* I1 {% G. J/ m8 E
Item.iImage = nImage;
4 h7 k- K& u; l5 U; Q Item.mask = nMask;! r" X' n1 \& B7 f9 B V6 Z
BOOL bReturn = InsertItem(&Item);
, x& a' {/ m1 |! r8 h8 f delete Item.pszText;7 I: l8 E! [% C# @% o( c' F
return bReturn;
# f9 Y" I- w( Y) e}
8 V/ a( N6 I2 a& m$ Z: e* P/ B2 bBOOL CListCtrlEx:eleteItem (int nItem)
& Q1 t( o( c0 J( D/ p# o% ^{
2 V* J- R1 }# H( G& n( g void* pDataEx = (void*)pThis->GetItemData(nItem);5 C' T3 N$ {" ^* h
if(pDataEx != NULL)* _4 }& {* P& C4 q$ Y7 a
{2 t, O, P2 _" W
delete pDataEx;/ f8 }) A+ q3 G1 {" w
}% d' L l- U6 @* g: s1 V2 ^
return pThis->DeleteItem(nItem);8 H; l, ~( v9 n3 Q% d# s
}5 w$ B( x& i4 b- x& E% z0 r2 ?
BOOL CListCtrlEx:eleteAllItems ()
8 U. C& y" `0 M3 _( D{( k$ @* p, [( p1 n9 _, N4 q
int nCount = GetItemCount();
8 k7 [! }: t# Y4 y( w5 D for (int i = 0;i<nCount;i++)0 t- O2 s# O" M* x
{# |! a0 Z4 U, e- a$ }
if(!DeleteItem(0))9 ~6 U4 v3 y* k3 @+ c
{5 p5 b3 B1 D- e+ }2 x
return 0;1 K* R: C* f; g) O) i
}
4 f$ U S. e0 v7 T }; r1 l) T W! j) k0 a. P2 s
return 1;1 n' b7 c4 S$ {) n) _: V' Y5 Y
}# S: L; f. L7 H0 P1 A: }' Z
BOOL CListCtrlEx::GetItem(LVITEM* pItem)( n- a0 s' A e0 w& e, z
{
7 R; n) V) C6 W% H8 g BOOL bReturn = pThis->GetItem(pItem);
/ `+ M' k( Y+ X+ o8 v& J if (pItem->lParam)" k( v$ ` B2 r; V
{
7 ~! b7 j; f$ N* I+ r pItem->lParam = ((ItemDataEx*)pItem->lParam)->SrclParam;9 R1 B. H0 g. H: M8 j
} return bReturn;
/ C3 D4 a; J$ q7 f% ^" N}
& K. S$ v- v/ c7 uvoid CListCtrlEx::OnDestroy()) v( e4 n7 `& ~6 N3 h& N$ B
{
/ l8 |7 y0 V2 N" X8 o8 I // 关键是清除申请的内存空间* A8 g. {" c. u% ?8 v/ M' R& u
DeleteAllItems();
^& B0 [& R4 P P1 a CListCtrl::OnDestroy();/ V8 h0 Q* R- ^/ Q) p
} BOOL CListCtrlEx::SwitchItem(int nItem1, int nItem2)) ?5 ]6 d7 Y$ T' P! P
{7 j% k& K }, L& j
if (nItem1 == nItem2 || nItem1 >= GetItemCount() || nItem2 >= GetItemCount() || nItem1 < 0 || nItem2 < 0)& P6 S1 I1 T+ P2 T
{; G" i5 ?( D: m' x- ?3 Y, X% M
return 0;7 l/ Y j) Y; w
}& I) ?' P1 t3 a& M1 O( I
LVITEM ItemData1,ItemData2;
" ~( E6 m' k5 X* T1 b9 Q9 t int ItemLength1 = pThis->GetItemText(nItem1,0).GetLength();2 R( E, A3 B7 L/ X' z1 y5 u2 C
int ItemLength2 = pThis->GetItemText(nItem2,0).GetLength();7 k Q; O5 ` p# Q! \! L- u
ItemData1.pszText = new TCHAR[ItemLength1+1];/ o3 _1 k6 N+ z% u2 P8 @
ItemData2.pszText = new TCHAR[ItemLength2+1];( Q; b, o3 q ]9 _
ItemData1.cchTextMax = ItemLength1+1;% [. j$ o* i3 {3 n. G9 ~
ItemData2.cchTextMax = ItemLength2+1;% Y4 X" c( d7 b" I# S
ItemData1.iItem = nItem1;
0 L1 |' I* W4 \; e+ U ItemData2.iItem = nItem2;9 ]& L- A2 ^+ V$ g0 z, j; V+ u
ItemData1.iSubItem = ItemData2.iSubItem = 0;
4 k7 x/ f* x# O4 F# Z ItemData1.mask = ItemData2.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE|LVIF_STATE; pThis->GetItem(&ItemData1);# N `! v3 k) ^
pThis->GetItem(&ItemData2);
! j) U& M4 i0 I: i& L) ~7 ?6 o // 交换数据% O, M5 C/ i3 }* P* D* Q2 ~8 p) q
ItemData1.iItem = nItem2;/ r4 O/ f& }3 x7 E& W- I C( Z
ItemData2.iItem = nItem1;2 E+ j) W$ R9 W X
pThis->SetItem(&ItemData1);
& b! J6 K/ A0 S* w' f1 N' E pThis->SetItem(&ItemData2); delete ItemData1.pszText;
( @5 J8 s+ m1 K9 D( {; U: ^' T: h delete ItemData2.pszText;
: K5 l5 P* {$ D6 s6 k K1 n // 得到报表模式的列数" l l; N: m1 d4 m2 C
int nColCount = pThis->GetHeaderCtrl()->GetItemCount();
/ u e" R. L5 p CString strBuffer;
" |* X( h$ {' P for (int iSubItem = 1;iSubItem < nColCount;iSubItem++)
/ w4 c0 x1 a9 ?2 c2 U { b& }# t1 l3 V! M* P5 o) v
strBuffer = pThis->GetItemText(nItem1,iSubItem);) w/ z7 H' N3 R. E6 W
pThis->SetItemText(nItem1,iSubItem,pThis->GetItemText(nItem2,iSubItem));
; X5 E, \ Z# V$ ]2 H pThis->SetItemText(nItem2,iSubItem,strBuffer);
3 L' M% |# i; }7 Z1 N7 d }
' |) Y# D, @+ W( _ // 把原来的选中状态也转移过去, Y2 {: D$ F" x3 s$ |
// 不知道为什么这个属性没有体现在LVITEM结构中……
( |5 X: J/ s x bool bItemSelected1 = GetItemState(nItem1,LVIS_SELECTED) == LVIS_SELECTED;
5 `# M: ~$ }1 ~ bool bItemSelected2 = GetItemState(nItem2,LVIS_SELECTED) == LVIS_SELECTED; SetItemState(nItem1,bItemSelected2?LVIS_SELECTED:0,LVIS_SELECTED);
2 n4 ]8 f0 M* ] SetItemState(nItem2,bItemSelected1?LVIS_SELECTED:0,LVIS_SELECTED);7 i; J5 L0 z3 O- [
return 1;7 x( W$ `* y" L; N l, |1 a% r/ w
} BOOL CListCtrlEx:reCreateWindow(CREATESTRUCT& cs)& P+ ?- e" V: E
{: @0 [: M9 h. @. `0 _0 Z
// 本类报表模式专用 c0 m6 C, h: @ o% J
cs.style |= LVS_REPORT ;
5 Z) {6 k+ n' b1 {* D3 } // cs.dwExStyle |= LVS_EX_FULLROWSELECT;
9 l3 ~' O- c1 E# H T return CListCtrl:reCreateWindow(cs);
& y7 r8 {1 u, R9 ^: T. C} 0 g- s; H7 r# F" Z7 ~5 \) L
COLORREF CListCtrlEx::SetHighLightColor(COLORREF clrHighLight)0 P: I `3 k4 u& r" @
{: r; h4 U1 F4 S* ~8 O, h
COLORREF clr = GetHighLightColor();6 d& Y+ e) s- s
m_clrHighLight = clrHighLight;
$ ~/ S6 t+ s9 O( w return clr;6 ~& t$ N% r0 `. Y' J
} COLORREF CListCtrlEx::SetHighLightEdgeColor(COLORREF clrHighLightEdge)
0 d# F" ?) K3 T, m{
. ]! ` {# r) o1 I( A5 l4 u COLORREF clr = GetHighLightEdgeColor();
( }# v/ J& T3 l4 M5 T. g# E- t m_clrHighLightEdge = clrHighLightEdge;
a) D: n( {' x return clr;. N& U: n( o# n; |
} |