下沙论坛

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

QQ登录

QQ登录

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

又是垃圾,CListCtrlEx Base CListCtrl

[复制链接]

该用户从未签到

跳转到指定楼层
1
发表于 2005-2-23 21:29:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
// ---------------------------------------------$ [( J9 ^5 `" u' W
// ListCtrlEx.h6 v% L2 |: m, o* t7 B& ^8 }8 F9 C, v  ?
// ---------------------------------------------  J8 R1 g9 O! W0 E: G1 d0 c, u7 i3 }
#pragma once
5 j; V, p5 F3 f; T, U- @9 v, U( o1 ^6 B' O
// CListCtrlEx( G8 U4 h! ?- m
class CListCtrlEx : public CListCtrl
: v  \7 \2 t# J. d{
1 V8 ?+ t4 r0 o    DECLARE_DYNAMIC(CListCtrlEx)
/ {. Q2 [8 e* ^1 Lpublic:
5 @  }( W! b& ?, V/ H# T& |; U    CListCtrlEx();
5 s, ?8 F, w5 t& y    virtual ~CListCtrlEx();1 J6 Z% o8 e- g7 A
protected:2 i# l5 ^. c( f2 G
    DECLARE_MESSAGE_MAP()( p; m$ L5 B# n$ D2 O2 e
    CListCtrl* pThis;
" e8 s) z4 `4 Y; G4 ^    virtual BOOL PreCreateWindow(CREATESTRUCT& cs);& b  F# c) F. f0 x+ H/ c
private:
0 u( W% T' I% R. P  L: L    struct ItemDataEx {6 R7 k: G, |8 e4 Z0 E: ?& M0 M
        LPARAM SrclParam;
3 Z$ O5 w' R4 F        COLORREF clrItemTextColor;2 A, ?" [) J- b. K3 w
        COLORREF clrItemBackColor;
! ?2 z0 r( Z; q4 v4 @    };
  C/ ]3 K( A" e( T    COLORREF m_clrHighLight;: I4 e: P. j& X  e2 E
    COLORREF m_clrHighLightEdge;
" Y: @8 x  @9 O/ Epublic:
" I+ |- }1 x7 l, G$ @    // 新增方法
+ d! g6 T2 z4 m8 V    COLORREF SetItemTextColor(int nItem,COLORREF clrText);( R& `1 ^* d, \# U' ?
    COLORREF GetItemTextColor(int nItem) const;
/ `( ^* e# \$ Z4 x/ x    COLORREF SetItemBackColor(int nItem, COLORREF clrBack);5 V7 \7 p( H; u, ?! v
    COLORREF GetItemBackColor(int nItem) const;
/ _! Z# s. `4 b& d3 ^9 y    COLORREF SetHighLightColor(COLORREF clrHighLight);9 E  I6 _" O; a0 [
    COLORREF SetHighLightEdgeColor(COLORREF clrHighLightEdge);
$ E, V4 n" b. ?  E- y( e/ A    COLORREF GetHighLightColor(void) const;4 s4 E4 P$ H! @6 `& s
    COLORREF GetHighLightEdgeColor(void) const;) k) `4 \$ d; Q* l
    int GetItemImage(int nItem);
; H5 M( V% O! W. ~  K    BOOL SwitchItem(int nItem1, int nItem2);% `) n8 m" Z$ J/ N
    BOOL MoveItemUp(int nItem);/ ]- B4 b" X1 f3 p! {( }
    BOOL MoveItemDown(int nItem);
1 y7 X+ Y/ p3 G    // 重写原来的方法
: [1 U# Q6 v3 c! x    DWORD_PTR GetItemData (int nItem) const;
9 g# C2 R. m' e* L+ [    BOOL SetItemData (int nItem, DWORD_PTR dwData);8 U0 K8 `$ B/ b) r1 F+ N
    int InsertItem (const LVITEM* pItem);* E2 A7 c6 m- o5 T
    int InsertItem (int nItem, LPCTSTR lpszItem);. t% B. U- P) T) @  g, |8 x* O
    int InsertItem (int nItem, LPCTSTR lpszItem, int nImage);
+ [+ t  [; V9 @5 g! v5 w    int InsertItem (UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam);
' m  [8 n: P; a+ w8 E. O% ^    BOOL DeleteItem (int nItem);
, U% ]! R. [! t- |1 a% |0 p    BOOL DeleteAllItems ();
$ E! k+ }+ v/ R& Q" a. J    BOOL GetItem(LVITEM* pItem);
2 t0 V6 @" r! l% P: n    afx_msg void OnDestroy();' b3 ^; I* E! J3 i
    afx_msg void OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult);8 ?6 Y; E* T" g& m' D2 q
};
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 顶 踩

该用户从未签到

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

// --------------------------------------------- v. ^: A+ h! h1 S // ListCtrlEx.cpp" }6 w: L' G5 D9 {3 `0 @ // ---------------------------------------------) d& d( k+ b8 f" W7 |- U6 x // ListCtrlEx.cpp : 实现文件: |1 e3 k/ g" i1 w7 n //

#include "stdafx.h"5 T! T* I- T" c# F% M+ y #include "CreateNewClass.h": d0 r5 P* G/ Z) b' G7 \ #include "ListCtrlEx.h"9 b! E" {7 w, S3 @, w #include ".\listctrlex.h"

; T0 _1 q6 w- U8 W* ?, ~, T7 u // CListCtrlEx

IMPLEMENT_DYNAMIC(CListCtrlEx, CListCtrl)- [8 j' K: m% ^5 l& S CListCtrlEx::CListCtrlEx()+ ~+ V3 H+ c& u: P" O {& y$ E( O' s" C // 用于调用基本的方法 " ?, B3 n4 _; h& v( \, m, w" e pThis = (CListCtrl*)this; 1 P1 L+ ~ w3 e // 默认的选中框颜色 $ Y+ \1 }- ^ r, R$ a8 m& W! w m_clrHighLight = 0xF1DACC; h% K! g5 h! ` m_clrHighLightEdge = 0xC56A31; . W) a+ d, E! {}

CListCtrlEx::~CListCtrlEx()7 {( G$ ~- a7 t# a7 i {

}

4 c3 B' U, [( `0 Y BEGIN_MESSAGE_MAP(CListCtrlEx, CListCtrl) L! L: e" p5 _; M/ g( V ON_WM_DESTROY()6 [+ a6 l- d. I* m. e# D ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnNMCustomdraw) 9 W6 B* y. r5 `. U3 T, E2 }0 zEND_MESSAGE_MAP()

// CListCtrlEx 消息处理程序

+ T- }% T6 v0 T1 u/ s# S COLORREF CListCtrlEx::SetItemTextColor(int nItem,COLORREF clrText) . n; X @" ^, e, \1 k0 Y# L& _{' o, Y% R+ U5 G6 e COLORREF clr = GetItemTextColor(nItem); ; y+ Q5 l; O) V# c ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem); 8 T# d9 s3 o6 @6 |4 m pData->clrItemTextColor = clrText;& [; k0 [) W. T$ [. } return clr;' O$ r7 j( N7 c' Y% D; q% {5 ? }

COLORREF CListCtrlEx::GetItemTextColor(int nItem) const- O; b5 b/ B9 l9 x8 [& L { # N- J" o: ^" \+ ~6 c# a8 J# Y' ] ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);& Y4 j# ?! h. \4 ~0 m return pData->clrItemTextColor;( Y$ o) \4 p' L/ D+ B9 e }

COLORREF CListCtrlEx::SetItemBackColor(int nItem, COLORREF clrBack). j0 G( m( n8 [ { X5 f, f+ ]4 H6 R0 D8 b- O COLORREF clr = GetItemBackColor(nItem); 3 c- W4 E9 i9 p! Q# n6 Q9 B ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);. L, P- _. `. u. z" T7 X8 A2 | pData->clrItemBackColor = clrBack;! v1 [$ @- P6 E& m return clr; 1 I/ N' V6 C% p/ J3 j}

COLORREF CListCtrlEx::GetItemBackColor(int nItem) const0 P. a+ Z# V0 X/ ~% s; Z3 _" |8 A { + Q: w+ i I) k2 a3 C ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem); $ L D/ P; ]$ t( V return pData->clrItemBackColor; ! Z# N `) E S, t3 u; J+ M/ a}4 B- \& D: v$ P) V$ r8 Q# b( G2 ` DWORD_PTR CListCtrlEx::GetItemData (int nItem) const. c$ ?5 w0 c% s6 M# V$ b; _ { 5 q; j8 a! v% m- h if (nItem >= GetItemCount()) 5 z/ p0 x. C! M% v9 i$ A: N { - |' O$ _, K$ i% L2 O# ]" B return 0;- ~: l8 |9 J0 I" ?- o }9 }7 a9 b6 A1 p. L+ H# F$ H CListCtrl* pThis = (CListCtrl*)this; 2 P7 x+ [: }2 r/ O DWORD_PTR Data = pThis->GetItemData(nItem);; W8 z8 _/ t5 B. ^1 ~ return ((ItemDataEx*)Data)->SrclParam;1 x0 O# J3 c1 V0 O R }6 ?0 z8 A3 ]# C) e! W0 @ BOOL CListCtrlEx::SetItemData (int nItem, DWORD_PTR dwData) 9 s0 e9 F# @: C$ l* |% [{ 4 v# a2 Z, {% q! w$ G' e* W# k if (nItem >= GetItemCount()) " b( I4 B% b' C, d( ~+ u { , Z0 M' e' z- W7 Y1 S3 R- K return 0;2 O5 B2 o7 P" I } 4 a5 v) }3 t+ ` CListCtrl* pThis = (CListCtrl*)this;9 x: E0 I T1 h( d8 j DWORD_PTR Data = pThis->GetItemData(nItem);* S% U) C% {" h$ l: C ASSERT(Data); - g# A1 |5 [/ J$ M ((ItemDataEx*)Data)->SrclParam = dwData;# I9 w. S# v# y. U& U3 G return 1; ) G( ~; |. h2 i+ H7 A5 V5 n}0 T$ @9 ^$ s2 C! G: \& G2 ~. j, y int CListCtrlEx::InsertItem (const LVITEM* pItem)3 F' _0 C, F5 i+ {$ b, f {$ g, `( s& D5 r Q( A { int nReturn = pThis->InsertItem(pItem); % }1 M; K9 z, {/ j ItemDataEx *pData = new ItemDataEx;1 @+ ?* ?( E0 i% t" x // 设置默认颜色0 S. I. S4 }1 n2 I- E, A* v) r pData->clrItemBackColor = 0xFFFFFF;//pThis->GetTextBkColor();6 k- J$ t0 A m6 @ pData->clrItemTextColor = 0x000000;//pThis->GetTextColor(); & l3 F, W, V. F5 f pData->SrclParam = pItem->lParam; 4 u# y' U; ^$ L: @6 M4 S) C pThis->SetItemData(pItem->iItem,(DWORD_PTR)pData);" ]9 p. ~! Z( j& w0 q return nReturn; : y# _2 E# m% _$ P} % m, n9 G. S3 b* U- K ^int CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem) 0 L2 G0 U& ^3 K{ ( Y% ]& P9 c/ G LVITEM Item; $ t! x1 n9 o7 n0 i, x memset(&Item,0,sizeof(LVITEM));8 C. G2 A: b' q, t Item.pszText = new TCHAR[strlen(lpszItem)+1]; 7 l& g8 }7 Z; {- Y5 {* |* N3 W! _ strcpy(Item.pszText,lpszItem); 1 `( t: a. q j Item.iItem = nItem; 6 H/ j8 W# a& m- x1 x Item.lParam = 0;+ d' v2 ^) k0 U' y8 a% B6 R ]) L Item.mask = LVIF_TEXT |LVIF_PARAM;3 Y$ k& z/ e# W& D0 a! F. E; l BOOL bReturn = InsertItem(&Item);1 O- e& y% m& y delete Item.pszText; % S/ ]( ?, B/ ^4 v5 r return bReturn;; e7 M, a* K: ^( E0 k5 ` } ' V |. X9 n- Kint CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem, int nImage)6 h( l R: F( k {7 P; C9 k1 n4 E LVITEM Item; , r0 J; r0 O# m0 | memset(&Item,0,sizeof(LVITEM)); . l4 [& v/ l$ v' `0 [' e Item.pszText = new TCHAR[strlen(lpszItem)+1]; 4 i, K2 O% I \+ { strcpy(Item.pszText,lpszItem);/ E9 ^1 B$ L7 J" u Item.iItem = nItem; 7 F1 r! k+ @8 N. g Item.lParam = 0;0 z9 |) Q, n/ q Item.iImage = nImage; . D3 ^ E+ f( @1 Q' ~ Item.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE; 5 r) m, h3 q" x. f/ {) K BOOL bReturn = InsertItem(&Item);& _- m3 S& i6 C4 _* z! z delete Item.pszText; ( D3 g1 v7 z1 ~( W& M) P3 X, } return bReturn; / d/ l; s3 V9 C2 ~5 F s}& p. o" U9 z. r" i( @* n/ O$ ] int CListCtrlEx::InsertItem (UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam)% D" l( |; o) l! E {0 _$ i0 r4 G$ s0 z LVITEM Item;, B8 w! ~* t8 J' b6 n4 W% _# @. i memset(&Item,0,sizeof(LVITEM)); - E, ?. w9 b# X+ @; R! K' Y6 { Item.pszText = new TCHAR[strlen(lpszItem)+1];( B0 h8 u) E9 {" n. [! z! l; u, D strcpy(Item.pszText,lpszItem);* M' v7 q4 @; f0 q- A, @ Item.state = nState; " n& M; z6 D( p0 F( x+ U, U; K Item.stateMask = nStateMask;: ^+ U$ {3 }1 W3 D& n! n+ y2 Q9 p Item.iItem = nItem;' k- n# Y/ n. D. `$ K Item.lParam = lParam; : O% I1 s! Q0 b; U% h; V& I Item.iImage = nImage; ; b1 t3 |( ?% M1 X( p9 S% B Item.mask = nMask; 9 w$ M& _% c4 d/ i; D BOOL bReturn = InsertItem(&Item); U6 z* m+ ]# g% U( B2 X! g2 B delete Item.pszText; $ z: G* C; Z- Z1 }6 L$ H; J return bReturn;+ p3 Q. H% H Q7 h3 ]' n }: G' @% ?+ V5 k, x% J0 ] U5 \, x BOOL CListCtrlEx:eleteItem (int nItem)9 M) Y/ { o" V+ R: p$ \7 B J4 t {; e7 e0 F6 Y. j* X$ q void* pDataEx = (void*)pThis->GetItemData(nItem);' \; X: Y: _1 W6 \ if(pDataEx != NULL), G8 S# c) i [- e4 T { . F4 K. T( @; r, E$ L2 u9 }# T delete pDataEx; 3 a( Y" {& I* N2 F5 \' _* @6 p }$ b1 J8 M7 a' m2 ^ return pThis->DeleteItem(nItem); + d- P6 P& v6 b}" S+ C/ I, `0 K BOOL CListCtrlEx:eleteAllItems () 8 A- u/ H9 ?" g' R1 l{ * e1 P3 {+ Y$ S' F' J: d int nCount = GetItemCount(); ) l% N9 F9 H6 z. R for (int i = 0;i<nCount;i++)2 q& C5 i$ J/ k6 u { . u: x0 G) h$ g if(!DeleteItem(0)) 3 P( B) M$ q! _4 z: z/ e { . F( K) W" u$ T9 G2 [ return 0; - K% w) F, D! H! V } - D% H) K' t# T% c0 ? [ } ) N& I: j: Y# R/ E return 1; ) s) w9 C* L$ D! ]/ I! n} ! Z/ Q1 v0 ?/ n- |0 h7 DBOOL CListCtrlEx::GetItem(LVITEM* pItem)4 e' h" R3 {% |) i% t. ^1 c { + k6 k+ R) R; h2 B BOOL bReturn = pThis->GetItem(pItem);* V+ k4 s" V z if (pItem->lParam)5 ]* \3 P J* I {1 A6 r5 e6 R) d+ X7 X: \/ N pItem->lParam = ((ItemDataEx*)pItem->lParam)->SrclParam;: W' _2 O0 S& ?1 E) n/ h% ~ }

return bReturn;9 Q/ [( p- g: a, s R" H } : l2 ?" S; o2 C X' e5 ?void CListCtrlEx::OnDestroy()! t8 i4 d1 N( H4 l8 b) K$ ]1 u { . _" b! C. p3 O8 r // 关键是清除申请的内存空间 1 W" B1 e( [4 k' t4 T" u( l DeleteAllItems();) d1 h6 c2 b1 h& T9 C" d% E CListCtrl::OnDestroy(); 9 O4 v$ y- w/ Y: h; a8 i! O}

BOOL CListCtrlEx::SwitchItem(int nItem1, int nItem2) l/ @$ w" R' Y1 @{ 0 [& U4 l( j( A! H if (nItem1 == nItem2 || nItem1 >= GetItemCount() || nItem2 >= GetItemCount() || nItem1 < 0 || nItem2 < 0) " j p5 D! w, F: z+ g5 r {/ d; L9 y0 k g* }. N: A4 z4 A/ s return 0; 6 f8 U. v4 i4 \# d. s6 `( U } 8 ^* a7 a+ a% L& X, l b v& i; B LVITEM ItemData1,ItemData2; 9 _" t6 S2 U; L6 i; }5 B! t int ItemLength1 = pThis->GetItemText(nItem1,0).GetLength();& \; F4 L8 j% _3 \ int ItemLength2 = pThis->GetItemText(nItem2,0).GetLength(); , ~% v' c% c2 {/ T1 u V ItemData1.pszText = new TCHAR[ItemLength1+1]; ) K5 i# v- b( C8 N, M ItemData2.pszText = new TCHAR[ItemLength2+1]; ' h. b0 x; N& _6 B ItemData1.cchTextMax = ItemLength1+1; ) ^ I S. V: p) X, v) j, f ItemData2.cchTextMax = ItemLength2+1; : K- [8 u# e$ u. f& f ItemData1.iItem = nItem1;% T: @# A v. D( f( e) S1 t ItemData2.iItem = nItem2; 5 e6 z1 r8 [8 d ItemData1.iSubItem = ItemData2.iSubItem = 0;' d& O, }/ A1 B8 ^8 n4 x- z ItemData1.mask = ItemData2.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE|LVIF_STATE;

pThis->GetItem(&ItemData1);: j4 ^8 ?' x, w/ P+ w pThis->GetItem(&ItemData2);" `' x! j9 s4 _. z/ K // 交换数据2 x+ ~5 Y1 B. i ItemData1.iItem = nItem2;! f) Z& _ T" ?. U ItemData2.iItem = nItem1;- V2 [$ R! ~$ A pThis->SetItem(&ItemData1); + i; C6 L J0 `' i6 b" l- V( A pThis->SetItem(&ItemData2);

delete ItemData1.pszText; 2 M+ z$ i! q } delete ItemData2.pszText; # } D& T* F2 u' E9 K // 得到报表模式的列数, m4 f. n2 E" X* A2 t% j4 H9 h# @$ T int nColCount = pThis->GetHeaderCtrl()->GetItemCount(); 7 r1 y( u3 A3 U" [4 r* l! }& e CString strBuffer; # J% h* x4 O7 R% n+ p- P for (int iSubItem = 1;iSubItem < nColCount;iSubItem++)6 {4 C6 y2 x0 e/ `, Z {, A/ @; Y. o [% e' h; W strBuffer = pThis->GetItemText(nItem1,iSubItem);9 R2 C1 A, C$ o pThis->SetItemText(nItem1,iSubItem,pThis->GetItemText(nItem2,iSubItem));+ e c% n% a0 h; l. e: y pThis->SetItemText(nItem2,iSubItem,strBuffer);$ Y9 B. q W- W } 9 s1 X& w# R$ P% _ c3 N! Q' U8 t // 把原来的选中状态也转移过去 X7 u% q; h) H! M4 n* Q // 不知道为什么这个属性没有体现在LVITEM结构中…… 5 c. m' Q0 s9 v- @/ E3 N bool bItemSelected1 = GetItemState(nItem1,LVIS_SELECTED) == LVIS_SELECTED;# S2 k s6 r9 {$ Y6 t8 ^+ N" j E1 J bool bItemSelected2 = GetItemState(nItem2,LVIS_SELECTED) == LVIS_SELECTED;

SetItemState(nItem1,bItemSelected2?LVIS_SELECTED:0,LVIS_SELECTED);( [5 F/ e% O, L( V3 y SetItemState(nItem2,bItemSelected1?LVIS_SELECTED:0,LVIS_SELECTED);$ G. g( ~, K! Y) l! C return 1; V5 Z) X# B( o7 ^4 Z5 i- j8 L }

BOOL CListCtrlEx:reCreateWindow(CREATESTRUCT& cs)- M! H5 ~8 c$ a( w0 G {) F3 f: O% H1 J // 本类报表模式专用 & G X7 }) n& E/ k6 I& D3 j( P% y cs.style |= LVS_REPORT ;3 W l, u4 R& s, y // cs.dwExStyle |= LVS_EX_FULLROWSELECT; : t" w+ S/ {6 w. @ return CListCtrl:reCreateWindow(cs);" J- Z2 V. x9 b5 g }

& g4 D6 E* P" Z: T3 uCOLORREF CListCtrlEx::SetHighLightColor(COLORREF clrHighLight)# `6 x E& f9 ] { 8 I: p$ P! B0 ~( x; u" _, R COLORREF clr = GetHighLightColor(); ' B/ {0 z3 ]% k) E* m- {4 O9 n( T m_clrHighLight = clrHighLight; ( z+ P# f/ v+ L; r" z return clr; . L* T- g/ \" p}

COLORREF CListCtrlEx::SetHighLightEdgeColor(COLORREF clrHighLightEdge)$ @9 u2 _/ e! h" {3 S. r; V { 6 m# r9 l; [& W0 [7 v* j6 ` COLORREF clr = GetHighLightEdgeColor();4 d8 m0 B% {& O1 }% H m_clrHighLightEdge = clrHighLightEdge; + [. z- J& ^8 |/ c% @$ ` return clr; * u* t* ]- F/ Z9 L}

该用户从未签到

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

COLORREF CListCtrlEx::GetHighLightColor(void) const' V* \- A! N# |* Q# Y) a5 ^ {5 ~5 |& O$ t- g return m_clrHighLight; 7 U" _& j z% x6 ~) m1 d: m}

COLORREF CListCtrlEx::GetHighLightEdgeColor(void) const' n) S7 c4 I( R9 |! D5 S' A0 l {2 ?6 p5 o' }5 Y6 U: T7 D return m_clrHighLightEdge; 8 q8 ^$ |5 q5 Y% m8 m1 X6 m}

int CListCtrlEx::GetItemImage(int nItem)$ f- C6 G" g& ~4 }/ s1 [ {5 G/ s" B: m% D* @4 o" @# L% c LVITEM ItemData;4 ], ~' p. c. b9 w& W ItemData.iItem = nItem; 8 h) \- D* ^# q( [8 k ItemData.iSubItem = 0;7 k# {! b. j$ S. s( U- [. |% p ItemData.mask = LVIF_IMAGE; z1 c# s( m% z& W1 C pThis->GetItem(&ItemData); ) {. R! y- l( N0 m, P' Q* o return ItemData.iImage; - z& G) @0 ^7 G. j/ C8 K}

BOOL CListCtrlEx::MoveItemUp(int nItem); ~# S& z, i: S. _% J {9 y' |( }7 e& t. j1 t8 I% ?, ? if (nItem < 1 || nItem >= GetItemCount())# n2 v6 k* q/ s% [7 v T; V { 0 c! K% G! A% J6 j8 `+ E4 E return 0; / q3 G" n/ H! h+ ]/ |# Q }' c) z; S7 ^3 P& u0 O; ~ return SwitchItem(nItem,nItem - 1); ; I) v4 A n( Z}

BOOL CListCtrlEx::MoveItemDown(int nItem)! E" \$ w! a5 ]8 r) [ c+ S. X {2 ?8 _9 |! U& A1 p- M+ l9 N' q if (nItem < 0 || nItem >= GetItemCount() - 1) & y6 n* U* H/ v+ _ { 9 H% D. v1 s- l return 0; + v; X# ]8 V" {, U/ X } - @" J+ z: _1 i* J! C' o return SwitchItem(nItem,nItem + 1);+ l7 `/ r1 F' E8 o2 R. T }

void CListCtrlEx::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult) 1 x$ i8 l3 k& u{ . V$ B! S+ a9 z n+ C NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR);

*pResult = CDRF_DODEFAULT;

if (pLVCD->nmcd.dwDrawStage == CDDS_PREPAINT)1 _2 q- c% O7 U& T {+ O" S, G& z. P/ Q: d! p- K3 I *pResult = CDRF_NOTIFYITEMDRAW;& q( E. h( {& S2 L } 7 r2 J. N }& Z1 S) t4 r' ?0 x l1 q else if (pLVCD->nmcd.dwDrawStage == CDDS_ITEMPREPAINT) ; n+ V+ G$ I- h( g0 p {) p' t" J' M s: } *pResult = CDRF_NOTIFYSUBITEMDRAW;! D$ }& R* i8 ?, g/ c }) F/ y; q. O/ F! b* ? else if (pLVCD->nmcd.dwDrawStage == (CDDS_ITEMPREPAINT | CDDS_SUBITEM)) 5 e! Q* x% n# I+ G9 A4 ?( p5 Y9 z* a {

int nItem = static_cast<int> (pLVCD->nmcd.dwItemSpec); ' A* k' W" n' f int nSubItem = pLVCD->iSubItem;5 t5 y3 ?" S2 R3 F+ h1 @+ J if (nSubItem != 0)8 g/ k3 t+ }& m { , h4 C: g" P2 c( m5 l // 全部绘制过程在nSubItem == 0 时完成 " T( D9 j' m0 u! G2 \2 M: Z* p+ o# z // 分布完成会有小问题,我无法解决 - g/ ?- E+ {) d0 A; [ *pResult = CDRF_SKIPDEFAULT; # U' S* L5 F& J4 x4 c return;* j3 i" ]! S( g, x: |9 l& l } 1 ~) i2 N b* G$ E, N5 i CDC *pDC = CDC::FromHandle(pLVCD->nmcd.hdc); ; B- V% F0 n4 q6 i int nColCount = pThis->GetHeaderCtrl()->GetItemCount();' p# G9 e* {/ T CRect rectSubItem; ' ^% l8 V9 B! w CString strItemText; ! s- \3 J& Z6 \1 [4 ~0 I& A+ z6 r pDC->SetTextColor(((ItemDataEx*)pThis->GetItemData(nItem))->clrItemTextColor);+ b6 L" M" L# o# t# k pDC->SetBkMode(TRANSPARENT); " P4 [- d! U P. o2 l // 相当于橡皮 * e/ [4 I8 L E, ?; D: l3 | CBrush brEraser(GetItemBackColor(nItem));

// int nFirstWidth = GetColumnWidth(0);

- `! F1 q2 W/ Q8 F' g for (int i = 0;i < nColCount;i++) , d: n5 D, V8 ^& [* K { 3 t+ n/ Y6 m9 F4 T" g% t& ~ strItemText = GetItemText(nItem,i);

if (i == 0) ' B5 C4 w; p" B- y# ^/ r1 Y# v U {. T. D5 h/ }% a pThis->GetSubItemRect(nItem,i,LVIR_BOUNDS,rectSubItem);' Z- r$ H6 _" p1 P/ j // 擦掉原来的图形5 N( O+ }: t: z* O7 u: g pDC->FillRect(rectSubItem,&brEraser); 1 E! H7 M2 x* y y* z7 U if (GetExtendedStyle() & LVS_EX_CHECKBOXES) & ~7 s/ z/ U) f* [; b j {* r/ m ] o9 P7 v CRect rectCheckBox; " _5 @0 V3 c8 o8 Z+ p& c& \ GetSubItemRect(nItem,0,LVIR_ICON,rectCheckBox);0 h# h( U ], k5 p% f0 I0 q4 Z CRect::SwapLeftRight(rectCheckBox);: O2 `0 n. @) g' M, [ rectCheckBox.left = rectCheckBox.right - rectCheckBox.Height();

rectCheckBox -= CRect(2,2,2,2); Q4 c$ g5 H% d( k: a; Q- R8 |" ?/ I pDC->Draw3dRect(rectCheckBox,0x81521D,0x81521D);+ x. @) H: e, K; F //rectCheckBox += CRect(3,3,3,3); * [5 ~' K- }3 T/ E if (GetCheck(nItem)) 8 e- n6 X0 E# r6 _ { 6 p" @, u# i' R$ j9 P, x8 U // 绘制一个CHECKBOX 6 ]: i+ D7 P, J CPen penCheck(PS_SOLID, 1, 0x0021A221);& j' K, {& C' Y. I# p CPen *pOldpen = pDC->SelectObject(&penCheck); % Y2 S9 b5 i0 a+ K& M3 N for (int j = 3; j < 10; j++)3 K; o5 ^/ y% V# h* m {! F* L5 ~5 M/ x pDC->MoveTo(rectCheckBox.left + j, rectCheckBox.top + rectCheckBox.Height() /2 - 3 + ((j < 6) ? j - 1 : (9 - j)));, B6 m& z2 l" K0 i- n6 y pDC->LineTo(rectCheckBox.left + j, rectCheckBox.top + rectCheckBox.Height() /2 - 3 + ((j < 6) ? j + 2 : (12 - j)));7 U6 S( N1 v/ O7 ?5 E) w }6 @5 G u. L/ ~- o+ G; `2 L $ C# B3 _) l: Y$ S3 L$ L5 l pDC->SelectObject(pOldpen); 4 O- |' u7 `& H } ) W3 {* H4 {0 b$ n5 G } 9 ~$ P* l& o+ P // 根据需要画选中框 3 S# T, h0 y' y" J# D- ~; v if ( (GetFocus() == this || GetStyle() & LVS_SHOWSELALWAYS)&&(GetItemState(nItem,LVIS_SELECTED) == LVIS_SELECTED)) 9 I3 ~* O; W0 J {3 N7 U* Q5 n8 K/ x0 K E( V if (GetExtendedStyle() & LVS_EX_CHECKBOXES) m, t( i. f6 z# E$ x {// 存在检查框风格的时候需要对左坐标进行修正 2 ^$ j. F, ~, F8 [. Q" b' P. t: s CRect rectLeft;$ T8 ]# I8 z" j7 h GetItemRect(nItem,rectLeft,LVIR_ICON);" K* [/ S! e6 `; s b6 w) ~ rectSubItem.left = rectLeft.left;: ]% a! ]) m7 ~ }# M8 }, R% y( M* ? if (!(GetExtendedStyle() & LVS_EX_FULLROWSELECT)) 2 d+ z( {3 c. s- B8 U {// 非全行选择时右坐标需要修正) T$ o9 q) j$ C7 p9 `0 E CRect rectRight; ) L, X6 y- F& W a) Z( ^/ t1 b GetSubItemRect(nItem,nSubItem,LVIR_LABEL,rectRight);5 C' F# F' h* [6 X1 p0 i: j _, }* g rectSubItem.right = rectRight.right;+ v' X& y+ n0 _& q2 d1 {! ` }# Z' X9 d2 `1 u3 n) R CBrush brHighLight; , a( V1 g! h- ]8 M CPen penHighLightEdge; 4 X6 A4 L: s, d if (GetFocus() == this) 0 f4 l2 Y2 `: Z" l8 [* F& z: v {4 |) \8 B# a9 z8 s brHighLight.CreateSolidBrush(m_clrHighLight);+ l) n& X% K4 p9 U+ {* s penHighLightEdge.CreatePen(PS_SOLID,1,m_clrHighLightEdge); $ s: U/ y. A& v Q }$ N* t5 T' i. ]) H5 r else ' V/ F( W& B% U. K5 j {( ^" ^9 s& E0 u9 s9 Q; }6 l! }# Z // 不是焦点时,颜色70%不透明度 + b( e7 @; ^- f8 E# @ COLORREF clrBack = GetItemBackColor(nItem); $ Z9 }9 L. S n4 O) X WORD red = GetRValue(m_clrHighLight)*0.7 + GetRValue(clrBack)*0.3;) u1 X: [ W( w" X" P( S WORD green = GetGValue(m_clrHighLight)*0.7 + GetGValue(clrBack)*0.3; 4 R% g- x! S" I7 b& s: w* ^ WORD blue = GetBValue(m_clrHighLight)*0.7 + GetBValue(clrBack)*0.3; 5 Z1 g# g; T" b! R! Z! ]/ | brHighLight.CreateSolidBrush(RGB(red,green,blue)); |3 `5 ~0 J" ?3 W x red = GetRValue(m_clrHighLightEdge)*0.7 + GetRValue(clrBack)*0.3;" a2 t2 y4 L. \2 `- h/ |0 h# e green = GetGValue(m_clrHighLightEdge)*0.7 + GetGValue(clrBack)*0.3; 8 S# Z. \3 V/ j. | b blue = GetBValue(m_clrHighLightEdge)*0.7 + GetBValue(clrBack)*0.3;3 a' ~8 z4 l$ D6 V/ y) ^) L) L penHighLightEdge.CreatePen(PS_SOLID,1,RGB(red,green,blue));2 K- E' ]( t; t# w6 K0 G } " T/ A% v1 i+ A- m( r CBrush *pbrOld = pDC->SelectObject(&brHighLight); & a/ n3 _6 d" A/ }1 j4 O CPen *ppenHighLightEdge = pDC->SelectObject(&penHighLightEdge);9 T5 b9 G% {. z/ g, O, _ pDC->Rectangle(rectSubItem); $ X" A4 u, w0 T# E' I" b pDC->SelectObject(ppenHighLightEdge);( J! }% W$ H# E0 k: e pDC->SelectObject(pbrOld); T: e! H& }4 m9 b: m; N( R, |# C+ A } k9 \# u0 W T* S7 l } 2 s0 ^" o' W: E" I1 u' u pThis->GetSubItemRect(nItem,i,LVIR_LABEL,rectSubItem);+ D2 a6 f1 e) F UINT uFormat = DT_SINGLELINE|DT_VCENTER|DT_END_ELLIPSIS;5 }* E3 @' M: e% O$ T5 `( _ HDITEM hdi;% v6 G$ U$ A: o5 P hdi.mask = HDI_FORMAT; " H5 ^! v9 y! d# y pThis->GetHeaderCtrl()->GetItem(i,&hdi);3 X- z; U$ O' J: u% {- \% h7 ^( F if (hdi.fmt & LVCFMT_RIGHT) 3 H! V, L/ ]- n; ?4 ~4 `$ A" ~) D {( b3 b0 l0 Y: I0 ]2 L3 b uFormat |= DT_RIGHT;+ D+ X3 G; j- c" H* m } % G& E2 H8 |7 Y8 H( S7 C: r' u else if (hdi.fmt & LVCFMT_CENTER) ' x) y8 a7 t! l2 i: Z { 3 v6 `0 s' N) x6 _4 V6 [4 K. x uFormat |= DT_CENTER;# b0 l, v' {- x/ S( X$ C1 i2 d2 f } * H1 o( Z! a$ @ pDC->DrawText(strItemText,rectSubItem,uFormat);

CImageList *pImageList = pThis->GetImageList(LVSIL_SMALL); 4 _4 |2 a( d8 O, I+ U if (pImageList)2 ~1 {/ }1 E2 o$ Q6 \0 M7 j { 0 N! ~. A- L: U X; _; x CRect rectIcon; N9 W. o o1 Y9 Q& S) g& E GetItemRect(nItem,rectIcon,LVIR_ICON);2 Y" p, Y1 e; k/ n$ r: _7 l) Q( B# r pDC->DrawIcon(rectIcon.left,rectIcon.top,pImageList->ExtractIcon(GetItemImage(nItem))); , b* i) _ s! ?. Z. S$ T }4 K |) ]" A& O1 s; l } " n9 x' ?# v- K *pResult = CDRF_SKIPDEFAULT; 6 K6 b# w2 \7 o, Y g: ~ }

}

该用户从未签到

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

本版积分规则

关闭

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

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