// ---------------------------------------------
- p: o# Q1 a7 R7 _2 Q// ListCtrlEx.cpp
, K' M& h4 \: L; i2 }// ---------------------------------------------/ {# Y6 o! l3 E4 w- E7 w
// ListCtrlEx.cpp : 实现文件
3 x! Y: d# v2 ^- Q* x$ A// #include "stdafx.h"
+ {3 z% F! l6 n& d; R#include "CreateNewClass.h"! Z4 U# S# |1 M( m: X
#include "ListCtrlEx.h"
: v' B2 g8 o3 |#include ".\listctrlex.h"
# k/ T* p5 w$ d, u// CListCtrlEx IMPLEMENT_DYNAMIC(CListCtrlEx, CListCtrl)
2 o3 ~ X# z' z6 D& s- `: f! s$ V+ L8 ACListCtrlEx::CListCtrlEx()
3 k" k% _: I+ I$ ]9 x+ r# v6 F0 Q1 p{
2 B: Y# c/ R% E: N! g' G // 用于调用基本的方法
. P/ c5 `( e) a- H pThis = (CListCtrl*)this;
$ B8 ~( e x; o; P // 默认的选中框颜色
3 ~( N9 E ^. I c e1 J; z m_clrHighLight = 0xF1DACC;
; x/ w0 ~' V6 d0 l6 E. [3 q) C m_clrHighLightEdge = 0xC56A31;
% v) e0 t0 r" _0 |* Q+ c} CListCtrlEx::~CListCtrlEx()) O0 u- U- v0 m% N& O! h
{ }
& W5 ^' r8 O+ D+ K7 H7 ~4 vBEGIN_MESSAGE_MAP(CListCtrlEx, CListCtrl)
! K( s, M) W- n/ L ON_WM_DESTROY()
# Q$ K! x5 h2 s5 N) M1 I% X ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnNMCustomdraw)
$ x7 S/ F5 V3 \- w, w$ JEND_MESSAGE_MAP() // CListCtrlEx 消息处理程序 ) Q' Y' z, r6 ]( M; l& Q
COLORREF CListCtrlEx::SetItemTextColor(int nItem,COLORREF clrText)) L% }8 O" N6 q' G( d( i- D
{- d( ~: e& t! X
COLORREF clr = GetItemTextColor(nItem);3 J- F |3 b7 H1 D) \1 a1 S
ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);; Z5 @* u6 B# e8 v: v# c: [
pData->clrItemTextColor = clrText;2 I2 x S, _/ Y% H
return clr;. L- ~9 ]0 i4 y7 V* f
} COLORREF CListCtrlEx::GetItemTextColor(int nItem) const
. Q3 z) Y/ B9 I ^' I& ~$ m5 m{
O. S, u) ?! r3 Q ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);9 i0 `2 b3 u9 j9 e. b0 F
return pData->clrItemTextColor;
' Q) ~. K: A, m0 D& }} COLORREF CListCtrlEx::SetItemBackColor(int nItem, COLORREF clrBack); I& I' V% ^8 r6 N9 B: J5 ]1 w
{3 x" r) ~1 [ P x- ?; q! U
COLORREF clr = GetItemBackColor(nItem);
, H( ]% g* t& ~$ i- W( e ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);8 o- [7 [2 x7 V) m# a
pData->clrItemBackColor = clrBack;7 V$ E- ^" d W0 M$ y' k
return clr;8 C( L+ g' Z1 {2 `+ i
} COLORREF CListCtrlEx::GetItemBackColor(int nItem) const9 ]9 u% ?9 G' X& \9 H. G4 ]
{( L7 W$ I: m6 {( U/ O6 _7 u
ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);! |- L" Y: E1 H0 s
return pData->clrItemBackColor;
! Z4 y; } }0 v}
% I$ ~; s r" W2 WDWORD_PTR CListCtrlEx::GetItemData (int nItem) const
$ j' n$ e8 @& Q# g' A{/ n; J5 C% M/ s. A* ]' v
if (nItem >= GetItemCount())9 @' D) r' | w: n
{
& U5 N2 N$ ?' \+ H; w" k' L return 0;: Z: O: Q9 R7 C. w" b) o) y
}6 ], u. E$ ?8 \/ a' E$ U
CListCtrl* pThis = (CListCtrl*)this;8 g4 C M- }+ f3 p
DWORD_PTR Data = pThis->GetItemData(nItem);
+ Y& a3 g7 ]7 u! C return ((ItemDataEx*)Data)->SrclParam;# `+ P" d* H2 f- B9 K0 k- G7 Q
}2 c: D4 o# m# q ~& S9 ]
BOOL CListCtrlEx::SetItemData (int nItem, DWORD_PTR dwData)
3 i- ]& O8 U! @) L& D. Q{
7 Y1 ~9 Y+ `; h, e. N% t9 j% ^ if (nItem >= GetItemCount())7 x5 E4 ~7 g, m: D* @+ k- l
{
- f Q+ ~6 V3 T return 0;4 v6 @8 ~. @% O' o7 u. n5 r
}
4 R) ?0 o7 n+ v" H' M CListCtrl* pThis = (CListCtrl*)this;
# y) z2 U* T/ \9 u/ _( T% t DWORD_PTR Data = pThis->GetItemData(nItem);
, A4 u5 S( I' E0 q5 B7 g. t ASSERT(Data);
& y) z) E) c3 G4 n ((ItemDataEx*)Data)->SrclParam = dwData;
0 J% l2 W2 q1 s V, ^ return 1;
F9 I* B3 N- I) N) b9 N! m}: W& y3 D, K4 T2 X! h
int CListCtrlEx::InsertItem (const LVITEM* pItem)
& H- N) K% E- D" I* C2 [{5 {. b0 x% m/ K( X
int nReturn = pThis->InsertItem(pItem);
0 |4 b7 ]+ w0 p } ItemDataEx *pData = new ItemDataEx;
# j2 ]3 ^3 l/ z // 设置默认颜色
3 k9 B, G# E. T4 Y7 ` pData->clrItemBackColor = 0xFFFFFF;//pThis->GetTextBkColor();
) o) }8 E. K% R+ i! j) ? pData->clrItemTextColor = 0x000000;//pThis->GetTextColor();
3 m7 {& V5 D, p/ y pData->SrclParam = pItem->lParam;
7 ?3 j3 @$ s' E. O5 L* k+ _' d, i pThis->SetItemData(pItem->iItem,(DWORD_PTR)pData);
0 E5 s- a1 X1 s, B; L9 E( g return nReturn;
" S+ e: Y) ?, b) h1 Y. y4 g}
$ x+ Y3 Z' m3 @1 H5 g5 Dint CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem)
" f% x6 m' ?0 R5 l# z{& m' h+ v J# N3 B9 X5 f! Y
LVITEM Item;6 [5 m# r; c9 h* ]$ m. W! b% G
memset(&Item,0,sizeof(LVITEM));
; ^/ ~ r4 \* T* |5 j5 p Item.pszText = new TCHAR[strlen(lpszItem)+1];
$ A5 F) m) y% M! P/ B6 T strcpy(Item.pszText,lpszItem);
1 b3 a1 h8 b9 G9 c+ L Item.iItem = nItem;5 d$ ^5 X& n1 t o3 I: @" N x
Item.lParam = 0;
4 U5 L/ N' ^- V6 z8 e* L Item.mask = LVIF_TEXT |LVIF_PARAM;" J/ X: Q0 _: w/ ]4 X/ E& S
BOOL bReturn = InsertItem(&Item);
; d$ w5 ?; D- l6 H9 J2 } delete Item.pszText;
" k# S: S6 b5 K" I& v( i2 H return bReturn;
; S; ` {- X5 z& i1 v. Q}" H8 X+ @6 J9 v' q( Y+ H* |& Q
int CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem, int nImage)
; l; a6 N) j8 x{: o; I, a' w" ~4 q+ M% O, s
LVITEM Item;3 X ]3 v3 H) L: U; |: n: ~! V
memset(&Item,0,sizeof(LVITEM));2 D- ^9 z% [" z2 ~1 u0 N
Item.pszText = new TCHAR[strlen(lpszItem)+1];
+ _6 r- a4 w, O( j! y& w) a strcpy(Item.pszText,lpszItem);
4 b9 `: x% W3 m, v1 C Item.iItem = nItem;
8 Y; Y i" }: r7 G8 P1 `$ b. X: t Item.lParam = 0;
; p! Y' a$ X) Y$ q# ^ Item.iImage = nImage;4 k1 i$ v" c3 i, r6 `
Item.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE;
6 A9 i; }+ P6 C1 [ BOOL bReturn = InsertItem(&Item);
4 H' \8 E; B. W) r; N9 b! I delete Item.pszText;
. u' p8 i: V) _& ~ return bReturn;* f- q3 J* T$ U9 L8 e5 x" Z4 x
}4 P' @/ U4 I& z4 o c, d! l
int CListCtrlEx::InsertItem (UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam)
, E" s+ X% @! p) W2 R{
# B' G0 @( E7 j f2 S- M LVITEM Item;8 W: y: m3 h6 O& a' y- n
memset(&Item,0,sizeof(LVITEM));; o; r' }% g2 F% X* J; Y) }
Item.pszText = new TCHAR[strlen(lpszItem)+1];
) U) _ O9 G, R; I( ]. R, c! L strcpy(Item.pszText,lpszItem);8 l! u6 }- q3 \6 s# R
Item.state = nState;
# @' Y% W( S: l9 T Item.stateMask = nStateMask;
) v9 H, N: F' P8 h& m, ~ Item.iItem = nItem;
' P- x; n7 Y% @$ p Item.lParam = lParam;
2 O) q6 B6 N6 D* t4 t) J% M Item.iImage = nImage;
; b- W1 t/ C9 j5 G, u Item.mask = nMask;! b$ x. @2 `) B$ W" `
BOOL bReturn = InsertItem(&Item);
V' F' r9 i' _' I delete Item.pszText;7 ~) w" E1 G; N: u4 ]8 {* P
return bReturn;6 x9 j; [, @& c. |$ S! ]4 l$ M# ?
}0 h) C n. I* R7 ~1 V3 m& m
BOOL CListCtrlEx:eleteItem (int nItem)3 g0 L. R/ @0 B. F2 T
{
* q# O7 o+ S* q& Y' ` void* pDataEx = (void*)pThis->GetItemData(nItem);1 u5 Y0 }2 T# u
if(pDataEx != NULL)
8 ~4 c: R: |5 R) C6 k7 D [ { s7 {5 J7 P% U( R% u' G
delete pDataEx;: ~: @# y) q" t1 I, M' e9 b
}7 g2 h9 a* c3 m, i
return pThis->DeleteItem(nItem);% v% z3 a! h5 X3 E7 U
}2 O+ C# b1 h( ?% s9 ^
BOOL CListCtrlEx:eleteAllItems ()
0 J% U+ }7 E$ G' C* d1 K* U8 w{
h. Q4 \6 [2 O {3 F int nCount = GetItemCount();/ q( ]- D% \" Q7 f. B
for (int i = 0;i<nCount;i++)& H- T9 ~' O- z6 `% H9 `+ G
{ x. D) W) [: B* ]
if(!DeleteItem(0))! G8 s4 o8 ?: p
{
' p$ M5 v. Y! m* d( B return 0;' k' J. f- k. |# X) g, [% }; r
}. }7 u$ G0 t K- K
}
* z: ^. p% i3 R7 R; T8 ^2 } d return 1;
$ D, A1 d. \8 M# T& Y/ {}
* d2 s( K# v2 _3 i LBOOL CListCtrlEx::GetItem(LVITEM* pItem)
# Z: V: e" E: B5 F% g8 f{7 Z( v S: G* `
BOOL bReturn = pThis->GetItem(pItem);
9 o" t5 G" n8 v0 D8 }4 ] if (pItem->lParam)
. s1 n h* ~: Z {% ^6 W0 G8 k) o3 a: P" p
pItem->lParam = ((ItemDataEx*)pItem->lParam)->SrclParam;
, r ^! o1 |! E5 j } return bReturn;, a' \! g# U6 V h
}$ y2 H/ z; X. ]% C& Q( Y
void CListCtrlEx::OnDestroy()8 `/ U5 { M) h0 ?4 j" S
{
( [( g0 \% O2 h$ o0 g // 关键是清除申请的内存空间/ E. ?2 Q/ R2 V$ z
DeleteAllItems();
+ ^" ?; A9 N1 j/ n CListCtrl::OnDestroy();
; f( |0 e4 r' q" o3 S$ g4 `7 }, ]9 l! }( L; R} BOOL CListCtrlEx::SwitchItem(int nItem1, int nItem2)" A& f8 R& V/ }- W- s7 c) r. u$ h3 `
{2 V* _, ] G5 @+ y& X [
if (nItem1 == nItem2 || nItem1 >= GetItemCount() || nItem2 >= GetItemCount() || nItem1 < 0 || nItem2 < 0)% g. N# D- r R- C$ a$ [2 V
{
& D/ l. ]9 S( p) B return 0;
2 s) c4 W# Y" y0 y: ^: z) v }" c5 ^/ O, _( I7 y4 l3 s) D* B
LVITEM ItemData1,ItemData2;/ `2 ], [8 b) l# m+ _+ j% Q8 ?
int ItemLength1 = pThis->GetItemText(nItem1,0).GetLength();, P/ L( Z: T3 F& S/ }2 q
int ItemLength2 = pThis->GetItemText(nItem2,0).GetLength();6 f O( ? F8 o2 V& p8 h; ^
ItemData1.pszText = new TCHAR[ItemLength1+1];" l9 a# l/ J9 W# l! S$ p2 z4 F
ItemData2.pszText = new TCHAR[ItemLength2+1];
9 `4 q' K- ?, w3 J; r3 V ItemData1.cchTextMax = ItemLength1+1;
% B5 S+ M# F4 \6 S/ f ItemData2.cchTextMax = ItemLength2+1;9 q9 }; ~ z! Q0 W% o' I( o2 W, o
ItemData1.iItem = nItem1;. x- n. R5 l% d
ItemData2.iItem = nItem2;
& D; t+ ^# k3 X. e/ }0 A, `0 |2 G ItemData1.iSubItem = ItemData2.iSubItem = 0;( n; Z% B9 Z, O
ItemData1.mask = ItemData2.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE|LVIF_STATE; pThis->GetItem(&ItemData1);9 o: g c( V: E- j) `5 }
pThis->GetItem(&ItemData2);
- V/ P. e/ T$ L+ k: w // 交换数据& E4 ~' m" ~0 U) b7 _0 n) M
ItemData1.iItem = nItem2;! b' c* P0 N7 y4 A1 c/ x4 J7 W+ G8 E
ItemData2.iItem = nItem1;- v. }8 o0 {2 n# U
pThis->SetItem(&ItemData1);6 h5 X8 ^! s6 E5 X% H! q
pThis->SetItem(&ItemData2); delete ItemData1.pszText;: R0 J. A, i; r8 U
delete ItemData2.pszText;: o8 N+ s6 W) Q, v3 T
// 得到报表模式的列数
* s2 T `! D8 O$ J int nColCount = pThis->GetHeaderCtrl()->GetItemCount();' t$ |4 z# `, ]+ `- r3 O; |
CString strBuffer;
& H4 ]/ j6 t) X for (int iSubItem = 1;iSubItem < nColCount;iSubItem++)8 H5 J! E" P( w3 z4 d3 Y* u/ J
{
1 G6 K$ M( H0 G4 L+ u strBuffer = pThis->GetItemText(nItem1,iSubItem);
. g) [2 c% W* W) [1 g5 ?: S6 } pThis->SetItemText(nItem1,iSubItem,pThis->GetItemText(nItem2,iSubItem));
9 }' [% K) n# f: [- C pThis->SetItemText(nItem2,iSubItem,strBuffer);( K* @5 b0 D; R
}
5 [$ u4 q Q, k2 H7 x // 把原来的选中状态也转移过去
9 ^4 j7 h* h7 K: x4 [) r // 不知道为什么这个属性没有体现在LVITEM结构中……4 s) F+ l/ F; h) K% y, a! e
bool bItemSelected1 = GetItemState(nItem1,LVIS_SELECTED) == LVIS_SELECTED;+ V5 w# e5 \4 |, S
bool bItemSelected2 = GetItemState(nItem2,LVIS_SELECTED) == LVIS_SELECTED; SetItemState(nItem1,bItemSelected2?LVIS_SELECTED:0,LVIS_SELECTED);; M2 u S" |- G s0 ?4 w
SetItemState(nItem2,bItemSelected1?LVIS_SELECTED:0,LVIS_SELECTED);
+ t" c3 k3 ]# o- d- [) s! e: a- y1 q return 1;8 F+ Q/ B. @4 K7 d# d
} BOOL CListCtrlEx:reCreateWindow(CREATESTRUCT& cs); K* v& P( ?) E7 l5 S$ }: A! j
{* f' f8 H- \6 D. _3 o7 D
// 本类报表模式专用8 H9 r$ M$ ~1 i7 T6 [. ?+ n6 D% j! o
cs.style |= LVS_REPORT ;
9 M3 p& |' {9 P. ` // cs.dwExStyle |= LVS_EX_FULLROWSELECT;. E, u, O5 R- W! h; p' X
return CListCtrl:reCreateWindow(cs);
& R, e$ Q% D9 F} 7 C* R7 W/ B Y* D% Y1 |( v
COLORREF CListCtrlEx::SetHighLightColor(COLORREF clrHighLight)
' G% w1 P: P; ?$ }3 `! O) L" Z{
( J2 G; k9 C) q1 x6 {/ H7 L) t' z COLORREF clr = GetHighLightColor();( [( n) z! b# K3 H) G3 M0 N
m_clrHighLight = clrHighLight;
, d1 B' M; H* Q& y& R4 F# N/ s return clr;5 F6 f+ ^& [# g2 @4 d
} COLORREF CListCtrlEx::SetHighLightEdgeColor(COLORREF clrHighLightEdge)7 c$ I( f2 B' H$ @8 j
{& n/ H/ l% ?2 y
COLORREF clr = GetHighLightEdgeColor();
: r1 e4 S5 N M6 ?- z m_clrHighLightEdge = clrHighLightEdge;9 @) H; v; Y+ k
return clr;
" t2 X* d$ F7 t. `- x* |} |