// ---------------------------------------------+ _: \$ x' j/ W, m2 t
// ListCtrlEx.cpp
% e" K: Z# W5 b' ^' Z% _! a// ---------------------------------------------
2 ?2 |8 ?# z, o7 E4 H, x: E// ListCtrlEx.cpp : 实现文件4 X. r* x* D$ \ f S, |
// #include "stdafx.h"8 R% ~% o2 B2 k" c9 T6 V
#include "CreateNewClass.h") h' E2 h* Q e" V
#include "ListCtrlEx.h"
1 k; N. _9 u& G- \% b' K Z#include ".\listctrlex.h" ( G1 [- ^; F1 e/ r, B/ m
// CListCtrlEx IMPLEMENT_DYNAMIC(CListCtrlEx, CListCtrl)0 E" `6 t" a. e. w7 V: t; v, M0 Y
CListCtrlEx::CListCtrlEx()
- ^( t6 i+ O* V, w- z0 o{
+ W3 v8 n) G4 L, D // 用于调用基本的方法
. G9 ?6 z' o# z% V9 @3 H: V4 G pThis = (CListCtrl*)this;
: r1 @3 B" P. D: P6 }) z // 默认的选中框颜色
0 {1 x0 [: h$ K( z6 E% }: X m_clrHighLight = 0xF1DACC;
, h) ~1 j( u3 Q: v, y4 P m_clrHighLightEdge = 0xC56A31;& i& X; E7 w! a* \1 K. r8 i8 r$ u
} CListCtrlEx::~CListCtrlEx()# E, k& G# H1 t
{ } ( S' ~, k; H7 z$ e
BEGIN_MESSAGE_MAP(CListCtrlEx, CListCtrl)2 R" z) W+ ^# x
ON_WM_DESTROY()
" E7 D1 \6 J; a0 H ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnNMCustomdraw)
4 B/ ~+ s+ m' S7 C: N: XEND_MESSAGE_MAP() // CListCtrlEx 消息处理程序 , X n) d- \3 F0 s5 ~
COLORREF CListCtrlEx::SetItemTextColor(int nItem,COLORREF clrText) D# a0 z/ }, ~3 w3 C" h# g
{0 e6 p4 L G+ a+ H0 G- _
COLORREF clr = GetItemTextColor(nItem);
( e) V3 _: ~' r. K4 u- A. M ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);7 h; i& ^- V+ e8 U( Y
pData->clrItemTextColor = clrText;
9 S1 Y! T6 `4 d, `/ G; X return clr;
$ S7 c) Z; Y* K' p' E1 u} COLORREF CListCtrlEx::GetItemTextColor(int nItem) const! Q- n; l8 m% a
{, S: ]4 H% V9 K# o' m) q
ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);9 Y! ^& {7 u7 p( F& q/ I* k
return pData->clrItemTextColor;+ |+ ]8 T7 i8 C" a
} COLORREF CListCtrlEx::SetItemBackColor(int nItem, COLORREF clrBack)7 d" Z( A3 f- K, B/ m
{
v& A1 |4 i" Y' y+ E5 j* r COLORREF clr = GetItemBackColor(nItem);3 q1 Q" A G& @) k
ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem); A2 n4 e1 S. m2 C1 p* z* i% Q- R
pData->clrItemBackColor = clrBack;
! }5 J, g2 d5 G! z: P4 W$ R& d. G* W return clr;3 j, i; x7 a7 }& _' h6 Q& z
} COLORREF CListCtrlEx::GetItemBackColor(int nItem) const% P* z1 _' x# N* ^" ?) D
{
, ~! q( G; F4 t/ M2 B9 m; ]' r6 M ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);6 h, M* }) }; t' W7 m6 I+ [
return pData->clrItemBackColor;
, M+ [$ L% X0 V/ [: M}* T6 t2 N/ Z7 H' W
DWORD_PTR CListCtrlEx::GetItemData (int nItem) const
% t: U* X6 @1 }9 g# a1 j3 l" e{& N/ H* O3 K$ A
if (nItem >= GetItemCount())$ `' P2 ?+ {9 w5 h
{
+ m# `/ h6 ]+ x W5 ~ return 0;
( y1 B3 Q& W* j8 U }
2 j3 L v( X$ d& n+ X# ?( r- u CListCtrl* pThis = (CListCtrl*)this;0 W0 n& ]" k8 | y. r. V& c
DWORD_PTR Data = pThis->GetItemData(nItem);
) F0 J* W7 K+ \2 D, I- N& Z3 c return ((ItemDataEx*)Data)->SrclParam;
& A' F K3 G- O' \3 h5 `}+ u7 n, e$ G% `/ E& x/ ?5 G) C
BOOL CListCtrlEx::SetItemData (int nItem, DWORD_PTR dwData)" y$ N* v7 j6 L' e; s2 {( c. z
{$ V6 q% u! W" W4 z
if (nItem >= GetItemCount())( z( v8 G8 e, O
{
* b3 M8 X( f) A& M return 0;5 D/ |4 D- }+ C8 T! P; u9 w& J
}8 @1 v8 Z: ]% ^1 i1 [/ @9 d
CListCtrl* pThis = (CListCtrl*)this;
9 L6 G; E- C) _ DWORD_PTR Data = pThis->GetItemData(nItem);
% R0 Z$ j$ g! c1 p1 S ASSERT(Data);0 V% L' r9 q8 R1 J( t9 u6 Z* `
((ItemDataEx*)Data)->SrclParam = dwData; D$ k# v4 B5 K/ D, _7 A
return 1;
0 b, i' Z z* S2 A' H6 A+ L}: M3 q+ N8 J. }2 q3 T- i( b
int CListCtrlEx::InsertItem (const LVITEM* pItem)
: N0 Q0 j# `8 A; m1 G5 U0 g" c6 t{
$ [6 G* |1 e* N. t0 X+ O int nReturn = pThis->InsertItem(pItem);
0 Z/ K- O7 r L1 R2 ~' Y ItemDataEx *pData = new ItemDataEx;
( r7 j, V( ~4 ^ // 设置默认颜色
0 j! q/ A u0 ]1 h, c. T pData->clrItemBackColor = 0xFFFFFF;//pThis->GetTextBkColor();: x! {7 `1 c: Z( o; M0 C0 _" F
pData->clrItemTextColor = 0x000000;//pThis->GetTextColor();
" y( v' _* [( G' A pData->SrclParam = pItem->lParam;
- h4 P0 w) X/ E2 d1 j pThis->SetItemData(pItem->iItem,(DWORD_PTR)pData);9 [0 [/ W9 E$ ]/ C3 @3 d! i
return nReturn;3 [3 r3 B- U9 P3 `! G3 t) W
}
' g7 p4 O# l i* Q# t+ cint CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem)
1 ?$ o5 a4 E5 t. u{
! [9 b$ }( i& e; I6 g LVITEM Item; e/ a+ Q! Z5 ^4 ?& `
memset(&Item,0,sizeof(LVITEM));6 s4 s- s8 k# h: `3 k% {
Item.pszText = new TCHAR[strlen(lpszItem)+1];
8 W9 J3 I5 @8 m: s strcpy(Item.pszText,lpszItem);4 G: ^0 i3 A& y4 x- ~* t9 i
Item.iItem = nItem;
( X1 ~) G- Y4 @- A3 l$ ]2 w8 d Item.lParam = 0;
# T5 q' P3 O5 C% o6 U0 ] Item.mask = LVIF_TEXT |LVIF_PARAM;
; Z2 V% O! \6 `& i' R) A BOOL bReturn = InsertItem(&Item);& x/ Y; C6 T" I/ w
delete Item.pszText;" E V. N4 R) V/ e2 E& Y" ?# G2 E! b; J
return bReturn;
8 _2 i4 H( K$ c8 L L$ t}
. ]0 Z2 Z: z( R" t+ vint CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem, int nImage)
+ \2 l. q: w, @5 ]5 h{/ z: i% b/ t7 A6 f8 y! S) e- j
LVITEM Item;- ^3 V% u. o* R" Y4 c! }
memset(&Item,0,sizeof(LVITEM));
/ A G1 Y6 [4 z( w8 v Item.pszText = new TCHAR[strlen(lpszItem)+1];
5 F: v1 U2 P. t& _* Q- ^% a- c strcpy(Item.pszText,lpszItem);/ N6 W) ~$ Z6 w, n$ h# D
Item.iItem = nItem;
3 t- A; d1 {4 Q Item.lParam = 0;8 q0 [$ H2 B1 N& e; H5 P* d
Item.iImage = nImage;' ]- o6 K3 L5 s+ D; i
Item.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE;. u# u @2 @% J. w
BOOL bReturn = InsertItem(&Item);
* T# L1 d$ d6 g- Q delete Item.pszText;2 U8 L& ~) z. \. v7 K! C; E
return bReturn;
@4 [ [( L) ^7 S t}. [* g3 d5 G' _0 B6 R6 `
int CListCtrlEx::InsertItem (UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam)
4 W) _8 k9 f7 B0 A6 H( N+ B8 y/ i{
7 J$ X2 Y" a& ~6 e LVITEM Item;
* b! p/ m" }4 H7 W. j memset(&Item,0,sizeof(LVITEM));; e8 m" P0 {" c* w
Item.pszText = new TCHAR[strlen(lpszItem)+1];
) U4 j: e7 Y- N6 c& p% s7 G strcpy(Item.pszText,lpszItem);, a: _+ B3 b4 C, Y9 X
Item.state = nState;
) B8 i; }, K, Z Item.stateMask = nStateMask;+ I. Z. M, D9 u( e
Item.iItem = nItem;
) o' T) N2 E% H) z _/ n Item.lParam = lParam;, o, `* T q. J
Item.iImage = nImage;' I7 B9 j* ?; P& x1 H8 x0 |2 F- k
Item.mask = nMask;
7 w; X( F4 N! _, ?7 v BOOL bReturn = InsertItem(&Item);, k9 G! L) b/ D
delete Item.pszText;
2 B/ R1 F6 `/ w. @, n# x return bReturn;0 ]3 ~- ~: x% l9 T
}
% l: F6 S0 z! [0 F4 [2 O6 X! P) cBOOL CListCtrlEx:eleteItem (int nItem)+ s8 N7 Z5 M3 C! h. U" {
{
* n- `6 Q$ K# Y3 b7 z" v% x void* pDataEx = (void*)pThis->GetItemData(nItem);
. B& K4 L4 M- ?) w) y8 u: J7 n if(pDataEx != NULL)
5 _8 M6 e4 m O: A* s& F! } {
$ G C) @# s! p% o+ b delete pDataEx;7 [- f X6 f, C# D6 c2 g
}
9 k: y* G8 u2 B# e3 d. w8 B return pThis->DeleteItem(nItem); L" b& X+ q2 Q
}
" O* B9 H8 @& K. iBOOL CListCtrlEx:eleteAllItems ()3 v* e) T8 Y# O. a7 ]
{$ k3 j4 D: |1 L0 m' u. Q/ ?
int nCount = GetItemCount();
4 G" ^7 Z6 R1 k! \# P, Y for (int i = 0;i<nCount;i++)
& Z0 I; l7 S! c& p1 F) _5 i# B {
" z. P* v" x9 U6 \ if(!DeleteItem(0))0 L! Y$ s1 v! l
{
U S, W3 a i3 ^ return 0;5 [3 Q' j0 i: z9 l
}
5 m& d* H6 \# ^. c4 B$ I }! V( n8 @" x- P
return 1;. ]/ ?( W4 p ^
}
! T9 B/ Z1 w' q6 z7 A2 bBOOL CListCtrlEx::GetItem(LVITEM* pItem)& B3 C% C% n9 o% \2 n
{ Z J# g4 m- k/ p
BOOL bReturn = pThis->GetItem(pItem);
; m1 G1 T! {* I+ s% I if (pItem->lParam)7 ]% f7 i. y2 s7 U5 T
{4 Z7 A* `* z6 [2 `% c6 c
pItem->lParam = ((ItemDataEx*)pItem->lParam)->SrclParam;/ `* v1 d9 J6 j8 F$ {/ k" H: G7 H
} return bReturn;
) n2 h, A0 [. @6 `. X: X7 l}; o4 B. W6 \8 f* a- l6 { y# m
void CListCtrlEx::OnDestroy()
# j# C1 p# ^7 h% ~3 I{
9 l9 j4 q& Q4 @) g& q // 关键是清除申请的内存空间7 \" I$ U# E2 C1 C) I4 e/ u% j
DeleteAllItems();/ D8 p) N5 Q+ U6 D& ^7 |! c
CListCtrl::OnDestroy();
$ g; ?% v, J8 t; L% C2 }} BOOL CListCtrlEx::SwitchItem(int nItem1, int nItem2)1 s# N' T4 _8 @
{
7 {- l L0 v8 G# Q if (nItem1 == nItem2 || nItem1 >= GetItemCount() || nItem2 >= GetItemCount() || nItem1 < 0 || nItem2 < 0)
& a. t* L- y4 ~# T! D2 G2 q {
5 G8 A" W3 \1 s* A* R! a return 0;
; K+ B+ i/ m& ~& d }. ]" J, o6 y4 j3 W
LVITEM ItemData1,ItemData2;6 ^& V% W& N* z, P3 {+ v
int ItemLength1 = pThis->GetItemText(nItem1,0).GetLength();
6 b( [3 d5 O# ^6 Y( l9 J int ItemLength2 = pThis->GetItemText(nItem2,0).GetLength();& z+ L9 t- b3 y3 o, P! t
ItemData1.pszText = new TCHAR[ItemLength1+1];0 o" J3 ?2 A! X
ItemData2.pszText = new TCHAR[ItemLength2+1];" r [, }/ ?, F( _
ItemData1.cchTextMax = ItemLength1+1;+ r' K5 K/ o9 j+ q5 F/ |0 O$ e
ItemData2.cchTextMax = ItemLength2+1;
( t$ G2 I1 L4 J& n. A" s- c ItemData1.iItem = nItem1;
- ^; g2 k# E# F( t$ g# h1 J ItemData2.iItem = nItem2;
) w! w: w% f+ V! z' L# C ItemData1.iSubItem = ItemData2.iSubItem = 0;
0 h { E" X5 K0 q ]0 V: f ItemData1.mask = ItemData2.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE|LVIF_STATE; pThis->GetItem(&ItemData1);
$ X: d; x$ W/ r! s$ Y7 z pThis->GetItem(&ItemData2);" G6 l5 y& i {9 ]0 o6 N9 Y; m
// 交换数据& _, b$ c! h4 L' }' p
ItemData1.iItem = nItem2;
( E" A7 y5 c$ p" y; f1 q' l& q ItemData2.iItem = nItem1;
3 E5 G) P! V ~ pThis->SetItem(&ItemData1);
: [* w* S- A7 h9 l pThis->SetItem(&ItemData2); delete ItemData1.pszText;
( j( @+ d! N% \( O1 W( L% l* N: d delete ItemData2.pszText;
2 |: ]0 x, ^5 c8 U. y& @6 g // 得到报表模式的列数
/ b; j8 y" M2 q8 v int nColCount = pThis->GetHeaderCtrl()->GetItemCount();
& V! C9 G# L& p" X; f( J CString strBuffer;+ p# w$ f. k0 e
for (int iSubItem = 1;iSubItem < nColCount;iSubItem++)" g ?% B% O+ l8 g
{+ \' a1 T7 q9 j
strBuffer = pThis->GetItemText(nItem1,iSubItem);8 I+ g& B) M% q$ X
pThis->SetItemText(nItem1,iSubItem,pThis->GetItemText(nItem2,iSubItem));
1 D! `1 c+ ~2 [: O+ y pThis->SetItemText(nItem2,iSubItem,strBuffer);
, y9 H) @9 f9 T% `$ A1 z }. i8 d3 n- I+ P8 u4 V
// 把原来的选中状态也转移过去
; L5 o! r, W* ~; A3 V! l // 不知道为什么这个属性没有体现在LVITEM结构中……
. _$ k% b* J( j" c! n' X1 h bool bItemSelected1 = GetItemState(nItem1,LVIS_SELECTED) == LVIS_SELECTED; A6 e2 j2 h9 g' ^( z$ i
bool bItemSelected2 = GetItemState(nItem2,LVIS_SELECTED) == LVIS_SELECTED; SetItemState(nItem1,bItemSelected2?LVIS_SELECTED:0,LVIS_SELECTED);$ S' z3 r: ^& J$ ^6 B7 n/ a% _; a
SetItemState(nItem2,bItemSelected1?LVIS_SELECTED:0,LVIS_SELECTED);
- e* G( ^+ @% W7 m- R return 1;/ K7 e: Y! Q7 o9 w
} BOOL CListCtrlEx:reCreateWindow(CREATESTRUCT& cs)
& U" U0 i# W' t9 @) z{8 b1 ]9 y1 K" O
// 本类报表模式专用
! |4 y7 W& t1 m' ~& g cs.style |= LVS_REPORT ;
/ Y% p) r1 h, ?8 m- q // cs.dwExStyle |= LVS_EX_FULLROWSELECT;
6 J- B5 d+ ]' p; Q3 u' w return CListCtrl:reCreateWindow(cs);
3 o0 V- p0 a' r- i! a5 a8 c7 o}
* ~; {4 k4 }0 @7 i3 c2 T4 p3 GCOLORREF CListCtrlEx::SetHighLightColor(COLORREF clrHighLight)
9 z) i7 J$ g1 E. p{
- D( W7 A# F7 ] Q COLORREF clr = GetHighLightColor();! t( Z+ T! n( J7 ?5 ]. s% @
m_clrHighLight = clrHighLight;: k5 p. G3 d8 u9 Y7 [
return clr;1 F- }: a- [$ |
} COLORREF CListCtrlEx::SetHighLightEdgeColor(COLORREF clrHighLightEdge): y& M' r, D+ O3 O; F& L9 K1 Q
{( H) q1 i" i0 P) n2 M. F% L5 p: [
COLORREF clr = GetHighLightEdgeColor();* u1 p3 p. E& G5 u5 [
m_clrHighLightEdge = clrHighLightEdge;
+ Y4 M1 N8 v2 s1 q3 O2 r return clr;
4 ~( k- ` M3 k4 m* j/ L! c} |