下沙论坛

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

QQ登录

QQ登录

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

又是垃圾,CListCtrlEx Base CListCtrl

[复制链接]

该用户从未签到

跳转到指定楼层
1
发表于 2005-2-23 21:29:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
// ---------------------------------------------
0 {5 d* @: Y0 h// ListCtrlEx.h
( {0 K* b! J7 ~# o1 K1 N// ---------------------------------------------
3 h$ q3 ^9 x) D; q#pragma once
; b/ f% ^9 t2 E) j
. m- g+ T1 N! I7 F: f: m+ Y// CListCtrlEx! N' K1 H; c' V$ Z( R
class CListCtrlEx : public CListCtrl
4 l$ K" f9 Y# N4 y. j{+ D: v' L: {) H1 }! C/ z, B
    DECLARE_DYNAMIC(CListCtrlEx)! `/ `9 T- _) u& O" t) L0 H1 b
public:
8 S2 V- ~" @* E: t: L    CListCtrlEx();- s' A* m$ h, |7 U- f% |
    virtual ~CListCtrlEx();
* A# E0 F; j* dprotected:5 i' x& n5 d# D2 s! w
    DECLARE_MESSAGE_MAP()
) h3 p% f. `4 w2 i1 z/ m0 L3 X    CListCtrl* pThis;3 W- f! O& q  `% @8 Y# K
    virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
9 N4 {" Y/ Y- e* Yprivate:
3 u5 Y+ W* i1 \/ s$ i1 c2 P4 G! Q    struct ItemDataEx {5 r, k: U9 e5 e6 Y8 M- i' @
        LPARAM SrclParam;# s; f" I! u' X$ ~
        COLORREF clrItemTextColor;
! t7 x& b5 b% @        COLORREF clrItemBackColor;0 d2 x0 E, ?: l  B  e5 U; b0 C
    };+ b6 J% `, b5 x5 H# i+ H8 b
    COLORREF m_clrHighLight;) ]- a- {5 E4 z0 Y% H( Z$ L  B2 l* b
    COLORREF m_clrHighLightEdge;
9 O+ I( X5 W3 E1 B; p3 D, zpublic:
% w0 _& r) ^( v( Q    // 新增方法
5 M1 h7 m* _/ l6 k( Y+ T* D% g    COLORREF SetItemTextColor(int nItem,COLORREF clrText);/ Z/ y3 T0 Q0 \: a
    COLORREF GetItemTextColor(int nItem) const;6 w8 ]& a. @  m6 C3 U: V
    COLORREF SetItemBackColor(int nItem, COLORREF clrBack);" b$ F! }* g8 }- C  x7 J/ W! d
    COLORREF GetItemBackColor(int nItem) const;
1 R9 d% N4 C% c7 w" o    COLORREF SetHighLightColor(COLORREF clrHighLight);
1 q7 B4 O8 f* b    COLORREF SetHighLightEdgeColor(COLORREF clrHighLightEdge);
: D' D+ m9 V! U% \- d9 s    COLORREF GetHighLightColor(void) const;
0 o! X" r* R9 q  F% |, }    COLORREF GetHighLightEdgeColor(void) const;" f1 x8 v- S  T, r* ?/ t  T
    int GetItemImage(int nItem);
! U5 j5 K& T( g; T! K: s' A; ]    BOOL SwitchItem(int nItem1, int nItem2);7 y" w" }" d" M( ~
    BOOL MoveItemUp(int nItem);
% Y% l9 |9 n% j$ }; P    BOOL MoveItemDown(int nItem);
+ P; P( h0 t* w    // 重写原来的方法$ D0 ~, A# {8 o
    DWORD_PTR GetItemData (int nItem) const;0 d- g. k7 B" Z5 k; d! n
    BOOL SetItemData (int nItem, DWORD_PTR dwData);
5 ~- x5 w. e4 X: W6 z  U% J8 y2 B/ y    int InsertItem (const LVITEM* pItem);, u6 M; b+ e/ n4 z
    int InsertItem (int nItem, LPCTSTR lpszItem);
9 ]. h; k) a! `7 C- E- B    int InsertItem (int nItem, LPCTSTR lpszItem, int nImage);+ R9 M! ?; e. M" _2 b
    int InsertItem (UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam);
8 X6 [: V  ~. N/ i) W" O' b8 o    BOOL DeleteItem (int nItem);4 ~* l: i% K$ B$ {* E
    BOOL DeleteAllItems ();
* P3 |: k0 O# F) B. ~- J- J. ]    BOOL GetItem(LVITEM* pItem);! H1 ~7 \0 q) s% B6 N* R( J8 B
    afx_msg void OnDestroy();% H* L) A# ]1 I3 _6 Z+ e; l
    afx_msg void OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult);
