下沙论坛

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

QQ登录

QQ登录

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

又是垃圾,CListCtrlEx Base CListCtrl

[复制链接]

该用户从未签到

跳转到指定楼层
1
发表于 2005-2-23 21:29:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
// ---------------------------------------------& z: [0 d3 A- T) R! a: j2 k' e
// ListCtrlEx.h
% E" M4 V, r% B* p& Z$ T6 P8 ?' ~// ---------------------------------------------2 l' d7 T1 L2 d" D' ?. u% b' L
#pragma once
: t, a( M  S# }# }; |+ I" g& v& @1 c
// CListCtrlEx2 E2 `4 |* q# U( Y2 }5 H- B" S# S
class CListCtrlEx : public CListCtrl( V  G( `" V& Q0 c* d
{
9 p! P/ D+ G: f    DECLARE_DYNAMIC(CListCtrlEx)- a/ o) E# C- a; O% Y( s
public:
* A0 ?. S. b) k+ L+ |    CListCtrlEx();
5 v5 t: u' h/ N) Z' |9 T    virtual ~CListCtrlEx();
% i2 i" \+ l0 K- x. _protected:( T8 R( O1 w4 ]) {7 K
    DECLARE_MESSAGE_MAP()6 |  J9 \' d; A/ p, L* E4 o7 o8 S
    CListCtrl* pThis;
. A. h2 q: m( q8 |9 R2 x    virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
4 f9 j9 `4 l& H/ n7 y2 o, m4 oprivate:
( }; n% @: U$ v    struct ItemDataEx {) K/ t7 J% @6 h# d: D1 q
        LPARAM SrclParam;& r, D1 x6 T9 v, A7 q# ^
        COLORREF clrItemTextColor;5 H7 S# z/ B1 J5 E7 t, v/ Q8 r, h
        COLORREF clrItemBackColor;
7 v' y" R+ I  O1 k    };
7 y7 N8 y3 E4 V: o) q    COLORREF m_clrHighLight;
! ^! d% j& [" G- h: C  [0 {5 w    COLORREF m_clrHighLightEdge;
5 `! I/ t& M3 u6 V7 ]public:6 T0 ?/ A* r  G. V2 W
    // 新增方法
5 N) W/ l9 ]' k    COLORREF SetItemTextColor(int nItem,COLORREF clrText);5 m! k% b1 i0 }$ x2 D: V/ [  w( W
    COLORREF GetItemTextColor(int nItem) const;
2 v' }) ~  S' S. H! a3 Q    COLORREF SetItemBackColor(int nItem, COLORREF clrBack);
. g( N0 V2 G# E: }) a! }+ S; N    COLORREF GetItemBackColor(int nItem) const;8 w7 D8 ?! v4 |* d/ r
    COLORREF SetHighLightColor(COLORREF clrHighLight);( v6 f  E- X3 a0 c  w: m
    COLORREF SetHighLightEdgeColor(COLORREF clrHighLightEdge);
/ ^' S* Y8 {. d' I+ X% [) u    COLORREF GetHighLightColor(void) const;( n6 `4 @. e; j' o5 |
    COLORREF GetHighLightEdgeColor(void) const;3 q2 P- p0 }  Q/ }9 `. Q0 z, @" x
    int GetItemImage(int nItem);0 c* [! z. I8 n' b$ Y
    BOOL SwitchItem(int nItem1, int nItem2);
, X! P: o- E7 ]7 o    BOOL MoveItemUp(int nItem);
- N% z, ^% I& }' G0 Y, a    BOOL MoveItemDown(int nItem);3 ]: H7 V- ?3 I  W) ?
    // 重写原来的方法8 s9 [# {( C- N! ^( N( X
    DWORD_PTR GetItemData (int nItem) const;% a( k2 Q1 X7 E# U& s
    BOOL SetItemData (int nItem, DWORD_PTR dwData);( k) k7 M. }0 {
    int InsertItem (const LVITEM* pItem);; g7 D" R0 U# S1 y' p& `" s" k8 Q
    int InsertItem (int nItem, LPCTSTR lpszItem);. t4 l  U* \" Q* i
    int InsertItem (int nItem, LPCTSTR lpszItem, int nImage);) |# _) _, Y  E$ P. O, p0 P
    int InsertItem (UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam);1 @5 F) T3 P/ {
    BOOL DeleteItem (int nItem);& r0 ~" j3 S) R" @. t1 X/ ~1 L+ q
    BOOL DeleteAllItems ();! x9 }6 g- ~% b0 k4 c
    BOOL GetItem(LVITEM* pItem);
8 Z' d6 C& e* m% t  O    afx_msg void OnDestroy();
) M* b: z: W" `- |5 }: m    afx_msg void OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult);
2 n5 Q6 P$ c7 z};
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 顶 踩

