// ---------------------------------------------/ _' O9 s) Z( U1 S( h4 _" j
// ListCtrlEx.cpp
0 [$ e* W# m- l; s/ P// ---------------------------------------------
7 Q# d) p2 [! k0 U// ListCtrlEx.cpp : 实现文件
9 n0 { L4 ]- d1 h. p// #include "stdafx.h"& a! x" r- E$ r
#include "CreateNewClass.h"
5 W9 B% _+ x/ M0 K1 s( N; G4 j, J#include "ListCtrlEx.h"
! s1 d' Z; [% R+ {' ?#include ".\listctrlex.h" ) r9 Q5 j' M7 v" ?3 L
// CListCtrlEx IMPLEMENT_DYNAMIC(CListCtrlEx, CListCtrl)' A: z7 h6 ?7 m. o2 d
CListCtrlEx::CListCtrlEx()
2 ~, }# C; x- P, U) h{' ]. b' g* K* f; } j
// 用于调用基本的方法( J& p0 m9 P" L2 V' S* o
pThis = (CListCtrl*)this;1 f0 a# L. A+ I, k4 @
// 默认的选中框颜色) [0 i; }7 Y0 u) X' d1 r% q" v
m_clrHighLight = 0xF1DACC;
5 H. T! W- d: N1 m/ g m_clrHighLightEdge = 0xC56A31;
, @! G1 M/ Q7 J5 D4 K} CListCtrlEx::~CListCtrlEx()
8 t! t! t& m* ]9 ?! g{ }
6 f+ E1 W! w9 W7 GBEGIN_MESSAGE_MAP(CListCtrlEx, CListCtrl)
( T6 W1 e0 K9 W* j6 H- {- W) h/ i/ Y ON_WM_DESTROY()# ^2 b3 ^1 p* [4 |& s9 O$ r. }
ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnNMCustomdraw)" C& d9 A0 B! z" z( o4 T
END_MESSAGE_MAP() // CListCtrlEx 消息处理程序 ) g! t% Z) }. m0 y! \+ R- E- E6 k/ ^
COLORREF CListCtrlEx::SetItemTextColor(int nItem,COLORREF clrText)! f; f0 |# e- l; o5 j
{
6 ^- r1 J7 T+ u, w9 J COLORREF clr = GetItemTextColor(nItem);
( V. R" I- f. {0 p ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);- t9 I, s; O- Y- w3 x& D" u# o
pData->clrItemTextColor = clrText;6 [& Z1 o) f: n8 O$ T% h+ V: o' z
return clr;
+ v2 z$ u7 a9 d4 h# @} COLORREF CListCtrlEx::GetItemTextColor(int nItem) const" u) u( D" R' W
{
/ b/ E+ N% d; u" T" l' O0 O2 i ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);
& S; B }. M* Y! v: n* s3 y return pData->clrItemTextColor;
# s% p( x6 S; G& r+ Q} COLORREF CListCtrlEx::SetItemBackColor(int nItem, COLORREF clrBack)
3 @: v$ i( q+ x$ u# p6 i{
7 o% h' r: Z. J$ q& x$ C F, ^* r COLORREF clr = GetItemBackColor(nItem);0 m+ ^0 R2 ~* i9 m( n, T3 l6 W. q
ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);8 P, l4 o' p- f# r( D
pData->clrItemBackColor = clrBack;
* Q5 {2 V2 j* s# Q return clr;
4 c( G9 O6 k; L; ^} COLORREF CListCtrlEx::GetItemBackColor(int nItem) const
2 p6 c5 k2 H' f6 d/ l+ S H8 \{
4 q. o. X4 ~" S9 ` ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);. L c& O* k9 I9 u2 B
return pData->clrItemBackColor;- p4 W' V% E6 o6 ]0 l
}
1 x. g' H; f l {" @/ ODWORD_PTR CListCtrlEx::GetItemData (int nItem) const. i' g- P0 t: w |
{: u$ B1 | r p, K& G
if (nItem >= GetItemCount())0 D. m& X* O) @- H8 p" [
{
9 K0 |1 ?! o& s% f return 0;
; s6 Q; i- D$ n" @ }
* n% i' P# M. H6 \ k. q: c x CListCtrl* pThis = (CListCtrl*)this;
4 {- F- L% O% o0 f DWORD_PTR Data = pThis->GetItemData(nItem);
8 U7 f% g9 F+ P# f9 ]4 Q return ((ItemDataEx*)Data)->SrclParam;
4 o4 J# g7 a1 r6 B. D}" f, S' ]1 ^0 ^7 z0 V
BOOL CListCtrlEx::SetItemData (int nItem, DWORD_PTR dwData)8 X0 X& y/ D5 j: @: V5 D! m5 K! i4 T- D
{
* I, }' D* f5 F+ Q! v5 u0 A if (nItem >= GetItemCount())' D9 |; y7 r9 g
{( S) W: {, K+ ~+ S
return 0;3 W L' T( x; B3 n8 l5 g
}0 b; y) i; n+ ]4 s- a
CListCtrl* pThis = (CListCtrl*)this;
0 y2 h# l0 L0 } D DWORD_PTR Data = pThis->GetItemData(nItem);
w9 \4 x* h6 s* z) l' F5 \ ASSERT(Data);( t$ b/ K! P! h, U, f* O
((ItemDataEx*)Data)->SrclParam = dwData;1 \6 d5 g" k- B% b3 H3 d. A- X
return 1;. e& X0 i1 D& w6 ]& Y0 }. \
}
0 O8 o6 f/ B1 `( \0 Oint CListCtrlEx::InsertItem (const LVITEM* pItem)' \+ v- I6 z. a6 @& E
{& u' N. P0 B7 O9 l9 @2 X; G
int nReturn = pThis->InsertItem(pItem);9 y( X% _* N+ P M
ItemDataEx *pData = new ItemDataEx;
& {" y1 l9 ?( ?/ l4 B3 d! O // 设置默认颜色
1 L6 W2 W4 E# V6 g pData->clrItemBackColor = 0xFFFFFF;//pThis->GetTextBkColor();
+ t; e* l* ?7 Z2 J- a pData->clrItemTextColor = 0x000000;//pThis->GetTextColor();4 n' k# @( d/ J; b9 J0 P
pData->SrclParam = pItem->lParam;
& v" J. w" H/ C pThis->SetItemData(pItem->iItem,(DWORD_PTR)pData);9 d# o( _: j/ S1 K; H: P
return nReturn;
, o) D; C" C. X: D. p}! Q) q* I6 v7 m3 ~; A5 z
int CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem)+ ^$ r# P4 V a5 V6 Q
{
# C2 \! p; P. G! n LVITEM Item;4 V2 L& {" h0 L a1 M1 u
memset(&Item,0,sizeof(LVITEM));
6 v$ _& z& M% ^ Item.pszText = new TCHAR[strlen(lpszItem)+1];
2 U* q [* v/ Y8 r strcpy(Item.pszText,lpszItem);( y: X8 t+ n* r& Y& b
Item.iItem = nItem;
. w2 O, |/ w( \# C5 B! g& r Item.lParam = 0;6 r. m" J1 y- J5 P% B
Item.mask = LVIF_TEXT |LVIF_PARAM; X: j3 T5 h( O% W" P: d5 q
BOOL bReturn = InsertItem(&Item);
9 i* |/ H" o# z' _3 v7 I2 I delete Item.pszText;' l* T% h5 b1 z
return bReturn;0 p- c) X9 |. f" n* i
}
9 D3 l$ P4 {/ x; M2 Sint CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem, int nImage)* x/ X# L5 G R$ K) f/ m5 Z
{% S2 M. ]5 Q2 k7 H
LVITEM Item;
9 T2 E8 x& c, `; l" n4 S9 R2 f memset(&Item,0,sizeof(LVITEM));. h1 R! _! J; q* d5 R
Item.pszText = new TCHAR[strlen(lpszItem)+1];! M- \! e, B) D" O1 C' n5 E: H0 n
strcpy(Item.pszText,lpszItem);4 l( K! o0 F0 e
Item.iItem = nItem;
8 X' U; q) C# a Item.lParam = 0;
( w) t: ]* {% `0 V; \ Item.iImage = nImage;
# o4 X) v! Z9 F Item.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE;
. a1 Y R. R; ^0 F) ]! \! d1 z; O, r8 M BOOL bReturn = InsertItem(&Item);" G, Q3 ^/ U& H; T R `- \0 A
delete Item.pszText;8 Q) S+ a" a r0 F3 P# g. b0 D [
return bReturn;$ g& @) t2 l: j3 z) p7 [
}
9 ?5 |& s5 |) ^9 C# Vint CListCtrlEx::InsertItem (UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam)
; x) f x2 P$ h4 x" I; b{" M Q3 l) K, J3 j# s- y
LVITEM Item;! E; q: v% ~& J; z% v4 y+ f
memset(&Item,0,sizeof(LVITEM));4 Y8 \9 A) p | p
Item.pszText = new TCHAR[strlen(lpszItem)+1];
- J5 G$ _0 {# \, \/ O# R/ m2 x. ~ strcpy(Item.pszText,lpszItem);
j# v( v I' {! m Item.state = nState;, d, b8 G3 g) Q; ^1 a* X
Item.stateMask = nStateMask;5 {2 X# P+ }( D' I+ {* s0 L+ F
Item.iItem = nItem;
9 Y$ p, ]! C3 b6 R6 O7 [9 m H Item.lParam = lParam;
% y8 k2 {! h$ R. r ^$ O, r Item.iImage = nImage;
$ q/ e8 N: B8 L, N7 ]" i" i$ Y Item.mask = nMask;1 c% _) i' W( n v8 U1 }( g& m
BOOL bReturn = InsertItem(&Item);
6 D5 U' {( X' F( B. \2 e delete Item.pszText;
1 Y: C) N. z. K6 R l( I% T return bReturn;0 s. }% Y7 N# d4 n8 k! l
}+ \( m) U4 p! X2 H
BOOL CListCtrlEx:eleteItem (int nItem)7 y# }- @+ o6 [- y. ~' y
{' g, ~0 ^- w) o9 X% c
void* pDataEx = (void*)pThis->GetItemData(nItem);
: G, ^+ v; Q9 e+ T) \' \ if(pDataEx != NULL), M3 l" d% t1 x% _
{
: \- S& {" ]: N( k delete pDataEx;
; T3 I0 A- Y4 `6 O4 e" [' ~ }/ o" z( I2 r# }. \; r
return pThis->DeleteItem(nItem);
7 G5 k7 D. r8 W% j3 c}
$ B, V" x" O' \$ \9 fBOOL CListCtrlEx:eleteAllItems ()
/ G/ h' R0 z& x* `{
5 n/ M5 j8 i* f: t; P% e* @% \ int nCount = GetItemCount();
# A" S9 D) P* g! _, J% z1 U5 {% x for (int i = 0;i<nCount;i++)* J) w0 B. w2 F0 @" Q0 }+ }
{
& w% Z1 p8 e1 u% |& P1 e/ c, u if(!DeleteItem(0))
7 ~( N0 o+ D2 ] {
0 O( N: @ n" V% F( e% m return 0;
& V* ]& {6 S6 ^% |5 H6 r6 f }' x# s; v1 q" N' J: k1 X
}
1 K9 Q6 _( W+ p. {/ \) V return 1;
4 Z9 c" N) N. k% i. f% y}
- U1 P! R9 v0 I I: e# W1 Y" mBOOL CListCtrlEx::GetItem(LVITEM* pItem)
; z- P- j! _9 q3 E$ s1 `0 v e{
& y' f! U9 V0 h, f! K BOOL bReturn = pThis->GetItem(pItem);7 A3 C4 h- u" g0 A7 S2 n- [
if (pItem->lParam)9 ~4 X# a( r7 D7 S8 h+ W
{( `! d7 Y! v# V/ C* x, c
pItem->lParam = ((ItemDataEx*)pItem->lParam)->SrclParam;- f" x o2 B& n0 u
} return bReturn;
& a' z! q7 f# w0 K3 x: Q& H$ Y}/ m0 Z+ h: y; \5 v8 U, F
void CListCtrlEx::OnDestroy()
' V' P* B l0 E) P6 G9 t{8 S; L' h; A3 p( d( _1 h( ~
// 关键是清除申请的内存空间/ n; o1 s; u0 N; ]
DeleteAllItems();
1 R& K; M1 p* o* M2 l/ J7 | CListCtrl::OnDestroy();8 [* I. Q/ r0 F) J9 Z
} BOOL CListCtrlEx::SwitchItem(int nItem1, int nItem2)
/ L! A6 m# N5 O. h/ [9 ?{* S9 B' g& }( f/ @ ^3 W. t
if (nItem1 == nItem2 || nItem1 >= GetItemCount() || nItem2 >= GetItemCount() || nItem1 < 0 || nItem2 < 0)
) G9 L g% S7 s- O% d4 @2 x {
8 Z6 q/ u! F# [% [' Z2 L" X6 y return 0;$ @5 R" s9 s1 @0 f" e6 g
}+ w" E' H0 i0 F: w; F* v
LVITEM ItemData1,ItemData2;
$ j8 \6 ^+ a) `- i$ ~, {7 S) [ int ItemLength1 = pThis->GetItemText(nItem1,0).GetLength();) H6 c7 y; k5 \1 k, H
int ItemLength2 = pThis->GetItemText(nItem2,0).GetLength();
4 F/ `# O, H4 A7 m- [ ItemData1.pszText = new TCHAR[ItemLength1+1];
{# X6 |: ^/ {8 k ItemData2.pszText = new TCHAR[ItemLength2+1];
/ n! g9 Q o3 I7 R. d! W$ \7 Z' S ItemData1.cchTextMax = ItemLength1+1;8 V# R, a2 h+ t4 _. |- Y
ItemData2.cchTextMax = ItemLength2+1; ]7 L5 z k0 G& ]; X% ]
ItemData1.iItem = nItem1;
* A, B4 V9 I$ T3 q c# `! `! I6 F( M ItemData2.iItem = nItem2;
7 |$ E2 ?5 p# k; J' x4 g- t, D7 g1 } ItemData1.iSubItem = ItemData2.iSubItem = 0;% H# p. U, }& t4 u+ L
ItemData1.mask = ItemData2.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE|LVIF_STATE; pThis->GetItem(&ItemData1);
8 o& _! l! Z3 |4 Q pThis->GetItem(&ItemData2);
4 w( x. ^5 _0 y // 交换数据
; D- i: y; K4 I/ K& r ItemData1.iItem = nItem2;
2 ]1 M9 f4 E7 g9 f ItemData2.iItem = nItem1;
5 R/ C+ ?' m2 s9 {0 `3 G9 N0 F# G pThis->SetItem(&ItemData1);
& r* R4 ] S9 V pThis->SetItem(&ItemData2); delete ItemData1.pszText;7 ]; d. F9 s/ [9 [- O, b. I+ J" J% w! q
delete ItemData2.pszText;1 e& G2 ~# E& x1 b5 T
// 得到报表模式的列数* A c7 c6 e' A* z3 i& |
int nColCount = pThis->GetHeaderCtrl()->GetItemCount();1 X0 C8 y0 Q( v4 K
CString strBuffer;- k; Q4 ~* Z# f) |# j! \' B' s+ j7 w
for (int iSubItem = 1;iSubItem < nColCount;iSubItem++)7 r/ b: U( I4 K1 O* I( u
{
& v# n* _: r% B( P5 e strBuffer = pThis->GetItemText(nItem1,iSubItem);$ G c' v9 `( [ p
pThis->SetItemText(nItem1,iSubItem,pThis->GetItemText(nItem2,iSubItem));
& r" {, t- _ e5 E9 W" v pThis->SetItemText(nItem2,iSubItem,strBuffer);' I7 y2 e+ s9 s; n7 O. L5 a
}& A( s9 }! ]$ h1 B
// 把原来的选中状态也转移过去
" V6 p; I: m, E! I3 Q5 N" K // 不知道为什么这个属性没有体现在LVITEM结构中……3 u# s$ ^- B6 v8 J; u( {- N4 @
bool bItemSelected1 = GetItemState(nItem1,LVIS_SELECTED) == LVIS_SELECTED;; h3 Q. }3 w2 u" O7 z( x) ~
bool bItemSelected2 = GetItemState(nItem2,LVIS_SELECTED) == LVIS_SELECTED; SetItemState(nItem1,bItemSelected2?LVIS_SELECTED:0,LVIS_SELECTED);; v# p9 G" P& j. ?% L; E
SetItemState(nItem2,bItemSelected1?LVIS_SELECTED:0,LVIS_SELECTED);
; H4 v! D6 g: P7 S8 {% k% z return 1;
& Z' I! `) @9 l' `} BOOL CListCtrlEx:reCreateWindow(CREATESTRUCT& cs)1 _$ \3 ]' O* c7 u
{& z) k& Q5 Y4 G* t7 ~- J/ q/ O1 A! n# ]
// 本类报表模式专用
$ S+ L" h* O( F7 b, U$ Q1 ?1 y) R cs.style |= LVS_REPORT ;
% m; Z8 m) r9 l L // cs.dwExStyle |= LVS_EX_FULLROWSELECT;5 |) L( m3 v8 L$ [0 ]- W
return CListCtrl:reCreateWindow(cs);8 o5 A0 Y: w ]; V" j* |
}
% [' D. J# J2 gCOLORREF CListCtrlEx::SetHighLightColor(COLORREF clrHighLight)
6 }. q: g; k! u. b{6 W$ X* d7 r8 ~& w5 \- z$ L/ l
COLORREF clr = GetHighLightColor();
3 D! J) V, {. d! m1 }9 Y m_clrHighLight = clrHighLight;
+ ? w7 u r) I% _5 g return clr;/ W6 F6 r& H2 E2 D5 U
} COLORREF CListCtrlEx::SetHighLightEdgeColor(COLORREF clrHighLightEdge)4 S, p) X7 Z9 w6 b3 f {: o
{
+ |6 L0 r7 d6 j1 f COLORREF clr = GetHighLightEdgeColor();: s0 R# u& h. a5 g5 ^
m_clrHighLightEdge = clrHighLightEdge;4 w* h4 n' G, j' j$ l; ~
return clr;
0 m# z: F3 q( L} |