下沙论坛

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

QQ登录

QQ登录

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

又是垃圾,CListCtrlEx Base CListCtrl

[复制链接]

该用户从未签到

跳转到指定楼层
1
发表于 2005-2-23 21:29:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
// ---------------------------------------------! U) _9 u2 }3 b3 Q' c
// ListCtrlEx.h9 ]# k. Z  b" B3 [6 \2 f
// ---------------------------------------------& R* E1 r9 P* s8 |9 U7 o8 ~
#pragma once
& D$ _1 o5 m0 a6 R/ y' h+ r5 M- Y+ |! h! t
// CListCtrlEx) u7 H# L+ p9 E# @# d- Q
class CListCtrlEx : public CListCtrl
3 l# X% R8 z4 i* b{
- {  ?. I0 X6 m8 S2 ]6 \7 }    DECLARE_DYNAMIC(CListCtrlEx)
2 W$ s  B5 y  @! cpublic:* q' v8 b5 S+ k: h
    CListCtrlEx();
! r2 `7 v3 w- a    virtual ~CListCtrlEx();
6 I$ e$ Q1 C: u# z! pprotected:2 C/ O+ U. U, A/ z" Y9 Z8 L" \1 w: v. A
    DECLARE_MESSAGE_MAP()$ y) j  g5 w0 `  F+ u5 u" m: \7 U
    CListCtrl* pThis;
! Y+ D( J' k0 ]2 X' q    virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
1 E" f) M' k1 Tprivate:
3 ]2 K+ B/ s% x. u    struct ItemDataEx {
+ R5 v# |5 ^$ ?7 T& ]+ p- Q        LPARAM SrclParam;+ h1 s) I. k; l  Q4 \
        COLORREF clrItemTextColor;4 B5 @3 C& f' F9 t. ^$ F0 F! g6 q
        COLORREF clrItemBackColor;: L/ X# e( C  s
    };
* {6 a' w5 C0 U- h6 `0 x    COLORREF m_clrHighLight;8 g! G8 `8 u0 @: r  {& Y$ w' u
    COLORREF m_clrHighLightEdge;
. ?% e  Y0 P& Z- |" vpublic:
  y9 a) g/ E" A3 v6 D3 |/ e    // 新增方法" t. o! m3 }/ S$ B4 s
    COLORREF SetItemTextColor(int nItem,COLORREF clrText);
! {$ v8 d/ {! b& S; Z) `    COLORREF GetItemTextColor(int nItem) const;
3 `. j! X$ C. y: L; J    COLORREF SetItemBackColor(int nItem, COLORREF clrBack);( I- l( _5 I+ U8 m; w$ F) r* u$ w8 L
    COLORREF GetItemBackColor(int nItem) const;
: N8 }2 v: P/ Y    COLORREF SetHighLightColor(COLORREF clrHighLight);* r" p; Q, W- s; Y
    COLORREF SetHighLightEdgeColor(COLORREF clrHighLightEdge);, F* x  o& l5 H- l
    COLORREF GetHighLightColor(void) const;; l$ c8 L* [- _. [* M0 _# }
    COLORREF GetHighLightEdgeColor(void) const;
9 I4 ?/ B- x! a* p5 D7 X" H( [8 K    int GetItemImage(int nItem);. o4 i2 s1 x1 o& t8 o, d  F/ ?
    BOOL SwitchItem(int nItem1, int nItem2);
* p+ d' z) r. n: |# A$ h    BOOL MoveItemUp(int nItem);
+ r, v# R% F9 |9 U* q    BOOL MoveItemDown(int nItem);
! v" l7 i& E! a2 e% o    // 重写原来的方法
; T1 `* S& H$ V    DWORD_PTR GetItemData (int nItem) const;" H  G9 ]5 W4 r, R  j  M7 ]0 z1 s
    BOOL SetItemData (int nItem, DWORD_PTR dwData);9 N! V. C, |9 ^7 F
    int InsertItem (const LVITEM* pItem);4 l! f) I* h0 b( f) \" Q
    int InsertItem (int nItem, LPCTSTR lpszItem);
: T& d* ~! F4 k  C. z& _& q* n    int InsertItem (int nItem, LPCTSTR lpszItem, int nImage);6 U* P! ~4 o' E4 U  i8 Q# q
    int InsertItem (UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam);
+ ?) X  G7 c0 U* ~$ M- N: {2 j- P    BOOL DeleteItem (int nItem);! o" Z, y5 k; F' M
    BOOL DeleteAllItems ();7 S) V  T' [" C% Y
    BOOL GetItem(LVITEM* pItem);
+ j" ]+ u1 r3 Z6 P+ {    afx_msg void OnDestroy();7 N" h4 D9 ^2 F& s) [! H8 [
    afx_msg void OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult);/ K& `0 d; f: P) y% {, R
};
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 顶 踩

该用户从未签到

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

// ---------------------------------------------# Y0 h v2 u6 g7 L" U5 m7 H7 u // ListCtrlEx.cpp % t% H6 ^# u; x5 j( {// --------------------------------------------- ' B" K' g. x7 a: T! \// ListCtrlEx.cpp : 实现文件) f# x4 M/ }# A+ T! @ //

#include "stdafx.h"' O5 R9 t( W2 D$ s #include "CreateNewClass.h"- C; E* u: N6 t( r" f$ G #include "ListCtrlEx.h"2 u' n7 A# t* u q0 H! u5 P #include ".\listctrlex.h"

) A! v! q& O3 ]& |0 e// CListCtrlEx