该用户从未签到

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

// --------------------------------------------- 2 K( N3 z% h5 x+ ~// ListCtrlEx.cpp 6 j' L+ o% k, L$ ]$ n& e6 N// --------------------------------------------- $ D" u! C0 `- P' b// ListCtrlEx.cpp : 实现文件 4 G3 `3 t( x% U% F//

#include "stdafx.h") r3 a3 z. U8 a2 d' z! r7 R% b #include "CreateNewClass.h"/ }1 q2 p) x; K- l5 V: h #include "ListCtrlEx.h" & W" R/ E2 I% R; {: ^#include ".\listctrlex.h"

0 B7 c0 ]* ]5 x5 M4 P( N1 G1 [// CListCtrlEx

IMPLEMENT_DYNAMIC(CListCtrlEx, CListCtrl) : N( M' ^5 J+ i0 m: m7 f* iCListCtrlEx::CListCtrlEx() : j7 ]2 C3 h0 a6 O. Y/ o{2 X% |. r0 v g5 f$ y( @6 M // 用于调用基本的方法 ) y: @+ Z4 R$ B$ w, R& J pThis = (CListCtrl*)this; 0 k# r6 [" \* i. w" V4 o+ J // 默认的选中框颜色& F0 m( C! x u% K1 ?% y m_clrHighLight = 0xF1DACC; - [" ] k4 c5 r; _' h( E m_clrHighLightEdge = 0xC56A31; 3 g9 [9 K1 g3 k/ p$ _% z}

CListCtrlEx::~CListCtrlEx() ' I2 ~3 P0 P+ M, q{

}

2 m) y3 B/ ^1 t; o* I BEGIN_MESSAGE_MAP(CListCtrlEx, CListCtrl)# R1 l. C- J' h; E. M& I3 u: w4 T3 C- F ON_WM_DESTROY(), I: P. q9 v" M3 n5 J$ f ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnNMCustomdraw); M" Q3 L% m/ k, [$ k5 p' } END_MESSAGE_MAP()

// CListCtrlEx 消息处理程序

9 i% _6 z3 M+ t3 M8 W) NCOLORREF CListCtrlEx::SetItemTextColor(int nItem,COLORREF clrText) M6 E6 s; h& J% m I/ E { / e' K4 z2 E9 `3 k COLORREF clr = GetItemTextColor(nItem);1 o! B G0 f( Q2 a/ v1 \& c ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);* O, B2 M- r- I* p$ ? pData->clrItemTextColor = clrText; # e9 M, F Y8 D: u; x5 y return clr; & _) z. ?, G* s8 p& X. Z}

COLORREF CListCtrlEx::GetItemTextColor(int nItem) const. {/ c# n8 g }% q { $ Q' l& ^! L/ d5 x/ a ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem); + T% }, m$ U0 P" r6 {3 B$ x return pData->clrItemTextColor;, b6 h$ |- m4 t+ e& A }

COLORREF CListCtrlEx::SetItemBackColor(int nItem, COLORREF clrBack) 0 P$ Q1 a7 |% Y7 f{4 Z# [9 v3 n& Z* k, ] COLORREF clr = GetItemBackColor(nItem); . h( w: c; Y1 x& O5 y0 f ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem); + _+ B k: a7 t# J) h& V# [ pData->clrItemBackColor = clrBack;! ~: `% W, R& W) M; a) c/ W return clr; ) f' n0 V. r/ m U}

COLORREF CListCtrlEx::GetItemBackColor(int nItem) const 9 N/ ]" u3 f, B0 S: K4 e$ r{ ; a" p" P) q& ~) O6 ~ ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem); * Y* Y6 s& R& y& |: x, j9 O return pData->clrItemBackColor; 2 M/ J' P, h3 n2 D7 H! N% K' z ?}; t5 d% h$ |! W: S2 Y; F DWORD_PTR CListCtrlEx::GetItemData (int nItem) const% d9 u7 C7 |' D$ T6 _7 d/ m {; P7 U+ y0 g; e2 Q- I if (nItem >= GetItemCount()) ; v2 w( W4 ~2 S {6 G: `1 E2 }" Q1 A' Y7 w return 0; 9 L) `7 T3 Y" k) K+ _ } 8 O: S/ u: v+ v7 @/ K8 X3 P CListCtrl* pThis = (CListCtrl*)this; 6 W7 Z2 ~/ G, ]- a DWORD_PTR Data = pThis->GetItemData(nItem); " a$ ^; h) C$ b& s8 C& K4 y5 _ return ((ItemDataEx*)Data)->SrclParam;2 V9 g# ]; O% I5 s' e% n } % p2 _# r3 t1 e& G- yBOOL CListCtrlEx::SetItemData (int nItem, DWORD_PTR dwData) " C: }5 J* f: n3 e{ 3 o2 p/ @' }4 D- e3 B1 {% Y if (nItem >= GetItemCount()) ! T" {' G/ c5 i9 S { # P1 I+ Y# v! v! ^2 ?' d1 l return 0;5 S4 N! E6 A/ N( Y! E& s } + _- O+ V r) w! |2 d) } CListCtrl* pThis = (CListCtrl*)this;* V0 F1 F! a4 q# C L# I DWORD_PTR Data = pThis->GetItemData(nItem); : F6 z2 F. N! ]" w8 L9 p ASSERT(Data);( l+ M! M! ?+ N( I ((ItemDataEx*)Data)->SrclParam = dwData;& N4 e# B o5 D: I" m return 1;/ I2 M/ `. M8 w# {8 A } 5 V1 x0 ^, ]: S2 \7 `int CListCtrlEx::InsertItem (const LVITEM* pItem)/ i/ o' K1 H5 W4 x- d { 7 U8 f9 q& k( t" P4 e# C int nReturn = pThis->InsertItem(pItem);6 s. H. N1 Z; e4 m- p ItemDataEx *pData = new ItemDataEx;' k8 X+ v1 l7 K // 设置默认颜色% ?# Z7 H% u# M pData->clrItemBackColor = 0xFFFFFF;//pThis->GetTextBkColor(); 4 p8 W+ ?. \- x1 D2 ]4 z; k! Q pData->clrItemTextColor = 0x000000;//pThis->GetTextColor(); `5 `5 a( @$ u% e' Y5 e& j pData->SrclParam = pItem->lParam;& p: G0 S# F& P' d, h) } pThis->SetItemData(pItem->iItem,(DWORD_PTR)pData); ! b8 A6 E# B+ L3 Y return nReturn; 5 r% S a A4 D( X3 @: k6 {% n- f0 Y, f} 7 O& E: u! g& S+ I8 l, r9 \8 a; C- T, aint CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem), ~( D8 @5 q2 m+ b! p% E! p { " m0 O5 G7 d- T& m7 J LVITEM Item; ( \( o6 [- v P6 Z) E. \5 \ memset(&Item,0,sizeof(LVITEM));0 [% }# s( s6 e+ {2 c% t1 ~8 j Item.pszText = new TCHAR[strlen(lpszItem)+1]; ; U- L% @: v! d ] strcpy(Item.pszText,lpszItem); ! E4 d P8 ~' V Item.iItem = nItem; * j! W; _2 _9 F0 }! W5 q Item.lParam = 0;! i! ]: Y# b0 U. ` Item.mask = LVIF_TEXT |LVIF_PARAM;' X; z0 O7 U# U4 V0 ^3 K3 k BOOL bReturn = InsertItem(&Item);) {6 {5 `3 J" `* w5 B$ D4 G delete Item.pszText; & ~! W! m. @; ~9 a# ~( c return bReturn; - d2 f- U) R/ y% Q! @* A+ Z3 R} 7 n& Z7 Q. W* @+ Fint CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem, int nImage) ( O, y1 V) v0 v, h{ & P! Q2 u" E2 O) p: e( M LVITEM Item; 0 T b5 }0 p9 {$ o memset(&Item,0,sizeof(LVITEM)); 0 p+ M: I) Y# @ Item.pszText = new TCHAR[strlen(lpszItem)+1];- I/ y9 z5 f* h/ \" f strcpy(Item.pszText,lpszItem);& i$ @% y; e$ x0 { Item.iItem = nItem; O# v' q( u( P E Item.lParam = 0; 2 K* _% E2 q5 R' M& H Item.iImage = nImage; 1 C6 x4 B# _6 `9 F; N7 m1 T Item.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE;8 l/ z- y1 P$ E7 O x1 O7 G BOOL bReturn = InsertItem(&Item);: o3 O8 {! J1 F9 j9 c0 E1 h/ A delete Item.pszText; . A( X% p0 f6 b return bReturn;; q7 k; o* Y0 p. \+ V }1 S9 {$ K- }- A) n int CListCtrlEx::InsertItem (UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam) ! L7 y! e, k/ n X2 e) |{" @& x3 }" d' Z. k( x LVITEM Item; 0 O! O# r5 T6 y) B, h memset(&Item,0,sizeof(LVITEM));# E& ]8 C$ Z# A Item.pszText = new TCHAR[strlen(lpszItem)+1]; 8 m8 H; }* F% s+ J strcpy(Item.pszText,lpszItem); " V |0 z' d3 Z/ ^7 _+ S Item.state = nState;) H( I) u4 K3 Q: H0 Z) q+ J; V Item.stateMask = nStateMask; & |% E1 j- R' p- U# f& L Item.iItem = nItem;1 }6 V& W6 g0 W3 e1 a: f/ {/ r( h Item.lParam = lParam;$ o+ D4 @7 ?2 Z! D Item.iImage = nImage;% j( v* \. ~0 S. L Item.mask = nMask;$ M& B$ x' _0 |- I9 _! I# ~ BOOL bReturn = InsertItem(&Item);7 m8 k* l( G: s$ S3 X delete Item.pszText;% I( @# z+ K& L4 A( r, d5 J8 K return bReturn;& W4 w) T: j r8 X } + o4 A* Q: }0 qBOOL CListCtrlEx:eleteItem (int nItem) 5 U5 S4 M$ } F3 D V" f" }{& a" j4 z" F x m7 W' v void* pDataEx = (void*)pThis->GetItemData(nItem); 9 \: d8 e7 A8 G% ?" I/ O7 ? if(pDataEx != NULL) Z' m1 F& C1 f+ v9 Y& ^3 e" L, J {" D+ ` j+ w& `( d8 o! L- O# v delete pDataEx;7 {4 D* C$ s! g( e* x } 7 P) { O; F2 U5 _ return pThis->DeleteItem(nItem);% U! B# ]1 O$ } E8 V }5 a) w$ Y$ u( ?8 \" C BOOL CListCtrlEx:eleteAllItems () 2 D( p3 {/ t2 L+ F% e{ J. \# I& }2 {+ M; g1 A! @( X int nCount = GetItemCount();' G- Q! ]7 U7 T* j7 m' T$ d for (int i = 0;i<nCount;i++)+ t, m0 _( I; V4 F { 2 w& x1 D& d4 f" P9 O if(!DeleteItem(0))! Z! s# x5 p9 x# r3 @& C# i! m {8 V$ N3 i. M/ L7 k+ p5 Z& ` return 0;5 t3 v! E( Z$ y" ]3 G } 8 _9 W7 i9 K3 E" T$ ^ } 1 F9 B m% M9 B3 v* \6 _( t return 1;' t6 [3 t; a( ^* _ } 6 l% Q) r6 H, n2 q4 N, r5 pBOOL CListCtrlEx::GetItem(LVITEM* pItem) 0 u9 m4 M0 G) o2 V7 d4 [+ M% j0 `6 _{ 4 C* y! J5 H) {" }5 k- k. b# j BOOL bReturn = pThis->GetItem(pItem);& E) A; k4 K; u, V if (pItem->lParam)$ K1 H+ j+ @' F { # \3 Y, \/ t/ h4 o f* b pItem->lParam = ((ItemDataEx*)pItem->lParam)->SrclParam;8 ^8 R T. O7 l8 V; a }

return bReturn; / Q% z% X7 Z1 e}2 j: r; V% Q4 D void CListCtrlEx::OnDestroy() 4 p& i3 O/ M0 P, y, A6 J{ ; ^9 n$ B v2 N6 z& G // 关键是清除申请的内存空间& d" ^+ p/ P1 Y4 S1 e1 T- b/ @; B& N DeleteAllItems(); , J/ R: W6 B' h* R) c7 |0 `* z, M CListCtrl::OnDestroy();; L5 ^# _5 T* @$ C: | }

BOOL CListCtrlEx::SwitchItem(int nItem1, int nItem2) 0 p6 P6 e& Z" g+ \* [{ 7 V, P3 e% c/ ^5 @7 Z2 P if (nItem1 == nItem2 || nItem1 >= GetItemCount() || nItem2 >= GetItemCount() || nItem1 < 0 || nItem2 < 0) - @: J% m" n$ _/ Z- O; j& b; H { . E4 _* L+ v; r6 B) ~ d return 0;8 o2 m0 c( }( [: n: f2 } } 0 L r0 t- Y# z$ J6 ] LVITEM ItemData1,ItemData2;% A; C/ Z- ~; c! c" K2 K* z7 T$ I int ItemLength1 = pThis->GetItemText(nItem1,0).GetLength();6 J0 O4 H# Y" P* ] int ItemLength2 = pThis->GetItemText(nItem2,0).GetLength();. ?/ O t2 J/ K+ O: F( S- S# | ItemData1.pszText = new TCHAR[ItemLength1+1]; + B. ~" }" h) A: G F ItemData2.pszText = new TCHAR[ItemLength2+1]; u0 e8 d3 O2 v8 J: S- ~ ItemData1.cchTextMax = ItemLength1+1;4 O- c% Y. r1 r" \. Z* J( M- s& P1 I ItemData2.cchTextMax = ItemLength2+1;" e! d. E6 H5 R8 I6 B1 S4 I ItemData1.iItem = nItem1; ' V: N! P3 P' z I( x4 h* U ItemData2.iItem = nItem2;7 F: z2 F0 I E% M ItemData1.iSubItem = ItemData2.iSubItem = 0;% `9 b$ b8 c1 ?2 C$ C3 b: x* D ItemData1.mask = ItemData2.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE|LVIF_STATE;

pThis->GetItem(&ItemData1);4 R/ C6 t+ c. R- i/ h k) ` pThis->GetItem(&ItemData2);) P; M3 F- i9 m" L+ o- v // 交换数据 ' ?: g! E0 K7 w" o b ItemData1.iItem = nItem2; 5 B+ j3 q- Z4 Q7 m ItemData2.iItem = nItem1;. Z0 X) Q5 q& K0 B; ?( r pThis->SetItem(&ItemData1);" l* ^+ ]" \- C/ q/ c* d2 \/ a pThis->SetItem(&ItemData2);

delete ItemData1.pszText;$ C( k7 U* y1 L9 p* f( N- j delete ItemData2.pszText;. P8 w+ V# }3 X& z$ I' {9 q9 V // 得到报表模式的列数 6 ^1 _( i7 O% i2 m; i int nColCount = pThis->GetHeaderCtrl()->GetItemCount();' R6 d2 d0 v/ e( v* h; m) p CString strBuffer;0 ~4 y# L' s9 c1 m for (int iSubItem = 1;iSubItem < nColCount;iSubItem++) 5 v: A& G' l# |) \+ {- c N3 {% H1 P { K# M( D# R& H strBuffer = pThis->GetItemText(nItem1,iSubItem); 1 s( v0 W7 T# {% d pThis->SetItemText(nItem1,iSubItem,pThis->GetItemText(nItem2,iSubItem));7 R5 P! a" c. h' j pThis->SetItemText(nItem2,iSubItem,strBuffer); # H+ w R+ k# c } & T# @5 |( F1 L! A7 K/ H$ z // 把原来的选中状态也转移过去: s3 _& v+ A( x e% _ // 不知道为什么这个属性没有体现在LVITEM结构中…… + r6 F1 `% l* R3 G bool bItemSelected1 = GetItemState(nItem1,LVIS_SELECTED) == LVIS_SELECTED; 7 [& G3 p6 v% t3 A; f2 p$ O2 y0 U bool bItemSelected2 = GetItemState(nItem2,LVIS_SELECTED) == LVIS_SELECTED;

SetItemState(nItem1,bItemSelected2?LVIS_SELECTED:0,LVIS_SELECTED); ! u# P6 F( k, h0 m: F# ^ SetItemState(nItem2,bItemSelected1?LVIS_SELECTED:0,LVIS_SELECTED);' y: e2 ?) m/ G1 m return 1; : z( n8 g4 n" U& k9 Z0 F/ f}

BOOL CListCtrlEx:reCreateWindow(CREATESTRUCT& cs) ! O5 D( O: D& A: d, }- ?9 U+ [{ 5 A. p( a7 @! @, | // 本类报表模式专用 : p, v1 W7 h5 c P { cs.style |= LVS_REPORT ; # M% B* [) [ `2 P& Y // cs.dwExStyle |= LVS_EX_FULLROWSELECT;7 k6 j" s7 Z* c! v; r; l return CListCtrl:reCreateWindow(cs); 1 C7 o2 \# V7 C. B' c! z}

6 ~1 }# `& X& \COLORREF CListCtrlEx::SetHighLightColor(COLORREF clrHighLight)) |7 N: i9 N: l1 [& C {; z( \ X! P% t7 U& ?1 a COLORREF clr = GetHighLightColor(); & a/ ~' d H2 d' q% J; ] m_clrHighLight = clrHighLight;4 D& w5 r0 d; l+ m& x return clr; 3 U3 \ {2 J( d3 g H% P: L}

COLORREF CListCtrlEx::SetHighLightEdgeColor(COLORREF clrHighLightEdge) / ^- m8 B7 D3 k+ w) ]{ / X6 o/ O5 }" Y' d0 B COLORREF clr = GetHighLightEdgeColor(); ! [; T% r# F+ E2 L7 a- s m_clrHighLightEdge = clrHighLightEdge;0 A$ G) p8 T' L$ i; g return clr; ( |; C" P* L3 ~0 h9 T) v. f6 s% v: `}

该用户从未签到

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

COLORREF CListCtrlEx::GetHighLightColor(void) const 4 T4 T( V p& n2 P( p z* G0 _' O8 K{9 C) u2 g% q$ h return m_clrHighLight; 3 N% A" [( a, O}

COLORREF CListCtrlEx::GetHighLightEdgeColor(void) const" S6 N# g' c* L+ f' K) U { , K: }# F+ z- ^6 O& ~ return m_clrHighLightEdge;4 l' d2 k0 s+ W0 w* D }

int CListCtrlEx::GetItemImage(int nItem) ; |( L* E. g" ~ p{0 e8 ^7 Y4 q& v9 E LVITEM ItemData; + |8 u% L J# m% A( k3 ^! Q ItemData.iItem = nItem;* }, C8 ?( D4 J$ O2 E ItemData.iSubItem = 0; T6 @ {, |( _8 b ItemData.mask = LVIF_IMAGE; 1 K5 O* C# e! V* g; |' t pThis->GetItem(&ItemData);2 M4 P4 B# K" x! J2 q& w return ItemData.iImage;' v8 @: V- X! Q }

BOOL CListCtrlEx::MoveItemUp(int nItem)7 \+ O/ a" q, }/ H3 x6 n {6 Q* m4 F- }% f if (nItem < 1 || nItem >= GetItemCount()) 4 j( Z2 ^5 Q9 }& C Z { # x6 _9 ]4 l* h! w return 0;/ _5 I8 u( z, u7 w+ q, e$ t } ' H; T: Y" F) `6 |" k return SwitchItem(nItem,nItem - 1); . a6 M) E% U1 z- i}

BOOL CListCtrlEx::MoveItemDown(int nItem)1 |$ h0 d3 s; Y' f. W' X { ( c8 `1 {# R% n- M, K6 Z8 j if (nItem < 0 || nItem >= GetItemCount() - 1)1 E' ~$ |$ W" ~$ x. N! z { 3 ~' ?7 ]& J G5 y8 ~; j return 0; 7 [: J3 p0 F* A9 c. P9 I B% \' e }) W! O8 n, L0 [) R7 m return SwitchItem(nItem,nItem + 1);6 K$ c" \* O; C @% A }

void CListCtrlEx::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult): X( D0 R0 g7 u/ d {' F4 l3 _1 s7 p/ C" K( E$ v NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR);

*pResult = CDRF_DODEFAULT;

if (pLVCD->nmcd.dwDrawStage == CDDS_PREPAINT)" m) f& Y& Y% @, v { $ ]/ G1 N4 O$ q& r: t *pResult = CDRF_NOTIFYITEMDRAW; K; q2 k* H2 ^9 z } $ _! V3 O% _" q else if (pLVCD->nmcd.dwDrawStage == CDDS_ITEMPREPAINT); O6 j7 \# ^- a' b) L, T" Y { ' A* B4 D6 t6 t% P' s9 C0 M( I *pResult = CDRF_NOTIFYSUBITEMDRAW; B: o1 f u1 g" l' E8 J; [ }& e" r( i0 F4 o( _5 c else if (pLVCD->nmcd.dwDrawStage == (CDDS_ITEMPREPAINT | CDDS_SUBITEM)) a8 C0 t; d/ }# y. R% I% n: k" d {

int nItem = static_cast<int> (pLVCD->nmcd.dwItemSpec);; N8 I5 K6 `3 ^ int nSubItem = pLVCD->iSubItem; $ b. {& G. B+ Y$ m4 n7 G+ P if (nSubItem != 0)- \: ^$ s) D5 I) ~* s/ z3 `' N { # O! i- T7 s8 n& y0 W" b // 全部绘制过程在nSubItem == 0 时完成 7 o& _2 | F- `3 A* r2 U# m4 N // 分布完成会有小问题,我无法解决 ) t- P6 q6 u+ A6 i# P! q# d *pResult = CDRF_SKIPDEFAULT; 2 {5 @/ n9 Z3 _& d2 G return;, A+ x7 A( U5 W+ j; h5 N } ; b, e3 m% J4 i1 {& i4 b CDC *pDC = CDC::FromHandle(pLVCD->nmcd.hdc);: j4 z3 Y) G9 u2 s( \0 N int nColCount = pThis->GetHeaderCtrl()->GetItemCount(); 9 D, @) m# T2 `) Z8 e CRect rectSubItem;) y- b6 B5 ?2 T CString strItemText; # W7 e1 i6 k5 w) V* \) G, c" m' L& z pDC->SetTextColor(((ItemDataEx*)pThis->GetItemData(nItem))->clrItemTextColor); # E! M9 o' [% q; G pDC->SetBkMode(TRANSPARENT);, `, l S: w0 S // 相当于橡皮 6 h- A2 i7 R; b; x( n3 c CBrush brEraser(GetItemBackColor(nItem));

// int nFirstWidth = GetColumnWidth(0);

( i0 R2 }* q8 _0 y4 P for (int i = 0;i < nColCount;i++) : Y/ v9 P, j+ v& c; F { 7 ?3 m. }5 N/ ]4 U r) j0 B' ~ strItemText = GetItemText(nItem,i);

