下沙论坛

 找回密码
 注册论坛(EC通行证)

QQ登录

QQ登录

下沙大学生网QQ群8(千人群)
群号:6490324 ,验证:下沙大学生网。
用手机发布本地信息严禁群发,各种宣传贴请发表在下沙信息版块有问必答,欢迎提问 提升会员等级,助你宣传
新会员必读 大学生的论坛下沙新生必读下沙币获得方法及使用
查看: 8810|回复: 3
打印 上一主题 下一主题

又是垃圾,CListCtrlEx Base CListCtrl

[复制链接]

该用户从未签到

跳转到指定楼层
1
发表于 2005-2-23 21:29:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
// ---------------------------------------------4 C" A! W: {9 `
// ListCtrlEx.h/ e  u3 r( \  L# [$ K% u# f
// ---------------------------------------------
. p! y: k* N& o. E$ ^4 `#pragma once
$ J3 S. u, o; [( z) Y& s+ U* X
, k. I5 J6 A3 R+ Y& o' s* P// CListCtrlEx
: f* W# N  C# Q# u' b, Z# D9 yclass CListCtrlEx : public CListCtrl
+ m' K7 H. ]. y0 E0 k8 Q{
7 V! Z* H, `7 z, Z" z7 r    DECLARE_DYNAMIC(CListCtrlEx)( p0 q5 `8 U' u. l
public:6 j3 ~6 c7 _& y$ |( q# u. B
    CListCtrlEx();
- d$ [) m# a1 i% O2 G; Z6 b    virtual ~CListCtrlEx();
4 o0 {# x- V& S! f- }( bprotected:
7 X( t) J& K: w# T9 v. m  F    DECLARE_MESSAGE_MAP()
5 |2 L% m+ s$ K9 E; Q. K    CListCtrl* pThis;
/ \& p3 E, y6 G+ }8 F' |+ a    virtual BOOL PreCreateWindow(CREATESTRUCT& cs);3 t$ z3 F6 U+ _) ^2 @
private:- \+ M, R3 [; [, v( p1 ]3 N) U
    struct ItemDataEx {
# w, ?6 B1 K+ R        LPARAM SrclParam;
. a# e% |3 V+ I" p        COLORREF clrItemTextColor;
+ g, n0 U; G' m: u  O        COLORREF clrItemBackColor;
: v3 G# \$ z( L  T- u( ~( i    };
$ [" U9 a& G# t: t0 t' e( k3 J    COLORREF m_clrHighLight;
# k- q5 Z5 n% P6 r    COLORREF m_clrHighLightEdge;+ }2 J$ H) E2 ]2 N. U) Q/ W0 g9 D$ K
public:
. N- f( Q& m, a    // 新增方法8 Z) I, ]: s! |% i# c, N" q
    COLORREF SetItemTextColor(int nItem,COLORREF clrText);
1 {9 @3 E! ]) Z3 f! f2 z- o    COLORREF GetItemTextColor(int nItem) const;
  B* F+ F4 U$ Q9 x  h9 v0 |9 {4 U    COLORREF SetItemBackColor(int nItem, COLORREF clrBack);
! J* t8 L2 @/ n    COLORREF GetItemBackColor(int nItem) const;
. p* \5 R5 w1 V, a( [* _    COLORREF SetHighLightColor(COLORREF clrHighLight);1 B! Z7 ~3 U- S) C
    COLORREF SetHighLightEdgeColor(COLORREF clrHighLightEdge);
+ ?: g+ S! L. H, |5 ^( P    COLORREF GetHighLightColor(void) const;1 }2 y5 p5 B. r3 O& g4 W+ E# Q
    COLORREF GetHighLightEdgeColor(void) const;
1 ]; Q2 _) q0 w& j9 f3 Y$ y    int GetItemImage(int nItem);
9 I6 Y' o+ S8 F! H    BOOL SwitchItem(int nItem1, int nItem2);. S, T) I# w* x5 Q
    BOOL MoveItemUp(int nItem);
; u4 d7 p' q4 Y4 C: Z- x- e    BOOL MoveItemDown(int nItem);
( D! ]; t: [" J6 x4 Q' N7 Z    // 重写原来的方法
: l- d; n/ h4 C* V3 \# D    DWORD_PTR GetItemData (int nItem) const;
5 X' ~, {2 Z* m5 @8 e& B    BOOL SetItemData (int nItem, DWORD_PTR dwData);
# R1 y9 e+ W/ \4 A. Y% k    int InsertItem (const LVITEM* pItem);
8 q  ?8 q( R( B9 y    int InsertItem (int nItem, LPCTSTR lpszItem);6 d2 T4 v' V5 f
    int InsertItem (int nItem, LPCTSTR lpszItem, int nImage);
! v" I3 l2 y* e, G% f4 t    int InsertItem (UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam);; Y% ]  Z6 H) ^/ S( p# _, S
    BOOL DeleteItem (int nItem);
7 l9 M+ u# }5 f+ x; {    BOOL DeleteAllItems ();, p& `  }$ v1 a
    BOOL GetItem(LVITEM* pItem);/ Q/ B( @# Q8 {9 J; ~  J
    afx_msg void OnDestroy();* B7 Z1 x( B3 b" {
    afx_msg void OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult);
4 R8 K9 C7 y9 L' ~$ z" T* G};
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 顶 踩

该用户从未签到

2
 楼主| 发表于 2005-2-23 21:30:00 | 只看该作者

// ---------------------------------------------+ _: \$ x' j/ W, m2 t // ListCtrlEx.cpp % e" K: Z# W5 b' ^' Z% _! a// --------------------------------------------- 2 ?2 |8 ?# z, o7 E4 H, x: E// ListCtrlEx.cpp : 实现文件4 X. r* x* D$ \ f S, | //

#include "stdafx.h"8 R% ~% o2 B2 k" c9 T6 V #include "CreateNewClass.h") h' E2 h* Q e" V #include "ListCtrlEx.h" 1 k; N. _9 u& G- \% b' K Z#include ".\listctrlex.h"

( G1 [- ^; F1 e/ r, B/ m // CListCtrlEx

IMPLEMENT_DYNAMIC(CListCtrlEx, CListCtrl)0 E" `6 t" a. e. w7 V: t; v, M0 Y CListCtrlEx::CListCtrlEx() - ^( t6 i+ O* V, w- z0 o{ + W3 v8 n) G4 L, D // 用于调用基本的方法 . G9 ?6 z' o# z% V9 @3 H: V4 G pThis = (CListCtrl*)this; : r1 @3 B" P. D: P6 }) z // 默认的选中框颜色 0 {1 x0 [: h$ K( z6 E% }: X m_clrHighLight = 0xF1DACC; , h) ~1 j( u3 Q: v, y4 P m_clrHighLightEdge = 0xC56A31;& i& X; E7 w! a* \1 K. r8 i8 r$ u }

CListCtrlEx::~CListCtrlEx()# E, k& G# H1 t {

}

( S' ~, k; H7 z$ e BEGIN_MESSAGE_MAP(CListCtrlEx, CListCtrl)2 R" z) W+ ^# x ON_WM_DESTROY() " E7 D1 \6 J; a0 H ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnNMCustomdraw) 4 B/ ~+ s+ m' S7 C: N: XEND_MESSAGE_MAP()

// CListCtrlEx 消息处理程序

, X n) d- \3 F0 s5 ~ COLORREF CListCtrlEx::SetItemTextColor(int nItem,COLORREF clrText) D# a0 z/ }, ~3 w3 C" h# g {0 e6 p4 L G+ a+ H0 G- _ COLORREF clr = GetItemTextColor(nItem); ( e) V3 _: ~' r. K4 u- A. M ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);7 h; i& ^- V+ e8 U( Y pData->clrItemTextColor = clrText; 9 S1 Y! T6 `4 d, `/ G; X return clr; $ S7 c) Z; Y* K' p' E1 u}

COLORREF CListCtrlEx::GetItemTextColor(int nItem) const! Q- n; l8 m% a {, S: ]4 H% V9 K# o' m) q ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);9 Y! ^& {7 u7 p( F& q/ I* k return pData->clrItemTextColor;+ |+ ]8 T7 i8 C" a }

COLORREF CListCtrlEx::SetItemBackColor(int nItem, COLORREF clrBack)7 d" Z( A3 f- K, B/ m { v& A1 |4 i" Y' y+ E5 j* r COLORREF clr = GetItemBackColor(nItem);3 q1 Q" A G& @) k ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem); A2 n4 e1 S. m2 C1 p* z* i% Q- R pData->clrItemBackColor = clrBack; ! }5 J, g2 d5 G! z: P4 W$ R& d. G* W return clr;3 j, i; x7 a7 }& _' h6 Q& z }

COLORREF CListCtrlEx::GetItemBackColor(int nItem) const% P* z1 _' x# N* ^" ?) D { , ~! q( G; F4 t/ M2 B9 m; ]' r6 M ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);6 h, M* }) }; t' W7 m6 I+ [ return pData->clrItemBackColor; , M+ [$ L% X0 V/ [: M}* T6 t2 N/ Z7 H' W DWORD_PTR CListCtrlEx::GetItemData (int nItem) const % t: U* X6 @1 }9 g# a1 j3 l" e{& N/ H* O3 K$ A if (nItem >= GetItemCount())$ `' P2 ?+ {9 w5 h { + m# `/ h6 ]+ x W5 ~ return 0; ( y1 B3 Q& W* j8 U } 2 j3 L v( X$ d& n+ X# ?( r- u CListCtrl* pThis = (CListCtrl*)this;0 W0 n& ]" k8 | y. r. V& c DWORD_PTR Data = pThis->GetItemData(nItem); ) F0 J* W7 K+ \2 D, I- N& Z3 c return ((ItemDataEx*)Data)->SrclParam; & A' F K3 G- O' \3 h5 `}+ u7 n, e$ G% `/ E& x/ ?5 G) C BOOL CListCtrlEx::SetItemData (int nItem, DWORD_PTR dwData)" y$ N* v7 j6 L' e; s2 {( c. z {$ V6 q% u! W" W4 z if (nItem >= GetItemCount())( z( v8 G8 e, O { * b3 M8 X( f) A& M return 0;5 D/ |4 D- }+ C8 T! P; u9 w& J }8 @1 v8 Z: ]% ^1 i1 [/ @9 d CListCtrl* pThis = (CListCtrl*)this; 9 L6 G; E- C) _ DWORD_PTR Data = pThis->GetItemData(nItem); % R0 Z$ j$ g! c1 p1 S ASSERT(Data);0 V% L' r9 q8 R1 J( t9 u6 Z* ` ((ItemDataEx*)Data)->SrclParam = dwData; D$ k# v4 B5 K/ D, _7 A return 1; 0 b, i' Z z* S2 A' H6 A+ L}: M3 q+ N8 J. }2 q3 T- i( b int CListCtrlEx::InsertItem (const LVITEM* pItem) : N0 Q0 j# `8 A; m1 G5 U0 g" c6 t{ $ [6 G* |1 e* N. t0 X+ O int nReturn = pThis->InsertItem(pItem); 0 Z/ K- O7 r L1 R2 ~' Y ItemDataEx *pData = new ItemDataEx; ( r7 j, V( ~4 ^ // 设置默认颜色 0 j! q/ A u0 ]1 h, c. T pData->clrItemBackColor = 0xFFFFFF;//pThis->GetTextBkColor();: x! {7 `1 c: Z( o; M0 C0 _" F pData->clrItemTextColor = 0x000000;//pThis->GetTextColor(); " y( v' _* [( G' A pData->SrclParam = pItem->lParam; - h4 P0 w) X/ E2 d1 j pThis->SetItemData(pItem->iItem,(DWORD_PTR)pData);9 [0 [/ W9 E$ ]/ C3 @3 d! i return nReturn;3 [3 r3 B- U9 P3 `! G3 t) W } ' g7 p4 O# l i* Q# t+ cint CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem) 1 ?$ o5 a4 E5 t. u{ ! [9 b$ }( i& e; I6 g LVITEM Item; e/ a+ Q! Z5 ^4 ?& ` memset(&Item,0,sizeof(LVITEM));6 s4 s- s8 k# h: `3 k% { Item.pszText = new TCHAR[strlen(lpszItem)+1]; 8 W9 J3 I5 @8 m: s strcpy(Item.pszText,lpszItem);4 G: ^0 i3 A& y4 x- ~* t9 i Item.iItem = nItem; ( X1 ~) G- Y4 @- A3 l$ ]2 w8 d Item.lParam = 0; # T5 q' P3 O5 C% o6 U0 ] Item.mask = LVIF_TEXT |LVIF_PARAM; ; Z2 V% O! \6 `& i' R) A BOOL bReturn = InsertItem(&Item);& x/ Y; C6 T" I/ w delete Item.pszText;" E V. N4 R) V/ e2 E& Y" ?# G2 E! b; J return bReturn; 8 _2 i4 H( K$ c8 L L$ t} . ]0 Z2 Z: z( R" t+ vint CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem, int nImage) + \2 l. q: w, @5 ]5 h{/ z: i% b/ t7 A6 f8 y! S) e- j LVITEM Item;- ^3 V% u. o* R" Y4 c! } memset(&Item,0,sizeof(LVITEM)); / A G1 Y6 [4 z( w8 v Item.pszText = new TCHAR[strlen(lpszItem)+1]; 5 F: v1 U2 P. t& _* Q- ^% a- c strcpy(Item.pszText,lpszItem);/ N6 W) ~$ Z6 w, n$ h# D Item.iItem = nItem; 3 t- A; d1 {4 Q Item.lParam = 0;8 q0 [$ H2 B1 N& e; H5 P* d Item.iImage = nImage;' ]- o6 K3 L5 s+ D; i Item.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE;. u# u @2 @% J. w BOOL bReturn = InsertItem(&Item); * T# L1 d$ d6 g- Q delete Item.pszText;2 U8 L& ~) z. \. v7 K! C; E return bReturn; @4 [ [( L) ^7 S t}. [* g3 d5 G' _0 B6 R6 ` int CListCtrlEx::InsertItem (UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam) 4 W) _8 k9 f7 B0 A6 H( N+ B8 y/ i{ 7 J$ X2 Y" a& ~6 e LVITEM Item; * b! p/ m" }4 H7 W. j memset(&Item,0,sizeof(LVITEM));; e8 m" P0 {" c* w Item.pszText = new TCHAR[strlen(lpszItem)+1]; ) U4 j: e7 Y- N6 c& p% s7 G strcpy(Item.pszText,lpszItem);, a: _+ B3 b4 C, Y9 X Item.state = nState; ) B8 i; }, K, Z Item.stateMask = nStateMask;+ I. Z. M, D9 u( e Item.iItem = nItem; ) o' T) N2 E% H) z _/ n Item.lParam = lParam;, o, `* T q. J Item.iImage = nImage;' I7 B9 j* ?; P& x1 H8 x0 |2 F- k Item.mask = nMask; 7 w; X( F4 N! _, ?7 v BOOL bReturn = InsertItem(&Item);, k9 G! L) b/ D delete Item.pszText; 2 B/ R1 F6 `/ w. @, n# x return bReturn;0 ]3 ~- ~: x% l9 T } % l: F6 S0 z! [0 F4 [2 O6 X! P) cBOOL CListCtrlEx:eleteItem (int nItem)+ s8 N7 Z5 M3 C! h. U" { { * n- `6 Q$ K# Y3 b7 z" v% x void* pDataEx = (void*)pThis->GetItemData(nItem); . B& K4 L4 M- ?) w) y8 u: J7 n if(pDataEx != NULL) 5 _8 M6 e4 m O: A* s& F! } { $ G C) @# s! p% o+ b delete pDataEx;7 [- f X6 f, C# D6 c2 g } 9 k: y* G8 u2 B# e3 d. w8 B return pThis->DeleteItem(nItem); L" b& X+ q2 Q } " O* B9 H8 @& K. iBOOL CListCtrlEx:eleteAllItems ()3 v* e) T8 Y# O. a7 ] {$ k3 j4 D: |1 L0 m' u. Q/ ? int nCount = GetItemCount(); 4 G" ^7 Z6 R1 k! \# P, Y for (int i = 0;i<nCount;i++) & Z0 I; l7 S! c& p1 F) _5 i# B { " z. P* v" x9 U6 \ if(!DeleteItem(0))0 L! Y$ s1 v! l { U S, W3 a i3 ^ return 0;5 [3 Q' j0 i: z9 l } 5 m& d* H6 \# ^. c4 B$ I }! V( n8 @" x- P return 1;. ]/ ?( W4 p ^ } ! T9 B/ Z1 w' q6 z7 A2 bBOOL CListCtrlEx::GetItem(LVITEM* pItem)& B3 C% C% n9 o% \2 n { Z J# g4 m- k/ p BOOL bReturn = pThis->GetItem(pItem); ; m1 G1 T! {* I+ s% I if (pItem->lParam)7 ]% f7 i. y2 s7 U5 T {4 Z7 A* `* z6 [2 `% c6 c pItem->lParam = ((ItemDataEx*)pItem->lParam)->SrclParam;/ `* v1 d9 J6 j8 F$ {/ k" H: G7 H }

return bReturn; ) n2 h, A0 [. @6 `. X: X7 l}; o4 B. W6 \8 f* a- l6 { y# m void CListCtrlEx::OnDestroy() # j# C1 p# ^7 h% ~3 I{ 9 l9 j4 q& Q4 @) g& q // 关键是清除申请的内存空间7 \" I$ U# E2 C1 C) I4 e/ u% j DeleteAllItems();/ D8 p) N5 Q+ U6 D& ^7 |! c CListCtrl::OnDestroy(); $ g; ?% v, J8 t; L% C2 }}

BOOL CListCtrlEx::SwitchItem(int nItem1, int nItem2)1 s# N' T4 _8 @ { 7 {- l L0 v8 G# Q if (nItem1 == nItem2 || nItem1 >= GetItemCount() || nItem2 >= GetItemCount() || nItem1 < 0 || nItem2 < 0) & a. t* L- y4 ~# T! D2 G2 q { 5 G8 A" W3 \1 s* A* R! a return 0; ; K+ B+ i/ m& ~& d }. ]" J, o6 y4 j3 W LVITEM ItemData1,ItemData2;6 ^& V% W& N* z, P3 {+ v int ItemLength1 = pThis->GetItemText(nItem1,0).GetLength(); 6 b( [3 d5 O# ^6 Y( l9 J int ItemLength2 = pThis->GetItemText(nItem2,0).GetLength();& z+ L9 t- b3 y3 o, P! t ItemData1.pszText = new TCHAR[ItemLength1+1];0 o" J3 ?2 A! X ItemData2.pszText = new TCHAR[ItemLength2+1];" r [, }/ ?, F( _ ItemData1.cchTextMax = ItemLength1+1;+ r' K5 K/ o9 j+ q5 F/ |0 O$ e ItemData2.cchTextMax = ItemLength2+1; ( t$ G2 I1 L4 J& n. A" s- c ItemData1.iItem = nItem1; - ^; g2 k# E# F( t$ g# h1 J ItemData2.iItem = nItem2; ) w! w: w% f+ V! z' L# C ItemData1.iSubItem = ItemData2.iSubItem = 0; 0 h { E" X5 K0 q ]0 V: f ItemData1.mask = ItemData2.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE|LVIF_STATE;

pThis->GetItem(&ItemData1); $ X: d; x$ W/ r! s$ Y7 z pThis->GetItem(&ItemData2);" G6 l5 y& i {9 ]0 o6 N9 Y; m // 交换数据& _, b$ c! h4 L' }' p ItemData1.iItem = nItem2; ( E" A7 y5 c$ p" y; f1 q' l& q ItemData2.iItem = nItem1; 3 E5 G) P! V ~ pThis->SetItem(&ItemData1); : [* w* S- A7 h9 l pThis->SetItem(&ItemData2);

delete ItemData1.pszText; ( j( @+ d! N% \( O1 W( L% l* N: d delete ItemData2.pszText; 2 |: ]0 x, ^5 c8 U. y& @6 g // 得到报表模式的列数 / b; j8 y" M2 q8 v int nColCount = pThis->GetHeaderCtrl()->GetItemCount(); & V! C9 G# L& p" X; f( J CString strBuffer;+ p# w$ f. k0 e for (int iSubItem = 1;iSubItem < nColCount;iSubItem++)" g ?% B% O+ l8 g {+ \' a1 T7 q9 j strBuffer = pThis->GetItemText(nItem1,iSubItem);8 I+ g& B) M% q$ X pThis->SetItemText(nItem1,iSubItem,pThis->GetItemText(nItem2,iSubItem)); 1 D! `1 c+ ~2 [: O+ y pThis->SetItemText(nItem2,iSubItem,strBuffer); , y9 H) @9 f9 T% `$ A1 z }. i8 d3 n- I+ P8 u4 V // 把原来的选中状态也转移过去 ; L5 o! r, W* ~; A3 V! l // 不知道为什么这个属性没有体现在LVITEM结构中…… . _$ k% b* J( j" c! n' X1 h bool bItemSelected1 = GetItemState(nItem1,LVIS_SELECTED) == LVIS_SELECTED; A6 e2 j2 h9 g' ^( z$ i bool bItemSelected2 = GetItemState(nItem2,LVIS_SELECTED) == LVIS_SELECTED;

SetItemState(nItem1,bItemSelected2?LVIS_SELECTED:0,LVIS_SELECTED);$ S' z3 r: ^& J$ ^6 B7 n/ a% _; a SetItemState(nItem2,bItemSelected1?LVIS_SELECTED:0,LVIS_SELECTED); - e* G( ^+ @% W7 m- R return 1;/ K7 e: Y! Q7 o9 w }

BOOL CListCtrlEx:reCreateWindow(CREATESTRUCT& cs) & U" U0 i# W' t9 @) z{8 b1 ]9 y1 K" O // 本类报表模式专用 ! |4 y7 W& t1 m' ~& g cs.style |= LVS_REPORT ; / Y% p) r1 h, ?8 m- q // cs.dwExStyle |= LVS_EX_FULLROWSELECT; 6 J- B5 d+ ]' p; Q3 u' w return CListCtrl:reCreateWindow(cs); 3 o0 V- p0 a' r- i! a5 a8 c7 o}

* ~; {4 k4 }0 @7 i3 c2 T4 p3 GCOLORREF CListCtrlEx::SetHighLightColor(COLORREF clrHighLight) 9 z) i7 J$ g1 E. p{ - D( W7 A# F7 ] Q COLORREF clr = GetHighLightColor();! t( Z+ T! n( J7 ?5 ]. s% @ m_clrHighLight = clrHighLight;: k5 p. G3 d8 u9 Y7 [ return clr;1 F- }: a- [$ | }

COLORREF CListCtrlEx::SetHighLightEdgeColor(COLORREF clrHighLightEdge): y& M' r, D+ O3 O; F& L9 K1 Q {( H) q1 i" i0 P) n2 M. F% L5 p: [ COLORREF clr = GetHighLightEdgeColor();* u1 p3 p. E& G5 u5 [ m_clrHighLightEdge = clrHighLightEdge; + Y4 M1 N8 v2 s1 q3 O2 r return clr; 4 ~( k- ` M3 k4 m* j/ L! c}

该用户从未签到

3
 楼主| 发表于 2005-2-23 21:30:00 | 只看该作者

COLORREF CListCtrlEx::GetHighLightColor(void) const2 _. C' b6 ?% Y' S3 h o {1 R' E2 `$ \; c1 Z. Z return m_clrHighLight;; l: {. K' {& z& j2 g }

COLORREF CListCtrlEx::GetHighLightEdgeColor(void) const4 @( T; a( {; L: I { 5 ^) L% b7 z" z% @ return m_clrHighLightEdge; 9 |- \9 W' B1 z0 F% N9 L$ _}

int CListCtrlEx::GetItemImage(int nItem) ) w, e( J3 a; Q: k& l1 I* W{ + @ e2 U( u% f* r LVITEM ItemData;4 h K, j* Z5 {, } ItemData.iItem = nItem; ) ] G6 E4 S( e* U/ U: Q ItemData.iSubItem = 0;( A, e( t4 Z, E+ t. C6 x ItemData.mask = LVIF_IMAGE;0 F! I# d( t2 ]2 P* S pThis->GetItem(&ItemData);+ c5 e; X' N/ D1 }" w. ` return ItemData.iImage;3 B! H' G4 b7 G$ J- P" } }

BOOL CListCtrlEx::MoveItemUp(int nItem)* P: I9 ?! P' v0 s& ^ g' K { a6 U$ _1 ^% X+ g if (nItem < 1 || nItem >= GetItemCount()) : t) C! t% x/ }9 K, h7 ~ {# W: S% k, m/ z% W1 n return 0;4 F, t9 U7 ?+ v9 n+ }1 B7 r3 k } 7 |2 O/ f. D* m7 Y1 R* z8 O4 A return SwitchItem(nItem,nItem - 1); 7 v' j# F5 ?: J A ?+ q. g! Z}

BOOL CListCtrlEx::MoveItemDown(int nItem)- c1 W2 c2 T& P. q" U# b- M! Q) S { % F, A7 l( p" _+ P if (nItem < 0 || nItem >= GetItemCount() - 1) - ?3 u" h. s% A+ z' m o' |6 \ { + L$ _' e& w, O4 c" O7 |! A return 0; ( L* K- `1 H. {6 |4 u } . ?8 H/ i9 T c$ S* [7 v- H% ] return SwitchItem(nItem,nItem + 1);6 l# E% p, `: V. E3 a5 ~ }

void CListCtrlEx::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult) % y2 _3 I7 p$ f3 }+ E% Z{ 5 g. f, Z9 p/ v6 ? NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR);

*pResult = CDRF_DODEFAULT;

if (pLVCD->nmcd.dwDrawStage == CDDS_PREPAINT)7 B. i- S" G3 ?! w& x {* T8 E, h7 F( n: } *pResult = CDRF_NOTIFYITEMDRAW;8 H( T7 ]) G) B$ z } + n% U: @+ d# {5 z+ j0 F5 f2 g! \ else if (pLVCD->nmcd.dwDrawStage == CDDS_ITEMPREPAINT) 8 x. g( B: C7 r0 e% } { 1 M% B# N9 ?( k0 f *pResult = CDRF_NOTIFYSUBITEMDRAW;) Z( h0 s$ I, G: ]% d; w }+ B9 R! i4 t4 V5 S' W else if (pLVCD->nmcd.dwDrawStage == (CDDS_ITEMPREPAINT | CDDS_SUBITEM)) 6 C. M: Z( z8 G, } {

int nItem = static_cast<int> (pLVCD->nmcd.dwItemSpec);. M1 I( \: {) B$ T# L) A int nSubItem = pLVCD->iSubItem; 5 w# r6 K$ O! o if (nSubItem != 0)- P% K4 |2 ^$ e, B& m {# D; q3 }* w- Z$ C* v // 全部绘制过程在nSubItem == 0 时完成 - a+ m1 W1 |+ b // 分布完成会有小问题,我无法解决 ) [1 ~7 t1 ~7 B) p; O6 x *pResult = CDRF_SKIPDEFAULT; % r: [* t# g* B- |! A; M8 y return; 9 I0 G4 w( v6 p, r7 x8 Q2 f } 4 S3 Z8 \! H1 a. R8 c CDC *pDC = CDC::FromHandle(pLVCD->nmcd.hdc); - H; j1 H! D, p$ \4 x+ k' h int nColCount = pThis->GetHeaderCtrl()->GetItemCount(); % e& k3 F0 L9 w- K CRect rectSubItem;* W9 i `9 p* ~$ T, H- V CString strItemText; # ?7 j2 N/ P: v& U7 ` pDC->SetTextColor(((ItemDataEx*)pThis->GetItemData(nItem))->clrItemTextColor);. M/ `& J. J' ` z( n9 k pDC->SetBkMode(TRANSPARENT); % I: {5 e: j4 f8 K1 o // 相当于橡皮 , E) |. X' j/ x9 p CBrush brEraser(GetItemBackColor(nItem));

// int nFirstWidth = GetColumnWidth(0);

1 |( Y7 S- O5 G _) _; v$ w5 V for (int i = 0;i < nColCount;i++) 8 R( G0 I$ M+ [) _ {- Z+ f M5 ?' x& x) m strItemText = GetItemText(nItem,i);

if (i == 0) 5 w* t( k6 f! r) l/ x- q) K, G! } {5 [6 N, s! X6 l pThis->GetSubItemRect(nItem,i,LVIR_BOUNDS,rectSubItem); , T( u, v F0 W2 N // 擦掉原来的图形+ N- U, m. F e8 w1 Z pDC->FillRect(rectSubItem,&brEraser); 5 x* q; x7 a: V. H6 u: D7 S, n if (GetExtendedStyle() & LVS_EX_CHECKBOXES) 3 F, A6 H, Z' W# ]( q2 D0 x" F { * D0 b# Y2 ^6 F5 P3 o2 G8 Y CRect rectCheckBox; * \+ Z+ H5 {9 I/ w! C* n8 O+ f GetSubItemRect(nItem,0,LVIR_ICON,rectCheckBox); g4 @- N0 p/ v+ E: P+ q CRect::SwapLeftRight(rectCheckBox); % u- i, M7 l: b* l' g* W) z; M rectCheckBox.left = rectCheckBox.right - rectCheckBox.Height();

rectCheckBox -= CRect(2,2,2,2);! v" f9 a$ b8 _1 r2 ?" t. w pDC->Draw3dRect(rectCheckBox,0x81521D,0x81521D); 9 v* T$ {/ y% D4 I //rectCheckBox += CRect(3,3,3,3); z1 Q2 { J0 s2 @ if (GetCheck(nItem)) 4 r% v' b. s; {2 \, \ { , P. D" a G) R7 e# t // 绘制一个CHECKBOX 8 h K" {6 C; o. q- w/ i- C: x+ H# i CPen penCheck(PS_SOLID, 1, 0x0021A221);& i& J q+ i1 H8 ]$ N5 o6 W CPen *pOldpen = pDC->SelectObject(&penCheck);3 }) ~2 s, a2 t& q for (int j = 3; j < 10; j++)% p: j) r/ E' ~* I0 g {5 z" O" L/ U; K pDC->MoveTo(rectCheckBox.left + j, rectCheckBox.top + rectCheckBox.Height() /2 - 3 + ((j < 6) ? j - 1 : (9 - j)));. {( y( x) R* k ^1 y; X0 K pDC->LineTo(rectCheckBox.left + j, rectCheckBox.top + rectCheckBox.Height() /2 - 3 + ((j < 6) ? j + 2 : (12 - j))); 3 u; M/ I1 Q2 X. w }* b) y3 e) W2 F* w; u) v/ L5 \+ o( \ 8 K! ]3 }1 ^1 }" ^9 Y; F5 r pDC->SelectObject(pOldpen);# {; S4 Z2 ?0 Q$ ^ } ; t, m" q& R1 v: f1 V& j' Y. m: _( r }8 S- v, Y, b' i' A // 根据需要画选中框/ }# s; t3 I1 D- P6 h0 T* L if ( (GetFocus() == this || GetStyle() & LVS_SHOWSELALWAYS)&&(GetItemState(nItem,LVIS_SELECTED) == LVIS_SELECTED)) 0 b0 {7 ?. V- O7 ]/ T" \ {/ F+ V7 V8 O4 T) t$ _ y& B& \- E if (GetExtendedStyle() & LVS_EX_CHECKBOXES) / A& d' a: \( _! Y# a {// 存在检查框风格的时候需要对左坐标进行修正 3 f/ z! M$ x# z CRect rectLeft;1 p) G+ @ o$ d Z) F* m) b; n( P GetItemRect(nItem,rectLeft,LVIR_ICON); 0 L1 i( B6 v! X9 k9 `' j5 M rectSubItem.left = rectLeft.left;% c2 ]6 [# U8 r" { } % C) ^# |9 A1 X* O8 N3 ` if (!(GetExtendedStyle() & LVS_EX_FULLROWSELECT))4 w# t& E, E. T {// 非全行选择时右坐标需要修正 $ y* ~; y( w4 i1 X$ D: Z, q' J+ X CRect rectRight;2 [3 t {8 B9 T/ x9 v GetSubItemRect(nItem,nSubItem,LVIR_LABEL,rectRight);3 h( d }( R; i6 M: c; P$ K- H rectSubItem.right = rectRight.right; ( R# J" M; J1 M8 w9 \4 l }, z6 o5 L! m0 X! F: @ O3 H' D CBrush brHighLight;) [: X2 K! C8 ` CPen penHighLightEdge;4 b F! N. ^. y& O% Y% A if (GetFocus() == this) L# @1 x9 j5 g8 B. y$ r U# V4 p { . y7 R: R. h' q# z; E, s; a brHighLight.CreateSolidBrush(m_clrHighLight); + _, S' B5 } f5 ]' N penHighLightEdge.CreatePen(PS_SOLID,1,m_clrHighLightEdge);& T% X- t% ^( G( {* @4 A }0 Y0 T, A( q& t$ V6 D else # a$ U5 ?. b9 ]' c& p: B5 O/ }2 V { / i9 c% {( p/ f7 d) b // 不是焦点时,颜色70%不透明度, A% b" ~" c5 e: B. P4 R& j7 ]. O COLORREF clrBack = GetItemBackColor(nItem);1 Y, L0 }- |5 J/ e WORD red = GetRValue(m_clrHighLight)*0.7 + GetRValue(clrBack)*0.3;$ p; c* b9 W+ f+ C, |# C WORD green = GetGValue(m_clrHighLight)*0.7 + GetGValue(clrBack)*0.3;( M; A( b5 g; \) M3 s) G" | WORD blue = GetBValue(m_clrHighLight)*0.7 + GetBValue(clrBack)*0.3; - M+ c0 }6 g8 D brHighLight.CreateSolidBrush(RGB(red,green,blue));: j$ e4 i0 N. t5 e red = GetRValue(m_clrHighLightEdge)*0.7 + GetRValue(clrBack)*0.3;9 a6 x! ?/ c- e/ V5 o: J8 l1 T green = GetGValue(m_clrHighLightEdge)*0.7 + GetGValue(clrBack)*0.3;0 r. B5 D9 c% X k9 j, ` blue = GetBValue(m_clrHighLightEdge)*0.7 + GetBValue(clrBack)*0.3;! P. ~0 o, H/ i0 Q penHighLightEdge.CreatePen(PS_SOLID,1,RGB(red,green,blue)); / s+ U8 U9 E: L- h3 b } ( O0 D8 ^+ m4 @8 a. `, M( E) b/ w# \ CBrush *pbrOld = pDC->SelectObject(&brHighLight); 1 p' V% w8 q$ e% S+ i; z! w CPen *ppenHighLightEdge = pDC->SelectObject(&penHighLightEdge); * }8 i( L0 @" T8 g6 A* u- X) T* _ pDC->Rectangle(rectSubItem); 8 `' Y+ E9 V1 }5 a) @# c4 Y ` pDC->SelectObject(ppenHighLightEdge); ' j$ l$ ?( W: C+ p& l( }# P pDC->SelectObject(pbrOld);3 f0 z {- n- c, _( c- C }9 R/ C* c6 l* H! @2 K } : `/ {0 [' k. ? pThis->GetSubItemRect(nItem,i,LVIR_LABEL,rectSubItem); " {9 \4 \0 q' ~5 r( } UINT uFormat = DT_SINGLELINE|DT_VCENTER|DT_END_ELLIPSIS;& x% ?, M q3 m! S! _0 Y2 |+ Z8 M- c HDITEM hdi;9 |) s# a% Z5 ~. v/ H/ }: h2 R hdi.mask = HDI_FORMAT;( F9 c( L, o+ B) r% H5 J pThis->GetHeaderCtrl()->GetItem(i,&hdi);3 s) [3 ]) H4 f if (hdi.fmt & LVCFMT_RIGHT) 8 X0 b% |2 C$ e: C6 T { % A" R# T# a$ o! {. o7 p0 P- k2 E uFormat |= DT_RIGHT; ( |4 }. U. l6 d& s }, A! j8 v- ^" Z# g else if (hdi.fmt & LVCFMT_CENTER)% | X0 \5 H! M: F# B { + f. `$ ^+ G/ \# ~5 N uFormat |= DT_CENTER;+ W) l; m- ?+ I4 j e* E& W2 k } ! E( F. N6 X2 _6 R pDC->DrawText(strItemText,rectSubItem,uFormat);

CImageList *pImageList = pThis->GetImageList(LVSIL_SMALL);( G+ D/ E" `8 s9 @ if (pImageList)* T+ G5 A6 u! Z0 G& Z% u4 K { + k; d. s9 S2 N( q5 H! Q CRect rectIcon;3 B: P l, h2 x3 _ GetItemRect(nItem,rectIcon,LVIR_ICON); $ k; k8 v2 [ a; g% j: Y" u pDC->DrawIcon(rectIcon.left,rectIcon.top,pImageList->ExtractIcon(GetItemImage(nItem)));: X1 R5 o$ }- C } 7 ^0 F, k! ^. t6 W, F; ^ }3 d" U+ y/ i& @ *pResult = CDRF_SKIPDEFAULT;3 K" u0 W' Y, W% i }

}

该用户从未签到

4
发表于 2005-2-24 08:41:00 | 只看该作者
太长了,不看。代码不是这么看的。用VC环境来看代码比较方便。

本版积分规则

关闭

下沙大学生网推荐上一条 /1 下一条

快速回复 返回顶部 返回列表