9 ^/ c- @2 i. A8 ^( Z9 I};
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 顶 踩

该用户从未签到

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

// --------------------------------------------- - p: o# Q1 a7 R7 _2 Q// ListCtrlEx.cpp , K' M& h4 \: L; i2 }// ---------------------------------------------/ {# Y6 o! l3 E4 w- E7 w // ListCtrlEx.cpp : 实现文件 3 x! Y: d# v2 ^- Q* x$ A//

#include "stdafx.h" + {3 z% F! l6 n& d; R#include "CreateNewClass.h"! Z4 U# S# |1 M( m: X #include "ListCtrlEx.h" : v' B2 g8 o3 |#include ".\listctrlex.h"

# k/ T* p5 w$ d, u// CListCtrlEx

IMPLEMENT_DYNAMIC(CListCtrlEx, CListCtrl) 2 o3 ~ X# z' z6 D& s- `: f! s$ V+ L8 ACListCtrlEx::CListCtrlEx() 3 k" k% _: I+ I$ ]9 x+ r# v6 F0 Q1 p{ 2 B: Y# c/ R% E: N! g' G // 用于调用基本的方法 . P/ c5 `( e) a- H pThis = (CListCtrl*)this; $ B8 ~( e x; o; P // 默认的选中框颜色 3 ~( N9 E ^. I c e1 J; z m_clrHighLight = 0xF1DACC; ; x/ w0 ~' V6 d0 l6 E. [3 q) C m_clrHighLightEdge = 0xC56A31; % v) e0 t0 r" _0 |* Q+ c}

CListCtrlEx::~CListCtrlEx()) O0 u- U- v0 m% N& O! h {

}

& W5 ^' r8 O+ D+ K7 H7 ~4 vBEGIN_MESSAGE_MAP(CListCtrlEx, CListCtrl) ! K( s, M) W- n/ L ON_WM_DESTROY() # Q$ K! x5 h2 s5 N) M1 I% X ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnNMCustomdraw) $ x7 S/ F5 V3 \- w, w$ JEND_MESSAGE_MAP()

// CListCtrlEx 消息处理程序

) Q' Y' z, r6 ]( M; l& Q COLORREF CListCtrlEx::SetItemTextColor(int nItem,COLORREF clrText)) L% }8 O" N6 q' G( d( i- D {- d( ~: e& t! X COLORREF clr = GetItemTextColor(nItem);3 J- F |3 b7 H1 D) \1 a1 S ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);; Z5 @* u6 B# e8 v: v# c: [ pData->clrItemTextColor = clrText;2 I2 x S, _/ Y% H return clr;. L- ~9 ]0 i4 y7 V* f }

COLORREF CListCtrlEx::GetItemTextColor(int nItem) const . Q3 z) Y/ B9 I ^' I& ~$ m5 m{ O. S, u) ?! r3 Q ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);9 i0 `2 b3 u9 j9 e. b0 F return pData->clrItemTextColor; ' Q) ~. K: A, m0 D& }}

