// ---------------------------------------------
/ R: ~: A" c5 B& H// ListCtrlEx.cpp& ?& z4 {3 V3 y5 {3 i8 ]6 q
// ---------------------------------------------7 ]# b7 o8 F0 h, o
// ListCtrlEx.cpp : 实现文件
' [& ^# W6 X+ s3 d- U// #include "stdafx.h" K v. Y) T1 e
#include "CreateNewClass.h"
3 N% r! Z! r, m, i) l#include "ListCtrlEx.h"
- i& Q2 Z" H8 U) G, E#include ".\listctrlex.h"
; m8 V5 s4 u7 f& j, @// CListCtrlEx IMPLEMENT_DYNAMIC(CListCtrlEx, CListCtrl)+ E' x6 R; z# b
CListCtrlEx::CListCtrlEx() @+ o r, s& B `
{) H% S9 s6 x$ }
// 用于调用基本的方法9 ?- o7 d% L% F7 ]+ [. r
pThis = (CListCtrl*)this;
7 O c* x( W1 j( N // 默认的选中框颜色
0 Y9 i1 `1 X; X6 P4 L, Y8 h; l m_clrHighLight = 0xF1DACC;
+ G9 L$ U u+ @( c m_clrHighLightEdge = 0xC56A31;# z2 R5 C4 p0 C
} CListCtrlEx::~CListCtrlEx()/ f: s, w/ e& C0 o+ Q i
{ }
8 w1 U- K: v% lBEGIN_MESSAGE_MAP(CListCtrlEx, CListCtrl). r5 G/ x7 n. n
ON_WM_DESTROY()1 z. h O7 U: d: e
ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnNMCustomdraw)
+ Y' `" e) j: R- `! DEND_MESSAGE_MAP() // CListCtrlEx 消息处理程序
% a0 D6 z1 ?1 Y! pCOLORREF CListCtrlEx::SetItemTextColor(int nItem,COLORREF clrText) X6 q- m. i# f* ?3 e7 Q
{
' y: Z1 o4 {3 B COLORREF clr = GetItemTextColor(nItem);) l) m# x" i: H; t
ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);
* ]3 t) T+ ?) W. p0 g1 u pData->clrItemTextColor = clrText;
( E0 `- s( N- m5 P return clr;
9 r4 K- d" e* ^+ w} COLORREF CListCtrlEx::GetItemTextColor(int nItem) const8 o. D8 `. E! j% F7 D% D# F5 U
{
" `3 I1 a! {( m ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);
+ B% C, F! v: t* ` return pData->clrItemTextColor;/ x& i& e$ {# M% P
} COLORREF CListCtrlEx::SetItemBackColor(int nItem, COLORREF clrBack)
' a% }2 [! O! q3 O& `" r{
" \# k$ ~& b. d, O: Z7 T7 w COLORREF clr = GetItemBackColor(nItem);7 z6 n8 v, X) a
ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);" g2 ~% X. q* O# |* v1 N
pData->clrItemBackColor = clrBack;
, @1 x1 a v x/ Y0 Q" a return clr;: k( j! A. i" S3 n
} COLORREF CListCtrlEx::GetItemBackColor(int nItem) const$ B+ w& }# b, F. |7 W' k7 u$ r& J
{ U8 m& q( {6 U
ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);& x ?+ x5 y6 N0 k/ z( `
return pData->clrItemBackColor;
6 c. F B$ ]: y) W" z}& c, s; v$ h* m* b& V1 O$ Z/ E! C
DWORD_PTR CListCtrlEx::GetItemData (int nItem) const- }7 T: w$ ^* Y5 _" b
{
" @2 W0 v: E5 A" X if (nItem >= GetItemCount())
7 ~* M! \4 k/ |" z8 M) p {
" Z6 O- T1 K+ W3 i1 `3 I return 0;
# q: f$ J$ d2 N$ f4 v2 V. C }
2 K$ n, V, ]; m CListCtrl* pThis = (CListCtrl*)this;
3 r5 H9 o* I# K6 Y DWORD_PTR Data = pThis->GetItemData(nItem); v+ x% R4 w! `7 S* [
return ((ItemDataEx*)Data)->SrclParam;, d+ U: p6 p: ~3 S# F, m( ~
}
" o8 V% ^ `; w9 k/ F& B1 U- RBOOL CListCtrlEx::SetItemData (int nItem, DWORD_PTR dwData)/ s; Y1 i* b# o* Y1 l' v/ j: K
{4 g! c7 _- R9 F
if (nItem >= GetItemCount())
2 f3 q: ]+ Y4 _! L0 L' n {
5 _& y0 T5 A+ U6 b4 z return 0;
* \' j/ Y% P2 M5 O7 U9 a; B9 y }7 t6 T0 ]5 X7 ^" n1 q3 }. m: ^
CListCtrl* pThis = (CListCtrl*)this;
1 D- u/ ]4 `8 r5 B DWORD_PTR Data = pThis->GetItemData(nItem);# i3 K1 g# ~7 P! T
ASSERT(Data);
* }/ F/ Q9 y* {$ {5 t+ ~ ((ItemDataEx*)Data)->SrclParam = dwData;# i. m: b/ F6 e+ U; j D' N, e$ N+ R
return 1;/ l( P* e3 v/ Y m( J
}
2 A9 D( B. n1 C, jint CListCtrlEx::InsertItem (const LVITEM* pItem)8 Q; \; i `9 y' U1 {
{
7 c1 m! i% M# [" } int nReturn = pThis->InsertItem(pItem);! E8 [' Y# O1 n9 }" a$ Y( Q
ItemDataEx *pData = new ItemDataEx;' d3 Q3 @* e+ y. [1 O& g
// 设置默认颜色
7 a8 b' {, V2 B6 a pData->clrItemBackColor = 0xFFFFFF;//pThis->GetTextBkColor(); W3 C6 d, m& j/ M- I, Y2 Z
pData->clrItemTextColor = 0x000000;//pThis->GetTextColor();1 |# k$ l6 g+ _2 w% @2 `' o
pData->SrclParam = pItem->lParam;/ H. _6 a U& y! q) v" ]
pThis->SetItemData(pItem->iItem,(DWORD_PTR)pData);6 g' U( |; G& K, S7 Q
return nReturn;5 y+ l5 } f' U" A1 v
}: \) m. a) @) l9 ]
int CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem)( v; I7 R6 @- r5 J: |
{. O1 n% ~9 I0 [/ |9 X+ [
LVITEM Item;; M) n5 d# d s4 L' g
memset(&Item,0,sizeof(LVITEM));4 I) O" P& e4 B9 x# ^
Item.pszText = new TCHAR[strlen(lpszItem)+1];
0 d% u" R# l1 ?' S% T! b$ [% ] strcpy(Item.pszText,lpszItem);, H1 K7 }. I5 k3 S% C
Item.iItem = nItem;8 Y& |( V9 A' r7 `( n# T( j* K- Y
Item.lParam = 0;) f5 x' P( X6 ^! W
Item.mask = LVIF_TEXT |LVIF_PARAM;
y+ `4 O9 g" J4 D; v3 W BOOL bReturn = InsertItem(&Item);
0 B/ {/ Z' o! `9 c9 g( n delete Item.pszText;% M2 l8 z- V7 N+ P& w' c& R
return bReturn;
) z! ^1 @! o6 Z" n$ U! E}6 O6 Z% @" U9 W% H
int CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem, int nImage)
; P5 X/ V1 b3 v* s{
% j ^6 V5 a6 R7 g8 s; u& _- n LVITEM Item;) v$ w" b) W* \& r
memset(&Item,0,sizeof(LVITEM));4 A) Z/ \- \1 X/ O! `1 p5 n8 k/ ^
Item.pszText = new TCHAR[strlen(lpszItem)+1];& y0 V+ K% F# M* ?, Y8 s; N
strcpy(Item.pszText,lpszItem);+ [: ~" _7 d5 Z
Item.iItem = nItem;* F) j# r5 X' A) k2 M$ B
Item.lParam = 0;
p3 V) T; W! o1 h Item.iImage = nImage;5 ` i4 a- c. l: J, T8 l
Item.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE;
; d& S' k$ z, K# _$ d6 @1 R* B) O BOOL bReturn = InsertItem(&Item);
& z. P3 P; B* F" O% b1 t, |, ^ J- z delete Item.pszText;7 u" P! n W# m/ a+ F7 f* s
return bReturn;3 ]/ h3 S( p1 {& X
}
2 Y$ W" G2 M* K# d6 z, Aint CListCtrlEx::InsertItem (UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam)( i# ~: c! x4 l( U8 |% c" O9 f
{: G' D( N" A6 u
LVITEM Item;! Q+ Z0 E1 ]: y# d e/ D- N
memset(&Item,0,sizeof(LVITEM));! u" ^0 G2 y. u% H& I
Item.pszText = new TCHAR[strlen(lpszItem)+1];
3 B. q; |. S; y/ K strcpy(Item.pszText,lpszItem);
8 x. a( Z# ]2 @" E, e: |' D Item.state = nState;# W. O, L. q Q" N
Item.stateMask = nStateMask;
, w* P9 r# a' q& {4 i Item.iItem = nItem;
! \$ X$ ~+ e: ^7 E% p6 A. L% h [! | Item.lParam = lParam;3 p+ C3 q4 ?: ^; p! G
Item.iImage = nImage;% {' L* x; s" e8 R' i' K4 Y$ _% q" t
Item.mask = nMask;
% f; [" ^6 H+ c7 \. i' |. a7 A6 V BOOL bReturn = InsertItem(&Item);
7 W* }5 W a& t- p delete Item.pszText;* {2 h) K. c3 G
return bReturn;
# g" ]+ D6 e* E$ ]+ M3 W4 T}
2 g/ y) Q! P+ i2 H z0 r5 ?' _+ nBOOL CListCtrlEx:eleteItem (int nItem)! m4 I% ^0 [& N- s
{
) v9 Y% r( t6 s) g5 [ void* pDataEx = (void*)pThis->GetItemData(nItem);8 g& X* o8 |, x5 t0 z8 h% a
if(pDataEx != NULL)+ m8 L8 `( Y1 L6 w. ~
{# K. G! d6 J3 ~0 \
delete pDataEx;6 ]- E' Y9 w4 X$ }/ {$ n
}6 D; ?1 \* {4 F, x4 Q( I# w2 i
return pThis->DeleteItem(nItem);4 `$ i0 S' f; z/ e/ H
}
F2 u7 l5 B) ^! SBOOL CListCtrlEx:eleteAllItems ()! i0 S V7 m! ~2 B$ G& U
{
. E/ d. z) Y" T8 K1 S int nCount = GetItemCount();& b! {& k1 S. s: K, B7 N
for (int i = 0;i<nCount;i++); H4 p5 a; C' h/ }, O9 L
{) V) j& J' {) u/ [- z7 Q: v5 q
if(!DeleteItem(0))
8 x2 T0 Y5 C6 {4 M* z) J {+ c* P' b! A9 H X; S
return 0;
; w& G) _/ ~: f4 t4 E }9 ^ j% S2 ]9 W& q% k7 R0 d
}0 [5 h5 o* h" f$ y; Y- n
return 1;9 V1 V& D3 o5 r+ ]$ E( u: z
}
* T' l6 e h/ [! O6 ^6 A+ KBOOL CListCtrlEx::GetItem(LVITEM* pItem)
" ~3 `0 v6 B0 n9 U( ^8 ~{" m& @- ~; T9 G
BOOL bReturn = pThis->GetItem(pItem);4 L/ S1 r. n8 i7 v; `
if (pItem->lParam): V1 d& ]. t. q1 Z
{' T0 U; p7 [/ D
pItem->lParam = ((ItemDataEx*)pItem->lParam)->SrclParam;
2 N" [/ M/ g+ S G7 { } return bReturn;, ~) C _5 P+ L
}
4 F z7 o, z7 ]$ L) [ Vvoid CListCtrlEx::OnDestroy()
, }' }, x! L9 S4 x{8 y, |, E! p0 w$ [' _
// 关键是清除申请的内存空间" R5 y$ X$ m3 s7 ^/ t2 S
DeleteAllItems();# g" Z$ [% a' b; I$ V7 x2 u! D+ M! O
CListCtrl::OnDestroy();, ]" E: q& P6 p* D# O
} BOOL CListCtrlEx::SwitchItem(int nItem1, int nItem2)3 E8 g+ A" ?/ |$ \- ^9 d
{
8 g" f) }/ _# Y' s" T D/ w% K if (nItem1 == nItem2 || nItem1 >= GetItemCount() || nItem2 >= GetItemCount() || nItem1 < 0 || nItem2 < 0)
: x6 S+ G0 r, q. |0 p3 U4 x5 I. h+ k {# A7 J8 g2 @: ]2 q% l' i
return 0;
7 g* ], j# i% H' S }9 z$ U4 ^$ q/ F! J
LVITEM ItemData1,ItemData2;6 L, A0 X L- S; j8 S
int ItemLength1 = pThis->GetItemText(nItem1,0).GetLength();
+ W* `/ s8 E0 ]& l8 m. _ int ItemLength2 = pThis->GetItemText(nItem2,0).GetLength();* Y7 W# l9 j6 F0 p+ y
ItemData1.pszText = new TCHAR[ItemLength1+1];1 f% b0 K0 o5 u o/ D4 U/ E
ItemData2.pszText = new TCHAR[ItemLength2+1];
6 l# W7 n7 e1 `2 K& @, }3 J/ e' E ItemData1.cchTextMax = ItemLength1+1;
! p( d% k: d. L+ m: E [; B5 \ ItemData2.cchTextMax = ItemLength2+1; l" M1 N! g, J. x, k$ b
ItemData1.iItem = nItem1;
+ d# D9 Q0 U* P3 c* Z! s1 M ItemData2.iItem = nItem2;, j* x5 I" `1 x# _ U6 Q$ x- f3 P
ItemData1.iSubItem = ItemData2.iSubItem = 0;
& L! {6 X' r0 p9 {$ U* d ItemData1.mask = ItemData2.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE|LVIF_STATE; pThis->GetItem(&ItemData1);
6 V, W# T1 o$ F! [: {! j5 V8 Z1 A pThis->GetItem(&ItemData2);) v* G( @& j% N0 f( F
// 交换数据
! |% e+ R/ e: H4 a( V. i6 {$ A ItemData1.iItem = nItem2;
9 L) L0 g3 c. w4 y' N& H ItemData2.iItem = nItem1;! `) ~4 ^& T- q( n8 ]4 n
pThis->SetItem(&ItemData1);) Y% N. e/ Q. ~2 u* R: ~+ |
pThis->SetItem(&ItemData2); delete ItemData1.pszText;
. a) K- J/ F7 a( _6 c0 D8 k delete ItemData2.pszText;! n( n9 K% f: U0 i, v# Q$ w
// 得到报表模式的列数
/ [' K( I z7 v( q$ \9 @ int nColCount = pThis->GetHeaderCtrl()->GetItemCount(); t! n0 [# t3 B c7 F9 {, z
CString strBuffer;
% _4 V3 [0 L4 M) g/ `, Q7 Z/ g" T for (int iSubItem = 1;iSubItem < nColCount;iSubItem++)
$ h6 ]1 L" \: Q+ f2 b8 x! ?- B {
2 V4 q# i; |/ A4 V* M& x strBuffer = pThis->GetItemText(nItem1,iSubItem);- s/ A, q/ k2 R7 g5 B
pThis->SetItemText(nItem1,iSubItem,pThis->GetItemText(nItem2,iSubItem));% y8 \' _3 G" e3 T* ~/ ?
pThis->SetItemText(nItem2,iSubItem,strBuffer);6 [7 x' L' _! y! e
}
* W; }7 B4 w4 G# s; S$ F* L // 把原来的选中状态也转移过去- N& m3 I* v( m' h1 y- |& L
// 不知道为什么这个属性没有体现在LVITEM结构中……) p1 ?& x3 f2 a7 W8 s6 z
bool bItemSelected1 = GetItemState(nItem1,LVIS_SELECTED) == LVIS_SELECTED;
5 i- r7 Z) A1 I bool bItemSelected2 = GetItemState(nItem2,LVIS_SELECTED) == LVIS_SELECTED; SetItemState(nItem1,bItemSelected2?LVIS_SELECTED:0,LVIS_SELECTED);. m, ^# _; _+ u
SetItemState(nItem2,bItemSelected1?LVIS_SELECTED:0,LVIS_SELECTED);* }8 i# L3 x# n: W7 C( K. ~5 \$ A
return 1;
) `6 t6 N7 G) ^5 t. C} BOOL CListCtrlEx:reCreateWindow(CREATESTRUCT& cs)$ h; l" b' m) R
{
$ I0 b5 d/ d7 P3 w# F" O: @ // 本类报表模式专用
2 Q* y. Z% O- D7 A' H3 X7 g { cs.style |= LVS_REPORT ;
! D! k2 g) K+ f% ?9 h // cs.dwExStyle |= LVS_EX_FULLROWSELECT;- R) K: o3 z$ Q- J% n
return CListCtrl:reCreateWindow(cs);
! n7 a/ X+ T1 s9 }! O4 L}
" g' G2 X' t. h: Q# f. TCOLORREF CListCtrlEx::SetHighLightColor(COLORREF clrHighLight)2 t0 N5 P; W H J! q3 U9 t
{
, }! f4 k6 o- a: e% J! { COLORREF clr = GetHighLightColor();
9 w, ~" L& M Q$ J m_clrHighLight = clrHighLight;
1 v- I- C* } ]/ k; V3 ~ return clr;
3 T1 G+ W$ G* y4 Q" u- G" M/ I} COLORREF CListCtrlEx::SetHighLightEdgeColor(COLORREF clrHighLightEdge)- l' u1 m6 I0 E4 A' `7 x, `
{
0 X) l) c& E. s* k* t COLORREF clr = GetHighLightEdgeColor();3 R1 ^! p* ?5 {
m_clrHighLightEdge = clrHighLightEdge;8 O9 A7 ]! g/ ], R, d
return clr;/ Y% i6 W" j9 W! o9 S& _! z: M
} |