IMPLEMENT_DYNAMIC(CListCtrlEx, CListCtrl)3 R# _* H& |; Y/ q CListCtrlEx::CListCtrlEx() \. d+ J0 s+ w) v) C) P {1 m3 [4 s, h# r& I- ?3 w: \ // 用于调用基本的方法$ g- d' w T! d7 |# d. n' S2 j pThis = (CListCtrl*)this;: U' k9 ~: ?! U" Z4 {" u // 默认的选中框颜色 # ]6 F% J$ T) u( H/ } z m_clrHighLight = 0xF1DACC; 5 x# [5 R8 }# u7 E! `- m! Y: t m_clrHighLightEdge = 0xC56A31; : \! y2 K$ J% C1 B}

CListCtrlEx::~CListCtrlEx()5 r/ U2 V& ~8 o+ _) o. c: s {

}

9 l R F" c! A* D BEGIN_MESSAGE_MAP(CListCtrlEx, CListCtrl) . `$ A. O$ h8 v9 `- V ON_WM_DESTROY() * c6 I5 V. V1 V. b: q4 Q ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnNMCustomdraw) $ p6 H* _5 ~; }" Q0 N% J& r1 jEND_MESSAGE_MAP()

// CListCtrlEx 消息处理程序

8 H( [3 x9 q) _, mCOLORREF CListCtrlEx::SetItemTextColor(int nItem,COLORREF clrText)( c3 Z: k+ D. s1 A { K; @- m" U. B% A8 k6 V! x9 @, _ COLORREF clr = GetItemTextColor(nItem); 9 r& [, k i" x) X x ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);7 ^6 c+ v+ t/ B1 g9 S6 U5 {$ g4 ~9 n pData->clrItemTextColor = clrText; Q3 H) Q @" k return clr; 7 ], e8 M! ^5 N3 O( r4 `( ~}

COLORREF CListCtrlEx::GetItemTextColor(int nItem) const' H8 i/ I) L" S {! `, z; _. h/ w" [ ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem); 0 O# T* A9 h X# J' E# V return pData->clrItemTextColor; 1 r! n2 N; i, V4 a}

COLORREF CListCtrlEx::SetItemBackColor(int nItem, COLORREF clrBack) 7 H. B) ?1 u# O3 w, L{9 D$ g- X7 o5 E: g COLORREF clr = GetItemBackColor(nItem);. e& D+ e. O! s0 [ ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem); O3 j$ k' \" R9 o8 _/ J pData->clrItemBackColor = clrBack; ) r# z" R- X# \ return clr;& S7 n; T5 D' V V }