COLORREF CListCtrlEx::SetItemBackColor(int nItem, COLORREF clrBack); I& I' V% ^8 r6 N9 B: J5 ]1 w {3 x" r) ~1 [ P x- ?; q! U COLORREF clr = GetItemBackColor(nItem); , H( ]% g* t& ~$ i- W( e ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);8 o- [7 [2 x7 V) m# a pData->clrItemBackColor = clrBack;7 V$ E- ^" d W0 M$ y' k return clr;8 C( L+ g' Z1 {2 `+ i }

COLORREF CListCtrlEx::GetItemBackColor(int nItem) const9 ]9 u% ?9 G' X& \9 H. G4 ] {( L7 W$ I: m6 {( U/ O6 _7 u ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);! |- L" Y: E1 H0 s return pData->clrItemBackColor; ! Z4 y; } }0 v} % I$ ~; s r" W2 WDWORD_PTR CListCtrlEx::GetItemData (int nItem) const $ j' n$ e8 @& Q# g' A{/ n; J5 C% M/ s. A* ]' v if (nItem >= GetItemCount())9 @' D) r' | w: n { & U5 N2 N$ ?' \+ H; w" k' L return 0;: Z: O: Q9 R7 C. w" b) o) y }6 ], u. E$ ?8 \/ a' E$ U CListCtrl* pThis = (CListCtrl*)this;8 g4 C M- }+ f3 p DWORD_PTR Data = pThis->GetItemData(nItem); + Y& a3 g7 ]7 u! C return ((ItemDataEx*)Data)->SrclParam;# `+ P" d* H2 f- B9 K0 k- G7 Q }2 c: D4 o# m# q ~& S9 ] BOOL CListCtrlEx::SetItemData (int nItem, DWORD_PTR dwData) 3 i- ]& O8 U! @) L& D. Q{ 7 Y1 ~9 Y+ `; h, e. N% t9 j% ^ if (nItem >= GetItemCount())7 x5 E4 ~7 g, m: D* @+ k- l { - f Q+ ~6 V3 T return 0;4 v6 @8 ~. @% O' o7 u. n5 r } 4 R) ?0 o7 n+ v" H' M CListCtrl* pThis = (CListCtrl*)this; # y) z2 U* T/ \9 u/ _( T% t DWORD_PTR Data = pThis->GetItemData(nItem); , A4 u5 S( I' E0 q5 B7 g. t ASSERT(Data); & y) z) E) c3 G4 n ((ItemDataEx*)Data)->SrclParam = dwData; 0 J% l2 W2 q1 s V, ^ return 1; F9 I* B3 N- I) N) b9 N! m}: W& y3 D, K4 T2 X! h int CListCtrlEx::InsertItem (const LVITEM* pItem) & H- N) K% E- D" I* C2 [{5 {. b0 x% m/ K( X int nReturn = pThis->InsertItem(pItem); 0 |4 b7 ]+ w0 p } ItemDataEx *pData = new ItemDataEx; # j2 ]3 ^3 l/ z // 设置默认颜色 3 k9 B, G# E. T4 Y7 ` pData->clrItemBackColor = 0xFFFFFF;//pThis->GetTextBkColor(); ) o) }8 E. K% R+ i! j) ? pData->clrItemTextColor = 0x000000;//pThis->GetTextColor(); 3 m7 {& V5 D, p/ y pData->SrclParam = pItem->lParam; 7 ?3 j3 @$ s' E. O5 L* k+ _' d, i pThis->SetItemData(pItem->iItem,(DWORD_PTR)pData); 0 E5 s- a1 X1 s, B; L9 E( g return nReturn; " S+ e: Y) ?, b) h1 Y. y4 g} $ x+ Y3 Z' m3 @1 H5 g5 Dint CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem) " f% x6 m' ?0 R5 l# z{& m' h+ v J# N3 B9 X5 f! Y LVITEM Item;6 [5 m# r; c9 h* ]$ m. W! b% G memset(&Item,0,sizeof(LVITEM)); ; ^/ ~ r4 \* T* |5 j5 p Item.pszText = new TCHAR[strlen(lpszItem)+1]; $ A5 F) m) y% M! P/ B6 T strcpy(Item.pszText,lpszItem); 1 b3 a1 h8 b9 G9 c+ L Item.iItem = nItem;5 d$ ^5 X& n1 t o3 I: @" N x Item.lParam = 0; 4 U5 L/ N' ^- V6 z8 e* L Item.mask = LVIF_TEXT |LVIF_PARAM;" J/ X: Q0 _: w/ ]4 X/ E& S BOOL bReturn = InsertItem(&Item); ; d$ w5 ?; D- l6 H9 J2 } delete Item.pszText; " k# S: S6 b5 K" I& v( i2 H return bReturn; ; S; ` {- X5 z& i1 v. Q}" H8 X+ @6 J9 v' q( Y+ H* |& Q int CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem, int nImage) ; l; a6 N) j8 x{: o; I, a' w" ~4 q+ M% O, s LVITEM Item;3 X ]3 v3 H) L: U; |: n: ~! V memset(&Item,0,sizeof(LVITEM));2 D- ^9 z% [" z2 ~1 u0 N Item.pszText = new TCHAR[strlen(lpszItem)+1]; + _6 r- a4 w, O( j! y& w) a strcpy(Item.pszText,lpszItem); 4 b9 `: x% W3 m, v1 C Item.iItem = nItem; 8 Y; Y i" }: r7 G8 P1 `$ b. X: t Item.lParam = 0; ; p! Y' a$ X) Y$ q# ^ Item.iImage = nImage;4 k1 i$ v" c3 i, r6 ` Item.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE; 6 A9 i; }+ P6 C1 [ BOOL bReturn = InsertItem(&Item); 4 H' \8 E; B. W) r; N9 b! I delete Item.pszText; . u' p8 i: V) _& ~ return bReturn;* f- q3 J* T$ U9 L8 e5 x" Z4 x }4 P' @/ U4 I& z4 o c, d! l int CListCtrlEx::InsertItem (UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam) , E" s+ X% @! p) W2 R{ # B' G0 @( E7 j f2 S- M LVITEM Item;8 W: y: m3 h6 O& a' y- n memset(&Item,0,sizeof(LVITEM));; o; r' }% g2 F% X* J; Y) } Item.pszText = new TCHAR[strlen(lpszItem)+1]; ) U) _ O9 G, R; I( ]. R, c! L strcpy(Item.pszText,lpszItem);8 l! u6 }- q3 \6 s# R Item.state = nState; # @' Y% W( S: l9 T Item.stateMask = nStateMask; ) v9 H, N: F' P8 h& m, ~ Item.iItem = nItem; ' P- x; n7 Y% @$ p Item.lParam = lParam; 2 O) q6 B6 N6 D* t4 t) J% M Item.iImage = nImage; ; b- W1 t/ C9 j5 G, u Item.mask = nMask;! b$ x. @2 `) B$ W" ` BOOL bReturn = InsertItem(&Item); V' F' r9 i' _' I delete Item.pszText;7 ~) w" E1 G; N: u4 ]8 {* P return bReturn;6 x9 j; [, @& c. |$ S! ]4 l$ M# ? }0 h) C n. I* R7 ~1 V3 m& m BOOL CListCtrlEx:eleteItem (int nItem)3 g0 L. R/ @0 B. F2 T { * q# O7 o+ S* q& Y' ` void* pDataEx = (void*)pThis->GetItemData(nItem);1 u5 Y0 }2 T# u if(pDataEx != NULL) 8 ~4 c: R: |5 R) C6 k7 D [ { s7 {5 J7 P% U( R% u' G delete pDataEx;: ~: @# y) q" t1 I, M' e9 b }7 g2 h9 a* c3 m, i return pThis->DeleteItem(nItem);% v% z3 a! h5 X3 E7 U }2 O+ C# b1 h( ?% s9 ^ BOOL CListCtrlEx:eleteAllItems () 0 J% U+ }7 E$ G' C* d1 K* U8 w{ h. Q4 \6 [2 O {3 F int nCount = GetItemCount();/ q( ]- D% \" Q7 f. B for (int i = 0;i<nCount;i++)& H- T9 ~' O- z6 `% H9 `+ G { x. D) W) [: B* ] if(!DeleteItem(0))! G8 s4 o8 ?: p { ' p$ M5 v. Y! m* d( B return 0;' k' J. f- k. |# X) g, [% }; r }. }7 u$ G0 t K- K } * z: ^. p% i3 R7 R; T8 ^2 } d return 1; $ D, A1 d. \8 M# T& Y/ {} * d2 s( K# v2 _3 i LBOOL CListCtrlEx::GetItem(LVITEM* pItem) # Z: V: e" E: B5 F% g8 f{7 Z( v S: G* ` BOOL bReturn = pThis->GetItem(pItem); 9 o" t5 G" n8 v0 D8 }4 ] if (pItem->lParam) . s1 n h* ~: Z {% ^6 W0 G8 k) o3 a: P" p pItem->lParam = ((ItemDataEx*)pItem->lParam)->SrclParam; , r ^! o1 |! E5 j }

