// ---------------------------------------------
. h7 o& G4 b) J4 `0 t) R4 [// ListCtrlEx.cpp* R B; ^: V2 x" X
// ---------------------------------------------
l' j$ K8 G, d( M" o t// ListCtrlEx.cpp : 实现文件
3 ]8 v$ n/ X5 p8 m// #include "stdafx.h"% o% L9 ~: b4 X. B
#include "CreateNewClass.h"7 ^+ S w2 ]+ g v
#include "ListCtrlEx.h"
- a6 b4 B( A2 |# ]2 q#include ".\listctrlex.h"
3 Q1 ^9 y5 I) C9 r4 d$ c# @8 A5 q// CListCtrlEx IMPLEMENT_DYNAMIC(CListCtrlEx, CListCtrl)! h7 x2 n* U$ M ^
CListCtrlEx::CListCtrlEx(). Z. _; R2 Y- a, `
{
% S, \3 W% b1 ~% _4 ~1 _7 c // 用于调用基本的方法( m5 |$ n, e3 U* K5 g, k
pThis = (CListCtrl*)this;
- w8 \3 f* l9 ^% L7 l // 默认的选中框颜色- I4 b7 v" l+ q8 v3 r
m_clrHighLight = 0xF1DACC;
. ?" P! y% i5 |7 H3 H m_clrHighLightEdge = 0xC56A31;* a5 c7 a9 i+ A$ p9 N# c1 I
} CListCtrlEx::~CListCtrlEx()8 y0 z" }. l7 _1 W0 c' {
{ }
" ~% T2 x& W4 n& K- k% s( iBEGIN_MESSAGE_MAP(CListCtrlEx, CListCtrl)/ I/ v2 |+ G- C% V
ON_WM_DESTROY()
! g7 d6 D; x* s2 A ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnNMCustomdraw)$ a, X" W# v* Y0 J# X: _; |
END_MESSAGE_MAP() // CListCtrlEx 消息处理程序 ! G3 v" p ?+ k; }) P* Y% S' a, _
COLORREF CListCtrlEx::SetItemTextColor(int nItem,COLORREF clrText); ^0 b' L$ n( g" O7 m! m7 \
{( O5 v' j0 `# Y: V
COLORREF clr = GetItemTextColor(nItem);4 ?( R) Q3 N; y5 Y* _- z
ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);
3 i6 d3 ^& L& X+ Z pData->clrItemTextColor = clrText;4 c# ~* ]+ {/ S6 b; Y0 R3 |
return clr;! b+ C9 d% \+ _2 ?" ]8 i
} COLORREF CListCtrlEx::GetItemTextColor(int nItem) const
! K! K7 ?$ I3 S& k8 ~8 i j{2 _' N7 R- \" t/ y4 G
ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);0 K* z7 N9 C! v/ Z$ Y8 Y
return pData->clrItemTextColor;7 @. Q+ M5 b. e# Q5 j
} COLORREF CListCtrlEx::SetItemBackColor(int nItem, COLORREF clrBack)
0 b) f5 e" S r T{
' R8 S5 v, o! d5 F8 [! t1 r& r COLORREF clr = GetItemBackColor(nItem);
7 E& L+ x9 u4 O+ d/ G# U* { ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);7 G; k6 J6 J/ @9 ]' {
pData->clrItemBackColor = clrBack;) D/ M# x% [$ ~
return clr;
6 }" A! T; A) D) M3 F. Z$ `} COLORREF CListCtrlEx::GetItemBackColor(int nItem) const3 L8 m; j' F$ x; {
{( x1 ?, N/ w! n6 u
ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);
: [$ n% J9 X! j' E/ t$ B3 \ return pData->clrItemBackColor;* V5 O# A6 h1 V
}
0 E! x' A9 T0 T5 `: f3 DDWORD_PTR CListCtrlEx::GetItemData (int nItem) const6 E$ f& k! f% c$ m W x
{4 t, V4 y* k6 f6 W3 L. y, a8 u2 a$ }
if (nItem >= GetItemCount())
$ m' A8 _0 ^0 D3 n5 \ {
9 ?* U) v% Q/ Q6 Q return 0;
% | `# E' c% W9 J _! S$ s$ V+ U1 Q- y }1 P1 e; T1 [3 L* n
CListCtrl* pThis = (CListCtrl*)this;
2 E& ?, ^+ E- z0 D, R! _ DWORD_PTR Data = pThis->GetItemData(nItem);
/ L! b2 ?: ^5 F; {, P return ((ItemDataEx*)Data)->SrclParam;5 Q* G, v6 h/ A0 m% d9 F
}! J6 ]( P/ u1 \
BOOL CListCtrlEx::SetItemData (int nItem, DWORD_PTR dwData)7 L4 _ w1 Q4 J8 v( C6 W0 G+ `# ^2 L2 J
{# x& x# ~$ t' F% A
if (nItem >= GetItemCount())2 J+ D4 J* ^" j$ ~
{* X# T( J) t" j" D0 A
return 0;
, {) T8 i+ b" Z4 J& c }' _( j/ @8 O) N s, \
CListCtrl* pThis = (CListCtrl*)this;
y/ A" A1 ~0 h. r0 D DWORD_PTR Data = pThis->GetItemData(nItem);) r, ^. W/ S6 X/ g
ASSERT(Data);
* A3 y5 |5 ]) S' Y" G. E+ `: M ((ItemDataEx*)Data)->SrclParam = dwData;
' _% n. J' D+ O( j" `: w return 1;
: [4 L! m% H2 v8 Z}
V; o% F5 Q1 y% [" qint CListCtrlEx::InsertItem (const LVITEM* pItem)
0 T* t$ Y5 r" e# u{& n8 ]& G5 ^) h- d
int nReturn = pThis->InsertItem(pItem);
1 w1 \6 i7 a& t# V. O. H ItemDataEx *pData = new ItemDataEx;
- m6 ^' j0 `/ ]) ^. K# h" M // 设置默认颜色6 Y0 n0 _/ S% y' Q+ o
pData->clrItemBackColor = 0xFFFFFF;//pThis->GetTextBkColor();
& O5 I7 ~: a( r7 g8 H% c pData->clrItemTextColor = 0x000000;//pThis->GetTextColor();
* W+ J9 }. G( p, T4 u1 \ pData->SrclParam = pItem->lParam;
. P# e7 I6 y% t pThis->SetItemData(pItem->iItem,(DWORD_PTR)pData);
) O; x+ ?1 j2 Y; z6 k% D% o return nReturn;) V' O0 j d9 @% Q( B" \0 ?
}1 S: f! `5 ?, s9 `( h- v) M7 M2 }
int CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem)
7 V B4 ?' Q* E: F{6 A& M u- n: l- N
LVITEM Item;# T4 G+ Q5 |; \% e
memset(&Item,0,sizeof(LVITEM));' g6 b3 i. ?$ X3 s4 |2 q
Item.pszText = new TCHAR[strlen(lpszItem)+1];/ ~2 \ U, m* D6 n* K k! b
strcpy(Item.pszText,lpszItem);2 z1 m) W- a2 |3 q% c U
Item.iItem = nItem;4 e) J. @( _% b# S+ L; f
Item.lParam = 0;
( X8 w& D0 `2 b7 ?" ] Item.mask = LVIF_TEXT |LVIF_PARAM;% A0 c2 p8 |# C1 t; }
BOOL bReturn = InsertItem(&Item);% P0 {4 r# Y0 k( ]# A4 _ z/ s7 D
delete Item.pszText;
4 x4 |, k, f! K! W return bReturn;! D. _7 J: R3 g ]7 Q: z
}2 ?% J% c/ d( n3 a' a" ?% S
int CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem, int nImage)! H, j: i) d% |- B* l; e
{
, U* x* _$ B! R8 F. a! J LVITEM Item;
% P# O/ }3 [* c, b7 f! K M, j2 h memset(&Item,0,sizeof(LVITEM));
; w; {2 B8 q) ? [' u Item.pszText = new TCHAR[strlen(lpszItem)+1];: t6 J! q3 G1 c2 B* m* w
strcpy(Item.pszText,lpszItem);; n. a; h9 p) v5 |) P' e* K
Item.iItem = nItem;
+ X. g, v- n" ~ l6 k E Item.lParam = 0;
" @2 m" K" F V Item.iImage = nImage;
* }, g4 n( ] d! J# _6 I Item.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE;
+ K1 l" g% ?) x6 |; v8 E' T BOOL bReturn = InsertItem(&Item);
2 | A1 A1 T5 G delete Item.pszText;/ s& T( e* k2 {) A' r. ^
return bReturn;
( h) J+ t: b7 u/ k" I4 D0 v3 i* [}* z5 M7 L0 ], j/ {
int CListCtrlEx::InsertItem (UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam)8 s# d8 N! q' j7 C
{
( C1 o. t4 e& K& m: o4 Q LVITEM Item;
, z# B( z2 `0 f8 [% O1 A memset(&Item,0,sizeof(LVITEM));1 F$ b9 g3 a1 d' F
Item.pszText = new TCHAR[strlen(lpszItem)+1];8 u2 w& N! N* z" {
strcpy(Item.pszText,lpszItem);) S1 ~4 @+ L2 ~, A
Item.state = nState;: c2 Y) Y5 }$ n+ M; ]7 }5 i' L
Item.stateMask = nStateMask;
: U0 Q# k- Q1 u; H Item.iItem = nItem;6 g6 I$ [2 i; m
Item.lParam = lParam;7 A L: ^& T' {: `. E8 K6 b5 V
Item.iImage = nImage;. H' e1 D4 U7 E
Item.mask = nMask;9 i% t% d$ k* a/ B8 P
BOOL bReturn = InsertItem(&Item);
/ P {# ~# Q8 R7 P( Z/ U9 ?' P delete Item.pszText;; Y' o0 K4 m# W/ d% R: F
return bReturn;
+ H& |7 t8 u- m) H) a}+ U0 V, L/ z: K2 L7 E9 p
BOOL CListCtrlEx:eleteItem (int nItem)6 S5 v) X: `" ^4 F
{ a( h- }9 r( G
void* pDataEx = (void*)pThis->GetItemData(nItem);
& Y- ?: p4 p# c, W& j if(pDataEx != NULL)& c h* K. O3 w% C3 I
{
4 Y. S* G: s" V: _' v1 W delete pDataEx;
- `$ q0 ^- b2 B2 {, M# d }9 U/ C8 Q4 N' `+ C4 Y- f0 y& S- {
return pThis->DeleteItem(nItem);
4 M, z( c" w7 ?* @5 P3 h}
: Z8 U) L' k$ Y& w9 y& R4 l( l YBOOL CListCtrlEx:eleteAllItems ()7 R! T! t+ X; h; m1 H
{
! h0 ]; t1 A4 M% z' _" R5 n int nCount = GetItemCount();
- {4 ~# d- o% I" M; u for (int i = 0;i<nCount;i++)$ S2 E# n& c0 R3 [* W5 W+ S
{
' d- @/ d* ]- J3 k; M1 @ if(!DeleteItem(0))
2 y/ _% G2 Y% i. {/ r {2 l, O, I2 J$ T0 y! x" q% F
return 0;1 v5 u: I& N6 g- r2 Q) ]% A7 q
}
' R. K8 ~6 ?$ k" u* A }
( s% }6 K- k7 [* e return 1;
+ _% k& z3 w( U% P}/ n& X4 l( A; g9 t5 S, t1 i9 Q
BOOL CListCtrlEx::GetItem(LVITEM* pItem)( Y! m% Y, V: w6 r1 @) y2 [2 l
{
. i6 f4 v5 {: W* L BOOL bReturn = pThis->GetItem(pItem);4 A& U, i1 l/ B2 r
if (pItem->lParam), Y+ O( ?0 C- \
{
( z" c V1 u/ m. U) `4 D$ L pItem->lParam = ((ItemDataEx*)pItem->lParam)->SrclParam;, p$ L6 c( _. ]/ W4 ]% C* k& D; M" m
} return bReturn;. W; M/ V$ g0 }: d* }4 G- Z2 p; \
}: k4 @% t- m! d) S5 [8 m, o, n
void CListCtrlEx::OnDestroy()! Y( r: U: y% y' h5 I, O
{
# t' o8 n; ]: _- O. }4 ?0 [ // 关键是清除申请的内存空间
1 u; K) K( @/ ~2 X+ l h* K DeleteAllItems();
, ]; X9 S% L8 ^ CListCtrl::OnDestroy();
' b7 {& |* e# X} BOOL CListCtrlEx::SwitchItem(int nItem1, int nItem2)
2 U2 V+ _1 D* _{4 L: T2 n6 ^4 \3 T7 w6 P9 ^5 n4 ^
if (nItem1 == nItem2 || nItem1 >= GetItemCount() || nItem2 >= GetItemCount() || nItem1 < 0 || nItem2 < 0)
" Z+ {/ y) x. G( v {
9 x# p) S+ P2 X! Y2 v* j9 Z# g return 0;4 \6 G" c( i* h$ F% a* |, b# {
}% p) Q9 _" w$ Q h
LVITEM ItemData1,ItemData2;
0 C6 C' r2 \; i9 t& }+ { int ItemLength1 = pThis->GetItemText(nItem1,0).GetLength();1 U) a: ~! O6 E7 E1 a( m
int ItemLength2 = pThis->GetItemText(nItem2,0).GetLength();# c5 C/ q( z3 y" ?+ I* Q! g% O
ItemData1.pszText = new TCHAR[ItemLength1+1];
5 f \8 X2 V9 r$ v( \0 I) C ItemData2.pszText = new TCHAR[ItemLength2+1];0 M M+ u* f! b k s" u
ItemData1.cchTextMax = ItemLength1+1;
8 E: j: K# }' R ItemData2.cchTextMax = ItemLength2+1;( H9 s0 y% g8 j
ItemData1.iItem = nItem1;
2 z, @- j' ?+ D ItemData2.iItem = nItem2;
+ G# G$ Q3 R; s, S ItemData1.iSubItem = ItemData2.iSubItem = 0;
3 g3 t1 [& Y' @$ v) o3 D ItemData1.mask = ItemData2.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE|LVIF_STATE; pThis->GetItem(&ItemData1);
% V8 Z& K$ U- y; j2 H2 R; f pThis->GetItem(&ItemData2);' F) H8 B* w+ u1 Q
// 交换数据& V# `7 l/ A$ N3 h
ItemData1.iItem = nItem2;
. S( w1 H- m" Q ItemData2.iItem = nItem1;
' @; v1 Q7 p4 Y2 ]" h: C pThis->SetItem(&ItemData1);
# _5 I( b: w; I/ L- G pThis->SetItem(&ItemData2); delete ItemData1.pszText;
: V; \7 \# b, _- b. t delete ItemData2.pszText;1 `3 V: \ E4 O* P
// 得到报表模式的列数
/ a8 R2 V9 T5 [! @: t int nColCount = pThis->GetHeaderCtrl()->GetItemCount();
4 F- z9 `5 Y( o. j CString strBuffer;
% S6 K1 O& F) Z2 D$ ` for (int iSubItem = 1;iSubItem < nColCount;iSubItem++)1 X c$ n3 _5 a: c9 h
{
2 n# ~) O% b0 z6 V strBuffer = pThis->GetItemText(nItem1,iSubItem);. z# [2 x8 u' `
pThis->SetItemText(nItem1,iSubItem,pThis->GetItemText(nItem2,iSubItem));
8 A4 l5 k4 |2 g5 { pThis->SetItemText(nItem2,iSubItem,strBuffer);9 L7 j5 u, i" }) n: B" P
}# X, U& E8 ?. x- d
// 把原来的选中状态也转移过去( D: a2 }' O, z4 U* C! M
// 不知道为什么这个属性没有体现在LVITEM结构中……: t( M" d7 C4 r' u
bool bItemSelected1 = GetItemState(nItem1,LVIS_SELECTED) == LVIS_SELECTED;- V7 m! z* ~" y( w4 K
bool bItemSelected2 = GetItemState(nItem2,LVIS_SELECTED) == LVIS_SELECTED; SetItemState(nItem1,bItemSelected2?LVIS_SELECTED:0,LVIS_SELECTED);+ @4 m1 d7 t" z1 D6 O
SetItemState(nItem2,bItemSelected1?LVIS_SELECTED:0,LVIS_SELECTED);
& E$ U- u1 f$ n" Q" z" \9 K( p+ ] return 1;+ s" W1 _5 X; X
} BOOL CListCtrlEx:reCreateWindow(CREATESTRUCT& cs)
8 }' S, P+ z: T. S* i{
1 p* E: U$ _, }: ^$ e // 本类报表模式专用
6 c; @0 I% Q# D cs.style |= LVS_REPORT ;
5 f2 L/ A. ^9 e# G) i/ h // cs.dwExStyle |= LVS_EX_FULLROWSELECT;: N3 C% L3 D# D: K# D1 }
return CListCtrl:reCreateWindow(cs);
, ^5 Y. H, W: p; F& A' ?6 J) d2 i}
: u# X! F5 a$ | o- \COLORREF CListCtrlEx::SetHighLightColor(COLORREF clrHighLight)# z% S* c2 n+ l5 p+ C
{
4 L2 @9 t4 _) ^, ?1 x0 R COLORREF clr = GetHighLightColor();
" Y9 }+ V" V0 a2 j& [$ M9 `/ J. | m_clrHighLight = clrHighLight;: {5 t% Y8 e( P+ \5 E4 ~
return clr;4 k4 z# Y$ ?0 ]9 H; p, |
} COLORREF CListCtrlEx::SetHighLightEdgeColor(COLORREF clrHighLightEdge)
! f) @* k5 ^9 p, K* P, K) \3 b- I{- n& g4 T7 r* W
COLORREF clr = GetHighLightEdgeColor();% H) n+ r1 X/ n: `. U* K
m_clrHighLightEdge = clrHighLightEdge;
: A k* B) v( Z5 H5 `) _ return clr;) v5 m$ g% R8 l; m( R" k% h
} |