下沙论坛

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

QQ登录

QQ登录

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

又是垃圾,CListCtrlEx Base CListCtrl

[复制链接]

该用户从未签到

跳转到指定楼层
1
发表于 2005-2-23 21:29:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
// ---------------------------------------------
9 j  P4 f" e7 C0 @$ ]- n// ListCtrlEx.h& T1 R  a) S3 p" n0 Z
// ---------------------------------------------
! |* W8 z: y+ Z; n#pragma once! V: t5 B! [4 U7 H  P

* f( ^' J6 Q% v// CListCtrlEx
0 V8 _- X% A9 f8 J+ R& L0 A' W$ Zclass CListCtrlEx : public CListCtrl
: f2 h3 t+ b% m, t, n* G. o' }- [{' z& x7 {. e, V( {8 j) ], T
    DECLARE_DYNAMIC(CListCtrlEx)
* m; f! p; Y: P, R( D: Epublic:! ], x0 X# i/ O, z
    CListCtrlEx();. K6 _3 e- q' e" i( `$ n$ e8 O0 i
    virtual ~CListCtrlEx();1 _5 h( I% ~1 s0 k1 l  y
protected:+ A4 O6 t& B: m2 T* r7 r
    DECLARE_MESSAGE_MAP(). s1 M. e* W( l5 j) d
    CListCtrl* pThis;6 [7 X. y9 E2 K
    virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
+ L) j4 @! \8 h9 P& ?9 s. U) oprivate:1 O5 b+ m& Y# e" k! y0 @% e
    struct ItemDataEx {
' ]# F) }6 H( n8 {* }2 t        LPARAM SrclParam;
2 A, w0 u; W4 {# z! ^9 M" h        COLORREF clrItemTextColor;
/ z7 h. o$ ?# ~3 @' l9 A        COLORREF clrItemBackColor;
- K* }" c* W) T. g4 M    };& F% Z9 b' y# _+ Z; R1 H
    COLORREF m_clrHighLight;6 i3 G+ F4 m, h/ V6 y* x+ N( q2 O
    COLORREF m_clrHighLightEdge;
, M6 J6 ^# d( J% R4 J. }8 Hpublic:
, x# x& x- g- e2 _7 m+ ^5 Z$ m) i    // 新增方法  d9 a7 g$ L  v: v
    COLORREF SetItemTextColor(int nItem,COLORREF clrText);6 a( t0 ]0 H: J$ v5 g7 z# W% {
    COLORREF GetItemTextColor(int nItem) const;: D" [% C" f6 ~2 K* P4 n+ l3 {
    COLORREF SetItemBackColor(int nItem, COLORREF clrBack);# `, p8 T, w/ a8 e, a$ K' \. h
    COLORREF GetItemBackColor(int nItem) const;
' \) @( t; f% H8 F) L* g    COLORREF SetHighLightColor(COLORREF clrHighLight);
/ X  ?4 M$ ~: p" K; U' X/ [  v* I1 w    COLORREF SetHighLightEdgeColor(COLORREF clrHighLightEdge);" P" t- t" ^8 M7 i- F
    COLORREF GetHighLightColor(void) const;" T( D- i  }# |4 J
    COLORREF GetHighLightEdgeColor(void) const;
5 Y1 b7 N6 f8 G! N5 z    int GetItemImage(int nItem);
3 U8 G% t; b: F( w  L* _( F    BOOL SwitchItem(int nItem1, int nItem2);! ?# ^" |, v; V8 [& \3 l& Z
    BOOL MoveItemUp(int nItem);
! h! q$ j$ u: D+ e/ Z" @    BOOL MoveItemDown(int nItem);0 E3 b% t. @5 a! e- I- z1 W+ u7 e
    // 重写原来的方法
4 z, t2 [1 n9 l: W; h    DWORD_PTR GetItemData (int nItem) const;
' ]* F' X' |1 e1 a0 W) e    BOOL SetItemData (int nItem, DWORD_PTR dwData);
+ t0 E. g( R2 h/ S, U- X$ o    int InsertItem (const LVITEM* pItem);
0 j' ?. ]8 R1 B7 m- O+ I/ R8 Z    int InsertItem (int nItem, LPCTSTR lpszItem);
0 r" ?5 m8 d* |' W/ w    int InsertItem (int nItem, LPCTSTR lpszItem, int nImage);" X2 ^5 C0 S2 h. J
    int InsertItem (UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam);( c$ `  x+ t$ S! {- \
    BOOL DeleteItem (int nItem);! {. m8 ?! j* K2 g* f, D
    BOOL DeleteAllItems ();  p: K/ {' {& S/ H, y: o' r3 [, _
    BOOL GetItem(LVITEM* pItem);0 x9 T  \' u: ]4 c3 ^
    afx_msg void OnDestroy();# M/ e/ o7 T# _0 p
    afx_msg void OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult);
0 ~4 h- p% G, }7 y};
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 顶 踩

该用户从未签到

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

// --------------------------------------------- ! ]1 L9 [5 v. r; P S9 Q) f% `// ListCtrlEx.cpp1 j0 p1 v0 m: L V0 C7 ] // ---------------------------------------------- P( k' T5 z c ~& J6 K // ListCtrlEx.cpp : 实现文件9 z b( r: r. t. F1 M1 y //

#include "stdafx.h"3 U0 g0 y$ L9 D& k #include "CreateNewClass.h" ! `4 G: s9 T ]) v% p& L- Y9 W% ~#include "ListCtrlEx.h" ( h! x5 H+ @0 d6 S#include ".\listctrlex.h"

0 M! s% W$ t6 G; o# t8 | // CListCtrlEx

IMPLEMENT_DYNAMIC(CListCtrlEx, CListCtrl), n0 j% F& L* H& K& \2 i CListCtrlEx::CListCtrlEx() 8 f$ `7 B0 N1 i! z2 e{ 5 B7 c/ P; A7 r( R- H+ E // 用于调用基本的方法$ K; _2 X$ z$ P2 N6 L pThis = (CListCtrl*)this;9 J. k* z5 r8 j4 \4 H8 N: a // 默认的选中框颜色 1 Q/ K. w. S: f: z" I m_clrHighLight = 0xF1DACC; 3 ^3 B% w/ B p; t, z4 W4 e3 V m_clrHighLightEdge = 0xC56A31; $ L! J+ T- |+ e$ j N6 b}

CListCtrlEx::~CListCtrlEx() 0 _& R+ h* b5 ]7 j2 c% m. [{

}

! p c b" o4 W, } BEGIN_MESSAGE_MAP(CListCtrlEx, CListCtrl) 0 S) P- N3 l3 O ON_WM_DESTROY() # H8 e2 W' h. f$ D+ \ ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnNMCustomdraw) - m2 ^" F9 o) u' F9 `6 B) M+ b3 mEND_MESSAGE_MAP()

// CListCtrlEx 消息处理程序

! `# I' B/ \* ^: ~ F& {1 Q% O COLORREF CListCtrlEx::SetItemTextColor(int nItem,COLORREF clrText) / E: H3 S$ @- f! _! {7 k3 i{ 6 p0 k4 o2 t5 {/ p7 ` COLORREF clr = GetItemTextColor(nItem); 9 P( i" v9 M/ y' r) [ ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem); 7 _# B+ ?- R; N4 @, a3 a& i' _/ A pData->clrItemTextColor = clrText;# o. [# J% l6 M; a0 D8 }6 S. G6 q return clr; 6 b. b% {" U$ Q}

COLORREF CListCtrlEx::GetItemTextColor(int nItem) const' j4 r, u; |. V7 F. x* q { 7 V/ i2 ?9 x* X9 n ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);- y# _# e5 C3 | return pData->clrItemTextColor; & @6 v, L8 R3 i8 G; m' E}

COLORREF CListCtrlEx::SetItemBackColor(int nItem, COLORREF clrBack)# @3 y: _/ V- m" z7 E7 S+ s" @ {) P5 Q; ]! ]* s/ ^ COLORREF clr = GetItemBackColor(nItem); 7 K) s) |* d$ f6 { ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem); ' U- ]# a, O: f6 g3 [6 } pData->clrItemBackColor = clrBack;1 C7 \0 u/ o) S return clr;0 C$ U1 P) c+ X0 I3 Z }

COLORREF CListCtrlEx::GetItemBackColor(int nItem) const8 Z6 _; ]1 y: ^7 F { & {+ o6 O/ U% i$ ?, F H( Z ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem); 3 S+ G6 p7 |3 \* g return pData->clrItemBackColor; / t# ~) O' e8 G0 g0 |} 7 y; ]. P' H8 h4 L! C* W1 NDWORD_PTR CListCtrlEx::GetItemData (int nItem) const # L) [# h! g1 {( r" \, @{ ! c# k; H2 n4 T if (nItem >= GetItemCount()) " `9 L- l, a# W0 Q { 9 W9 d# p8 r! R9 ~* N U return 0;4 w1 U2 A- e. G) X% g. D- T% @+ h3 ]: Q } " {8 n1 Y' E+ y9 t* z# k$ f CListCtrl* pThis = (CListCtrl*)this;5 E9 T6 t* g0 X. S2 ~ DWORD_PTR Data = pThis->GetItemData(nItem); " l' b* b+ f2 M/ H$ F return ((ItemDataEx*)Data)->SrclParam; 3 L, C( V% a+ C}7 b2 d0 e& Z5 U$ x BOOL CListCtrlEx::SetItemData (int nItem, DWORD_PTR dwData)8 ]" A# z1 k$ X3 j {3 Z3 c6 w7 U8 P% [- }: K if (nItem >= GetItemCount())- @9 I+ i/ O6 E! _7 ?% n {$ _' a% k( P) V' T% g& @2 m F0 ^0 X return 0; / G; ^. h) b8 i+ a6 f: P }$ |' F4 z6 D) z) u CListCtrl* pThis = (CListCtrl*)this; ( G1 g4 w/ D0 X DWORD_PTR Data = pThis->GetItemData(nItem); . J$ ?& K9 L9 |7 ]# {5 E ASSERT(Data); : w, q; m" }$ e. _; | ((ItemDataEx*)Data)->SrclParam = dwData; : }; P# W( }3 `6 v) O return 1;$ t# _' K a% _& _' _9 G# h } / Q) f" C+ z9 Y& T/ e7 oint CListCtrlEx::InsertItem (const LVITEM* pItem) ' P$ E/ q4 F5 d{, p1 j. o2 v- p; i9 k5 ? int nReturn = pThis->InsertItem(pItem); 4 _! P9 l5 M! }, k ItemDataEx *pData = new ItemDataEx;, M/ a/ E6 o% }7 ]4 G4 | // 设置默认颜色 J' P* m5 Y; k. T3 _2 b pData->clrItemBackColor = 0xFFFFFF;//pThis->GetTextBkColor(); 8 b) F" @# _9 X) z/ \$ Q pData->clrItemTextColor = 0x000000;//pThis->GetTextColor(); " }1 T# x) j( f% w2 j& ^# J* b pData->SrclParam = pItem->lParam; & ^% R" D+ i, } pThis->SetItemData(pItem->iItem,(DWORD_PTR)pData); " y: [1 N( q6 a6 u l return nReturn; 0 K0 |6 R1 \. ~- T# Z} " Y: Z- M9 i: Y8 Pint CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem)5 |% r0 j1 G O { ) x9 D9 W7 {5 e( h0 u2 }) F) g+ \ LVITEM Item;. t, d5 M8 ?/ e& L3 O( Y memset(&Item,0,sizeof(LVITEM));7 x. x% ?3 y) p$ u; M' M% { Item.pszText = new TCHAR[strlen(lpszItem)+1]; 0 }5 }0 A8 B9 R; T2 M" u! K strcpy(Item.pszText,lpszItem);5 j6 y: n! B* K4 n3 Q" |' l$ t Item.iItem = nItem; & v- E0 b2 o) p* G# G2 W Y Item.lParam = 0;* C( X1 z r6 p) m Item.mask = LVIF_TEXT |LVIF_PARAM;7 ?' }0 O8 V5 ? BOOL bReturn = InsertItem(&Item);! b/ E$ @: f: ~' Y+ `$ Q: w delete Item.pszText;. \8 q* x+ C2 s2 ] return bReturn; . ?/ ~4 P/ n& T$ ]} $ s n0 k' w+ Y- ], R) `( y! cint CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem, int nImage)2 i. Q( W- v3 U% a) [2 u2 i4 X { $ |/ {5 l) _* j2 j8 y LVITEM Item; # ^/ b2 y" X/ v8 R memset(&Item,0,sizeof(LVITEM));8 e& c) q$ t0 W* @9 g% R Item.pszText = new TCHAR[strlen(lpszItem)+1]; , ^/ y2 _& v0 N$ v strcpy(Item.pszText,lpszItem); 1 \; ^0 A& e/ y Item.iItem = nItem; # w/ @; W' w0 K Item.lParam = 0;$ F7 `0 r. t2 o+ s4 z& v Item.iImage = nImage; % j) k! U& T3 J# ?1 k% _ n Item.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE;* T$ `& m: m1 t0 l( o BOOL bReturn = InsertItem(&Item);% u) @% H8 }, } delete Item.pszText; 4 r% q, R# y8 s3 h$ m7 g5 u return bReturn; ' M. Y Q e' Z) K' @+ W6 {}% d2 y) w3 [+ |5 w; ]$ Z int CListCtrlEx::InsertItem (UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam)& i) d* z8 b! p {) F; Q* c/ \" {- c3 b4 d4 F2 G, z" P LVITEM Item; 4 |1 r# X4 V; `9 K) r9 f: o# @1 p memset(&Item,0,sizeof(LVITEM)); 1 s( @% |: V1 R" [# k+ P" H Item.pszText = new TCHAR[strlen(lpszItem)+1];& T! W! h7 X6 S5 s% x* c strcpy(Item.pszText,lpszItem);9 s" C2 u: X m" X9 p- n3 k" o Item.state = nState;8 Y- D1 {- U" s Item.stateMask = nStateMask;& g5 Q# s, T, o3 f# |( E Item.iItem = nItem;1 [6 {" q4 S) I8 }8 b+ v% Q Item.lParam = lParam; 2 A/ M1 U% F: v( s& `6 l7 z( C: L Item.iImage = nImage; 8 M8 b0 `$ v9 J, s7 f! l. | Item.mask = nMask;/ o# I- p" ]( U/ e' ^* z# | BOOL bReturn = InsertItem(&Item); + M9 y% f' c2 w* [2 H2 L delete Item.pszText;" k+ R# \ K m; d/ ^/ ` return bReturn; 6 Z5 h- I8 m3 E3 O% ~} ( c- s6 c* k" P: u& W" LBOOL CListCtrlEx:eleteItem (int nItem)% \: d+ J, K( S H- T$ _- N; y {, y2 n" \8 l# @0 F* O void* pDataEx = (void*)pThis->GetItemData(nItem);; Q& S* f! X; \ if(pDataEx != NULL), V$ b- W$ `/ K {5 F1 t/ e/ o7 m* o( l delete pDataEx;' `' ]$ w1 d5 o' x+ r1 \7 M } ' y4 n4 _2 o4 x: M2 a0 D return pThis->DeleteItem(nItem); ) g% k. w2 ?4 b9 g6 E) W1 W8 j+ [; N}% p. `% o. N- D6 F* ^3 E. U BOOL CListCtrlEx:eleteAllItems () ; @/ G4 z, N2 b3 s& g% b4 P) x L, g{' E9 x5 O& o( a. j( c int nCount = GetItemCount();. n& u/ J+ f& m4 O: S! w" ~ for (int i = 0;i<nCount;i++) & p- H, V2 x; Z5 O2 c {6 a8 W% z! B! |- x8 j3 f if(!DeleteItem(0))3 Z: V: b+ J+ K* V1 O { 6 B0 H; g$ f: F8 B return 0;, W" \9 y, P5 N }8 [8 ]- v5 ], m) F( S, n% X% k }& n4 a. A' H5 u, J) p return 1; ' _" f, t' N7 m7 s! C [}" v4 J) j- i( a5 `1 t# C BOOL CListCtrlEx::GetItem(LVITEM* pItem) + z- r! q. ~- ~2 x' M& p' B{9 o( e1 R# |8 }7 ?' E3 s e. ^ BOOL bReturn = pThis->GetItem(pItem);, r/ w: o: z5 {! X2 K if (pItem->lParam), ^% k) i# e3 d H4 P1 v {# [" W; X: l: X pItem->lParam = ((ItemDataEx*)pItem->lParam)->SrclParam; $ B( { \! E" R+ |7 ` }

return bReturn; 9 \. v7 Y# Z0 r3 l: i}# c% j- H }( T2 O, S# r void CListCtrlEx::OnDestroy()/ D8 }! h/ t: g9 }& S { / Z% ?6 S4 B0 n/ R& Z* N! t // 关键是清除申请的内存空间 1 @( O1 ?' ^, |4 o- D DeleteAllItems();; T n X5 s4 p! m* e$ e1 v A8 \ CListCtrl::OnDestroy(); , q, z; p) H: @3 e& ?* h}

BOOL CListCtrlEx::SwitchItem(int nItem1, int nItem2), |( R% m- U, B% ~' k {2 ?( A. H# _" s& R6 U- Q if (nItem1 == nItem2 || nItem1 >= GetItemCount() || nItem2 >= GetItemCount() || nItem1 < 0 || nItem2 < 0) ! w# m. W; K; T4 j { % f' _2 N2 ?( n return 0; ! e- n2 o0 E3 ]2 W } , h) A' S( h& {4 ]& b- y7 k8 } LVITEM ItemData1,ItemData2;6 m# n1 J+ p! M' w2 [ int ItemLength1 = pThis->GetItemText(nItem1,0).GetLength();' S V. r! E- V c3 k& s int ItemLength2 = pThis->GetItemText(nItem2,0).GetLength(); ) x( J1 K, T% N ItemData1.pszText = new TCHAR[ItemLength1+1]; 6 J6 k1 x: y' X: l3 Q9 C ItemData2.pszText = new TCHAR[ItemLength2+1]; ( j/ N& u" Q/ y$ S* s% d' n: ]1 Y ItemData1.cchTextMax = ItemLength1+1;5 L& U8 d1 H% w7 O! S3 p ItemData2.cchTextMax = ItemLength2+1;) W9 t A9 Z6 y) I Z4 a ItemData1.iItem = nItem1;$ p5 Z+ m" A+ y! p% Y- c9 R8 u ItemData2.iItem = nItem2;; u4 {/ C; M3 N8 [ ItemData1.iSubItem = ItemData2.iSubItem = 0;7 e( O$ Z; e) a& N& w5 F5 y4 ^ ItemData1.mask = ItemData2.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE|LVIF_STATE;

pThis->GetItem(&ItemData1);% y/ K+ B! M0 H1 L* l pThis->GetItem(&ItemData2); ! `8 U& x# R4 d // 交换数据 ' a3 |1 R3 _. b2 O ]5 K& m ItemData1.iItem = nItem2;( p1 j, _! b/ b' }" ^5 @ ItemData2.iItem = nItem1; 8 I4 G' d( Q- C/ v1 e pThis->SetItem(&ItemData1);; e h1 y+ F) o+ @ pThis->SetItem(&ItemData2);

delete ItemData1.pszText;* M, K$ O0 t! s# e5 @7 S/ k delete ItemData2.pszText; $ J3 d1 x# c) f8 ?: u! a+ u // 得到报表模式的列数3 G5 \' _5 z# C9 ?3 @# G4 r int nColCount = pThis->GetHeaderCtrl()->GetItemCount(); $ d. h9 i$ Z# P4 c" {* {; B0 j CString strBuffer; " e+ I l9 _6 i$ S2 X1 i# M1 [, m for (int iSubItem = 1;iSubItem < nColCount;iSubItem++) 2 T1 S3 Q3 l( d; f2 ~- t0 @ {0 {' x! G2 u/ s' p6 ^4 a" j strBuffer = pThis->GetItemText(nItem1,iSubItem);- Y' O1 A. F* a1 h$ ` pThis->SetItemText(nItem1,iSubItem,pThis->GetItemText(nItem2,iSubItem)); & g% ^; X4 O9 U1 i% B" A pThis->SetItemText(nItem2,iSubItem,strBuffer); 3 |% ]( Y8 x* d8 f5 m } , Q% N, Z* A3 u4 [% [ // 把原来的选中状态也转移过去 3 f! |9 P' I0 M6 j- b3 _ // 不知道为什么这个属性没有体现在LVITEM结构中…… 2 @1 W/ G2 U7 C. D bool bItemSelected1 = GetItemState(nItem1,LVIS_SELECTED) == LVIS_SELECTED;/ d/ m3 i7 {4 Z5 n bool bItemSelected2 = GetItemState(nItem2,LVIS_SELECTED) == LVIS_SELECTED;

SetItemState(nItem1,bItemSelected2?LVIS_SELECTED:0,LVIS_SELECTED);) P" f. |4 I8 r* q! F5 x5 p SetItemState(nItem2,bItemSelected1?LVIS_SELECTED:0,LVIS_SELECTED); - }; j( J$ `/ ]4 h return 1; w2 x: _. Z. G }

BOOL CListCtrlEx:reCreateWindow(CREATESTRUCT& cs) ( C- s* A' ?$ w{ 5 Q) j: q8 G( E; J9 X) I // 本类报表模式专用$ o$ R+ d, d: | cs.style |= LVS_REPORT ;5 b9 x. [4 x+ }9 O. J- } // cs.dwExStyle |= LVS_EX_FULLROWSELECT; 1 W+ {% X' C3 n. | return CListCtrl:reCreateWindow(cs); . r" K5 i2 C" D- N' i}

3 z$ F/ W2 j& e' ]+ g0 b- MCOLORREF CListCtrlEx::SetHighLightColor(COLORREF clrHighLight). @- w, K) @& r' @, J" m: W {& U) k8 o: n9 C9 h COLORREF clr = GetHighLightColor(); * L$ A( ^2 q% R ?. e5 F m_clrHighLight = clrHighLight;5 q( ^# k/ i c9 g! _ return clr;. x8 H, [9 K8 I Q6 W }

COLORREF CListCtrlEx::SetHighLightEdgeColor(COLORREF clrHighLightEdge) 0 z1 g; X* x. x/ h! ^{ 5 n3 t/ D; `& d+ \ COLORREF clr = GetHighLightEdgeColor();3 Y) z0 Y9 t+ b$ K3 b/ h' `2 p m_clrHighLightEdge = clrHighLightEdge; ' P) p' C6 b2 j# X, r( x, Z% g+ P+ C5 z return clr; , k; z, @6 B F" X% D) _}

该用户从未签到

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

COLORREF CListCtrlEx::GetHighLightColor(void) const u( s( U! r- s3 V{ & ]1 @4 t# F4 r- T# \3 C5 U return m_clrHighLight;! V9 R9 `3 f( K. ?5 y }

COLORREF CListCtrlEx::GetHighLightEdgeColor(void) const8 T. Q* ?' j1 D3 g$ W2 K6 M ? { * ^9 I. U4 l! F return m_clrHighLightEdge; 5 s! K$ p: y; q}

int CListCtrlEx::GetItemImage(int nItem)' |3 t4 U, Z: H# Q {+ ]* ?* {7 s, x5 X LVITEM ItemData; 2 j% m1 s5 Y; X$ W3 M# u ItemData.iItem = nItem; % |+ J6 D/ t5 |- G. q5 F! s ItemData.iSubItem = 0;* _6 m% [- I+ O7 y ItemData.mask = LVIF_IMAGE; & A# y( X4 U( P) c' V& x2 H pThis->GetItem(&ItemData);. \8 \9 N$ R" }4 q return ItemData.iImage; # X: h# t9 r: N* x& b+ M, s}

BOOL CListCtrlEx::MoveItemUp(int nItem) / T k2 m% Z# x8 j* j{# i: F( s j) ]; N if (nItem < 1 || nItem >= GetItemCount()) , W- N+ U9 l4 s) {$ q' E# } { 7 z+ l, r4 s- S/ U return 0;1 `0 T U( E. c+ [) z! t" S1 c# o6 g }7 b f) s' O$ g; Q. j5 r# C return SwitchItem(nItem,nItem - 1); y. C3 \3 d/ T% W7 k( V }

BOOL CListCtrlEx::MoveItemDown(int nItem)2 j3 J! k( {* a) S { : x ~0 Z h0 j" Y" l' J( q: p7 J if (nItem < 0 || nItem >= GetItemCount() - 1) ! p" c( b) m( T# a1 @' | {+ P8 U D: u" ^- s+ m. Y return 0; 3 B+ h% y- _6 ? }; y4 i; m1 r2 ~6 q- z" f+ B' M' S0 B# I return SwitchItem(nItem,nItem + 1);$ n7 g& o% M) p. c4 W2 Z }

void CListCtrlEx::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult) 7 T! H5 `# F3 Y: L{ 4 S( V I5 b6 c: |6 d- u NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR);

*pResult = CDRF_DODEFAULT;

if (pLVCD->nmcd.dwDrawStage == CDDS_PREPAINT) # {# J. i5 `# \# W# \ {1 i( t: E! A2 ^# D *pResult = CDRF_NOTIFYITEMDRAW;- T" J% m; U# U/ p9 q+ o$ B: z3 ^' C, \ } # K9 Q2 o0 \- C+ w: K( h. M# X% @! B else if (pLVCD->nmcd.dwDrawStage == CDDS_ITEMPREPAINT) + V1 s6 Q* `3 U: }! p! D) ` { ' o# d" c a7 F *pResult = CDRF_NOTIFYSUBITEMDRAW; 6 f# j( f5 C3 }3 Q& L }2 j# {, \1 z# g9 v% g: ~3 | else if (pLVCD->nmcd.dwDrawStage == (CDDS_ITEMPREPAINT | CDDS_SUBITEM)), f- g+ p0 ?: S {

int nItem = static_cast<int> (pLVCD->nmcd.dwItemSpec);& k& s1 L' M1 ]" R& W, w b& q int nSubItem = pLVCD->iSubItem;- {5 e7 z8 y. Z3 @: p if (nSubItem != 0) ) R% e1 K: e5 h w i: d {0 p( N! Q, v0 g0 Z+ p // 全部绘制过程在nSubItem == 0 时完成 , G C" K0 h ]+ M2 s/ z. X# N* C // 分布完成会有小问题,我无法解决 1 W1 G" w$ C% Y& @; s G *pResult = CDRF_SKIPDEFAULT; 6 @) V8 s; A# a4 N return;9 O8 Q1 g, k+ M# T }2 }/ u4 x; {% A9 _ CDC *pDC = CDC::FromHandle(pLVCD->nmcd.hdc);6 S1 `3 ?( R. X' p J5 Y int nColCount = pThis->GetHeaderCtrl()->GetItemCount(); + e1 ]( s+ a; w* [8 g! ^& ?; _/ R CRect rectSubItem;+ O! n2 d" b T. V7 O CString strItemText;$ T0 O% n( k& @/ V+ |/ ] pDC->SetTextColor(((ItemDataEx*)pThis->GetItemData(nItem))->clrItemTextColor); ) m1 b. c8 i! d pDC->SetBkMode(TRANSPARENT);: x6 F# Z7 U; a1 z // 相当于橡皮 / j+ Z' C! `( |. d CBrush brEraser(GetItemBackColor(nItem));

// int nFirstWidth = GetColumnWidth(0);

. G0 w0 \3 ?" } H( i" _8 _ for (int i = 0;i < nColCount;i++)% h+ @7 H) `: X2 O$ G' j) s9 R( n$ g% | { " |0 T5 M- P7 u3 P. X8 \, N5 v strItemText = GetItemText(nItem,i);

if (i == 0)1 {% a& j( B3 P3 f { 5 f$ C0 Y( f m1 V1 w pThis->GetSubItemRect(nItem,i,LVIR_BOUNDS,rectSubItem); 0 g3 [1 S4 V6 } Q L1 F$ z+ W1 ? // 擦掉原来的图形 7 m! e& P% M: B% m+ q! H# w* ] pDC->FillRect(rectSubItem,&brEraser);* n* o) E" B" c# B if (GetExtendedStyle() & LVS_EX_CHECKBOXES) & q8 X/ A8 P, H* t h7 j0 c q {) `% f2 ?" C4 Z9 t$ p% H CRect rectCheckBox; 4 F; I8 F( @+ \8 h# t% B, @' L GetSubItemRect(nItem,0,LVIR_ICON,rectCheckBox);6 M! X& x, ?. }! E. y+ A CRect::SwapLeftRight(rectCheckBox);" x6 A+ \1 r1 h8 T7 u2 b$ h rectCheckBox.left = rectCheckBox.right - rectCheckBox.Height();

rectCheckBox -= CRect(2,2,2,2);( ?1 [ U' S2 {0 H, o5 g pDC->Draw3dRect(rectCheckBox,0x81521D,0x81521D); ) o8 E3 ^, Y' k( g //rectCheckBox += CRect(3,3,3,3); ) S" g, p, l' h1 F) Y if (GetCheck(nItem))7 ^- B2 k3 y- i' Q! Y0 [ {& }- V- h! C( I5 ?0 N; F1 _ // 绘制一个CHECKBOX - ]: v9 n( o- N) {4 L4 l$ `9 p CPen penCheck(PS_SOLID, 1, 0x0021A221);' e; B# v- \( [" R: V: J1 S CPen *pOldpen = pDC->SelectObject(&penCheck);5 c/ S0 w- m$ ~7 t1 w7 q for (int j = 3; j < 10; j++) " A s$ s* W/ W' w4 j6 @ { 3 K& r5 l+ v- J% a pDC->MoveTo(rectCheckBox.left + j, rectCheckBox.top + rectCheckBox.Height() /2 - 3 + ((j < 6) ? j - 1 : (9 - j))); + j- T( X4 I d" Q! p" d# I! P pDC->LineTo(rectCheckBox.left + j, rectCheckBox.top + rectCheckBox.Height() /2 - 3 + ((j < 6) ? j + 2 : (12 - j)));' {& o# w& x( K, D" V" Z6 U }- Y" r: l; R5 |+ G$ f % l; T; f* A6 h j9 L k pDC->SelectObject(pOldpen);% B+ q: x( ^. y7 J }! u+ L2 ^) ]1 Q: s- r }8 w) t# a& m8 i' T- M% B' P // 根据需要画选中框# k' S* n8 @% n/ d! f, c if ( (GetFocus() == this || GetStyle() & LVS_SHOWSELALWAYS)&&(GetItemState(nItem,LVIS_SELECTED) == LVIS_SELECTED))- @/ {) U2 g0 q8 l/ G" Y {8 A* [8 m1 ~8 `% O$ V% k if (GetExtendedStyle() & LVS_EX_CHECKBOXES) ' w& \7 H" k g" X! i7 I. q: ] {// 存在检查框风格的时候需要对左坐标进行修正* N' Z" N2 }. q' I CRect rectLeft; V: f- e ]% v W GetItemRect(nItem,rectLeft,LVIR_ICON); 6 `; ^: v8 A( b2 J( f y( M0 Z rectSubItem.left = rectLeft.left;. |1 _8 l8 S* k |5 k } ( n6 a' c \ _5 x if (!(GetExtendedStyle() & LVS_EX_FULLROWSELECT)) 4 b6 T4 T( k) [& w: [( m. B {// 非全行选择时右坐标需要修正. m# i, k- ~) ^3 T" T CRect rectRight; 1 U( D/ T/ Z& ^ GetSubItemRect(nItem,nSubItem,LVIR_LABEL,rectRight); $ D# e& K/ U, P rectSubItem.right = rectRight.right;" X# d+ V' E% B7 S+ ` } 7 b/ B Z: A- Q( s% `3 b. v* o CBrush brHighLight;- }. W! c) b2 G3 B CPen penHighLightEdge;" B6 O$ v- s0 S' v/ f# F if (GetFocus() == this) + V9 D) q0 v0 I8 [8 {& @; i% U { ( {1 _, Y" C: S6 E brHighLight.CreateSolidBrush(m_clrHighLight); % \' o+ P6 o1 z) d penHighLightEdge.CreatePen(PS_SOLID,1,m_clrHighLightEdge); & q, \! S9 c) Z0 t6 c! ~3 K }& _; Y1 L9 y: Z& o6 j% {" ` else - E5 Q) h. \. E& w {' P& f: A& a7 J1 M6 \ // 不是焦点时,颜色70%不透明度8 L- o- x' m& ]7 Q/ Y2 X COLORREF clrBack = GetItemBackColor(nItem); 9 |8 f3 v A, {) |1 {/ h WORD red = GetRValue(m_clrHighLight)*0.7 + GetRValue(clrBack)*0.3;5 i# G6 v3 E7 a WORD green = GetGValue(m_clrHighLight)*0.7 + GetGValue(clrBack)*0.3; h; x0 B+ [; M+ H* H- q WORD blue = GetBValue(m_clrHighLight)*0.7 + GetBValue(clrBack)*0.3; $ ~9 g* u% y: B" V& P) R brHighLight.CreateSolidBrush(RGB(red,green,blue)); - R! A+ T0 E+ h* W4 D red = GetRValue(m_clrHighLightEdge)*0.7 + GetRValue(clrBack)*0.3; 0 z: e) k2 t4 X: n/ s! s green = GetGValue(m_clrHighLightEdge)*0.7 + GetGValue(clrBack)*0.3; 0 A9 h. g& |7 g7 Y blue = GetBValue(m_clrHighLightEdge)*0.7 + GetBValue(clrBack)*0.3;1 R3 O+ Y2 [& x0 _* l2 s0 H penHighLightEdge.CreatePen(PS_SOLID,1,RGB(red,green,blue)); & c* V7 v/ k" Z* N( A } 1 P" Y( [- V4 f% @, ~( s$ x& T8 c CBrush *pbrOld = pDC->SelectObject(&brHighLight);, n7 e) R! t8 d: X+ F0 c CPen *ppenHighLightEdge = pDC->SelectObject(&penHighLightEdge); , l( @8 J" X4 A+ N2 d! r8 d: g pDC->Rectangle(rectSubItem);" Q, T2 M, W( }8 \9 z pDC->SelectObject(ppenHighLightEdge); ) O' d, U2 f& ~# T pDC->SelectObject(pbrOld); ) x) `; B7 X$ ~ }+ Y; _) q) A: t7 g/ v } - U" k8 l: ?% S1 u! A+ G" R pThis->GetSubItemRect(nItem,i,LVIR_LABEL,rectSubItem);' V) O# K4 C7 V9 u# u UINT uFormat = DT_SINGLELINE|DT_VCENTER|DT_END_ELLIPSIS; n# |* ?5 O" N- Q1 \$ p; e HDITEM hdi; * h' I, G& s7 | hdi.mask = HDI_FORMAT;# m0 U- Q- w3 \* k pThis->GetHeaderCtrl()->GetItem(i,&hdi);' P) B, ^! b* G7 C+ J0 ?8 W, ^ if (hdi.fmt & LVCFMT_RIGHT)# u1 b) q4 E( `& U* t { w9 Q; X9 h1 z% B& Q5 ^' F uFormat |= DT_RIGHT; / K- z2 \( k d }. k6 }- t& t7 o& s" f& Z4 W/ m) C" k else if (hdi.fmt & LVCFMT_CENTER)) j# V/ P$ h+ t/ J* z: t { : E, ?0 ~2 ?2 P. \4 t8 |+ R& \ uFormat |= DT_CENTER; - f8 w2 `1 N0 W: b9 T* }+ U }; J& G! O4 ~/ }, h, e n7 s pDC->DrawText(strItemText,rectSubItem,uFormat);

CImageList *pImageList = pThis->GetImageList(LVSIL_SMALL); : e3 f( `8 t; D if (pImageList)0 L- L- d9 C; Q/ R) s# K {9 W5 z' b0 `: Z3 @+ C CRect rectIcon;, x% v3 `1 J1 ?. u$ P GetItemRect(nItem,rectIcon,LVIR_ICON); $ O4 e# K: p0 l pDC->DrawIcon(rectIcon.left,rectIcon.top,pImageList->ExtractIcon(GetItemImage(nItem))); % h3 n0 D1 H2 p/ O }: d+ E+ {% J9 R; ~$ }' _% a }7 Y5 |2 x) ~# @ U3 ~4 \$ V *pResult = CDRF_SKIPDEFAULT; + c5 W& x1 b A' l8 x3 S) J; @9 N7 p, X }

}

该用户从未签到

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

本版积分规则

关闭

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

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