return bReturn;, a' \! g# U6 V h }$ y2 H/ z; X. ]% C& Q( Y void CListCtrlEx::OnDestroy()8 `/ U5 { M) h0 ?4 j" S { ( [( g0 \% O2 h$ o0 g // 关键是清除申请的内存空间/ E. ?2 Q/ R2 V$ z DeleteAllItems(); + ^" ?; A9 N1 j/ n CListCtrl::OnDestroy(); ; f( |0 e4 r' q" o3 S$ g4 `7 }, ]9 l! }( L; R}

BOOL CListCtrlEx::SwitchItem(int nItem1, int nItem2)" A& f8 R& V/ }- W- s7 c) r. u$ h3 ` {2 V* _, ] G5 @+ y& X [ if (nItem1 == nItem2 || nItem1 >= GetItemCount() || nItem2 >= GetItemCount() || nItem1 < 0 || nItem2 < 0)% g. N# D- r R- C$ a$ [2 V { & D/ l. ]9 S( p) B return 0; 2 s) c4 W# Y" y0 y: ^: z) v }" c5 ^/ O, _( I7 y4 l3 s) D* B LVITEM ItemData1,ItemData2;/ `2 ], [8 b) l# m+ _+ j% Q8 ? int ItemLength1 = pThis->GetItemText(nItem1,0).GetLength();, P/ L( Z: T3 F& S/ }2 q int ItemLength2 = pThis->GetItemText(nItem2,0).GetLength();6 f O( ? F8 o2 V& p8 h; ^ ItemData1.pszText = new TCHAR[ItemLength1+1];" l9 a# l/ J9 W# l! S$ p2 z4 F ItemData2.pszText = new TCHAR[ItemLength2+1]; 9 `4 q' K- ?, w3 J; r3 V ItemData1.cchTextMax = ItemLength1+1; % B5 S+ M# F4 \6 S/ f ItemData2.cchTextMax = ItemLength2+1;9 q9 }; ~ z! Q0 W% o' I( o2 W, o ItemData1.iItem = nItem1;. x- n. R5 l% d ItemData2.iItem = nItem2; & D; t+ ^# k3 X. e/ }0 A, `0 |2 G ItemData1.iSubItem = ItemData2.iSubItem = 0;( n; Z% B9 Z, O ItemData1.mask = ItemData2.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE|LVIF_STATE;

