// ---------------------------------------------
% u7 [' L. c, x* X2 B// ListCtrlEx.cpp
* V3 v+ d" j7 t' Z// ---------------------------------------------4 c8 F6 W' e3 s/ Q" M
// ListCtrlEx.cpp : 实现文件
; ^/ ?; J2 O0 o! W; a2 u// #include "stdafx.h"
) S2 S$ r. p) T9 }#include "CreateNewClass.h"
+ X% n* R' Y0 A#include "ListCtrlEx.h"
# C3 E) H A* K8 O: J3 [6 h5 ?) i#include ".\listctrlex.h"
O$ Z( v2 K- l5 N# g/ O// CListCtrlEx IMPLEMENT_DYNAMIC(CListCtrlEx, CListCtrl)
; L) L, d0 ^0 D4 n6 }# wCListCtrlEx::CListCtrlEx()
9 G( f; T. ?* _0 e0 u! P3 a8 B* r/ A{
4 x" z/ t' @) I // 用于调用基本的方法
0 s5 ]+ H& \' }& j; }1 ^ pThis = (CListCtrl*)this;
& j+ N2 B) ` l2 K) g0 T* n" { // 默认的选中框颜色
* H# n& t: b1 }* g7 p7 e m_clrHighLight = 0xF1DACC;
0 N5 N( b* C# Z! E- I m_clrHighLightEdge = 0xC56A31;9 o6 _( y( |8 o# Z* {
} CListCtrlEx::~CListCtrlEx()
2 \' O+ Y5 |- y/ I9 Y{ } - t. f9 \" ]7 r0 Z; t
BEGIN_MESSAGE_MAP(CListCtrlEx, CListCtrl)
' [) _, [, }7 P& H" X$ w ON_WM_DESTROY()& P! H( H- g2 t2 ?
ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnNMCustomdraw)
# m" U1 f5 {3 U, vEND_MESSAGE_MAP() // CListCtrlEx 消息处理程序
" o! X& E: G) u* e& Q1 n) ?2 |COLORREF CListCtrlEx::SetItemTextColor(int nItem,COLORREF clrText)
$ N7 V; Y4 Z/ P* {1 Q+ o5 [{
4 e: y" _5 C! I1 a+ f; x0 N COLORREF clr = GetItemTextColor(nItem);$ q) V( g& `- y/ q [! _( I8 l
ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);
) {2 D" S; I" f$ \: x: |" s pData->clrItemTextColor = clrText;$ k- Z& w5 m* c6 E4 r$ R
return clr;+ @3 T$ |, C! m6 L9 o
} COLORREF CListCtrlEx::GetItemTextColor(int nItem) const
% \: P4 N: y# I2 R3 U; ]2 W{
) a, t5 A6 H! ?! d) P) M. x2 I+ [ n ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);
, t2 f" `& Z U return pData->clrItemTextColor;
9 W( o% _5 c- j} COLORREF CListCtrlEx::SetItemBackColor(int nItem, COLORREF clrBack)
% c# ^) b6 m* O' W) e: ?: ?) K{- W, z& ]7 ^. @/ t# H; c
COLORREF clr = GetItemBackColor(nItem);
2 _% b) n( J) B1 c5 j3 S# X ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);
+ Q" x5 ]/ B4 u1 Q' O0 _) e pData->clrItemBackColor = clrBack;
( i: e R6 b' s9 R) `8 f0 l7 J return clr;
' J6 _) I2 s) |5 t0 D5 w} COLORREF CListCtrlEx::GetItemBackColor(int nItem) const- i# x4 x9 c- e! O; k; s
{
4 k. O& {) v8 [2 v ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);( A( ~2 L. u2 {/ ?( N7 `' F
return pData->clrItemBackColor;% S' t1 l& d8 n4 U# W
}- O6 H& z4 g: G1 u B' ^
DWORD_PTR CListCtrlEx::GetItemData (int nItem) const2 x* l; A& W7 q: U
{
, _8 ~! [; }! f4 t% L1 R6 |/ s5 z if (nItem >= GetItemCount())
Q: }% c0 m2 ]" r' z {
$ b- A' C$ u* q! ~' j2 _* t4 a return 0;
" a5 }; f% N) _7 f5 @ }
7 o0 j: ?' f. ?# Q CListCtrl* pThis = (CListCtrl*)this;
8 X% ]2 {: u8 s8 y$ O DWORD_PTR Data = pThis->GetItemData(nItem);
" j# a Y# {) t% D return ((ItemDataEx*)Data)->SrclParam;
2 _. e! v: j( ^5 u! g$ V}% `2 W: V+ s) q
BOOL CListCtrlEx::SetItemData (int nItem, DWORD_PTR dwData)! b7 _& ` ]2 U& x4 b; [2 i
{2 N( ]2 n+ S6 U: t C) o1 p$ j: B
if (nItem >= GetItemCount())! W" I1 m' T0 `
{4 A: ]3 S! F3 d0 s
return 0;
" W8 l+ q5 J: o }
, f. Y1 j2 L4 O2 O' a7 @, B CListCtrl* pThis = (CListCtrl*)this;
! l2 [1 O+ ?- Z) v$ Y DWORD_PTR Data = pThis->GetItemData(nItem);1 C" n+ q- S# U/ k. o; @
ASSERT(Data);
' I% ]- o% Q Z/ j8 d ((ItemDataEx*)Data)->SrclParam = dwData;* A3 k& {7 r. R V9 _' e
return 1;
9 _5 R0 Y9 v$ c* Y}! ?) f9 w! l' u# X
int CListCtrlEx::InsertItem (const LVITEM* pItem)
" _7 A$ d' r2 z& u u) O+ Z' d{
, o* j/ V3 b9 |. E z/ l" } int nReturn = pThis->InsertItem(pItem);
6 B/ L2 S2 o! n6 C) D ItemDataEx *pData = new ItemDataEx;9 I+ A' Z" h: G( H: O
// 设置默认颜色7 m2 T' \0 ^$ B1 u' |
pData->clrItemBackColor = 0xFFFFFF;//pThis->GetTextBkColor();% c, j/ B% G* [6 V: w. ]4 K
pData->clrItemTextColor = 0x000000;//pThis->GetTextColor();1 g9 e+ U" n' I
pData->SrclParam = pItem->lParam;
; |1 M3 M- n0 h& x4 G( ]$ a pThis->SetItemData(pItem->iItem,(DWORD_PTR)pData);) h3 p" k' m; C* N
return nReturn;! P. d7 _, d; Z2 H, P& V# x% l
}
0 Y: x$ d; f9 y5 U9 ^, ~7 c3 E) Fint CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem). J4 ]2 B2 |8 e* c
{. o2 D" f5 c4 E
LVITEM Item;; d% ~1 l3 \8 C
memset(&Item,0,sizeof(LVITEM));' x, C" n |; o
Item.pszText = new TCHAR[strlen(lpszItem)+1];
& X+ H% Z! _4 E; k6 h8 W1 s6 e" E1 V strcpy(Item.pszText,lpszItem);$ w b# ?" h0 V
Item.iItem = nItem;; N: y2 G; w. U6 k7 o4 `5 i% F
Item.lParam = 0;7 N6 q/ H5 B" @
Item.mask = LVIF_TEXT |LVIF_PARAM;! H6 q- Y `: b5 p/ D. F' W
BOOL bReturn = InsertItem(&Item);
! q& e% M% Q" | delete Item.pszText;! R8 `3 K0 n1 B% `) x$ ~0 O
return bReturn;; u4 |! _5 H0 N! L" i2 Z7 [
}: h+ f6 {1 @6 w. G+ P# v3 x. k
int CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem, int nImage). k7 L V& h) J3 ~: v! m
{
7 L0 p- ]1 T2 l6 T) Q. `( P LVITEM Item; m. x) }; M! Y4 t+ U4 d
memset(&Item,0,sizeof(LVITEM));
; p' I2 e5 j" Z Item.pszText = new TCHAR[strlen(lpszItem)+1];
9 M) D/ U" ~# s+ `1 S0 ~ H strcpy(Item.pszText,lpszItem);
0 |/ I5 a, D7 M+ |# ^ X) ]& v5 k, n Item.iItem = nItem;
- j, @3 H t+ D# o) H% R Item.lParam = 0;
j# k# R$ O0 M0 `0 p Item.iImage = nImage;
( r3 x0 w7 \" x: A7 C) @: E Item.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE;6 @ S7 x2 Z. f# N, e
BOOL bReturn = InsertItem(&Item);
e8 c: y: b% Z6 h3 D delete Item.pszText;
6 z8 e6 X' s' i% q# f2 A5 A! W return bReturn;
" v; }! P5 w m/ Z}
+ H5 C0 ]6 i7 s: `/ dint CListCtrlEx::InsertItem (UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam)
& I* N8 R: h; B# G{
; _( N q ~. {- R LVITEM Item;* p% M& o) h' B7 t5 _ X U
memset(&Item,0,sizeof(LVITEM));
+ y# u9 B* Y& F Item.pszText = new TCHAR[strlen(lpszItem)+1];/ s3 z( q r* p) e. m4 ?/ `
strcpy(Item.pszText,lpszItem);5 G% ~; Y: X. m7 B: p) C, V
Item.state = nState;# X7 v9 m. `+ l* @ g
Item.stateMask = nStateMask;3 g0 S' Q8 Z2 v4 S9 u
Item.iItem = nItem;
+ D: r3 p% S7 h4 n! s& `, Z7 \ Item.lParam = lParam;5 T3 i+ Y9 y8 C: c# d3 a1 l" A. s" |" C
Item.iImage = nImage;
0 P. ^: e7 t0 U7 G: T Item.mask = nMask;
) x2 k* H8 w" ~9 X" D7 V: w; o BOOL bReturn = InsertItem(&Item);4 H, m. J1 l) [& A: [) J2 f
delete Item.pszText;7 {3 S Q# I/ k, b
return bReturn;3 d: a: M& u7 g0 r
}
4 E* g/ N& X) R" [+ K/ A! ]BOOL CListCtrlEx:eleteItem (int nItem)+ F# { D" x& ?' A5 C6 ^0 z
{; F8 E0 c' d. f
void* pDataEx = (void*)pThis->GetItemData(nItem);
6 Q4 W6 a3 _+ v$ ` if(pDataEx != NULL)5 n3 t5 y2 ]% Y' g( a7 B9 U2 q/ I! A
{( y) ]3 Y# y% t. L0 o
delete pDataEx;$ h& T1 G4 N% ~' x7 z* D4 e
}* s% G9 g+ D1 A" @; @# T
return pThis->DeleteItem(nItem);2 C1 g6 H% O$ @/ T/ S+ b
}
& F# G9 H& c" k7 F/ Q; pBOOL CListCtrlEx:eleteAllItems ()
6 g8 o; [& @( G1 c* g; a5 b. J{
T1 D; |8 s: l* f int nCount = GetItemCount();- O6 ` b3 s4 _, \9 }
for (int i = 0;i<nCount;i++). F3 k1 u' Y( l& ~7 d
{
2 t. S* y' Y5 s' b, i! N7 Q if(!DeleteItem(0))9 {" G- R/ a. e F# b- @
{3 G* V( K, F7 Y7 h* J* i
return 0;4 x' U3 h, N/ V ~# a# p* H) C# z
}
" ] ~3 ] j0 O# G7 R }5 a* e& s4 e3 f. Y+ @' D8 b; R3 C4 `$ C
return 1;9 M- U, R& r- X- E) x; Y4 ^2 O% Y
}" L5 D- Y' T3 e9 P6 M7 L
BOOL CListCtrlEx::GetItem(LVITEM* pItem)6 l/ R8 ?! ?* @- Z9 s8 s
{9 C4 K% K# l' n5 ]
BOOL bReturn = pThis->GetItem(pItem);- x+ N: h) j" P! ]" n
if (pItem->lParam)
* V- G+ _& T- ]% x/ X {
5 ~ J; n; ~8 U( e* i' q pItem->lParam = ((ItemDataEx*)pItem->lParam)->SrclParam;
2 y4 k. J" c( p7 D& H3 c* F } return bReturn;
8 Q% ^9 l4 A2 U& _; @! J" e# b6 Z}3 h- i* P+ k1 L: b3 F; d! z
void CListCtrlEx::OnDestroy()" ]- @& ^4 B5 U6 b0 I
{
# e+ u& z0 e; v0 S, ~/ g // 关键是清除申请的内存空间
2 |& `% _% V% I+ |- |% \ DeleteAllItems();3 n# {, f3 w, _% s, a7 f% ~7 _
CListCtrl::OnDestroy();
( P( f# E h. g, @; O% T} BOOL CListCtrlEx::SwitchItem(int nItem1, int nItem2)' X( R; P+ m2 l `3 E
{9 X* P& u$ w' |# w1 @. m* j
if (nItem1 == nItem2 || nItem1 >= GetItemCount() || nItem2 >= GetItemCount() || nItem1 < 0 || nItem2 < 0)
6 w& ~7 a% P( l ~1 ^ {
4 M6 D- v' u3 K' Y, k& g return 0;
* E1 Q+ a% F7 d4 ?& E& F4 V$ U }
1 z; D& t% k/ m LVITEM ItemData1,ItemData2;
/ E5 T F% ]& L8 X0 D int ItemLength1 = pThis->GetItemText(nItem1,0).GetLength();
% R) k% q( l& q( h/ C int ItemLength2 = pThis->GetItemText(nItem2,0).GetLength();
; e. t# \- O( ]. n ItemData1.pszText = new TCHAR[ItemLength1+1];# |/ x/ ~ k- J: r' o& V5 o4 C2 g
ItemData2.pszText = new TCHAR[ItemLength2+1]; Z& d3 D% ?$ e1 H
ItemData1.cchTextMax = ItemLength1+1;: E' ]" H: \# s: G( V) F
ItemData2.cchTextMax = ItemLength2+1;! q4 B2 L* |6 @) M) e6 I; J6 |- A
ItemData1.iItem = nItem1;
' |8 i/ k& u4 {% y0 k ItemData2.iItem = nItem2;/ P. d O1 N3 a1 r- P* L& ]
ItemData1.iSubItem = ItemData2.iSubItem = 0;
7 n& g( N6 U+ ~3 K2 W: R. l/ X ItemData1.mask = ItemData2.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE|LVIF_STATE; pThis->GetItem(&ItemData1);
3 \1 x j' o$ ^; j% Y0 g, o# I1 Z pThis->GetItem(&ItemData2);
4 s( p: K% b+ P // 交换数据* M. G/ Z" d+ l7 d- M
ItemData1.iItem = nItem2;/ ^2 ^5 f' T' s
ItemData2.iItem = nItem1;
9 t+ O5 L3 Q$ S% z: X# \* t* j$ c pThis->SetItem(&ItemData1);/ a6 n, B$ p e! G4 V0 V
pThis->SetItem(&ItemData2); delete ItemData1.pszText;7 G. O5 c5 a: Z9 u' q
delete ItemData2.pszText;
7 L3 w2 O& C% ^, u$ N, p- }) Q' E4 i) g // 得到报表模式的列数: N: o1 j) @1 k \: o7 r5 P
int nColCount = pThis->GetHeaderCtrl()->GetItemCount();
, i: Y) O. l6 ~ A5 e) ]4 P CString strBuffer;+ j6 X9 N# r5 P4 q9 S% D; B" ?
for (int iSubItem = 1;iSubItem < nColCount;iSubItem++)
/ Q2 m4 ]/ a# {+ C, F5 c$ h {
% I: f8 k: d: A* f9 P strBuffer = pThis->GetItemText(nItem1,iSubItem);
" _3 C6 M5 u0 V' g, w0 K7 x# `0 I pThis->SetItemText(nItem1,iSubItem,pThis->GetItemText(nItem2,iSubItem));! Z# ^! G# G) d7 F5 a0 D) G
pThis->SetItemText(nItem2,iSubItem,strBuffer);
* z3 t8 a! I$ a* e }7 ]2 E- n; Q/ z0 c
// 把原来的选中状态也转移过去
( {2 \5 D+ ~* b // 不知道为什么这个属性没有体现在LVITEM结构中……) n0 y- t% |5 A z0 B
bool bItemSelected1 = GetItemState(nItem1,LVIS_SELECTED) == LVIS_SELECTED;
" E) `3 c$ J8 e. n bool bItemSelected2 = GetItemState(nItem2,LVIS_SELECTED) == LVIS_SELECTED; SetItemState(nItem1,bItemSelected2?LVIS_SELECTED:0,LVIS_SELECTED);3 S' Y7 y; X q' b. o
SetItemState(nItem2,bItemSelected1?LVIS_SELECTED:0,LVIS_SELECTED);
2 l9 K ^& w7 [& k( m' n return 1;* E( A6 f* t1 J6 @
} BOOL CListCtrlEx:reCreateWindow(CREATESTRUCT& cs)
# J5 s) Y' H) s6 \$ a2 r T{2 _6 b, B m- j3 d: u
// 本类报表模式专用2 `0 B" W$ U4 U+ ^ W
cs.style |= LVS_REPORT ;; e# |( i) _ L& S9 B1 a
// cs.dwExStyle |= LVS_EX_FULLROWSELECT;
, n8 h+ V/ T; E; S4 Z1 y4 J return CListCtrl:reCreateWindow(cs);- \2 e3 o2 E5 V' v0 F/ r a) V. Z+ \
}
& U. |7 g; k! E- QCOLORREF CListCtrlEx::SetHighLightColor(COLORREF clrHighLight)1 B% e" @. P" O3 v0 v
{
( j8 g7 ~6 [: o& C COLORREF clr = GetHighLightColor();- t( `3 ?2 R2 Z
m_clrHighLight = clrHighLight;+ k0 F+ B% w: p$ T1 S3 G; V; s
return clr;9 l" |; W: z* D8 w4 W" J v$ \9 T9 \
} COLORREF CListCtrlEx::SetHighLightEdgeColor(COLORREF clrHighLightEdge)3 i6 k, Z/ O" n! M; O& P
{) z- k/ R3 e4 x* u2 O3 P6 Q" Q
COLORREF clr = GetHighLightEdgeColor();
0 T7 ^* w! F5 c m_clrHighLightEdge = clrHighLightEdge;
) d; ]7 s2 A" B- V& l5 a( }% k$ U return clr;9 t8 l7 d9 y7 f3 f- G) m
} |