COLORREF CListCtrlEx::GetItemBackColor(int nItem) const$ r* m+ a$ f# \% d7 X8 J+ S { ; a* L+ S$ f( a# i5 ? ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);$ J. g" X1 w* W# o- I$ V8 f' F' c return pData->clrItemBackColor;4 B; P1 p5 s2 |& ~ }2 I0 ~5 g& t& P" W DWORD_PTR CListCtrlEx::GetItemData (int nItem) const ; A1 }6 a8 ^8 Q% a& c" }{ . |3 B5 J" [7 f9 \/ O if (nItem >= GetItemCount()) $ o' K& E8 ^9 D5 X {. B# O% L; N/ a5 p5 @" f) B# T return 0;; z6 ~$ ]; X, H3 ?1 I }7 u- f9 D, D3 y- c$ f# V CListCtrl* pThis = (CListCtrl*)this; ) X6 p1 A: A/ U& Z6 r1 m% g DWORD_PTR Data = pThis->GetItemData(nItem);$ }4 s* K5 |6 ~7 ^ return ((ItemDataEx*)Data)->SrclParam; 3 i; a+ O5 u7 i b$ L} 3 Z6 i: w' b$ k3 Z2 e9 mBOOL CListCtrlEx::SetItemData (int nItem, DWORD_PTR dwData)2 {* k7 X0 q" S- T. B* c- z {: o/ B. j1 L& A/ E0 X* [ if (nItem >= GetItemCount()) }3 q, {- L- u& W; N- S u {( X# L1 G* M: Q: X4 ]9 v/ H return 0;7 t% y0 \ ^( T" N) U* U! A } 2 T( h" V% _% o6 j CListCtrl* pThis = (CListCtrl*)this;) `, f) J7 l* M. T DWORD_PTR Data = pThis->GetItemData(nItem);% L- h9 U) ?& A& O. W ASSERT(Data); # L* s1 Y& C( u$ x6 J) G$ [4 y ((ItemDataEx*)Data)->SrclParam = dwData;* t: g) Q& Z% c) p% E5 [( S2 b return 1;$ c8 X& _7 X2 Y, F) K }' _& @/ b* y7 ]$ Y. j! [' R int CListCtrlEx::InsertItem (const LVITEM* pItem), M* V2 o2 z9 T1 h9 _ {, ?. K4 [# z' k3 X, _5 a int nReturn = pThis->InsertItem(pItem); 7 }( P% v" C# F( y) n ItemDataEx *pData = new ItemDataEx;# G# T$ k8 b' x: J+ G // 设置默认颜色3 U, _9 e# l V2 x1 g x: n pData->clrItemBackColor = 0xFFFFFF;//pThis->GetTextBkColor();: ^9 D* s# f O' C8 B pData->clrItemTextColor = 0x000000;//pThis->GetTextColor();9 I6 a' Z* g% G3 Q5 c5 q pData->SrclParam = pItem->lParam;$ B- F1 k$ `) @ _ pThis->SetItemData(pItem->iItem,(DWORD_PTR)pData); # a+ L' p' }3 D2 E- v. `" m return nReturn;5 L2 p- z9 w; j }3 M7 D# k) X2 c7 l int CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem)1 O; m, {: X' g) U9 s* \ {7 h/ \: \+ [$ U, E: n. ~) K LVITEM Item; ; y9 m4 u' |! B3 o. |1 T* H4 q* I memset(&Item,0,sizeof(LVITEM));8 T% w8 o9 z6 G+ a) i" r Item.pszText = new TCHAR[strlen(lpszItem)+1];, [9 \. W0 S- V$ w6 ]2 f: V strcpy(Item.pszText,lpszItem); 3 G* c: L4 |: z$ \ Item.iItem = nItem; E' C4 p3 Y/ a" R( F p) d Item.lParam = 0; 4 |$ W4 [/ e: ?7 U7 B( S- g Item.mask = LVIF_TEXT |LVIF_PARAM;# B. j Z5 P& B$ H BOOL bReturn = InsertItem(&Item);/ t% L( L! B; q* B! }& ^ delete Item.pszText;$ K% ^- ^" z" Q- ? return bReturn; 3 f; J4 b6 |2 e! ~% P0 a6 O* c$ I}. Z, U' h5 ?/ Z8 I int CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem, int nImage)$ S: N" W% u& X& \0 i) Q { 1 f; [5 O" _+ x. n8 B: j LVITEM Item; ' J+ F% f/ c3 W& a6 k# K/ L, K \ memset(&Item,0,sizeof(LVITEM)); # Z r4 I3 J0 |+ h Item.pszText = new TCHAR[strlen(lpszItem)+1];4 U' f) G6 V }! S strcpy(Item.pszText,lpszItem); 4 O( D2 u. c( ^. p# h) M Item.iItem = nItem;/ j' R: {# j& b& i$ j8 w. x M- a Item.lParam = 0;5 E7 P$ T) G0 k* g; c- s# k Item.iImage = nImage; L4 h1 ~# o; A" |& e: u/ A Item.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE;" W, H: q3 b( U! C3 j BOOL bReturn = InsertItem(&Item); , ]; K% z" @1 A Y Z4 { delete Item.pszText; : ^' [# |, c% H* Z9 u return bReturn; , a0 q' e) @1 W- t8 v1 G' V} 4 D& z' y9 _: ~* b6 q# `' A/ vint CListCtrlEx::InsertItem (UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam)% G7 f- g8 k$ C6 F6 } { ) }: C' @& t& R, R' d LVITEM Item; - c. P+ U/ ?8 }$ c memset(&Item,0,sizeof(LVITEM));- O' b7 x( ^# A: s( } Item.pszText = new TCHAR[strlen(lpszItem)+1];- U# T3 D* E+ o% V3 D7 s0 d% I strcpy(Item.pszText,lpszItem); 6 E' Z- C8 ^1 _ Item.state = nState; 3 Q3 A% q$ R9 b Item.stateMask = nStateMask;% i2 `8 k4 K) O" a9 m7 @ Item.iItem = nItem;& g0 {3 h# ~, `! b Item.lParam = lParam;$ x, i i5 }$ f, @ Item.iImage = nImage; - g: h. J, n4 f8 Q& q Item.mask = nMask;6 i9 I# |9 m% ~% j) \, W BOOL bReturn = InsertItem(&Item); , Y0 f+ h7 k% O; } delete Item.pszText; * c4 X* b6 M" |- `' i. z return bReturn;0 @) j1 v0 o- l! D- Y5 c } ; J/ |, s4 _- aBOOL CListCtrlEx:eleteItem (int nItem)& {. d, s: L$ m4 C {, X8 m6 \6 B ^" l. d, E void* pDataEx = (void*)pThis->GetItemData(nItem); ; |( T* D7 F/ ~2 r; K+ a if(pDataEx != NULL) - c, G' v( L' h# `* }& e( o$ b7 @$ R {" \ E0 y# U2 ]" O1 J delete pDataEx;6 @2 q9 b I a }" o, ?! X- ^+ Q. ~2 I+ x: t return pThis->DeleteItem(nItem);& M' b3 V5 A; @" R/ u }. ~' E4 P" r4 c0 D( V BOOL CListCtrlEx:eleteAllItems ()% J- j- K: T3 v! U0 s7 A {3 \9 t- J4 l. }' ]8 Z+ G0 @ int nCount = GetItemCount();( Q F# D( D1 ~' W* D( X for (int i = 0;i<nCount;i++) {6 X% y7 F2 i" O {1 n, c$ B" W% @ if(!DeleteItem(0))2 R* y' Z* Y! S {5 w* G# ^, c# h' L6 Z return 0;& _0 W: z* ^* S- s }0 a4 ]' D% ]" `6 [: d' T9 l, Q } 0 q' h6 L% O) P& b2 { return 1;- I& R! S! O2 f8 z1 O, O }8 B% a6 P5 o/ a; N( P( F BOOL CListCtrlEx::GetItem(LVITEM* pItem) * Y* L3 s& N! y$ s7 V4 T{ ) c0 F: B/ x% Q! U& B BOOL bReturn = pThis->GetItem(pItem);6 l8 D F1 d3 q) S; L% F! H( u o if (pItem->lParam)3 n9 S7 P+ J1 n" A! A6 A! g {5 [3 r/ P* F/ m8 I pItem->lParam = ((ItemDataEx*)pItem->lParam)->SrclParam;. K2 [# A) p5 O6 {8 e/ H$ ]' r }