pThis->GetItem(&ItemData1);9 o: g c( V: E- j) `5 } pThis->GetItem(&ItemData2); - V/ P. e/ T$ L+ k: w // 交换数据& E4 ~' m" ~0 U) b7 _0 n) M ItemData1.iItem = nItem2;! b' c* P0 N7 y4 A1 c/ x4 J7 W+ G8 E ItemData2.iItem = nItem1;- v. }8 o0 {2 n# U pThis->SetItem(&ItemData1);6 h5 X8 ^! s6 E5 X% H! q pThis->SetItem(&ItemData2);

delete ItemData1.pszText;: R0 J. A, i; r8 U delete ItemData2.pszText;: o8 N+ s6 W) Q, v3 T // 得到报表模式的列数 * s2 T `! D8 O$ J int nColCount = pThis->GetHeaderCtrl()->GetItemCount();' t$ |4 z# `, ]+ `- r3 O; | CString strBuffer; & H4 ]/ j6 t) X for (int iSubItem = 1;iSubItem < nColCount;iSubItem++)8 H5 J! E" P( w3 z4 d3 Y* u/ J { 1 G6 K$ M( H0 G4 L+ u strBuffer = pThis->GetItemText(nItem1,iSubItem); . g) [2 c% W* W) [1 g5 ?: S6 } pThis->SetItemText(nItem1,iSubItem,pThis->GetItemText(nItem2,iSubItem)); 9 }' [% K) n# f: [- C pThis->SetItemText(nItem2,iSubItem,strBuffer);( K* @5 b0 D; R } 5 [$ u4 q Q, k2 H7 x // 把原来的选中状态也转移过去 9 ^4 j7 h* h7 K: x4 [) r // 不知道为什么这个属性没有体现在LVITEM结构中……4 s) F+ l/ F; h) K% y, a! e bool bItemSelected1 = GetItemState(nItem1,LVIS_SELECTED) == LVIS_SELECTED;+ V5 w# e5 \4 |, S bool bItemSelected2 = GetItemState(nItem2,LVIS_SELECTED) == LVIS_SELECTED;

SetItemState(nItem1,bItemSelected2?LVIS_SELECTED:0,LVIS_SELECTED);; M2 u S" |- G s0 ?4 w SetItemState(nItem2,bItemSelected1?LVIS_SELECTED:0,LVIS_SELECTED); + t" c3 k3 ]# o- d- [) s! e: a- y1 q return 1;8 F+ Q/ B. @4 K7 d# d }

BOOL CListCtrlEx:reCreateWindow(CREATESTRUCT& cs); K* v& P( ?) E7 l5 S$ }: A! j {* f' f8 H- \6 D. _3 o7 D // 本类报表模式专用8 H9 r$ M$ ~1 i7 T6 [. ?+ n6 D% j! o cs.style |= LVS_REPORT ; 9 M3 p& |' {9 P. ` // cs.dwExStyle |= LVS_EX_FULLROWSELECT;. E, u, O5 R- W! h; p' X return CListCtrl:reCreateWindow(cs); & R, e$ Q% D9 F}

7 C* R7 W/ B Y* D% Y1 |( v COLORREF CListCtrlEx::SetHighLightColor(COLORREF clrHighLight) ' G% w1 P: P; ?$ }3 `! O) L" Z{ ( J2 G; k9 C) q1 x6 {/ H7 L) t' z COLORREF clr = GetHighLightColor();( [( n) z! b# K3 H) G3 M0 N m_clrHighLight = clrHighLight; , d1 B' M; H* Q& y& R4 F# N/ s return clr;5 F6 f+ ^& [# g2 @4 d }

COLORREF CListCtrlEx::SetHighLightEdgeColor(COLORREF clrHighLightEdge)7 c$ I( f2 B' H$ @8 j {& n/ H/ l% ?2 y COLORREF clr = GetHighLightEdgeColor(); : r1 e4 S5 N M6 ?- z m_clrHighLightEdge = clrHighLightEdge;9 @) H; v; Y+ k return clr; " t2 X* d$ F7 t. `- x* |}

该用户从未签到

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

COLORREF CListCtrlEx::GetHighLightColor(void) const 0 }9 K* h. p8 t& N{" \$ `9 `7 I6 U g6 V return m_clrHighLight;7 p2 |; v3 x+ c# p# h }

COLORREF CListCtrlEx::GetHighLightEdgeColor(void) const3 E" M1 G' w6 c% V, U/ g { & L) T0 ^2 _4 h' U0 g6 n& B return m_clrHighLightEdge;6 U* [4 C, E* e, f; E }

int CListCtrlEx::GetItemImage(int nItem) 8 V ?% D0 }2 R/ r$ u& a{8 |$ |* E5 t4 ]5 e! _3 E LVITEM ItemData;! E r& o! x7 n/ w ItemData.iItem = nItem; * I1 w" d# M# g6 K ItemData.iSubItem = 0; 8 h. h$ M9 n) T ItemData.mask = LVIF_IMAGE;3 o" p, }8 G0 d* S# v1 O pThis->GetItem(&ItemData); ( D6 `! V* B: }' { return ItemData.iImage;% N" Y3 F# i N& o+ [ }

