// ---------------------------------------------
7 Z6 C% h0 T) i// ListCtrlEx.cpp8 S: D) B+ g% ~6 W2 s1 F t
// ---------------------------------------------
C1 s# V# U+ g5 ~! d// ListCtrlEx.cpp : 实现文件
6 G7 E& Q" g% @5 M// #include "stdafx.h"
; L9 m* T( M/ ?( \1 H c#include "CreateNewClass.h"
- ^1 {% \5 t" x: I#include "ListCtrlEx.h"% c& K- S+ ~- S" S; { z
#include ".\listctrlex.h" ) I% e4 |; A w; P% W# L
// CListCtrlEx IMPLEMENT_DYNAMIC(CListCtrlEx, CListCtrl)
% U7 M% Q3 H4 Y# X1 t2 MCListCtrlEx::CListCtrlEx()
3 v5 u& \' q$ g, n{
) R/ K& R- i: {6 y" | // 用于调用基本的方法
9 L3 q H, X' C, K$ A+ M pThis = (CListCtrl*)this;! e, g9 W( H/ Y! |6 U
// 默认的选中框颜色
( |0 w5 f) _! t7 ]7 O5 j& m& k7 B m_clrHighLight = 0xF1DACC; " S2 E3 @5 v& x* z
m_clrHighLightEdge = 0xC56A31;/ ~+ r {: X9 Z& X4 Z
} CListCtrlEx::~CListCtrlEx()
5 g# f; s y y9 O0 s [9 q: W{ }
$ u$ d; Y1 O, E& j6 n; S7 QBEGIN_MESSAGE_MAP(CListCtrlEx, CListCtrl), ]! P# ~6 ?9 g- B9 i, o( I
ON_WM_DESTROY()
) x& o; j: m/ [ ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnNMCustomdraw)- b: w' t: B7 d+ k+ e8 Q
END_MESSAGE_MAP() // CListCtrlEx 消息处理程序
% w4 S, J+ Q L6 b$ O* F) fCOLORREF CListCtrlEx::SetItemTextColor(int nItem,COLORREF clrText)4 |5 r$ _! J F0 Q) m9 M& x
{
& V) W' \( O& S: K3 c' J$ Y- R COLORREF clr = GetItemTextColor(nItem);! |" v* [' _. s) o# m9 v0 H
ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);! @7 R% H( d% u3 H
pData->clrItemTextColor = clrText;
2 c+ [4 S: ?2 d0 e return clr;, ]9 K- @0 j2 m: I" ^4 m9 X6 H, ]: [
} COLORREF CListCtrlEx::GetItemTextColor(int nItem) const
9 j3 X# k1 s9 f) `{2 r* u8 ^. R) c
ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);8 y2 A6 J5 u v8 v% e8 K7 L
return pData->clrItemTextColor;
# `; j. z# J% n} COLORREF CListCtrlEx::SetItemBackColor(int nItem, COLORREF clrBack)# P" G5 P* z/ }0 M9 m! ^+ o
{
l6 T6 r0 [9 q COLORREF clr = GetItemBackColor(nItem);
# p& {5 y, \, |! m5 l- W( j+ d. a ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);
( g. i3 r0 G3 P9 B- { pData->clrItemBackColor = clrBack;
9 s: _1 B2 Y# H4 x) J# U return clr;
N, E* A& m" M7 \* i/ f0 n( r} COLORREF CListCtrlEx::GetItemBackColor(int nItem) const: ]5 g2 p& k( Y
{/ R- C" u G+ e4 x; G6 s& k( V
ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);8 R/ v$ u/ H l: P( ~& V) N
return pData->clrItemBackColor;7 `9 {6 F, N: S+ k
}
" _7 a! i B6 gDWORD_PTR CListCtrlEx::GetItemData (int nItem) const
$ ~" [5 U1 Z/ [. s{+ X0 A3 E; a) b" J2 Q+ I
if (nItem >= GetItemCount())! B* f, D9 M3 p3 Q& x0 [4 G! x
{
" S9 C3 w, l$ i2 U2 P return 0;
$ h( p( d; y" `5 u }
: `* Q4 f& Q" Y; s1 N# U$ b CListCtrl* pThis = (CListCtrl*)this;
: p) q& o; K; U3 p( U _ DWORD_PTR Data = pThis->GetItemData(nItem);
9 e$ x' \; _3 A8 ]6 u1 A return ((ItemDataEx*)Data)->SrclParam;
3 B' ~! Z) R5 W* R" T}
, i: y6 ^& D2 w9 H0 T, o) T/ k. jBOOL CListCtrlEx::SetItemData (int nItem, DWORD_PTR dwData)' _* e8 U% W( Y. c7 m$ g% D
{
7 Y7 o9 R' K" B if (nItem >= GetItemCount()) ^! u( _. Y( @5 A
{
4 I( T4 t3 V- _( [( U return 0;
- U: ]) e* @1 R. ?* C }
, I0 d: {3 g# g7 f/ |; E+ l CListCtrl* pThis = (CListCtrl*)this;, S3 N r: ~$ F5 A$ H7 c
DWORD_PTR Data = pThis->GetItemData(nItem);( {5 ?) ~! O$ ?+ q& v f
ASSERT(Data);, k$ }* ]% _/ E$ H: e
((ItemDataEx*)Data)->SrclParam = dwData;
# w+ ~. v4 g; b) g, B6 Y$ I3 J2 T return 1;4 w: B1 b, M/ S! m! |" O6 z
}( _) V% {8 `9 S
int CListCtrlEx::InsertItem (const LVITEM* pItem)8 D3 c% z7 g; z( B S
{
' D. v5 X& w" [/ ^" ?3 U4 C int nReturn = pThis->InsertItem(pItem);
4 T) H" u: W5 R8 Z& C. z ItemDataEx *pData = new ItemDataEx;
0 O) _) R$ C9 s8 @ // 设置默认颜色4 R, Q: ?$ d' s c0 J
pData->clrItemBackColor = 0xFFFFFF;//pThis->GetTextBkColor();+ |3 e% T3 I( b* I C o* j# y, U; K% L
pData->clrItemTextColor = 0x000000;//pThis->GetTextColor();
7 \& f K, P) c% ]: g pData->SrclParam = pItem->lParam;
0 J" b5 l. w2 J0 d" ^/ B: V pThis->SetItemData(pItem->iItem,(DWORD_PTR)pData);
- u7 ], |# r7 Q$ @6 A return nReturn;' Z- K# e' c" {/ D* G7 B
}2 ~. j2 f, z& N0 M
int CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem)
1 {- B1 ~. B( T' C{( K, P5 f y8 }- S! {
LVITEM Item;& D" G' V! q# z2 Y6 |
memset(&Item,0,sizeof(LVITEM));: \8 @4 Z& `2 J$ V" ~! g
Item.pszText = new TCHAR[strlen(lpszItem)+1];* d- K% k% z6 B' W
strcpy(Item.pszText,lpszItem);
6 k4 K2 `( W( |8 ^5 m O Item.iItem = nItem;4 J) ]$ Q% \* h9 ]. G, ~
Item.lParam = 0;7 f! Q! H6 ]& V' s0 h& C
Item.mask = LVIF_TEXT |LVIF_PARAM;) i' d% L/ y( A# k
BOOL bReturn = InsertItem(&Item);
$ j' Z) Y# g h2 S2 E$ s delete Item.pszText;
& g h5 s7 D8 Z return bReturn;0 S* F2 \ l8 l+ K
}
* p; N. \5 d# N# G" P3 kint CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem, int nImage) D) w+ ?6 r2 x5 C
{
/ @5 a0 n4 g L4 b. L LVITEM Item;. q1 L, B! K. `3 Q4 O |
memset(&Item,0,sizeof(LVITEM));2 O* r1 Z8 Y9 ~* G/ w1 X
Item.pszText = new TCHAR[strlen(lpszItem)+1];4 L; }& u% g8 j4 q7 s1 g- T
strcpy(Item.pszText,lpszItem);: J6 e/ R4 I8 J: D4 T
Item.iItem = nItem;4 Q, X2 f8 V) E: U( M) `; `
Item.lParam = 0;( Z' h" ^9 t' H9 S J
Item.iImage = nImage;5 t6 a+ s* t5 q3 n
Item.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE;
* T7 {! v& \- s5 r1 P6 r BOOL bReturn = InsertItem(&Item);
& k9 I: l# S) r, V, x/ r# C delete Item.pszText; p3 W' e; o* p2 v) |! j! X
return bReturn;
" q; o T( D# q$ Z7 h1 j5 h8 Y}$ ~* t0 F" b) w
int CListCtrlEx::InsertItem (UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam)
2 T, f6 x S$ V3 U: m0 x{
8 _' X7 d- p7 k* B+ z+ f F b# W LVITEM Item;
* T& P+ p2 x$ g memset(&Item,0,sizeof(LVITEM));
7 y( q% j- z) T+ }: j Item.pszText = new TCHAR[strlen(lpszItem)+1];
P' A* g: H" \3 U: V9 h* n: l: x* ]9 y strcpy(Item.pszText,lpszItem);
6 g$ f7 S3 P; } Item.state = nState;
) F+ P; P7 H+ @6 A( Q+ h Item.stateMask = nStateMask;+ C) i# {' J' c8 l7 O
Item.iItem = nItem;
1 I% @0 J& p O' Y/ e Item.lParam = lParam;
, m: h' ^. V' f" P# C5 b; V Item.iImage = nImage;; M: f5 R; p W9 |/ c0 K) L
Item.mask = nMask;$ h& e3 Z& Z4 \. V3 a, I0 ?$ K
BOOL bReturn = InsertItem(&Item);: O# f9 L$ T; }- G: t
delete Item.pszText;
: _! u0 X# ^ O" R return bReturn;& x. L7 Y5 `5 x; P
}
: G; A) u( M% v1 ~BOOL CListCtrlEx:eleteItem (int nItem)9 v! Y8 I4 ? q
{
7 ?, @7 T A) _. Z7 o void* pDataEx = (void*)pThis->GetItemData(nItem);
; ]4 n R6 ?8 s' e if(pDataEx != NULL) A' w/ v# t h% M& B1 U% f; [1 Q" D
{# B; O7 d2 a. g1 N# o
delete pDataEx;; G+ }- y% w, D: O b
}7 C* g/ H5 A7 C" Z- o( R" p1 O' ~
return pThis->DeleteItem(nItem); e) p+ o$ L3 x+ Q
}" A P+ ~/ g5 @
BOOL CListCtrlEx:eleteAllItems ()) A. [) i# r& h, x6 s$ ~/ [2 L
{
0 {. B0 r! S# e# f* x int nCount = GetItemCount();( t/ v, d8 c: r0 l9 n; \/ ?% x
for (int i = 0;i<nCount;i++)% R/ }) H1 N1 B) \7 `& O
{
4 ]0 v0 E7 d: S9 J! p if(!DeleteItem(0))
& _# r4 h, I0 j2 H9 c- K {
, w8 b- M5 i' {5 @ return 0;
- @8 u. b4 \$ d& f+ X9 Z }. L. f: X1 U; e$ c4 F
}
/ m" {1 c* p" \8 t$ u* v. d return 1;, B" Q8 ~, p" z8 ~5 T, W, S* S
}$ r0 @: ^: T" [ p' C1 v
BOOL CListCtrlEx::GetItem(LVITEM* pItem)- w5 W' f4 k2 e+ u d& ]
{
) Y- Q( ] E: X' V O, H. x, Y- L BOOL bReturn = pThis->GetItem(pItem);1 h8 C5 {$ P/ F" j6 _0 t' h% q
if (pItem->lParam)
8 L- o$ L# i. q/ d+ | {. [1 d* ]- ~. b6 z' `, p
pItem->lParam = ((ItemDataEx*)pItem->lParam)->SrclParam;
/ u6 \! Z" l# |$ z Q; ?) }9 K } return bReturn;$ [2 U* }2 {7 P# I# o# Y1 f
}
; Z2 }0 N# g/ u8 \5 mvoid CListCtrlEx::OnDestroy()9 U% L) J" P4 _3 N
{, l! ?" {' `4 _& z7 N. ?1 w
// 关键是清除申请的内存空间$ h$ U3 x4 x h$ o
DeleteAllItems();! U. a/ `1 O7 P: N
CListCtrl::OnDestroy();5 `9 O7 s) K8 w6 {. G" y, \
} BOOL CListCtrlEx::SwitchItem(int nItem1, int nItem2). } C2 w0 t4 u M, [
{- c" [' h' \" R% |/ F/ B( n
if (nItem1 == nItem2 || nItem1 >= GetItemCount() || nItem2 >= GetItemCount() || nItem1 < 0 || nItem2 < 0)
4 M; Q" z" s: v4 j" j {
# D. n8 @' R% U$ U0 T return 0;
6 n9 }( s$ W( t }
( g+ L0 {+ B9 P LVITEM ItemData1,ItemData2; R% G& Y2 l4 l) Z- ^. {
int ItemLength1 = pThis->GetItemText(nItem1,0).GetLength();
9 @5 g; m0 t+ Y) c3 o6 w int ItemLength2 = pThis->GetItemText(nItem2,0).GetLength();6 k+ Z5 n T; v
ItemData1.pszText = new TCHAR[ItemLength1+1];0 H: t# V: B5 q% _9 c" x
ItemData2.pszText = new TCHAR[ItemLength2+1];* ~" _: b6 \$ H2 R
ItemData1.cchTextMax = ItemLength1+1;
% @# H8 E5 w' i0 g# q; P ItemData2.cchTextMax = ItemLength2+1;
3 Z/ ?/ r5 _# A* E5 T8 U ItemData1.iItem = nItem1;" o' s% G* B# X4 c
ItemData2.iItem = nItem2;2 T% N/ M. n/ W! E- w
ItemData1.iSubItem = ItemData2.iSubItem = 0;& q4 f, [7 x3 U7 u( h
ItemData1.mask = ItemData2.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE|LVIF_STATE; pThis->GetItem(&ItemData1);
8 r: I7 [- _+ M; Q pThis->GetItem(&ItemData2);; m4 `0 z+ I4 B; k( d1 _: s2 J
// 交换数据: s% [7 E/ Z+ A# J4 u
ItemData1.iItem = nItem2;
8 O% U* h4 |& |. |. A8 B/ ?8 s ItemData2.iItem = nItem1;
4 _8 a1 \: R) d pThis->SetItem(&ItemData1);
& ]8 Y- F) F, d3 s: e) E! e pThis->SetItem(&ItemData2); delete ItemData1.pszText;2 Y& r' J! t: z/ g
delete ItemData2.pszText;
& i2 s; ~4 o; b // 得到报表模式的列数
/ I1 ]9 _' @6 M( y int nColCount = pThis->GetHeaderCtrl()->GetItemCount();, b* j% d1 V: s* V; p" X3 P+ I
CString strBuffer;
( I/ B- ?8 T9 s. ^* L, g for (int iSubItem = 1;iSubItem < nColCount;iSubItem++)
" ^! }0 s5 N8 _) y+ P( v& Z {
& u0 S% {( S" c strBuffer = pThis->GetItemText(nItem1,iSubItem);
* c. P* _5 _' A0 i- `+ @ pThis->SetItemText(nItem1,iSubItem,pThis->GetItemText(nItem2,iSubItem));
8 |9 ^- v0 N1 A* s pThis->SetItemText(nItem2,iSubItem,strBuffer);
- K+ @" B! N F4 y: T }
* J2 P; d' S: Q9 U // 把原来的选中状态也转移过去
+ p7 `" {: H# e# v. ?1 |, q% O // 不知道为什么这个属性没有体现在LVITEM结构中……
8 F6 |7 L( a1 N/ E6 D bool bItemSelected1 = GetItemState(nItem1,LVIS_SELECTED) == LVIS_SELECTED;' d- G3 h7 C, p: k6 ?, H. \
bool bItemSelected2 = GetItemState(nItem2,LVIS_SELECTED) == LVIS_SELECTED; SetItemState(nItem1,bItemSelected2?LVIS_SELECTED:0,LVIS_SELECTED);
# e% S4 t% H. X* {# k SetItemState(nItem2,bItemSelected1?LVIS_SELECTED:0,LVIS_SELECTED);6 f; V( e7 c# P u8 j1 z
return 1;/ c7 U+ m5 t9 m+ y, n2 `; X! V
} BOOL CListCtrlEx:reCreateWindow(CREATESTRUCT& cs). Z: h5 ~' G- d
{6 z' M( b# z) T/ E1 x' j
// 本类报表模式专用
0 }) Z1 t. a$ j1 z# i cs.style |= LVS_REPORT ;" I; Z1 @8 ^8 K
// cs.dwExStyle |= LVS_EX_FULLROWSELECT;1 N5 P% ?0 ~ l* g$ c7 w
return CListCtrl:reCreateWindow(cs); @3 |/ o' u- ` G% Z" H
}
! s/ J% B! k: B9 A' JCOLORREF CListCtrlEx::SetHighLightColor(COLORREF clrHighLight)" o+ ?$ g: q2 a( b0 ~$ P# |
{
9 z. o2 ?# v9 r9 Q; d6 ? COLORREF clr = GetHighLightColor();
& d7 i8 ^6 f' c. A9 @' T: o5 } m_clrHighLight = clrHighLight;( \# {* Q& l- x+ E. [
return clr;) x" b) u) m8 p, S- k5 H- E+ Q
} COLORREF CListCtrlEx::SetHighLightEdgeColor(COLORREF clrHighLightEdge)
; k) M/ }2 ~% e, q1 E5 h+ a{
: G1 `- O' A" z. | COLORREF clr = GetHighLightEdgeColor();
# E6 W6 k y' i6 `9 v1 p- I m_clrHighLightEdge = clrHighLightEdge;' i h3 b2 K" ^; ?
return clr;1 r0 p& k! | O* t
} |