// ---------------------------------------------
: D% W, Q9 [+ Y: L) H. Z7 [// ListCtrlEx.cpp
+ B; S- f7 W; d// ---------------------------------------------0 s0 R3 v1 d0 m- x! E
// ListCtrlEx.cpp : 实现文件# P& t0 Y( K* s8 c
// #include "stdafx.h"
4 Y2 K" C: n' ?3 Z& s#include "CreateNewClass.h"
) u; O5 m4 p( Y1 s$ D" X7 {#include "ListCtrlEx.h"
1 W1 a$ R& V" s0 u1 D1 x I#include ".\listctrlex.h" ' B! a6 p' b- b' \) _ j% t6 Y
// CListCtrlEx IMPLEMENT_DYNAMIC(CListCtrlEx, CListCtrl)
( ~. I: I$ D, B0 RCListCtrlEx::CListCtrlEx()
F3 C! I) `+ M& a. n+ S$ d{9 Z3 b& T2 v! v: Y/ m& t
// 用于调用基本的方法6 F% E( P% h, t* M9 q
pThis = (CListCtrl*)this;
( l7 ^7 D+ W1 V$ @! q/ I& Q8 I // 默认的选中框颜色4 r6 E# \) [0 S7 Z. o
m_clrHighLight = 0xF1DACC;
1 A! j5 N3 S- k: R$ [ m_clrHighLightEdge = 0xC56A31;5 Y) o; Y: V1 O* s" k
} CListCtrlEx::~CListCtrlEx()
( a, s$ K! C, U! H8 r, y- C{ } & x1 H. v) Z3 G( C7 h( c ^3 w
BEGIN_MESSAGE_MAP(CListCtrlEx, CListCtrl)
' P# q% u. m& X! o5 Y) v9 t+ ? ON_WM_DESTROY()* _# t) ^; W4 e, w! B
ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnNMCustomdraw)5 d3 d4 j# h* G$ G
END_MESSAGE_MAP() // CListCtrlEx 消息处理程序 1 f5 s* c( f+ |4 ]; V% L, h) P. L
COLORREF CListCtrlEx::SetItemTextColor(int nItem,COLORREF clrText)% }5 Q# e/ K+ ~
{
s5 X! l( i1 O9 _5 R* j COLORREF clr = GetItemTextColor(nItem);4 h: Z; b6 R; b1 v+ W2 T
ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);
1 p' F+ `- \4 g$ R" `# | pData->clrItemTextColor = clrText;( \5 C3 C- h6 g/ z0 Y
return clr;( J" z5 H: W! Z q
} COLORREF CListCtrlEx::GetItemTextColor(int nItem) const( Y6 \& r- _2 j! d
{ [( N: s' P6 p$ {; d
ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem); p) A1 V3 y8 \0 c
return pData->clrItemTextColor;
- S% L j7 g( V7 c7 Y6 S Q} COLORREF CListCtrlEx::SetItemBackColor(int nItem, COLORREF clrBack)
+ }- f: ]9 l! b! G4 b- Z1 w3 m{
$ }$ _. z6 o4 ?/ ?$ D* Z' S8 Q COLORREF clr = GetItemBackColor(nItem);
' O$ _6 v1 n% X1 T ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);
9 X9 p' E3 N7 O0 O' h) ^$ m pData->clrItemBackColor = clrBack;- U/ J' }- P7 s5 [2 R5 n* H
return clr;% O( I9 c6 R9 v" [+ d6 u' x8 z
} COLORREF CListCtrlEx::GetItemBackColor(int nItem) const" y1 _$ e9 u7 a& ?; ~7 F8 Y5 u* k
{0 ]% E4 G1 j$ }: Z |; N) u. ^
ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);" w. y+ d3 o7 \
return pData->clrItemBackColor;
( ?- D. ?. ^$ v& |2 ~8 m* R}
3 c4 X2 ^, g! N: Q/ I1 `DWORD_PTR CListCtrlEx::GetItemData (int nItem) const
, W) [$ w* h& U+ x/ z/ W2 u{
& `1 U/ I& j& D2 c! M2 b. P' V if (nItem >= GetItemCount())
; J6 G c' C4 M% w {
8 m9 W6 r" a5 d return 0;. b1 e; \7 p" A
}
3 J" ^" X1 m' Q) n0 S" U CListCtrl* pThis = (CListCtrl*)this;- j0 F$ m x8 P* g7 H* X
DWORD_PTR Data = pThis->GetItemData(nItem);
+ k0 I$ j, v H3 z% i3 X return ((ItemDataEx*)Data)->SrclParam;$ s' q. U# \7 p4 N* _
}# \! C. L. C) B& p- R
BOOL CListCtrlEx::SetItemData (int nItem, DWORD_PTR dwData)9 n+ v) P( ?0 s* s5 D" _6 ?
{/ K8 b- u4 ?2 t0 B- R2 S* _
if (nItem >= GetItemCount())+ }% O! L% n0 u- Q8 O
{4 z% \% P( E2 h- u; _# U' f
return 0;' u( U4 s% P; G4 H6 I% X! A; j
}# k( p4 _8 `5 H0 f
CListCtrl* pThis = (CListCtrl*)this;
' D3 K" c. ]& Z: F! [ DWORD_PTR Data = pThis->GetItemData(nItem);# L( ?( z' v* ?4 ~
ASSERT(Data);9 y& k# ?/ S, A+ z0 C N' z
((ItemDataEx*)Data)->SrclParam = dwData;
- {# T$ }, y- [! W return 1;
" Q: i0 s/ m% Y}
3 R+ D; q1 t6 s* i- fint CListCtrlEx::InsertItem (const LVITEM* pItem)
- w+ Q# D% Y* t9 u1 V{2 Z* g$ T8 N5 o% d% C1 I
int nReturn = pThis->InsertItem(pItem);/ \* N$ C% K' V5 C) p, B8 F
ItemDataEx *pData = new ItemDataEx;
8 Z3 M, I; s' G5 n) b' b // 设置默认颜色
! M3 m. X9 Z; Y2 s; ~4 c+ v pData->clrItemBackColor = 0xFFFFFF;//pThis->GetTextBkColor();3 p. |1 G- L% a
pData->clrItemTextColor = 0x000000;//pThis->GetTextColor();
3 [2 e1 ]1 K% Q( w& A4 z3 M pData->SrclParam = pItem->lParam;- I2 _8 ~9 ~" H: x
pThis->SetItemData(pItem->iItem,(DWORD_PTR)pData);
% j: L5 U( w6 E- I return nReturn;4 y: g/ h+ u3 O: ]* W$ m
}9 Q% d" K6 a: S) A! a+ Z; f
int CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem)1 e! @: `2 G3 m, n6 A, F( i9 t
{
% Y+ U5 ]% } }# l; b LVITEM Item;! H$ I- W( O. \3 U
memset(&Item,0,sizeof(LVITEM));1 C8 S& v3 Q# x7 w, W
Item.pszText = new TCHAR[strlen(lpszItem)+1];3 `0 ?, f* s! {- W
strcpy(Item.pszText,lpszItem);
1 K+ ?8 F( @( n, w+ @3 Q Item.iItem = nItem;4 S7 ]& R' f9 ~2 T; M2 Y
Item.lParam = 0;
. g+ s. R3 D; Q" ] Item.mask = LVIF_TEXT |LVIF_PARAM;/ N( R/ E0 l/ \- G! O; G
BOOL bReturn = InsertItem(&Item);
% C9 g: N9 {' f! c' n! b3 e delete Item.pszText;
, L. e8 Z& t5 } return bReturn;
! Z; u# w; {" K [4 x, a, {2 G}* O* {" m8 `. H7 U
int CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem, int nImage)( j% P- e! g. r x
{* Z7 g1 A1 u& _* R' v
LVITEM Item;
& e4 y& j+ h8 ]3 C6 K/ ^3 o# f% g memset(&Item,0,sizeof(LVITEM));
5 L) X: m3 I7 H) Z" q& [; J Item.pszText = new TCHAR[strlen(lpszItem)+1];% M% ~4 d9 L R; v. h) C
strcpy(Item.pszText,lpszItem);' E6 K) {8 w) T: x& H# z
Item.iItem = nItem;
- b c6 e; [5 c1 I* j) a# J; O( N Item.lParam = 0;0 l# b- l' J/ s: e X
Item.iImage = nImage;
9 |- @. T8 t2 N5 k9 H Item.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE;
: `4 c" t5 K. ~8 \+ n0 Y BOOL bReturn = InsertItem(&Item);
' p2 ^! l* k! {" @- J, g* M& D delete Item.pszText;+ A8 H1 ^0 H/ m+ j
return bReturn;% [* J& F' I1 d8 d0 {
}
* u6 P+ _/ V6 v& {4 h0 H, {int CListCtrlEx::InsertItem (UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam)4 F9 A9 r) o4 _3 y
{9 K0 G$ f$ \/ X W3 F' S
LVITEM Item;
- o: B# ?" w, A; _) r# ~ memset(&Item,0,sizeof(LVITEM));
0 O. a/ P, X; c# u: v6 d! p Item.pszText = new TCHAR[strlen(lpszItem)+1];2 P# r6 l1 O' @1 `$ w
strcpy(Item.pszText,lpszItem);- `2 M5 L E1 G1 Z6 Y
Item.state = nState;
2 K) L3 G% I8 i: [+ { Item.stateMask = nStateMask;6 A0 s4 q8 ?' `+ q
Item.iItem = nItem;/ P) Z1 V9 ~6 T1 F( f( F. |
Item.lParam = lParam;
5 e% ]3 L0 o% e" m L Item.iImage = nImage;
7 ~, `4 Y) }+ K f Item.mask = nMask;0 Y; D! v( Q1 t W# D# `6 W- S
BOOL bReturn = InsertItem(&Item);
( \9 V7 \ a3 g: x* c delete Item.pszText;: ~: |/ a/ G. W5 A
return bReturn; j' g: M; Q) M- }% E; u9 [
}4 @, ]# K% o& i' \9 V4 ?9 f
BOOL CListCtrlEx:eleteItem (int nItem)
) c$ u" U2 `. i! \$ I3 a2 G{2 O9 M* K2 f4 ~; V
void* pDataEx = (void*)pThis->GetItemData(nItem);* P, {/ s; }3 G
if(pDataEx != NULL)2 P3 U# c* Y" I, J
{+ _) S2 }; ^/ Q
delete pDataEx;
$ W- b* W* d$ k) x0 R9 F/ N }7 B" X; N1 N$ e7 O
return pThis->DeleteItem(nItem);
! h u8 V! C$ ~% j! g# n& Q$ T}
& p; H: Y$ ^7 b0 ]% G- R' cBOOL CListCtrlEx:eleteAllItems ()
# |* p8 {9 |0 D+ V: r1 I9 }{* T6 }6 [- A6 ]8 N" l5 R$ k: n
int nCount = GetItemCount();
- g) f" t( v k/ C6 i for (int i = 0;i<nCount;i++)6 y% m: X4 X- n
{# E1 K6 P; ]/ b! u% F
if(!DeleteItem(0))! r: q: x3 A; E; W
{0 y B4 h/ e: s. p4 p
return 0;
6 \& q' t$ W) T/ v5 X9 I+ g6 d# ?# d }
( w5 c6 j% |' A' E0 @0 e9 I }
5 Q3 _/ k# F9 q8 \" E" W3 a9 M return 1;
( B! h* k% y4 Z% m}2 @, g# [5 ]. t8 ?% \- s
BOOL CListCtrlEx::GetItem(LVITEM* pItem)
; q' `$ q [3 z+ M" T" k/ z{- c( t$ i* \4 ]9 k9 S
BOOL bReturn = pThis->GetItem(pItem);: r; @8 O6 p+ A3 y; R
if (pItem->lParam). E/ S) F6 B" ~# X
{
- L2 z; ^4 M, R7 ?( A+ { pItem->lParam = ((ItemDataEx*)pItem->lParam)->SrclParam;
) N, M4 b; ~$ z* ~1 U, Y } return bReturn;
; i2 m: N, ~- k2 X}% v- t3 N, h5 h6 A8 F; Y
void CListCtrlEx::OnDestroy()+ K ]+ L: z) e1 A% o" _
{7 }/ } D* B/ _ |( D6 y
// 关键是清除申请的内存空间5 H+ ]5 |& @* i% u2 y
DeleteAllItems();
' A" ~7 T* p; t" X* C CListCtrl::OnDestroy();
) W- Y& @* h. w8 I$ d} BOOL CListCtrlEx::SwitchItem(int nItem1, int nItem2)5 J0 T( \4 Y! e! P! x- M
{( @9 p0 X2 H8 B. c" L2 S, s9 L0 }% E0 i
if (nItem1 == nItem2 || nItem1 >= GetItemCount() || nItem2 >= GetItemCount() || nItem1 < 0 || nItem2 < 0)
- N! n/ J% o: T3 R E) S) F U7 } {8 a% o" d; w& q, h, ], ^
return 0;
9 P1 S9 ^) V( b( R' y }
; k/ _! u9 n0 s ^0 Y& S LVITEM ItemData1,ItemData2;
1 a6 R- ` f0 Y, M, Y$ J int ItemLength1 = pThis->GetItemText(nItem1,0).GetLength();: A6 J0 O6 O( [3 o& B
int ItemLength2 = pThis->GetItemText(nItem2,0).GetLength();7 U' C* w) Q5 S6 j' ]- @
ItemData1.pszText = new TCHAR[ItemLength1+1];: ~( i' f$ P1 |( F# B' b5 t
ItemData2.pszText = new TCHAR[ItemLength2+1];
$ R) J1 x- D3 K. ? D ItemData1.cchTextMax = ItemLength1+1;. `+ C; W: z2 Q6 Z% m" Z
ItemData2.cchTextMax = ItemLength2+1;
5 I! ]6 a' [3 y- D ItemData1.iItem = nItem1;
9 u; @6 H" q- b! J7 Q7 [3 e2 `% } ItemData2.iItem = nItem2;
. K. \6 X+ e) h3 W" a* X ItemData1.iSubItem = ItemData2.iSubItem = 0;
- e* q! C; o( [% x, S1 ~ ItemData1.mask = ItemData2.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE|LVIF_STATE; pThis->GetItem(&ItemData1);6 v' g, h* R# z$ n
pThis->GetItem(&ItemData2);
, q6 s8 v' Y) K1 D- w. v) Q // 交换数据
' D) G4 S( \" C/ Y. F4 b% N, e ItemData1.iItem = nItem2; k& ^/ U1 |* s( D, p+ H0 c7 T0 x
ItemData2.iItem = nItem1;8 @; W9 m# K7 V- a
pThis->SetItem(&ItemData1);- q. E3 T5 A# j0 m m6 g) z
pThis->SetItem(&ItemData2); delete ItemData1.pszText;" {# M' {% N; L% w( q
delete ItemData2.pszText;
6 O5 O- }- N8 R; U // 得到报表模式的列数
2 o9 b$ t* q) S& J0 I7 x int nColCount = pThis->GetHeaderCtrl()->GetItemCount();, i0 v8 P2 P; f+ ]3 o7 v2 }
CString strBuffer;/ Z! R/ f: [1 [' M3 _
for (int iSubItem = 1;iSubItem < nColCount;iSubItem++) f; s+ n% c) v9 V3 y( _4 |6 U
{7 g/ x- v1 |( A% A+ Y) t. w
strBuffer = pThis->GetItemText(nItem1,iSubItem);
1 ~1 u. L, v. {2 l pThis->SetItemText(nItem1,iSubItem,pThis->GetItemText(nItem2,iSubItem));1 o7 ?2 B4 c5 Q* H7 u
pThis->SetItemText(nItem2,iSubItem,strBuffer);
$ ~; `0 ?6 w3 T) c }. \4 P5 o1 u- r
// 把原来的选中状态也转移过去+ D/ N# c0 ` ?" e. ?5 @, R7 c8 ]
// 不知道为什么这个属性没有体现在LVITEM结构中……" A0 O1 B4 J# w
bool bItemSelected1 = GetItemState(nItem1,LVIS_SELECTED) == LVIS_SELECTED;
) l. D7 U: c' V! r bool bItemSelected2 = GetItemState(nItem2,LVIS_SELECTED) == LVIS_SELECTED; SetItemState(nItem1,bItemSelected2?LVIS_SELECTED:0,LVIS_SELECTED);2 {: b8 w- k4 |9 F2 d0 u: q, O
SetItemState(nItem2,bItemSelected1?LVIS_SELECTED:0,LVIS_SELECTED);
7 I2 X E! @" p2 Y2 m; n return 1;/ T& C' I, g9 }+ B/ T
} BOOL CListCtrlEx:reCreateWindow(CREATESTRUCT& cs)$ m- f6 P4 ^# o7 }
{
: q% E; _# m- X } // 本类报表模式专用
- c% t$ S9 p& o1 }! a- V cs.style |= LVS_REPORT ;% ~7 D; Q% z9 D) x" g
// cs.dwExStyle |= LVS_EX_FULLROWSELECT;
6 V, X" R9 M N: l. \6 Y return CListCtrl:reCreateWindow(cs);% N1 N1 I0 W- A: K
} # H0 A+ m' P z3 ~6 p
COLORREF CListCtrlEx::SetHighLightColor(COLORREF clrHighLight)
. x! S: x& Q6 G, K4 x{5 A, g" i8 W. m0 F8 W0 m
COLORREF clr = GetHighLightColor();
, G2 L7 S7 r% F7 o* F. s m_clrHighLight = clrHighLight;- V3 o4 x _; d. C1 h. ]
return clr;
( p, W, `( L2 @, H/ Y: ?; H} COLORREF CListCtrlEx::SetHighLightEdgeColor(COLORREF clrHighLightEdge)& F- T7 `% U. h* d. A" s4 M7 }
{1 w$ L( N; T! X s7 ?: E
COLORREF clr = GetHighLightEdgeColor();
6 J- l: k" f: O# q. ]( v m_clrHighLightEdge = clrHighLightEdge;
- {- ~& D9 g, r. l! A: C return clr;
2 J/ \2 \, c- X; k, g) `+ c} |