BOOL CListCtrlEx::MoveItemUp(int nItem) ) {: P+ z2 g. p9 h# _' n# _+ e5 a{ 3 M; k# F7 z& O- J% T if (nItem < 1 || nItem >= GetItemCount())5 ^: E3 t/ K% b" |8 v {* e& u! W! c. D, p+ \ return 0;5 |2 [5 H W9 S } . x- `" ^+ i" n B return SwitchItem(nItem,nItem - 1);) j) K) ?; q9 D1 |/ J/ N6 Q$ ` }

BOOL CListCtrlEx::MoveItemDown(int nItem); o3 T k3 |# Z+ a8 ^# x {6 L& R$ k- X# j7 C2 m if (nItem < 0 || nItem >= GetItemCount() - 1)2 h6 i9 F) p& B0 A {- A' O. F) W+ N) {% N3 x0 X return 0; ~' f2 \( M" e) y }5 E; y) X7 x5 f, R return SwitchItem(nItem,nItem + 1); ! k2 C/ Z+ i% q' y5 U& u) A3 s3 w}

void CListCtrlEx::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult) 0 @& P7 S; z2 v' S) Q0 _{; y8 c# r9 ~, F: ^' D$ F8 t1 S NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR);

*pResult = CDRF_DODEFAULT;