return bReturn;" K; Z+ H6 _& f4 B/ a" b } 4 c/ c# w! t9 E7 y' q) Wvoid CListCtrlEx::OnDestroy() 2 L- U+ G& I0 M! c1 ~1 U; G( W{ % `: n8 n4 F& U: _ // 关键是清除申请的内存空间4 r1 r1 d7 c/ h. D( e6 N6 ~ DeleteAllItems();! P% i* Z2 Q* U" c( w CListCtrl::OnDestroy();" ]! w. [5 F+ f$ t. W# E; u7 R }

BOOL CListCtrlEx::SwitchItem(int nItem1, int nItem2) 1 c5 e8 Q+ _: o1 b% \4 A% u+ Y2 |{" z- k8 \1 g4 Y9 b4 G( L if (nItem1 == nItem2 || nItem1 >= GetItemCount() || nItem2 >= GetItemCount() || nItem1 < 0 || nItem2 < 0) % Y3 q0 c) i' _( N5 h8 q {7 P* [: F- {* `" H5 u return 0;4 [# _8 D% }- v$ ]& I5 Q) d! w, Q: {4 T } ' ` i9 c. Q9 C4 s LVITEM ItemData1,ItemData2;/ C! O' f9 ?0 ^+ y int ItemLength1 = pThis->GetItemText(nItem1,0).GetLength();( p! i' D' G8 r2 I+ v: k4 l int ItemLength2 = pThis->GetItemText(nItem2,0).GetLength(); 3 b+ i4 p1 F& H8 ? ItemData1.pszText = new TCHAR[ItemLength1+1]; }: d) L* ?3 g" b ItemData2.pszText = new TCHAR[ItemLength2+1]; / t; q2 P( M8 ?( C9 r ItemData1.cchTextMax = ItemLength1+1;, N6 V# ^ \3 u! I% e5 k ItemData2.cchTextMax = ItemLength2+1;' e1 _# e! [; v$ h+ k: \ ItemData1.iItem = nItem1; g9 P2 d- x5 s/ K8 K/ K$ D ItemData2.iItem = nItem2; 0 c& A8 y$ Z1 Q ItemData1.iSubItem = ItemData2.iSubItem = 0; 1 ?8 q( R+ z2 z" f3 G! B0 D2 E5 x ItemData1.mask = ItemData2.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE|LVIF_STATE;

pThis->GetItem(&ItemData1);4 B% I/ ~4 z/ K# o' D pThis->GetItem(&ItemData2); 7 x& L1 d8 Y+ W/ b // 交换数据 ) Y9 N$ Z' S$ D5 G' J ItemData1.iItem = nItem2;4 I' n: F: c# P8 q [ ItemData2.iItem = nItem1;9 z9 [8 J1 }3 K1 I! u4 Z pThis->SetItem(&ItemData1);; n9 ?! U8 I$ t- l2 z9 f: B' @ pThis->SetItem(&ItemData2);

delete ItemData1.pszText;" e; W: Z# P }8 f* m6 O" N2 { delete ItemData2.pszText; + ~0 V: o1 U) K( J! m9 p+ }% h // 得到报表模式的列数: `; e; c( [8 Z) C2 @/ X int nColCount = pThis->GetHeaderCtrl()->GetItemCount(); + m1 g- g7 U4 u CString strBuffer;7 D! k S9 `' w( D2 V* F for (int iSubItem = 1;iSubItem < nColCount;iSubItem++)$ b" \+ z) ? N, M) e3 D% l { 2 b: ]2 M4 @* ~" K) C( Q3 C, \# L strBuffer = pThis->GetItemText(nItem1,iSubItem); / N8 P) L! V, P9 V" M) M pThis->SetItemText(nItem1,iSubItem,pThis->GetItemText(nItem2,iSubItem));9 @8 A1 m D# |( G+ G- o1 m pThis->SetItemText(nItem2,iSubItem,strBuffer); 7 z% F( } |- r2 j5 s, ] } % I3 l" L. e+ m1 X // 把原来的选中状态也转移过去8 R9 Z9 b3 a2 |0 l* l, D+ e // 不知道为什么这个属性没有体现在LVITEM结构中…… + b$ @) N1 w6 B$ F1 F! g4 E bool bItemSelected1 = GetItemState(nItem1,LVIS_SELECTED) == LVIS_SELECTED; ! Q9 \, W; k/ {% O# z0 Z bool bItemSelected2 = GetItemState(nItem2,LVIS_SELECTED) == LVIS_SELECTED;

SetItemState(nItem1,bItemSelected2?LVIS_SELECTED:0,LVIS_SELECTED); 4 M+ x% |8 d3 D0 ^* U SetItemState(nItem2,bItemSelected1?LVIS_SELECTED:0,LVIS_SELECTED); `4 }& n; r& r" A return 1;) l7 D F* ^* H9 V' G2 m }

BOOL CListCtrlEx:reCreateWindow(CREATESTRUCT& cs), Z' A! H: b# G& O7 O/ X; \ { ) y: M1 Y- z4 Q; c* Q // 本类报表模式专用1 F3 P N: e4 d4 @/ P/ Y cs.style |= LVS_REPORT ;* R Z" F3 A/ t5 u& n: P // cs.dwExStyle |= LVS_EX_FULLROWSELECT;0 o( J. S# p- v: R1 r: W6 g return CListCtrl:reCreateWindow(cs); - Q+ G- K: \8 W' l0 ^; d}

% I$ {# Y- ^7 @& t8 i COLORREF CListCtrlEx::SetHighLightColor(COLORREF clrHighLight) ( m. t" c M5 }" u' Q{ 5 Q( ^& Q- a/ T- @. k# ^6 H COLORREF clr = GetHighLightColor(); , Q: F. L$ Q3 q. Y m_clrHighLight = clrHighLight; ; i: y5 J# a+ B! P: t" {2 U, S return clr;3 C9 f- x+ \ z9 Y/ M }

COLORREF CListCtrlEx::SetHighLightEdgeColor(COLORREF clrHighLightEdge) , w% H9 y+ L1 t{( ?) i6 R, v6 n a COLORREF clr = GetHighLightEdgeColor(); " i8 e: w$ T, m6 M: `( H6 f; n4 a m_clrHighLightEdge = clrHighLightEdge;9 l: l0 D, ?8 P& S return clr; 8 @. M) P' A" E1 k# U. g* e6 p0 x}

该用户从未签到

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

COLORREF CListCtrlEx::GetHighLightColor(void) const P8 n' D8 O0 D5 M { % G9 y* v2 D p2 X return m_clrHighLight; ) l G' Q& y1 x" X5 u+ B}

COLORREF CListCtrlEx::GetHighLightEdgeColor(void) const9 m5 ?% A, L% K; p9 F {4 ^2 F3 Q5 G+ J return m_clrHighLightEdge; . l6 n- h& s$ \! r}

int CListCtrlEx::GetItemImage(int nItem)3 G3 g9 C: O+ X! ]7 W3 H { 0 v9 Y0 l, V7 ^+ ? LVITEM ItemData;$ V1 v- ]3 j- I! U" ~8 r9 v ItemData.iItem = nItem;4 A2 e$ N! y! T- F! R2 |7 U ItemData.iSubItem = 0;# ^/ u% V" E/ Z; B+ g$ E# C ItemData.mask = LVIF_IMAGE;0 h4 u, ]( H, L0 J* d/ t* }+ L. } pThis->GetItem(&ItemData); % @% N! f, g+ w" S return ItemData.iImage; 1 P3 w1 H# x6 v, O9 L) M5 c/ m}

BOOL CListCtrlEx::MoveItemUp(int nItem)) w$ R' V4 L9 p* Y, e, T& [ {7 N( `/ J3 x7 p, e3 Q C8 l if (nItem < 1 || nItem >= GetItemCount()) # h* d% G9 b2 D8 W8 L0 X0 g {5 J& ? j3 q) k) Y8 V return 0; 1 r. r; f9 i' _ } : s3 |( ~% ^1 D' N" m& o* q8 Q return SwitchItem(nItem,nItem - 1);! `% t: J( K' {6 C; f: {1 t. D6 p" [# d }

BOOL CListCtrlEx::MoveItemDown(int nItem)4 v1 f0 z2 } Q Q8 T) t; d- n { 4 M# B" `; j% Y$ U3 y7 \" N5 M if (nItem < 0 || nItem >= GetItemCount() - 1)' ^, E0 ?( F, ~: h# B% m0 F: S8 z {7 U+ g# z* w2 J return 0; . S8 A) r0 F5 l3 K. J- |- g' T. r }* F0 d# w8 K( U- c* t return SwitchItem(nItem,nItem + 1); ' q, G( ?- c O}

void CListCtrlEx::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult) 2 ?0 T1 I9 ?4 Q1 D{ . r* d. x; k9 F0 I; K6 ^, w NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR);

*pResult = CDRF_DODEFAULT;

if (pLVCD->nmcd.dwDrawStage == CDDS_PREPAINT)/ u/ {. I; v8 Q- q {! I H4 \( F/ S, K# K; v) Y *pResult = CDRF_NOTIFYITEMDRAW; : R3 Q9 \1 O& Z0 ]1 R5 I* g } % T0 l. ~0 I1 P' q/ E2 O" f else if (pLVCD->nmcd.dwDrawStage == CDDS_ITEMPREPAINT)1 I/ B7 O: D2 Y& h& R; d2 M { ) F3 Z7 Q) d) R* p: |( h+ h *pResult = CDRF_NOTIFYSUBITEMDRAW; : B( e! N; e" F* r4 ?0 u5 Y" `4 v } b: H9 h4 P% ~ else if (pLVCD->nmcd.dwDrawStage == (CDDS_ITEMPREPAINT | CDDS_SUBITEM))0 J: w8 d- ]2 X0 W! \: G6 @ {

int nItem = static_cast<int> (pLVCD->nmcd.dwItemSpec);! V$ ~7 P" O- e. i, D0 V. q. u1 w int nSubItem = pLVCD->iSubItem;' i% d! d$ X7 D& X9 q4 f" w* J if (nSubItem != 0)& B& w l5 n# v _( i- h* M {" G3 d5 I, [! o7 A* ] // 全部绘制过程在nSubItem == 0 时完成) e, P1 S. {( G V0 r- A // 分布完成会有小问题,我无法解决) Q" K0 o p/ z0 _1 \ *pResult = CDRF_SKIPDEFAULT; 9 M9 c3 W8 F4 R! G return; & T: T+ m& t* W' P( r }$ U( ~9 Z# }0 z5 V$ u8 Q5 c7 X: O CDC *pDC = CDC::FromHandle(pLVCD->nmcd.hdc);+ N) }9 T7 m2 }" p int nColCount = pThis->GetHeaderCtrl()->GetItemCount();. {" \; d( k+ c5 b$ u1 P) i CRect rectSubItem;0 e0 r& f9 \' t/ ~* F- Q CString strItemText;+ H v( n- O# K0 B" q7 R7 h, K% O pDC->SetTextColor(((ItemDataEx*)pThis->GetItemData(nItem))->clrItemTextColor); ' R( Z, U7 H, {- F1 e& ^ pDC->SetBkMode(TRANSPARENT); ( @; D) \ T5 s% |) }' u. Z // 相当于橡皮# y6 T0 T) _1 ^ CBrush brEraser(GetItemBackColor(nItem));

// int nFirstWidth = GetColumnWidth(0);

/ k# n+ J+ H w( P: R for (int i = 0;i < nColCount;i++) 4 }$ C, C$ U+ W8 i% r {! p' d) t, ^# N( Z6 V) W( C" J# T' D strItemText = GetItemText(nItem,i);

if (i == 0)4 h& E& f# Y1 _" t( @1 F { 4 f5 e) @' g# [7 E8 C! w/ g* { pThis->GetSubItemRect(nItem,i,LVIR_BOUNDS,rectSubItem); 9 E: K6 m3 Y& |9 i1 e // 擦掉原来的图形1 `9 K2 a0 w: M. n4 V: V; s( f pDC->FillRect(rectSubItem,&brEraser);/ b& L) N, F4 ?# ]4 _ | K+ Q if (GetExtendedStyle() & LVS_EX_CHECKBOXES) ) A- k$ N* r. u1 l; c5 O" i { W6 w* _8 L; y/ Y. B CRect rectCheckBox;& b1 J Y, R5 l: W GetSubItemRect(nItem,0,LVIR_ICON,rectCheckBox); 8 W1 K# V" S5 e0 I% |! |0 z) ^2 J CRect::SwapLeftRight(rectCheckBox); - |( S D$ C% J! W4 f% Q. i- ~ rectCheckBox.left = rectCheckBox.right - rectCheckBox.Height();

rectCheckBox -= CRect(2,2,2,2); 8 _8 Q0 r% p( D- d) S$ T pDC->Draw3dRect(rectCheckBox,0x81521D,0x81521D); ; t. G1 w7 i9 j l# D. ` //rectCheckBox += CRect(3,3,3,3);/ P7 b+ L x3 p' W3 ~+ C# |/ m if (GetCheck(nItem))# q" j: U- N' H) V { : d8 o6 f6 ~8 m+ l& [3 r7 b // 绘制一个CHECKBOX. z3 |% x. W, t: C CPen penCheck(PS_SOLID, 1, 0x0021A221); : O1 O3 L: I; }; U% } CPen *pOldpen = pDC->SelectObject(&penCheck); ( r$ j- u2 X/ p2 A0 Z$ r for (int j = 3; j < 10; j++)' I u1 j2 I. Z3 {0 O* D+ v {" b0 ?% H3 r6 [( R pDC->MoveTo(rectCheckBox.left + j, rectCheckBox.top + rectCheckBox.Height() /2 - 3 + ((j < 6) ? j - 1 : (9 - j))); # s3 S9 H+ P& }+ P, z. F5 S; N' I pDC->LineTo(rectCheckBox.left + j, rectCheckBox.top + rectCheckBox.Height() /2 - 3 + ((j < 6) ? j + 2 : (12 - j)));7 z) Q! x) H. [+ b6 \: I2 E8 `) k }; a% F" l/ q ?, H 7 ^* W! C! `! ?; P; l pDC->SelectObject(pOldpen);' t. T( u: P3 Q$ R9 f } 8 s! P9 o4 A/ g; X. ^% ?' n9 H' t }; F- {1 T/ m1 b" j // 根据需要画选中框 . {) [; L4 w% ` if ( (GetFocus() == this || GetStyle() & LVS_SHOWSELALWAYS)&&(GetItemState(nItem,LVIS_SELECTED) == LVIS_SELECTED)) % U8 |* |! f! s! K/ K { 8 }9 I1 F: o1 j: D6 C% N+ Z if (GetExtendedStyle() & LVS_EX_CHECKBOXES) 6 A3 ]2 R# S% y- g, X3 { {// 存在检查框风格的时候需要对左坐标进行修正 9 b& g/ s" o( a, ` CRect rectLeft;. z# A; C* e7 u& z. X3 y GetItemRect(nItem,rectLeft,LVIR_ICON); 3 ?* _4 @! g* R5 {6 n) P. B, D8 d1 j rectSubItem.left = rectLeft.left; I& f4 L+ _! K } & k7 {8 j2 L k! a6 c' d/ U: z if (!(GetExtendedStyle() & LVS_EX_FULLROWSELECT))# `5 H9 a: O) I6 B6 I {// 非全行选择时右坐标需要修正; x; t1 ^2 S- t7 T CRect rectRight;# V( K! w9 R3 ~4 L0 e+ B) j GetSubItemRect(nItem,nSubItem,LVIR_LABEL,rectRight);7 H3 X4 x1 w G. i Z7 e rectSubItem.right = rectRight.right;+ `/ W' `! t* q0 Z8 I# l }7 e5 m7 s( b- X+ T# j CBrush brHighLight; ) D) P6 I! Z/ e$ r/ _ CPen penHighLightEdge;9 t1 O% o6 c0 W: d if (GetFocus() == this)5 l' D$ G/ L6 h8 [& A/ a; n. M { - r4 V3 n4 o$ C: M" T brHighLight.CreateSolidBrush(m_clrHighLight); # B7 m* w) e7 b6 g0 V$ f penHighLightEdge.CreatePen(PS_SOLID,1,m_clrHighLightEdge);8 _+ R. y1 c- ], E/ E$ U1 x }- n1 N C9 S* ~ else+ m. i' E% I. `4 Z4 X9 t {% x5 y$ G( x/ y // 不是焦点时,颜色70%不透明度( [- w8 H* _, | COLORREF clrBack = GetItemBackColor(nItem); . j1 n, B( M2 d1 u) j8 w' y WORD red = GetRValue(m_clrHighLight)*0.7 + GetRValue(clrBack)*0.3; . a) l8 R6 h3 f/ V9 U7 f WORD green = GetGValue(m_clrHighLight)*0.7 + GetGValue(clrBack)*0.3; % j; P S( V/ q; ]) E8 }3 I WORD blue = GetBValue(m_clrHighLight)*0.7 + GetBValue(clrBack)*0.3; , H$ @7 Q; l; a/ F# Y brHighLight.CreateSolidBrush(RGB(red,green,blue)); ' h/ @5 U7 S& z2 L red = GetRValue(m_clrHighLightEdge)*0.7 + GetRValue(clrBack)*0.3;: G7 f+ f" y2 h green = GetGValue(m_clrHighLightEdge)*0.7 + GetGValue(clrBack)*0.3; # T/ v" b- W8 _, I5 O- m6 Y* }4 X blue = GetBValue(m_clrHighLightEdge)*0.7 + GetBValue(clrBack)*0.3;; e& p# S- V5 z) t2 k* b& ] u penHighLightEdge.CreatePen(PS_SOLID,1,RGB(red,green,blue));5 s& Y# x7 p5 N" b2 z5 v# s } . _2 r3 K ~ I CBrush *pbrOld = pDC->SelectObject(&brHighLight);2 Z( H7 F. K1 g" ~$ @+ O CPen *ppenHighLightEdge = pDC->SelectObject(&penHighLightEdge); . ]" }! W% N' ]: D; D5 F; t7 W+ _ pDC->Rectangle(rectSubItem);4 s" j! C* o# H# r V pDC->SelectObject(ppenHighLightEdge); " X1 c1 Y8 |) _ pDC->SelectObject(pbrOld); , f6 k' n- i$ a6 g6 ^ }! e+ k: g1 T) f" f- O, [% | } , d! h- s: A( N0 h. D& F# V pThis->GetSubItemRect(nItem,i,LVIR_LABEL,rectSubItem); ( N1 t- x: Y6 ~& @ UINT uFormat = DT_SINGLELINE|DT_VCENTER|DT_END_ELLIPSIS;" m# X! ~' v7 p) {* k HDITEM hdi;8 T2 w+ X$ y( i6 o1 ]( S! ^ hdi.mask = HDI_FORMAT; ; ?2 j5 |' } O- A$ h4 l pThis->GetHeaderCtrl()->GetItem(i,&hdi);9 E4 |# h% z% i6 H3 Y2 U9 ~* | if (hdi.fmt & LVCFMT_RIGHT)" Q) G6 X# v2 Q6 i3 U { , n; k2 \8 j& D8 `! |* @ uFormat |= DT_RIGHT; % o& R" Y- @6 C0 x$ u* r. D) v% U }1 g: n; U# g* m1 u; t$ o* \ else if (hdi.fmt & LVCFMT_CENTER) 4 B o, }9 v' L1 a5 ]- X {9 l1 c# F* L1 A# o) |0 i$ Y: m9 Q uFormat |= DT_CENTER;3 F3 D+ S+ I+ A% U _" m! J } : h+ W) s4 [& B pDC->DrawText(strItemText,rectSubItem,uFormat);

CImageList *pImageList = pThis->GetImageList(LVSIL_SMALL);2 [% S, q. C v* g0 W if (pImageList) a$ F5 q/ J) a" y7 {3 C% | {' ]# [3 R+ L0 c# H CRect rectIcon;/ _! W: `# [1 e/ O9 S1 E GetItemRect(nItem,rectIcon,LVIR_ICON);$ |$ s9 H' {: v pDC->DrawIcon(rectIcon.left,rectIcon.top,pImageList->ExtractIcon(GetItemImage(nItem)));2 `4 ?. g7 a9 s+ Y1 c } 0 o# m5 ^# l& ^8 ^( M } 9 U! P6 f' V& N) _1 A; B' k8 a# } *pResult = CDRF_SKIPDEFAULT;6 J* g2 S* M3 |" V! g }

}

该用户从未签到

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

本版积分规则

关闭

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

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