下沙论坛

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

QQ登录

QQ登录

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

又是垃圾,CListCtrlEx Base CListCtrl

[复制链接]

该用户从未签到

跳转到指定楼层
1
发表于 2005-2-23 21:29:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
// ---------------------------------------------: A/ D: n6 y; b' c/ x
// ListCtrlEx.h4 s4 E/ g2 f9 _+ {' I: ]
// ---------------------------------------------3 @; z* ?# H. F! q. ^
#pragma once  Y% @0 x( W+ _; W

( @. n0 C( |0 O: M// CListCtrlEx
, T5 o- f+ m! c  Uclass CListCtrlEx : public CListCtrl
7 {! |+ G, H, m{
$ ~4 Y& K- u6 R6 N; N" g) S- w    DECLARE_DYNAMIC(CListCtrlEx)
3 h0 d+ i- N0 C9 b1 |9 Upublic:+ I: R" d) V4 m  w& j4 q/ n
    CListCtrlEx();
; w( @, Z. W+ ]    virtual ~CListCtrlEx();% h9 a0 k9 [3 @  Y
protected:% S5 c2 d" [' p/ w6 l/ \
    DECLARE_MESSAGE_MAP()) w0 r( K% A3 k+ H7 V2 b6 y! Q
    CListCtrl* pThis;
( Z' U4 m4 d5 a: z% m2 O! F    virtual BOOL PreCreateWindow(CREATESTRUCT& cs);( W  W- j( Z  L6 m
private:6 P) A. l; L* O9 p* J3 m7 `
    struct ItemDataEx {
+ F+ ]: L) j1 N" z& b        LPARAM SrclParam;4 @" }# `; A1 ~* h7 ^
        COLORREF clrItemTextColor;" k; h6 [; N' b0 H; y# t. k$ W- H) O  n
        COLORREF clrItemBackColor;
' W* K: j8 H1 O% O. j( z    };1 l4 ]) p; M1 V5 g  F
    COLORREF m_clrHighLight;1 W4 [0 {, a& g
    COLORREF m_clrHighLightEdge;- d- `2 A4 o: r
public:
5 w4 N) R) V6 F* z    // 新增方法
& L4 N3 L5 g+ b$ Y- l7 u7 E    COLORREF SetItemTextColor(int nItem,COLORREF clrText);
" q! K' s5 g( S  }    COLORREF GetItemTextColor(int nItem) const;
& m  k1 m5 l& }; O% B, _    COLORREF SetItemBackColor(int nItem, COLORREF clrBack);1 |! l0 }4 w) r% M# w9 U& I8 d
    COLORREF GetItemBackColor(int nItem) const;
7 L3 G. p0 f+ H! x( o' T. H$ h% ]    COLORREF SetHighLightColor(COLORREF clrHighLight);
) a4 w- s+ w) Z    COLORREF SetHighLightEdgeColor(COLORREF clrHighLightEdge);
6 d# [2 c: d! ~    COLORREF GetHighLightColor(void) const;' C" {9 Q  Z6 p( P! i, P) w- G
    COLORREF GetHighLightEdgeColor(void) const;$ P: O; O$ c$ `/ [& ^9 t
    int GetItemImage(int nItem);% z8 Z$ S, B; m8 O) {/ V
    BOOL SwitchItem(int nItem1, int nItem2);
) V6 X2 D" Z0 ~/ i( G9 q9 D; S    BOOL MoveItemUp(int nItem);
0 f: e3 [- O6 A$ K# y% v  `    BOOL MoveItemDown(int nItem);
6 K, Q) b# w: Y8 o; o1 |- x    // 重写原来的方法; [- ^4 c+ {! Q0 {% |* [
    DWORD_PTR GetItemData (int nItem) const;
7 D' o; G( @8 I" u( x    BOOL SetItemData (int nItem, DWORD_PTR dwData);" I& i2 ^+ p9 U6 |/ Y3 j
    int InsertItem (const LVITEM* pItem);' e  Z4 l/ _: z, r; ~8 L
    int InsertItem (int nItem, LPCTSTR lpszItem);  c$ o  x! Q; \) p
    int InsertItem (int nItem, LPCTSTR lpszItem, int nImage);
; F' F$ f" l) m' ^$ K8 c    int InsertItem (UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam);
4 j4 O, L6 d- x! Z) U    BOOL DeleteItem (int nItem);9 w( {* d7 M  p6 N+ t- @! d
    BOOL DeleteAllItems ();8 k# J% p$ Y7 t
    BOOL GetItem(LVITEM* pItem);
6 z- M1 F4 l: m% n) y    afx_msg void OnDestroy();
: M! H: _* h% F' f; {0 P    afx_msg void OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult);
4 D' X- W4 P- w& k$ d  I% J1 W1 a};
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 顶 踩

该用户从未签到

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

// ---------------------------------------------! E; A' l6 a# A s/ h' b* C // ListCtrlEx.cpp- P9 z" N) m7 E k4 ] // --------------------------------------------- / ?2 _3 X$ r, i! l6 q* a6 g3 J// ListCtrlEx.cpp : 实现文件 " h) X! K2 T( I( {3 {: L4 s//

#include "stdafx.h" * I% r- ~' H6 k( s#include "CreateNewClass.h"' U8 P/ [* d3 d5 w. f& r #include "ListCtrlEx.h" 2 Y' j. F1 F" f+ D' C. j2 r/ n#include ".\listctrlex.h"

% J* N' r- ~$ x // CListCtrlEx

IMPLEMENT_DYNAMIC(CListCtrlEx, CListCtrl)1 l: W6 G5 T$ W5 d8 y CListCtrlEx::CListCtrlEx()1 T4 B5 q8 Z# Y J5 Z {0 ?+ i+ ]8 |9 P' |, I( I( E // 用于调用基本的方法 L P a9 `3 K, R& ~* Z pThis = (CListCtrl*)this; + Y; U* l2 U# p0 ` // 默认的选中框颜色2 ~7 U4 V/ r5 m2 n m_clrHighLight = 0xF1DACC; , {( G5 [. W% @- o+ g/ t6 D m_clrHighLightEdge = 0xC56A31;/ }, ?# @+ I- a V3 s }

CListCtrlEx::~CListCtrlEx() & W+ |( E" e: A{

}

% B2 x% F1 i( ^2 G* U4 F BEGIN_MESSAGE_MAP(CListCtrlEx, CListCtrl) . ~# \8 i0 C) q8 e ON_WM_DESTROY()+ k/ m% w$ d, k4 q0 {; U0 b ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnNMCustomdraw) % p5 X6 o. M! z+ A2 cEND_MESSAGE_MAP()

// CListCtrlEx 消息处理程序

! I- Z% p# l6 g COLORREF CListCtrlEx::SetItemTextColor(int nItem,COLORREF clrText)$ z$ ^% ]' n2 g: {1 s' p {" u: ?" T) m) p- Z+ e9 Q; T COLORREF clr = GetItemTextColor(nItem);9 n6 h7 H! z; R6 ]5 K" f* _ ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);4 @/ p. r; Q" Z pData->clrItemTextColor = clrText; * ^4 b$ g' R7 N" q8 F7 R. u% @# L8 H return clr;0 F8 m8 \$ I$ q; u6 g! E) J }

COLORREF CListCtrlEx::GetItemTextColor(int nItem) const & O0 F* }# v5 q$ F& f5 j$ u) D& [. c{' ?4 P- R, d# z7 Y! U( j8 f9 M ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);9 ]% o; n3 W- }. o return pData->clrItemTextColor;: B" a* n9 T, x }

COLORREF CListCtrlEx::SetItemBackColor(int nItem, COLORREF clrBack) # q# X1 j/ [* S. v{ 2 P" [/ Y+ a* }4 c, k' K9 I COLORREF clr = GetItemBackColor(nItem);# w7 G. @; a- Q* z# Q- p% @3 \ ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem); ! ]7 a7 ]6 I3 K. p) `. P pData->clrItemBackColor = clrBack; , @8 n j5 u7 b; y# j$ [ return clr;( x1 D& X5 {# y1 y/ G }

COLORREF CListCtrlEx::GetItemBackColor(int nItem) const6 I! ?% F7 e0 c; e7 b { + z1 q+ g# I, F* W& g8 }1 v ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem); ) E. I K: V9 F. |- I9 Y return pData->clrItemBackColor; ^* j1 W& i1 ?& y6 R } 2 S' y f6 u m' O7 r# n e1 b' D; hDWORD_PTR CListCtrlEx::GetItemData (int nItem) const4 f& h6 e$ e* G {' N3 l, k: \% P% O6 }7 ~; M; I9 @ if (nItem >= GetItemCount()) # e6 m! o$ r* h/ y$ ^) w { 2 [4 X2 z0 d* i4 ?9 D) k$ B6 p+ k return 0; 9 j1 Z5 V# y' a6 C' l }- E& W( ?0 J: Q u: J CListCtrl* pThis = (CListCtrl*)this; + H" \1 J. f w6 o$ f DWORD_PTR Data = pThis->GetItemData(nItem); & G; g h, [8 v: z) h1 V+ _1 G, E return ((ItemDataEx*)Data)->SrclParam; ' K3 _" i/ {" e. U} $ i) W6 X Q/ ~BOOL CListCtrlEx::SetItemData (int nItem, DWORD_PTR dwData)) h# a" C% Y9 |' v$ h { " v$ E3 G- x; ], j5 m& g* e3 v if (nItem >= GetItemCount()) - P. `0 e' r @/ q5 L5 @7 { { . r. M* W ~* r8 c$ v return 0; + R! v) f1 g* {" M } $ }7 ^# x4 B1 W CListCtrl* pThis = (CListCtrl*)this; 7 a, g8 C0 Q. _0 m. ?) L! n: k; k DWORD_PTR Data = pThis->GetItemData(nItem);/ j9 G8 ?; c- l z) o: e! B2 N ASSERT(Data);- B- E" _; k! e7 k7 y, e ((ItemDataEx*)Data)->SrclParam = dwData;$ |& o' _6 V& S6 n return 1; 5 |; |' j5 G6 B) N}$ H# ~+ Z* M! t, R' P6 H int CListCtrlEx::InsertItem (const LVITEM* pItem)5 m6 _2 R" ~6 n4 y, l. p { # T' z( C" Z, i5 c/ }0 p int nReturn = pThis->InsertItem(pItem); 8 F- o/ Q0 j% j& T. O ItemDataEx *pData = new ItemDataEx; ; ^* a6 p/ b& a" n8 {. ` // 设置默认颜色& p7 r0 e2 l6 Q, H, |( M" L pData->clrItemBackColor = 0xFFFFFF;//pThis->GetTextBkColor(); 4 g2 m) F6 C9 d8 M6 k+ H pData->clrItemTextColor = 0x000000;//pThis->GetTextColor(); . }9 W. e k+ A* [1 f2 X) z pData->SrclParam = pItem->lParam;- r& W, Z& C4 X. Y( J- V; h( E+ p- B pThis->SetItemData(pItem->iItem,(DWORD_PTR)pData); 4 z8 j2 y& j/ ^/ q/ j3 S$ M return nReturn;$ f8 @6 G; v9 P: g, O# T. r } ; \$ R* a, l" ?2 r9 T& yint CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem) 8 `" z& l. V0 l* o! d" ?, @{ Q8 P" ]2 E# E* D, n! Z9 F7 w& { LVITEM Item;- ]8 o3 z% B' U& j1 u2 W. D( ] memset(&Item,0,sizeof(LVITEM)); 9 B9 ]+ u' P/ B' l3 O" N Item.pszText = new TCHAR[strlen(lpszItem)+1]; ' O0 [* [6 }8 ]$ ]2 M strcpy(Item.pszText,lpszItem);- P6 l0 W1 M4 Z; m0 t: k N2 K1 s j Item.iItem = nItem;: F, Z4 r8 F) A0 m Item.lParam = 0; ! V; P" c/ g- v$ P0 E5 Q Item.mask = LVIF_TEXT |LVIF_PARAM; 5 E, b$ e' v! F BOOL bReturn = InsertItem(&Item);% q2 b8 i9 [1 F# g) p- J delete Item.pszText;3 T8 R8 y a& @ return bReturn; " W8 X8 u" D m x7 {}! }0 ]; i {9 N int CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem, int nImage)8 Z/ s% ]* q, r! l { . k5 Q" F. S2 u. i4 F LVITEM Item; # [2 h3 Q, _6 a9 @ V# n$ U memset(&Item,0,sizeof(LVITEM)); + x( o0 K9 O' c$ K) A5 s+ q; y! t Item.pszText = new TCHAR[strlen(lpszItem)+1];5 D5 m2 }" P6 v. ]* ` strcpy(Item.pszText,lpszItem); 3 X) Q, a6 B( e+ n* ?( h* l Item.iItem = nItem;/ A% Q1 c6 ]! E/ t5 f5 V Item.lParam = 0;" N" q! r: w5 K) H Item.iImage = nImage; ) E; n ?: x' J6 d; J1 ` Item.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE; . S$ q$ r5 m5 Y7 Z: U' \ BOOL bReturn = InsertItem(&Item); / V; G1 Y" a Q1 P" l) m* t& P delete Item.pszText;7 z; p1 S. l. p3 ~4 T return bReturn; # ^! x8 T& g' L- P9 C} T* Z( A% G, y& |7 e6 B int CListCtrlEx::InsertItem (UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam)8 x8 W- ~2 W, N {$ A1 N j: s& L- r9 W$ E* C LVITEM Item; 2 r) i3 U! v# ]/ e memset(&Item,0,sizeof(LVITEM));. Y5 ?/ M. O" |0 ^ Item.pszText = new TCHAR[strlen(lpszItem)+1]; 9 |8 U1 X; o: r7 r s strcpy(Item.pszText,lpszItem); & \+ }/ D" G2 |; S" F% g Item.state = nState;6 H% _) }; s, c( p- d% L Item.stateMask = nStateMask;6 r9 U( t9 p6 d, z Item.iItem = nItem; / l8 }' Z+ k3 J" k" V Item.lParam = lParam; ; I7 i- w( Q: w5 u1 j, i5 b# E- \ Item.iImage = nImage; ( r) p H( ^ j- ^" U. ~1 o Item.mask = nMask; ) s- j% ? ?2 r7 ^3 U% Y4 { BOOL bReturn = InsertItem(&Item);; J! B1 G: N# O Q1 D* w% K- _- ] delete Item.pszText; / l7 H/ g* C( _6 ~- e( T5 p0 C return bReturn; + Q" @! K5 ~/ d- L) X! t9 i} 5 D1 r1 S+ U; \% HBOOL CListCtrlEx:eleteItem (int nItem)# g6 S' R; O- l' r1 a { $ m* `0 L% G) t# r( u void* pDataEx = (void*)pThis->GetItemData(nItem);+ f L4 N# z4 I8 x, a9 `5 O if(pDataEx != NULL)' W4 @# \4 m* @9 j { $ ?( z& P) `: c Y, Z delete pDataEx; 2 d! ~4 f% f3 C/ B } 5 [/ Q0 @) O- J/ A! [% U return pThis->DeleteItem(nItem); % S6 n6 }7 v% }; F}1 I* _/ m6 y" p. w$ t8 _9 F BOOL CListCtrlEx:eleteAllItems ()2 B# a( \3 T) x( S3 J { $ B* L/ y; d0 n% q0 h. F( l int nCount = GetItemCount(); 5 D" G. T, I$ i2 h% {$ ^ for (int i = 0;i<nCount;i++)# \ k/ ?* B% }5 R& A$ s4 } {0 R2 U0 w0 B/ \' w: O if(!DeleteItem(0)) / L, l* r" w# z: b6 O" A! s2 S( ~ { ( q+ k2 p7 T3 I6 a8 K; {* i return 0;" z8 I* {$ D: P# j2 Q: l }" J) m# U" u/ m+ } } / t& J- ^- \2 e& W# \0 X. D return 1; 5 b) D+ K: k) y} 4 ~+ e0 w i, ]3 ]) A% {) W' Y3 bBOOL CListCtrlEx::GetItem(LVITEM* pItem) 2 E9 Z7 Y" h9 U' _) B+ N2 R5 }{ , `7 r v2 ^. D- Z% [% M! d BOOL bReturn = pThis->GetItem(pItem); & W/ E! c: ?7 e* ^. F if (pItem->lParam) $ t* m3 G! M; \ { 2 @2 c4 G2 G, s8 U. x' K pItem->lParam = ((ItemDataEx*)pItem->lParam)->SrclParam;, j0 z5 x7 B7 {2 Y$ l }

return bReturn;8 J; {9 y7 w& L) g( R# k } 0 [1 A+ o, m& F! ?0 xvoid CListCtrlEx::OnDestroy() - r9 _. d' u# r' M1 J8 R{2 T( b' G" j* @ // 关键是清除申请的内存空间 , e2 n5 b" n# _$ y( J P1 Y# S# r) w DeleteAllItems();+ N' G! t3 {9 k1 y CListCtrl::OnDestroy(); % x# \( N m9 H& K( E}

BOOL CListCtrlEx::SwitchItem(int nItem1, int nItem2)/ M+ E1 y- C* H" M3 S { ) F( T' Z N% x& j- W9 h if (nItem1 == nItem2 || nItem1 >= GetItemCount() || nItem2 >= GetItemCount() || nItem1 < 0 || nItem2 < 0) $ p9 o/ W1 ^+ Z { ! i& u$ W9 |1 S6 Z6 N; O4 d3 j' ~1 T return 0; # U; ?+ |5 N# I% m# C; ^ } 5 n& V6 h" _ l: e k* D3 m LVITEM ItemData1,ItemData2; - ^3 z- O( {1 P int ItemLength1 = pThis->GetItemText(nItem1,0).GetLength();5 @- o+ c u! |+ r2 w- p; K int ItemLength2 = pThis->GetItemText(nItem2,0).GetLength();7 s# a; V1 i( B* n* Q8 g) U ItemData1.pszText = new TCHAR[ItemLength1+1]; ; U. U# }7 j# f8 T7 ^ ItemData2.pszText = new TCHAR[ItemLength2+1]; 9 {9 ^, f: W% c ItemData1.cchTextMax = ItemLength1+1; 6 T5 { S" M" u; a' f3 C$ e ItemData2.cchTextMax = ItemLength2+1;# T( S& M$ P6 l9 _& L3 } t ItemData1.iItem = nItem1;; {' y( t% j! T7 V ItemData2.iItem = nItem2; $ \; \" O9 N# I; {( f# I ItemData1.iSubItem = ItemData2.iSubItem = 0;+ ^! B- [/ _6 A ItemData1.mask = ItemData2.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE|LVIF_STATE;

pThis->GetItem(&ItemData1);6 D+ ~5 _* ?% n) `4 B- B( W% J2 y pThis->GetItem(&ItemData2); 0 N- m G4 k: n5 G6 v // 交换数据 2 R! v- u8 b. |( K4 R( z ItemData1.iItem = nItem2; ; O# }- j6 I3 J0 h/ A& H9 T ItemData2.iItem = nItem1;. t- J" C$ A/ r* `3 S8 j L' ] pThis->SetItem(&ItemData1); + j6 m* q6 G* A* x, R$ J pThis->SetItem(&ItemData2);

delete ItemData1.pszText; ! o, I4 B. `3 T: _8 E3 o+ c delete ItemData2.pszText; 5 {' o9 H* B6 h4 y5 T+ x- R // 得到报表模式的列数3 X; ^0 V6 M7 @- b int nColCount = pThis->GetHeaderCtrl()->GetItemCount(); 6 L! Q" O, e) S5 J9 K CString strBuffer;5 x" n1 u* J% m! B for (int iSubItem = 1;iSubItem < nColCount;iSubItem++)' h O! C6 i' A3 N8 Q0 Q& o4 N, o { , g( f% |# g" k) j strBuffer = pThis->GetItemText(nItem1,iSubItem); : {) N t$ j2 o pThis->SetItemText(nItem1,iSubItem,pThis->GetItemText(nItem2,iSubItem)); # u* {% ]0 Z8 Y. g* C. H pThis->SetItemText(nItem2,iSubItem,strBuffer); 9 d7 w3 H* h: u: p/ o } 3 m' w1 P0 {$ }$ H // 把原来的选中状态也转移过去 ! Y$ {" l, ?# z8 e1 ?. e // 不知道为什么这个属性没有体现在LVITEM结构中…… 6 ^9 p3 G# m( D N bool bItemSelected1 = GetItemState(nItem1,LVIS_SELECTED) == LVIS_SELECTED;) v1 G# l0 I7 K7 G5 @ bool bItemSelected2 = GetItemState(nItem2,LVIS_SELECTED) == LVIS_SELECTED;

SetItemState(nItem1,bItemSelected2?LVIS_SELECTED:0,LVIS_SELECTED);& W4 | o8 ]4 S) P/ E1 o SetItemState(nItem2,bItemSelected1?LVIS_SELECTED:0,LVIS_SELECTED); ( \6 s2 l% c1 h# K return 1; 2 v+ R( G2 J7 Y% q5 k- r}

BOOL CListCtrlEx:reCreateWindow(CREATESTRUCT& cs)( [% H& M. w u {' m, `0 D0 Y: T // 本类报表模式专用 7 i/ L, R, s x ?! C cs.style |= LVS_REPORT ; 2 y% v% }, I, L+ B9 G: i* e // cs.dwExStyle |= LVS_EX_FULLROWSELECT; 0 y! H0 Z |7 n return CListCtrl:reCreateWindow(cs);7 `6 i8 _1 X* P9 N8 f }

; r. `% r+ ]2 a5 o% G COLORREF CListCtrlEx::SetHighLightColor(COLORREF clrHighLight) 5 Z: @, ]* I( d# n/ i3 R( o* v{ 1 Q/ Z. }9 Q1 B+ g# g COLORREF clr = GetHighLightColor(); 5 a- a0 H3 ^ ? m_clrHighLight = clrHighLight;4 m2 c; K: ^( m3 w2 x( \$ M return clr;& @( p( r1 w$ D: y }

COLORREF CListCtrlEx::SetHighLightEdgeColor(COLORREF clrHighLightEdge)2 T8 z: E" S. p8 Z# T4 @ { # Z" x( I$ \% O% r1 @/ w COLORREF clr = GetHighLightEdgeColor(); ( W% N! j. n# V4 a. ? S* ~: h5 s2 H m_clrHighLightEdge = clrHighLightEdge;9 _0 |8 u' K k- o( t& Z return clr; ; J$ L1 ^! B/ o}

该用户从未签到

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

COLORREF CListCtrlEx::GetHighLightColor(void) const& {' W" |$ n$ H% S: E { % x' S2 F8 |* W) j+ h- J, h return m_clrHighLight; ; ]( ^: E* U8 j7 O( p' n7 ]}

COLORREF CListCtrlEx::GetHighLightEdgeColor(void) const * k2 i! O$ F5 e& L! _{. Z$ k8 n9 E+ R( w" K S- ^1 z return m_clrHighLightEdge; , N1 Z" P d) \! D# @8 s}

int CListCtrlEx::GetItemImage(int nItem)) `) |1 g6 g" Z- S* C/ y { / `# [3 q1 G' Z8 C9 V+ x LVITEM ItemData;3 o9 Q* l s, ~% s5 w3 b ItemData.iItem = nItem;# ^* E$ L5 B+ l ItemData.iSubItem = 0; $ L' ~# L8 H' R7 ~' r9 y4 w, |# O5 K' S ItemData.mask = LVIF_IMAGE; ( S' u9 i- E5 }* I2 ~" t5 Y pThis->GetItem(&ItemData); * D" u5 R! l' w4 ` return ItemData.iImage;! _5 x: n+ N! f5 \9 G, d: D8 D }

BOOL CListCtrlEx::MoveItemUp(int nItem) / I( I- C0 F( A5 L3 H{ ' c& h. a9 J4 A, H; V V% K, s if (nItem < 1 || nItem >= GetItemCount())4 M: d# h. ~* F* m7 ` {$ ]: K) C" W) a5 a return 0;7 V% w4 r, _8 A' K1 S } * Z! Y8 S9 M+ e$ t* d return SwitchItem(nItem,nItem - 1); 2 y/ k+ u$ s7 j* E# h% u}

BOOL CListCtrlEx::MoveItemDown(int nItem)+ k% G/ Y1 ~6 \' P0 @ {8 d5 p a% E B9 e3 @& j) W8 @, D if (nItem < 0 || nItem >= GetItemCount() - 1)9 u# l8 C ~7 U( c {9 F& j0 Z: V' b4 o5 v! m return 0;. O9 \0 u0 |" a0 o- M$ S: R }5 I) A6 z% D4 D; E$ b1 ? return SwitchItem(nItem,nItem + 1); ( c$ N ^+ U6 ~% p}

void CListCtrlEx::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult) 6 Z* F H: R5 Z" P{ 3 k, Z8 C: `1 S0 C* E NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR);

*pResult = CDRF_DODEFAULT;

if (pLVCD->nmcd.dwDrawStage == CDDS_PREPAINT)- N% I0 b1 V% w" a; s { - X0 {$ m6 X* C' l5 n- p *pResult = CDRF_NOTIFYITEMDRAW; % q2 J: s" ]- r! a }% b& Z! D" r. ` else if (pLVCD->nmcd.dwDrawStage == CDDS_ITEMPREPAINT)3 J% v7 u9 V% U2 D1 f { ; f. i8 v# J: } *pResult = CDRF_NOTIFYSUBITEMDRAW;- N' c" {/ y$ z; q0 P }, g$ `! @* s3 D4 r5 K else if (pLVCD->nmcd.dwDrawStage == (CDDS_ITEMPREPAINT | CDDS_SUBITEM))% j t @5 t+ G, s8 A5 E, b {

int nItem = static_cast<int> (pLVCD->nmcd.dwItemSpec); - e/ c& B' T2 |) i6 t, H, L8 g1 n int nSubItem = pLVCD->iSubItem;! t* @! b1 `$ z8 n8 F5 H if (nSubItem != 0)* y7 ]- @! a9 ^% \ S5 ?7 @3 c {+ L3 k- X% w/ z. {& C9 V // 全部绘制过程在nSubItem == 0 时完成 9 \; q# { ~1 D- l // 分布完成会有小问题,我无法解决( L+ b ^# ^% H) @& m5 H *pResult = CDRF_SKIPDEFAULT;* y7 W; W/ k! S3 D7 d return;5 J; n" W/ q& K$ b }& \, D, e( h7 Z CDC *pDC = CDC::FromHandle(pLVCD->nmcd.hdc);8 n9 P. [) I( k# ~9 W2 J1 ` int nColCount = pThis->GetHeaderCtrl()->GetItemCount();3 Z5 S) w& v3 k/ `% b" W CRect rectSubItem;5 ~, v6 a" Q* Z" C CString strItemText; ' \0 y4 d$ q1 V; ?0 t6 f pDC->SetTextColor(((ItemDataEx*)pThis->GetItemData(nItem))->clrItemTextColor);/ N, V/ l% k N pDC->SetBkMode(TRANSPARENT);8 |& @# n9 e9 h2 @# p1 E, } // 相当于橡皮" {6 J3 p- W% F' g CBrush brEraser(GetItemBackColor(nItem));

// int nFirstWidth = GetColumnWidth(0);

" h3 W0 S0 N, T# q* |6 R for (int i = 0;i < nColCount;i++)! ?0 j! K& Q& T# L { 3 Z" F8 s- n8 v strItemText = GetItemText(nItem,i);

if (i == 0)- _5 ~" E; M8 x* k) B {0 O% X6 C, Y% p& S; U pThis->GetSubItemRect(nItem,i,LVIR_BOUNDS,rectSubItem);! W2 z# n( [& B: T0 C // 擦掉原来的图形( o( i! \) d9 E; N) g4 {: { pDC->FillRect(rectSubItem,&brEraser); % [" ], c: y! y* r, c, F. d' M if (GetExtendedStyle() & LVS_EX_CHECKBOXES)) \5 l0 v+ f; e) l# q5 E) K { 5 b! H5 ]( z0 J: K6 ~ CRect rectCheckBox; 8 |! Q3 l4 U. t. s7 ], K5 R7 A) Q GetSubItemRect(nItem,0,LVIR_ICON,rectCheckBox); 0 U: m* n3 w* o8 `: p9 v: p CRect::SwapLeftRight(rectCheckBox);9 z" S1 }4 m. J P( d7 M! f rectCheckBox.left = rectCheckBox.right - rectCheckBox.Height();

rectCheckBox -= CRect(2,2,2,2);+ X3 F/ s8 z& Y pDC->Draw3dRect(rectCheckBox,0x81521D,0x81521D);: U: R6 U& D1 J8 c$ X& V //rectCheckBox += CRect(3,3,3,3); 3 I9 P! \( I _! d1 U if (GetCheck(nItem)) 2 I( u7 z/ f1 s& n: R- f; t7 [ { - M0 A3 b! f3 Z& { // 绘制一个CHECKBOX2 B2 C" l/ O( p$ g/ H CPen penCheck(PS_SOLID, 1, 0x0021A221); . v, X% U/ m8 {; G. } CPen *pOldpen = pDC->SelectObject(&penCheck);/ F* H0 K) W* M4 L! i5 k for (int j = 3; j < 10; j++)7 Q: l+ ^; Q( ]1 o# M8 R0 j {" f1 ~6 _; h& `6 q pDC->MoveTo(rectCheckBox.left + j, rectCheckBox.top + rectCheckBox.Height() /2 - 3 + ((j < 6) ? j - 1 : (9 - j))); ' Z" B: p; R3 n pDC->LineTo(rectCheckBox.left + j, rectCheckBox.top + rectCheckBox.Height() /2 - 3 + ((j < 6) ? j + 2 : (12 - j))); 3 R% a+ F( H; h }) {3 R) w7 o* N3 w d- x6 `# N1 H( C - Z+ v g8 {1 m8 e" o pDC->SelectObject(pOldpen); ! b. K+ J& `4 \7 @* `- B } $ ~6 {* X& S' X& a }: g! U6 w$ N* o5 { // 根据需要画选中框 / N' |( l7 _' U6 U8 K8 C: J if ( (GetFocus() == this || GetStyle() & LVS_SHOWSELALWAYS)&&(GetItemState(nItem,LVIS_SELECTED) == LVIS_SELECTED))5 F/ C) n4 X5 ]7 H: u { 4 g {5 g/ N* ?* Q1 F if (GetExtendedStyle() & LVS_EX_CHECKBOXES) ; W3 q, c, ^2 B {// 存在检查框风格的时候需要对左坐标进行修正: t. m6 J; t6 S; o/ V) n& u CRect rectLeft; 5 @6 J1 N/ x& u9 d @' _ GetItemRect(nItem,rectLeft,LVIR_ICON);2 T. u9 j( o( d( x N$ w rectSubItem.left = rectLeft.left; , T4 R$ I" F8 |5 g' [ } 1 K+ [: ?: T7 [4 {, D2 w; n5 d if (!(GetExtendedStyle() & LVS_EX_FULLROWSELECT)) 2 r. ?8 U, o+ I% p8 h& b {// 非全行选择时右坐标需要修正 9 p6 M& m& k8 A: ^ w' N1 ` CRect rectRight; 4 U- X* e: X- C" M$ `( L8 \ GetSubItemRect(nItem,nSubItem,LVIR_LABEL,rectRight);! ^ z9 s7 c7 ? rectSubItem.right = rectRight.right; # N, F1 D: Z- v: P4 U6 B } % U9 f1 k4 N% f1 ]* ]* ~. H CBrush brHighLight;6 q( r! [9 I* p% J2 x% f) j F CPen penHighLightEdge; , }, i: A6 \9 ]5 Y: R7 J; p if (GetFocus() == this)9 b o/ ~$ \+ q. e+ @# { {4 O8 b2 d6 f3 t' c. k5 X- d' Z brHighLight.CreateSolidBrush(m_clrHighLight); 4 k- m: U' I+ b. x penHighLightEdge.CreatePen(PS_SOLID,1,m_clrHighLightEdge); : X# ?0 ] L$ p8 ~9 G! D* E- f }' W+ W" k( r5 M4 O8 o+ P else R6 j c9 p4 U3 _5 b7 ~( Y& n {4 [' u5 I3 l9 b; M+ P6 a // 不是焦点时,颜色70%不透明度 , s* A0 U$ D' S' _1 D+ x COLORREF clrBack = GetItemBackColor(nItem); ; D/ S) E) `/ W1 `# f2 W WORD red = GetRValue(m_clrHighLight)*0.7 + GetRValue(clrBack)*0.3;+ C4 x9 u0 k7 w' h A' y( a, N WORD green = GetGValue(m_clrHighLight)*0.7 + GetGValue(clrBack)*0.3;( ?) s2 t4 F- A/ R- x7 C WORD blue = GetBValue(m_clrHighLight)*0.7 + GetBValue(clrBack)*0.3; " f/ `9 i+ g" [) a brHighLight.CreateSolidBrush(RGB(red,green,blue));; x* `0 z, z) h$ k* J red = GetRValue(m_clrHighLightEdge)*0.7 + GetRValue(clrBack)*0.3;( h5 I( M( z& v7 O. G$ @% c/ e green = GetGValue(m_clrHighLightEdge)*0.7 + GetGValue(clrBack)*0.3; 1 @" w: X- R' P, v' `4 t) Y. R' n blue = GetBValue(m_clrHighLightEdge)*0.7 + GetBValue(clrBack)*0.3; $ B/ y, h7 l6 x/ P penHighLightEdge.CreatePen(PS_SOLID,1,RGB(red,green,blue)); ' a$ I( F8 ~, A4 D2 m0 | } 6 W5 g& X( E7 z* j CBrush *pbrOld = pDC->SelectObject(&brHighLight);( {0 k! Y, t q& W4 i$ K3 B CPen *ppenHighLightEdge = pDC->SelectObject(&penHighLightEdge);6 D5 v9 s2 w9 R) w; Z+ b8 e pDC->Rectangle(rectSubItem);. A1 A& C, R+ W) I3 n pDC->SelectObject(ppenHighLightEdge); 6 k$ s8 v: Z. j4 c9 B* i pDC->SelectObject(pbrOld);8 k) f6 ?2 T. r' {- P9 p: Y2 p }4 U4 o2 f% Q/ P. T2 V `& U }5 o, z0 g" l4 l pThis->GetSubItemRect(nItem,i,LVIR_LABEL,rectSubItem);- H; o$ k% e6 s6 a1 { UINT uFormat = DT_SINGLELINE|DT_VCENTER|DT_END_ELLIPSIS;0 K# |8 b& u/ a, @ HDITEM hdi;; j# M/ i- a) K3 s; c$ u hdi.mask = HDI_FORMAT; 9 j J/ T; R+ B% l+ s2 c5 Q/ B pThis->GetHeaderCtrl()->GetItem(i,&hdi); 9 G7 ]. v8 `, t3 b+ n- Q if (hdi.fmt & LVCFMT_RIGHT) $ M" c6 i6 k/ n% I3 f { 3 \* z- k( [; @% b: ] uFormat |= DT_RIGHT;6 X+ W9 i7 q; u6 Y h: e } # f$ T3 t) G1 i" ` else if (hdi.fmt & LVCFMT_CENTER)* U: {7 s3 ]$ ?; N% g1 N7 C { # a1 X/ r, F$ b, b F0 J% l uFormat |= DT_CENTER;! } y! x m8 [0 h- d }& Z' q" ~2 [, h; X) C1 G pDC->DrawText(strItemText,rectSubItem,uFormat);

CImageList *pImageList = pThis->GetImageList(LVSIL_SMALL);5 ?5 q2 U8 J' L \+ Z4 G" A% L if (pImageList) / F: I) ?: a5 T; R& ^* Z {' X- Z5 F* `# a/ H% W CRect rectIcon; m; n ?9 q, ~( D) |% r GetItemRect(nItem,rectIcon,LVIR_ICON);! n t4 G9 R' f" i pDC->DrawIcon(rectIcon.left,rectIcon.top,pImageList->ExtractIcon(GetItemImage(nItem))); 9 d8 M/ m9 g* _, C$ M* `& q } 3 `) h A5 M# k9 `8 R) p( ^ } " c% X2 c- F* {% p/ L5 h# u *pResult = CDRF_SKIPDEFAULT;3 j/ \ y! ^0 J1 `$ {( W4 O0 e$ \, ^ }

}

该用户从未签到

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

本版积分规则

关闭

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

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