if (pLVCD->nmcd.dwDrawStage == CDDS_PREPAINT) 4 Q+ V$ ~2 O8 P; ?% n1 V { 4 K5 a% r( \' P5 I8 @ *pResult = CDRF_NOTIFYITEMDRAW; 4 ~7 W7 K& I; C6 R }! N; G& \3 _) ] else if (pLVCD->nmcd.dwDrawStage == CDDS_ITEMPREPAINT) ( a1 e) h, c6 r3 a. F, K {7 w6 F7 p1 `: q/ U *pResult = CDRF_NOTIFYSUBITEMDRAW;" C3 L" k7 {+ E4 t/ h4 C } 7 q* q2 c& Z) ?0 a( e else if (pLVCD->nmcd.dwDrawStage == (CDDS_ITEMPREPAINT | CDDS_SUBITEM)) 1 \' M; o! i7 e# X/ h. h: | {

int nItem = static_cast<int> (pLVCD->nmcd.dwItemSpec); + m; w+ {7 ` S3 ?) J int nSubItem = pLVCD->iSubItem;6 ~+ M% B5 I. H U; {+ V. t. ^ if (nSubItem != 0) ) w4 A; n* g5 K9 N( L; \$ a3 r {1 q8 E' v; C4 Y // 全部绘制过程在nSubItem == 0 时完成: o% N; Y+ A) B. n4 l // 分布完成会有小问题,我无法解决 / Z1 n( J3 Y1 c# H) _: ^$ X- ^ *pResult = CDRF_SKIPDEFAULT; . V6 k. x) v6 d3 R3 ~ return;; ?. o0 ]3 j. E1 l } " D% [; |* b3 Z3 ?1 P& x CDC *pDC = CDC::FromHandle(pLVCD->nmcd.hdc); 1 w; J% c& `8 A6 x/ c6 R/ C0 o8 y int nColCount = pThis->GetHeaderCtrl()->GetItemCount();! u Z, C2 X3 ]# d0 T i CRect rectSubItem; ! y6 C5 n9 \( @7 i CString strItemText; , N1 C+ E4 u/ r pDC->SetTextColor(((ItemDataEx*)pThis->GetItemData(nItem))->clrItemTextColor); , l- K8 y# x) A- {5 I* J% E& c4 U pDC->SetBkMode(TRANSPARENT);' P- Y9 a- Z' z2 r& d" Y // 相当于橡皮& R2 S; w2 x0 N8 G& `4 o CBrush brEraser(GetItemBackColor(nItem));

// int nFirstWidth = GetColumnWidth(0);

4 H( c' i. |0 h for (int i = 0;i < nColCount;i++) . C# _1 y* U5 b6 { { 4 G0 R9 C1 U* m8 s* l strItemText = GetItemText(nItem,i);

if (i == 0)/ Z1 {) B) g# D6 I$ s" h {3 A _) i2 d, m$ ?, H' N- z pThis->GetSubItemRect(nItem,i,LVIR_BOUNDS,rectSubItem);# N3 v: h1 m1 E9 E5 H4 O // 擦掉原来的图形 - r: |( C. y( O6 x- i- O pDC->FillRect(rectSubItem,&brEraser); 1 E1 _: @" Z& W if (GetExtendedStyle() & LVS_EX_CHECKBOXES)- }* o8 l2 \" I$ c8 y7 m7 K { . p% I' {; W. ^' F2 S! e% p- @: t' B CRect rectCheckBox; 0 e+ @. c. U: ~9 D- m: M8 ` GetSubItemRect(nItem,0,LVIR_ICON,rectCheckBox);/ V9 F7 p: f5 i9 l S; C8 T6 @5 m CRect::SwapLeftRight(rectCheckBox); |* r; V. P; f1 b" I rectCheckBox.left = rectCheckBox.right - rectCheckBox.Height();

rectCheckBox -= CRect(2,2,2,2); $ t0 |" [& [- p' b: u) l pDC->Draw3dRect(rectCheckBox,0x81521D,0x81521D); : w& w [4 l! W //rectCheckBox += CRect(3,3,3,3); , ~/ w; R! z7 h* U: `$ t if (GetCheck(nItem)) ' j6 \( Z3 w9 E: @6 w6 ~# o7 J {5 W' d( H5 j* n& u // 绘制一个CHECKBOX9 X0 W4 w: G1 ] CPen penCheck(PS_SOLID, 1, 0x0021A221); ) M# b) A' o! E CPen *pOldpen = pDC->SelectObject(&penCheck);0 ~ _5 N% t9 q5 K" W: e" S for (int j = 3; j < 10; j++) - }) G2 ]2 @4 X' b5 S {4 ^' C( M; Q: x/ X, I( y pDC->MoveTo(rectCheckBox.left + j, rectCheckBox.top + rectCheckBox.Height() /2 - 3 + ((j < 6) ? j - 1 : (9 - j)));- V. F" |: [. t: H7 Y3 X pDC->LineTo(rectCheckBox.left + j, rectCheckBox.top + rectCheckBox.Height() /2 - 3 + ((j < 6) ? j + 2 : (12 - j))); % C2 N/ n9 ^; @# f. P } 2 [3 q9 g5 F% ]* D d: K # Q7 r8 H8 F, P, q2 K5 }# J" i pDC->SelectObject(pOldpen); 0 f, }3 G* A9 M/ S; Z( e) c, F }2 t( R& a3 C _4 k } ) _" }( J: G$ X$ m) T // 根据需要画选中框 - G5 ~5 U7 _4 w! K7 q3 `# E1 R if ( (GetFocus() == this || GetStyle() & LVS_SHOWSELALWAYS)&&(GetItemState(nItem,LVIS_SELECTED) == LVIS_SELECTED))7 q2 i- g0 c. ~2 {1 q( A' ~ {3 f3 t- q3 x6 ^" V S if (GetExtendedStyle() & LVS_EX_CHECKBOXES) . c. U+ q# \4 c7 W! y8 _8 Z {// 存在检查框风格的时候需要对左坐标进行修正 2 J- j+ s6 ` f4 O. T4 H CRect rectLeft;8 w5 j" p, r/ n GetItemRect(nItem,rectLeft,LVIR_ICON);, X. |5 y: k4 C5 h2 P6 J5 O rectSubItem.left = rectLeft.left; r4 Q/ ]1 m9 O } % Y, @6 k' y# R+ [' l7 f6 d! J if (!(GetExtendedStyle() & LVS_EX_FULLROWSELECT))" N7 ~2 m! f9 d2 V) @* Z {// 非全行选择时右坐标需要修正# |1 K, G( \0 c+ A+ U$ A CRect rectRight;; u0 O0 ?; H) j0 t GetSubItemRect(nItem,nSubItem,LVIR_LABEL,rectRight); 1 d5 Z+ [7 z5 P* | rectSubItem.right = rectRight.right;; _8 A; q- k# F& ~! h" J! |, V3 { } & x2 D+ F: R& o3 O+ m! e N9 J CBrush brHighLight; / D$ r# W+ c- q6 u y+ N9 p' J CPen penHighLightEdge;: a: P% ?1 f. u6 T5 b9 b if (GetFocus() == this): y& r0 J( I$ K7 b0 h0 x { " d) n1 `1 G" ^$ n" l) W- G/ m brHighLight.CreateSolidBrush(m_clrHighLight); 4 u) k( n0 ^1 \% y* {* W4 d penHighLightEdge.CreatePen(PS_SOLID,1,m_clrHighLightEdge); 4 S3 Y( N# S3 n2 `' x }! j+ {1 a, v) @2 p$ `4 F else & E: C9 e' n: E/ l! y/ K# _ { * w. V5 Q# ]) Q. q! q+ c // 不是焦点时,颜色70%不透明度 1 |: x# p* P6 g COLORREF clrBack = GetItemBackColor(nItem);7 x0 b2 z1 O6 w- H; \! @+ C WORD red = GetRValue(m_clrHighLight)*0.7 + GetRValue(clrBack)*0.3; + R$ D" H, h( x& q* y! _ WORD green = GetGValue(m_clrHighLight)*0.7 + GetGValue(clrBack)*0.3; - e" A& X& p4 {. i8 O" n5 f WORD blue = GetBValue(m_clrHighLight)*0.7 + GetBValue(clrBack)*0.3;0 u+ t `; i: z5 a9 R brHighLight.CreateSolidBrush(RGB(red,green,blue)); ; j1 B( h+ G' p/ x! b# P A red = GetRValue(m_clrHighLightEdge)*0.7 + GetRValue(clrBack)*0.3;5 T; A. T8 ` l, m |( ^! a8 `1 ^ green = GetGValue(m_clrHighLightEdge)*0.7 + GetGValue(clrBack)*0.3; I7 q( D3 S/ Q% g4 d7 ]0 C" \ blue = GetBValue(m_clrHighLightEdge)*0.7 + GetBValue(clrBack)*0.3;: M) I4 k1 w' a penHighLightEdge.CreatePen(PS_SOLID,1,RGB(red,green,blue));9 A8 _" v; V+ j1 X2 m, k& b; x } ; q' b) k6 ?6 A. F1 ^$ }) w CBrush *pbrOld = pDC->SelectObject(&brHighLight);, }: t. I7 ]3 a1 f/ p CPen *ppenHighLightEdge = pDC->SelectObject(&penHighLightEdge);" L7 t7 L1 [* R$ O2 b- V pDC->Rectangle(rectSubItem);6 ~, h+ G: I' o pDC->SelectObject(ppenHighLightEdge);6 C3 W' T3 `/ A& O0 O! r7 \: j pDC->SelectObject(pbrOld);3 Y3 j! H6 @/ F# H' A- R, V! J } 8 o4 J( b4 p1 A5 J4 r } 0 H9 R# K% u1 t% x pThis->GetSubItemRect(nItem,i,LVIR_LABEL,rectSubItem);* L- d8 v9 d1 M: @# c* a UINT uFormat = DT_SINGLELINE|DT_VCENTER|DT_END_ELLIPSIS;: Q! x& }6 S& p2 ^ HDITEM hdi;9 ]: O; v% J0 c0 |* P- v hdi.mask = HDI_FORMAT;! z' d1 j8 F Q* z1 `4 ? pThis->GetHeaderCtrl()->GetItem(i,&hdi);+ t. @- x8 m, ]/ I if (hdi.fmt & LVCFMT_RIGHT)$ H- C" G9 U$ Y% l! [9 U% ^; q/ W {- |4 I6 h: H3 y& `( j5 k: y) ^ uFormat |= DT_RIGHT;/ n# R& Z, r9 _% x$ ?, P2 `8 Z+ l } + g5 m$ n. ^2 `, b else if (hdi.fmt & LVCFMT_CENTER)2 d6 W8 {; \; @& r% q4 i# K' Y3 { X {" `. M( V: m% j6 F h7 f5 G uFormat |= DT_CENTER; 7 X# T1 j; a. r8 R$ V }7 N0 o( n4 v: d) U pDC->DrawText(strItemText,rectSubItem,uFormat);

CImageList *pImageList = pThis->GetImageList(LVSIL_SMALL);- i- s( h9 t* n7 A if (pImageList) " f+ l; d0 i% m% B6 b# r {# p+ v& Y1 V- [7 \$ S& Z: {* s4 C CRect rectIcon; 9 G6 }$ H0 Q7 K GetItemRect(nItem,rectIcon,LVIR_ICON);; D1 B0 N0 c. e% S2 z+ P9 }' k1 i pDC->DrawIcon(rectIcon.left,rectIcon.top,pImageList->ExtractIcon(GetItemImage(nItem)));, A' q$ a: `% i) ` }* ?: ]# [6 n7 T3 g, n } 0 ^4 q) Z0 H3 |' ^$ {7 u *pResult = CDRF_SKIPDEFAULT; & _0 o' D' }6 _" t9 q8 f Q; }* f }

}

该用户从未签到

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

本版积分规则

关闭

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

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