if (i == 0) 4 P3 e* U9 D Q1 _ {' G; K# ]1 C! q' K3 l pThis->GetSubItemRect(nItem,i,LVIR_BOUNDS,rectSubItem); ' M9 x2 D, d+ A" T I( I // 擦掉原来的图形 4 R$ O2 K% M8 S8 O6 S8 n+ g pDC->FillRect(rectSubItem,&brEraser);3 Z1 a- s9 A7 c' e' B% V3 ?- Y if (GetExtendedStyle() & LVS_EX_CHECKBOXES) 8 T7 h y1 m3 y4 t; K' p0 n { - i' ]4 J+ g/ k% @! n2 C& \ CRect rectCheckBox; 1 A W8 Q7 K$ F6 z; q7 V m& s- l4 ?2 a/ w GetSubItemRect(nItem,0,LVIR_ICON,rectCheckBox); 0 c0 R. Q) u) E1 _/ S9 p CRect::SwapLeftRight(rectCheckBox);# ^ l* b4 P8 m1 i- E: y& D+ s5 C- g8 L rectCheckBox.left = rectCheckBox.right - rectCheckBox.Height();

rectCheckBox -= CRect(2,2,2,2); # K( F; a- [* V8 l- N5 L/ ~4 e pDC->Draw3dRect(rectCheckBox,0x81521D,0x81521D);4 p h6 T1 U' L3 o* ^7 d& P //rectCheckBox += CRect(3,3,3,3);% {6 ^+ [" t1 n1 a if (GetCheck(nItem))3 A! |& \1 j5 N- M1 w& X6 l { , ]7 r5 I6 w3 h4 W9 Y/ L // 绘制一个CHECKBOX 6 v6 i3 S x0 }6 f6 e- @ CPen penCheck(PS_SOLID, 1, 0x0021A221);, l0 N+ ]& j2 B5 B CPen *pOldpen = pDC->SelectObject(&penCheck);( }# v. S1 i6 _" i4 @ for (int j = 3; j < 10; j++), [7 s/ @+ C. {5 J' l0 \ {# A! O% Q4 [* S" @ pDC->MoveTo(rectCheckBox.left + j, rectCheckBox.top + rectCheckBox.Height() /2 - 3 + ((j < 6) ? j - 1 : (9 - j)));8 ~: i6 U% E5 l5 j' V pDC->LineTo(rectCheckBox.left + j, rectCheckBox.top + rectCheckBox.Height() /2 - 3 + ((j < 6) ? j + 2 : (12 - j))); 5 g/ ]1 R0 W- h w- `9 x }1 L8 I- @+ j, ]- Y( d % I: r y( } P: u: _' ] pDC->SelectObject(pOldpen); 1 S3 r G ?( ]! Y6 _7 | }* f1 g F5 @+ [( r } / |0 O# {) ]( z% l7 q // 根据需要画选中框 & }$ O3 A5 C6 h8 o5 h7 p if ( (GetFocus() == this || GetStyle() & LVS_SHOWSELALWAYS)&&(GetItemState(nItem,LVIS_SELECTED) == LVIS_SELECTED)) / ^! e" A( r# o0 E. T {0 _* y# \! n! |2 V% i+ a( Z1 K if (GetExtendedStyle() & LVS_EX_CHECKBOXES)8 v7 W0 ?5 f: ?' O {// 存在检查框风格的时候需要对左坐标进行修正 & M( `" b- k/ q' k CRect rectLeft; 2 M3 T1 J. y: s- E! J# W3 V+ O GetItemRect(nItem,rectLeft,LVIR_ICON);/ u% f3 P" x8 D2 O- ^ rectSubItem.left = rectLeft.left;. p- X7 W: I4 j! O& f& ^, U } . Q' L' c3 `5 G2 l- L/ n if (!(GetExtendedStyle() & LVS_EX_FULLROWSELECT))5 d4 O6 z( g1 ~ j1 _ K {// 非全行选择时右坐标需要修正 - G0 J4 c" ~7 w* D CRect rectRight; 4 [1 n0 u0 F9 {+ Q$ @7 G GetSubItemRect(nItem,nSubItem,LVIR_LABEL,rectRight);7 w' `! C+ t* y$ d8 v* {) W. b5 V rectSubItem.right = rectRight.right; 2 u! k# }. K _4 k: K }% l; Z W/ y! t' F5 F$ U CBrush brHighLight; + A7 P; d( u. O( M1 W1 S CPen penHighLightEdge; v1 b2 f+ w: C' _+ @ if (GetFocus() == this) % A6 B2 k6 n% c1 Z& _ {& r8 ^; w) K; w$ p) [9 N: y brHighLight.CreateSolidBrush(m_clrHighLight); 9 `: P, f S2 ?, K5 D, H# \% D5 }. s penHighLightEdge.CreatePen(PS_SOLID,1,m_clrHighLightEdge);, I# g+ @$ k- w' l! U+ ` }" M! W: S- J* {: P2 |$ v else : g2 [. a# _% D% `( C: d { 8 Y( b2 D+ K! G$ o/ J3 _ // 不是焦点时,颜色70%不透明度0 x' Z4 r" w+ k& B( G7 A4 x COLORREF clrBack = GetItemBackColor(nItem);# _+ R9 r2 g8 {4 |3 f A) U WORD red = GetRValue(m_clrHighLight)*0.7 + GetRValue(clrBack)*0.3;8 b/ j5 l4 z h8 Z0 c# m, t WORD green = GetGValue(m_clrHighLight)*0.7 + GetGValue(clrBack)*0.3; 1 Y4 \' a! S; ], Q WORD blue = GetBValue(m_clrHighLight)*0.7 + GetBValue(clrBack)*0.3;. ?% X2 F* H& M brHighLight.CreateSolidBrush(RGB(red,green,blue)); 2 d5 e# _5 d6 {4 G7 N. K red = GetRValue(m_clrHighLightEdge)*0.7 + GetRValue(clrBack)*0.3;5 l1 {! ?* K. h- h$ I green = GetGValue(m_clrHighLightEdge)*0.7 + GetGValue(clrBack)*0.3;6 f( ]6 u0 y* j5 j7 w blue = GetBValue(m_clrHighLightEdge)*0.7 + GetBValue(clrBack)*0.3;+ O& O+ M5 K: d( P+ `8 V8 C penHighLightEdge.CreatePen(PS_SOLID,1,RGB(red,green,blue));* n& t+ j e7 c. ^ }# ]& d5 ?* B) x: |* E) \7 t; B4 K CBrush *pbrOld = pDC->SelectObject(&brHighLight); @6 f: z1 ?' H1 u3 K CPen *ppenHighLightEdge = pDC->SelectObject(&penHighLightEdge); ) f: ~6 D* G! y0 X9 n5 B pDC->Rectangle(rectSubItem);' I Q4 N6 s% h$ l. t, R) Q. d pDC->SelectObject(ppenHighLightEdge);$ s6 ]) z: k3 O& @ pDC->SelectObject(pbrOld);/ D: g2 h! V' j( i4 \# Y }0 \9 u; T# x% h7 E2 q5 o } % K7 C6 ?$ K; D9 N+ _ pThis->GetSubItemRect(nItem,i,LVIR_LABEL,rectSubItem);) P# d* }' t8 v, z# W# q q1 z! ] UINT uFormat = DT_SINGLELINE|DT_VCENTER|DT_END_ELLIPSIS; , K& Q2 z2 n' w" g; n HDITEM hdi;& ~+ q! O3 l& N* S4 x$ f hdi.mask = HDI_FORMAT; * Z0 ~- F$ C1 \! K0 _. A3 t6 I% Y pThis->GetHeaderCtrl()->GetItem(i,&hdi); * `3 m. o% v: J, [' p2 ^ w) M/ X if (hdi.fmt & LVCFMT_RIGHT) - N Y6 L4 ~; |. ? { 4 v3 i& d0 z, [5 \1 L: D6 J uFormat |= DT_RIGHT;: c/ ^( h' a% V" y* @, E }8 E$ x! j1 P) k- f4 O& Z else if (hdi.fmt & LVCFMT_CENTER)! V4 E' f' f; A& l" K {* a. l- y- c" n' _ uFormat |= DT_CENTER;! B6 Q7 C. L1 I# c }+ z, d& I! L- f3 E' ? pDC->DrawText(strItemText,rectSubItem,uFormat);

CImageList *pImageList = pThis->GetImageList(LVSIL_SMALL); x' h* t+ `1 B& O7 a0 Z4 W if (pImageList)8 o( S% u9 r3 l; X {3 M& r) A% E4 m j! V CRect rectIcon; % k% e, D0 h: {) p$ \4 J2 x/ `% f GetItemRect(nItem,rectIcon,LVIR_ICON);0 M9 U/ U5 C3 \7 m; e' G8 \ pDC->DrawIcon(rectIcon.left,rectIcon.top,pImageList->ExtractIcon(GetItemImage(nItem))); ' Y( g; n, p5 E! p# K: p u3 N l } ; s' `; U- l: d& Z( @8 E }6 n6 J {& |7 l6 B" d *pResult = CDRF_SKIPDEFAULT; 9 T& k. j" g: N* ? }

}

该用户从未签到

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

本版积分规则

关闭

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

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