下沙论坛

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

QQ登录

QQ登录

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

又是垃圾,CListCtrlEx Base CListCtrl

[复制链接]

该用户从未签到

跳转到指定楼层
1
发表于 2005-2-23 21:29:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
// ---------------------------------------------
4 O+ [) L% c, ~3 e1 h// ListCtrlEx.h
. q& c( \; L9 c, |" ~// ---------------------------------------------- n- M, y  n  A% Q) S! _; }7 S
#pragma once' X" S0 G3 O! T! F8 g0 F

3 b: [+ D, t# @, H// CListCtrlEx
0 }! A( f  y% o0 Z0 Dclass CListCtrlEx : public CListCtrl/ W3 I' l( y; F1 y: [
{
$ ]0 h! {  D" l    DECLARE_DYNAMIC(CListCtrlEx)
  P7 z$ g/ z6 |- g# {$ d$ C- }# R7 J  ?public:
+ i4 k1 o+ m  P. D1 B* o    CListCtrlEx();
% H2 z+ c! \# Z    virtual ~CListCtrlEx();
) ^. F' G. x# R! q- F7 `$ _% O/ ]1 gprotected:$ r9 u; e* P0 u8 j; [9 J$ Y
    DECLARE_MESSAGE_MAP()' M# s, L$ j; T0 i' M5 I8 O8 m( M  ?  w
    CListCtrl* pThis;) v$ q* ]$ l# W* n1 p+ J
    virtual BOOL PreCreateWindow(CREATESTRUCT& cs);  |& G. l  |( W* Y
private:5 j! W' q7 i7 ]
    struct ItemDataEx {$ T" y% w  h, Z2 i8 g' Y
        LPARAM SrclParam;+ E4 t, T; R% g" l4 y- {
        COLORREF clrItemTextColor;
$ p6 N* K! r; P2 `9 o0 e9 ~) s+ X        COLORREF clrItemBackColor;
9 E; J. I; ^0 S+ u. V! [    };
0 p  C* R4 l% c, Q+ ?- o# U    COLORREF m_clrHighLight;, A  O% C& z, x* Z  `! _" [! x7 O- X
    COLORREF m_clrHighLightEdge;
4 b" D/ ~+ q: @; ^public:
' g" E  D0 l* P    // 新增方法
, }. t4 A( Y. h/ X% ^    COLORREF SetItemTextColor(int nItem,COLORREF clrText);0 ?. f/ Z* V- i3 i3 u
    COLORREF GetItemTextColor(int nItem) const;; t% A# m1 x0 P# I/ f, e( |8 x
    COLORREF SetItemBackColor(int nItem, COLORREF clrBack);: Y  d# p" N; _
    COLORREF GetItemBackColor(int nItem) const;
+ [! S+ g2 P9 L3 Q    COLORREF SetHighLightColor(COLORREF clrHighLight);
" W& k. f3 P& R; Y$ M. ~1 |    COLORREF SetHighLightEdgeColor(COLORREF clrHighLightEdge);
& z" v/ R. s7 J7 U, E    COLORREF GetHighLightColor(void) const;; M) s1 |* ?3 `  U' ~3 C$ y
    COLORREF GetHighLightEdgeColor(void) const;
: r( t. P: p5 v) i* J+ m    int GetItemImage(int nItem);0 K% g" k  k- R5 y5 k' I
    BOOL SwitchItem(int nItem1, int nItem2);7 Z; ^4 W) P( r; B- o
    BOOL MoveItemUp(int nItem);
% c; u/ h5 i9 k4 T, n, [    BOOL MoveItemDown(int nItem);/ e) A# f- o$ O; q! L4 G& C
    // 重写原来的方法# q, W) {; e+ l# i- D# G8 V; F
    DWORD_PTR GetItemData (int nItem) const;/ J0 f0 {2 l# ~( i% i. b
    BOOL SetItemData (int nItem, DWORD_PTR dwData);$ ^$ \# t" W6 m; \
    int InsertItem (const LVITEM* pItem);9 M. J7 o$ d  v8 z! e( y
    int InsertItem (int nItem, LPCTSTR lpszItem);4 F4 k( Y; E* M5 C
    int InsertItem (int nItem, LPCTSTR lpszItem, int nImage);
6 M0 P9 ~) ^1 h  T    int InsertItem (UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam);* q, n' _) w6 V# \! g! r5 A5 M. n, o
    BOOL DeleteItem (int nItem);" y( ?/ h0 ]% D% h& U- F& w0 k8 z( y
    BOOL DeleteAllItems ();1 {& z3 l# s" O! k" `6 q+ _
    BOOL GetItem(LVITEM* pItem);
, i# g  e9 M- b# u, D$ p    afx_msg void OnDestroy();, C9 D% J' g5 b( _5 T
    afx_msg void OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult);
! r; I4 m# e1 K};
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 顶 踩

该用户从未签到

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

// --------------------------------------------- : D% W, Q9 [+ Y: L) H. Z7 [// ListCtrlEx.cpp + B; S- f7 W; d// ---------------------------------------------0 s0 R3 v1 d0 m- x! E // ListCtrlEx.cpp : 实现文件# P& t0 Y( K* s8 c //

#include "stdafx.h" 4 Y2 K" C: n' ?3 Z& s#include "CreateNewClass.h" ) u; O5 m4 p( Y1 s$ D" X7 {#include "ListCtrlEx.h" 1 W1 a$ R& V" s0 u1 D1 x I#include ".\listctrlex.h"

' B! a6 p' b- b' \) _ j% t6 Y // CListCtrlEx

IMPLEMENT_DYNAMIC(CListCtrlEx, CListCtrl) ( ~. I: I$ D, B0 RCListCtrlEx::CListCtrlEx() F3 C! I) `+ M& a. n+ S$ d{9 Z3 b& T2 v! v: Y/ m& t // 用于调用基本的方法6 F% E( P% h, t* M9 q pThis = (CListCtrl*)this; ( l7 ^7 D+ W1 V$ @! q/ I& Q8 I // 默认的选中框颜色4 r6 E# \) [0 S7 Z. o m_clrHighLight = 0xF1DACC; 1 A! j5 N3 S- k: R$ [ m_clrHighLightEdge = 0xC56A31;5 Y) o; Y: V1 O* s" k }

CListCtrlEx::~CListCtrlEx() ( a, s$ K! C, U! H8 r, y- C{

}

& x1 H. v) Z3 G( C7 h( c ^3 w BEGIN_MESSAGE_MAP(CListCtrlEx, CListCtrl) ' P# q% u. m& X! o5 Y) v9 t+ ? ON_WM_DESTROY()* _# t) ^; W4 e, w! B ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnNMCustomdraw)5 d3 d4 j# h* G$ G END_MESSAGE_MAP()

// CListCtrlEx 消息处理程序

1 f5 s* c( f+ |4 ]; V% L, h) P. L COLORREF CListCtrlEx::SetItemTextColor(int nItem,COLORREF clrText)% }5 Q# e/ K+ ~ { s5 X! l( i1 O9 _5 R* j COLORREF clr = GetItemTextColor(nItem);4 h: Z; b6 R; b1 v+ W2 T ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem); 1 p' F+ `- \4 g$ R" `# | pData->clrItemTextColor = clrText;( \5 C3 C- h6 g/ z0 Y return clr;( J" z5 H: W! Z q }

COLORREF CListCtrlEx::GetItemTextColor(int nItem) const( Y6 \& r- _2 j! d { [( N: s' P6 p$ {; d ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem); p) A1 V3 y8 \0 c return pData->clrItemTextColor; - S% L j7 g( V7 c7 Y6 S Q}

COLORREF CListCtrlEx::SetItemBackColor(int nItem, COLORREF clrBack) + }- f: ]9 l! b! G4 b- Z1 w3 m{ $ }$ _. z6 o4 ?/ ?$ D* Z' S8 Q COLORREF clr = GetItemBackColor(nItem); ' O$ _6 v1 n% X1 T ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem); 9 X9 p' E3 N7 O0 O' h) ^$ m pData->clrItemBackColor = clrBack;- U/ J' }- P7 s5 [2 R5 n* H return clr;% O( I9 c6 R9 v" [+ d6 u' x8 z }

COLORREF CListCtrlEx::GetItemBackColor(int nItem) const" y1 _$ e9 u7 a& ?; ~7 F8 Y5 u* k {0 ]% E4 G1 j$ }: Z |; N) u. ^ ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);" w. y+ d3 o7 \ return pData->clrItemBackColor; ( ?- D. ?. ^$ v& |2 ~8 m* R} 3 c4 X2 ^, g! N: Q/ I1 `DWORD_PTR CListCtrlEx::GetItemData (int nItem) const , W) [$ w* h& U+ x/ z/ W2 u{ & `1 U/ I& j& D2 c! M2 b. P' V if (nItem >= GetItemCount()) ; J6 G c' C4 M% w { 8 m9 W6 r" a5 d return 0;. b1 e; \7 p" A } 3 J" ^" X1 m' Q) n0 S" U CListCtrl* pThis = (CListCtrl*)this;- j0 F$ m x8 P* g7 H* X DWORD_PTR Data = pThis->GetItemData(nItem); + k0 I$ j, v H3 z% i3 X return ((ItemDataEx*)Data)->SrclParam;$ s' q. U# \7 p4 N* _ }# \! C. L. C) B& p- R BOOL CListCtrlEx::SetItemData (int nItem, DWORD_PTR dwData)9 n+ v) P( ?0 s* s5 D" _6 ? {/ K8 b- u4 ?2 t0 B- R2 S* _ if (nItem >= GetItemCount())+ }% O! L% n0 u- Q8 O {4 z% \% P( E2 h- u; _# U' f return 0;' u( U4 s% P; G4 H6 I% X! A; j }# k( p4 _8 `5 H0 f CListCtrl* pThis = (CListCtrl*)this; ' D3 K" c. ]& Z: F! [ DWORD_PTR Data = pThis->GetItemData(nItem);# L( ?( z' v* ?4 ~ ASSERT(Data);9 y& k# ?/ S, A+ z0 C N' z ((ItemDataEx*)Data)->SrclParam = dwData; - {# T$ }, y- [! W return 1; " Q: i0 s/ m% Y} 3 R+ D; q1 t6 s* i- fint CListCtrlEx::InsertItem (const LVITEM* pItem) - w+ Q# D% Y* t9 u1 V{2 Z* g$ T8 N5 o% d% C1 I int nReturn = pThis->InsertItem(pItem);/ \* N$ C% K' V5 C) p, B8 F ItemDataEx *pData = new ItemDataEx; 8 Z3 M, I; s' G5 n) b' b // 设置默认颜色 ! M3 m. X9 Z; Y2 s; ~4 c+ v pData->clrItemBackColor = 0xFFFFFF;//pThis->GetTextBkColor();3 p. |1 G- L% a pData->clrItemTextColor = 0x000000;//pThis->GetTextColor(); 3 [2 e1 ]1 K% Q( w& A4 z3 M pData->SrclParam = pItem->lParam;- I2 _8 ~9 ~" H: x pThis->SetItemData(pItem->iItem,(DWORD_PTR)pData); % j: L5 U( w6 E- I return nReturn;4 y: g/ h+ u3 O: ]* W$ m }9 Q% d" K6 a: S) A! a+ Z; f int CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem)1 e! @: `2 G3 m, n6 A, F( i9 t { % Y+ U5 ]% } }# l; b LVITEM Item;! H$ I- W( O. \3 U memset(&Item,0,sizeof(LVITEM));1 C8 S& v3 Q# x7 w, W Item.pszText = new TCHAR[strlen(lpszItem)+1];3 `0 ?, f* s! {- W strcpy(Item.pszText,lpszItem); 1 K+ ?8 F( @( n, w+ @3 Q Item.iItem = nItem;4 S7 ]& R' f9 ~2 T; M2 Y Item.lParam = 0; . g+ s. R3 D; Q" ] Item.mask = LVIF_TEXT |LVIF_PARAM;/ N( R/ E0 l/ \- G! O; G BOOL bReturn = InsertItem(&Item); % C9 g: N9 {' f! c' n! b3 e delete Item.pszText; , L. e8 Z& t5 } return bReturn; ! Z; u# w; {" K [4 x, a, {2 G}* O* {" m8 `. H7 U int CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem, int nImage)( j% P- e! g. r x {* Z7 g1 A1 u& _* R' v LVITEM Item; & e4 y& j+ h8 ]3 C6 K/ ^3 o# f% g memset(&Item,0,sizeof(LVITEM)); 5 L) X: m3 I7 H) Z" q& [; J Item.pszText = new TCHAR[strlen(lpszItem)+1];% M% ~4 d9 L R; v. h) C strcpy(Item.pszText,lpszItem);' E6 K) {8 w) T: x& H# z Item.iItem = nItem; - b c6 e; [5 c1 I* j) a# J; O( N Item.lParam = 0;0 l# b- l' J/ s: e X Item.iImage = nImage; 9 |- @. T8 t2 N5 k9 H Item.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE; : `4 c" t5 K. ~8 \+ n0 Y BOOL bReturn = InsertItem(&Item); ' p2 ^! l* k! {" @- J, g* M& D delete Item.pszText;+ A8 H1 ^0 H/ m+ j return bReturn;% [* J& F' I1 d8 d0 { } * u6 P+ _/ V6 v& {4 h0 H, {int CListCtrlEx::InsertItem (UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam)4 F9 A9 r) o4 _3 y {9 K0 G$ f$ \/ X W3 F' S LVITEM Item; - o: B# ?" w, A; _) r# ~ memset(&Item,0,sizeof(LVITEM)); 0 O. a/ P, X; c# u: v6 d! p Item.pszText = new TCHAR[strlen(lpszItem)+1];2 P# r6 l1 O' @1 `$ w strcpy(Item.pszText,lpszItem);- `2 M5 L E1 G1 Z6 Y Item.state = nState; 2 K) L3 G% I8 i: [+ { Item.stateMask = nStateMask;6 A0 s4 q8 ?' `+ q Item.iItem = nItem;/ P) Z1 V9 ~6 T1 F( f( F. | Item.lParam = lParam; 5 e% ]3 L0 o% e" m L Item.iImage = nImage; 7 ~, `4 Y) }+ K f Item.mask = nMask;0 Y; D! v( Q1 t W# D# `6 W- S BOOL bReturn = InsertItem(&Item); ( \9 V7 \ a3 g: x* c delete Item.pszText;: ~: |/ a/ G. W5 A return bReturn; j' g: M; Q) M- }% E; u9 [ }4 @, ]# K% o& i' \9 V4 ?9 f BOOL CListCtrlEx:eleteItem (int nItem) ) c$ u" U2 `. i! \$ I3 a2 G{2 O9 M* K2 f4 ~; V void* pDataEx = (void*)pThis->GetItemData(nItem);* P, {/ s; }3 G if(pDataEx != NULL)2 P3 U# c* Y" I, J {+ _) S2 }; ^/ Q delete pDataEx; $ W- b* W* d$ k) x0 R9 F/ N }7 B" X; N1 N$ e7 O return pThis->DeleteItem(nItem); ! h u8 V! C$ ~% j! g# n& Q$ T} & p; H: Y$ ^7 b0 ]% G- R' cBOOL CListCtrlEx:eleteAllItems () # |* p8 {9 |0 D+ V: r1 I9 }{* T6 }6 [- A6 ]8 N" l5 R$ k: n int nCount = GetItemCount(); - g) f" t( v k/ C6 i for (int i = 0;i<nCount;i++)6 y% m: X4 X- n {# E1 K6 P; ]/ b! u% F if(!DeleteItem(0))! r: q: x3 A; E; W {0 y B4 h/ e: s. p4 p return 0; 6 \& q' t$ W) T/ v5 X9 I+ g6 d# ?# d } ( w5 c6 j% |' A' E0 @0 e9 I } 5 Q3 _/ k# F9 q8 \" E" W3 a9 M return 1; ( B! h* k% y4 Z% m}2 @, g# [5 ]. t8 ?% \- s BOOL CListCtrlEx::GetItem(LVITEM* pItem) ; q' `$ q [3 z+ M" T" k/ z{- c( t$ i* \4 ]9 k9 S BOOL bReturn = pThis->GetItem(pItem);: r; @8 O6 p+ A3 y; R if (pItem->lParam). E/ S) F6 B" ~# X { - L2 z; ^4 M, R7 ?( A+ { pItem->lParam = ((ItemDataEx*)pItem->lParam)->SrclParam; ) N, M4 b; ~$ z* ~1 U, Y }

return bReturn; ; i2 m: N, ~- k2 X}% v- t3 N, h5 h6 A8 F; Y void CListCtrlEx::OnDestroy()+ K ]+ L: z) e1 A% o" _ {7 }/ } D* B/ _ |( D6 y // 关键是清除申请的内存空间5 H+ ]5 |& @* i% u2 y DeleteAllItems(); ' A" ~7 T* p; t" X* C CListCtrl::OnDestroy(); ) W- Y& @* h. w8 I$ d}

BOOL CListCtrlEx::SwitchItem(int nItem1, int nItem2)5 J0 T( \4 Y! e! P! x- M {( @9 p0 X2 H8 B. c" L2 S, s9 L0 }% E0 i if (nItem1 == nItem2 || nItem1 >= GetItemCount() || nItem2 >= GetItemCount() || nItem1 < 0 || nItem2 < 0) - N! n/ J% o: T3 R E) S) F U7 } {8 a% o" d; w& q, h, ], ^ return 0; 9 P1 S9 ^) V( b( R' y } ; k/ _! u9 n0 s ^0 Y& S LVITEM ItemData1,ItemData2; 1 a6 R- ` f0 Y, M, Y$ J int ItemLength1 = pThis->GetItemText(nItem1,0).GetLength();: A6 J0 O6 O( [3 o& B int ItemLength2 = pThis->GetItemText(nItem2,0).GetLength();7 U' C* w) Q5 S6 j' ]- @ ItemData1.pszText = new TCHAR[ItemLength1+1];: ~( i' f$ P1 |( F# B' b5 t ItemData2.pszText = new TCHAR[ItemLength2+1]; $ R) J1 x- D3 K. ? D ItemData1.cchTextMax = ItemLength1+1;. `+ C; W: z2 Q6 Z% m" Z ItemData2.cchTextMax = ItemLength2+1; 5 I! ]6 a' [3 y- D ItemData1.iItem = nItem1; 9 u; @6 H" q- b! J7 Q7 [3 e2 `% } ItemData2.iItem = nItem2; . K. \6 X+ e) h3 W" a* X ItemData1.iSubItem = ItemData2.iSubItem = 0; - e* q! C; o( [% x, S1 ~ ItemData1.mask = ItemData2.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE|LVIF_STATE;

pThis->GetItem(&ItemData1);6 v' g, h* R# z$ n pThis->GetItem(&ItemData2); , q6 s8 v' Y) K1 D- w. v) Q // 交换数据 ' D) G4 S( \" C/ Y. F4 b% N, e ItemData1.iItem = nItem2; k& ^/ U1 |* s( D, p+ H0 c7 T0 x ItemData2.iItem = nItem1;8 @; W9 m# K7 V- a pThis->SetItem(&ItemData1);- q. E3 T5 A# j0 m m6 g) z pThis->SetItem(&ItemData2);

delete ItemData1.pszText;" {# M' {% N; L% w( q delete ItemData2.pszText; 6 O5 O- }- N8 R; U // 得到报表模式的列数 2 o9 b$ t* q) S& J0 I7 x int nColCount = pThis->GetHeaderCtrl()->GetItemCount();, i0 v8 P2 P; f+ ]3 o7 v2 } CString strBuffer;/ Z! R/ f: [1 [' M3 _ for (int iSubItem = 1;iSubItem < nColCount;iSubItem++) f; s+ n% c) v9 V3 y( _4 |6 U {7 g/ x- v1 |( A% A+ Y) t. w strBuffer = pThis->GetItemText(nItem1,iSubItem); 1 ~1 u. L, v. {2 l pThis->SetItemText(nItem1,iSubItem,pThis->GetItemText(nItem2,iSubItem));1 o7 ?2 B4 c5 Q* H7 u pThis->SetItemText(nItem2,iSubItem,strBuffer); $ ~; `0 ?6 w3 T) c }. \4 P5 o1 u- r // 把原来的选中状态也转移过去+ D/ N# c0 ` ?" e. ?5 @, R7 c8 ] // 不知道为什么这个属性没有体现在LVITEM结构中……" A0 O1 B4 J# w bool bItemSelected1 = GetItemState(nItem1,LVIS_SELECTED) == LVIS_SELECTED; ) l. D7 U: c' V! r bool bItemSelected2 = GetItemState(nItem2,LVIS_SELECTED) == LVIS_SELECTED;

SetItemState(nItem1,bItemSelected2?LVIS_SELECTED:0,LVIS_SELECTED);2 {: b8 w- k4 |9 F2 d0 u: q, O SetItemState(nItem2,bItemSelected1?LVIS_SELECTED:0,LVIS_SELECTED); 7 I2 X E! @" p2 Y2 m; n return 1;/ T& C' I, g9 }+ B/ T }

BOOL CListCtrlEx:reCreateWindow(CREATESTRUCT& cs)$ m- f6 P4 ^# o7 } { : q% E; _# m- X } // 本类报表模式专用 - c% t$ S9 p& o1 }! a- V cs.style |= LVS_REPORT ;% ~7 D; Q% z9 D) x" g // cs.dwExStyle |= LVS_EX_FULLROWSELECT; 6 V, X" R9 M N: l. \6 Y return CListCtrl:reCreateWindow(cs);% N1 N1 I0 W- A: K }

# H0 A+ m' P z3 ~6 p COLORREF CListCtrlEx::SetHighLightColor(COLORREF clrHighLight) . x! S: x& Q6 G, K4 x{5 A, g" i8 W. m0 F8 W0 m COLORREF clr = GetHighLightColor(); , G2 L7 S7 r% F7 o* F. s m_clrHighLight = clrHighLight;- V3 o4 x _; d. C1 h. ] return clr; ( p, W, `( L2 @, H/ Y: ?; H}

COLORREF CListCtrlEx::SetHighLightEdgeColor(COLORREF clrHighLightEdge)& F- T7 `% U. h* d. A" s4 M7 } {1 w$ L( N; T! X s7 ?: E COLORREF clr = GetHighLightEdgeColor(); 6 J- l: k" f: O# q. ]( v m_clrHighLightEdge = clrHighLightEdge; - {- ~& D9 g, r. l! A: C return clr; 2 J/ \2 \, c- X; k, g) `+ c}

该用户从未签到

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

COLORREF CListCtrlEx::GetHighLightColor(void) const 1 d' P) R& t9 `6 Y$ }{ 9 }# y3 i. a1 }0 k% `! |6 S# w% G return m_clrHighLight; ' Y+ d/ k; k; p4 T4 [}

COLORREF CListCtrlEx::GetHighLightEdgeColor(void) const . @4 K: a& u, [7 d1 @{ & o% k' b0 o; Y& g return m_clrHighLightEdge;, z K2 y9 p: h4 p5 ?8 d }

int CListCtrlEx::GetItemImage(int nItem)3 y: R: R" v% z {7 M1 @& `: N2 R3 r LVITEM ItemData; ( X9 c" ?, n) M! u& Q ItemData.iItem = nItem;: [( `2 O5 k6 g# q' \ ItemData.iSubItem = 0;3 e1 V* P: R8 Z- p8 g2 z ItemData.mask = LVIF_IMAGE; ; @ p) {+ ?3 Q& e! \1 h pThis->GetItem(&ItemData);" }3 E6 r4 h: w' k return ItemData.iImage; * i/ c' i& u* R, n; @9 L( ?2 y* a}

BOOL CListCtrlEx::MoveItemUp(int nItem)2 V9 a* L- p. @0 z& ~0 e {+ u! `4 ~' P6 {* ]" U/ j if (nItem < 1 || nItem >= GetItemCount())$ v8 g& s* p) Q$ C# b4 @6 c { 2 H# w& E! r7 { return 0;5 B! ^0 G* c0 h2 K6 d } 9 s' h4 T7 z* F) X2 D! ]5 g. U, U- ~ return SwitchItem(nItem,nItem - 1);; Q* w! ]1 v l/ C* v' s6 X7 A$ J }

BOOL CListCtrlEx::MoveItemDown(int nItem)9 ?0 ?1 A; u4 b* ? {8 d- f4 ~. y/ p# X9 H1 ~! K; a if (nItem < 0 || nItem >= GetItemCount() - 1) - L# B5 j; {/ j7 g8 o { - }$ E, o! ?6 X return 0;) F3 e; P/ ~8 G }& S$ V! D6 i! R' } return SwitchItem(nItem,nItem + 1);. \1 P" a$ f0 c) ^3 ]/ ?( w }

void CListCtrlEx::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult) # e; d6 ?" C1 Q( H/ u$ Y$ u+ z{ " [& f9 `$ p0 r2 ^. X' ]3 o8 O NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR);

*pResult = CDRF_DODEFAULT;

if (pLVCD->nmcd.dwDrawStage == CDDS_PREPAINT) , m, R" e$ n6 i! o* T { ! \! `: _ S; j1 V& C *pResult = CDRF_NOTIFYITEMDRAW;) \& V4 H, A: b& v; v8 R } ) g( K6 T4 A$ } Z/ y9 N# D else if (pLVCD->nmcd.dwDrawStage == CDDS_ITEMPREPAINT): M: x' m/ C& p- ~* ` {" E+ N l( z0 F0 w4 Y6 j* E *pResult = CDRF_NOTIFYSUBITEMDRAW;. T' ]; H% o. ^ }! `2 w- k' o0 z' V else if (pLVCD->nmcd.dwDrawStage == (CDDS_ITEMPREPAINT | CDDS_SUBITEM)) ' X' E% i# @ R; H+ J$ { n {

int nItem = static_cast<int> (pLVCD->nmcd.dwItemSpec);- Y' ~1 q8 @9 m5 E/ x, _ int nSubItem = pLVCD->iSubItem;- j" W. n+ P4 P H5 W) ? if (nSubItem != 0)# L/ e" s- B. j0 N' C% N5 z# ^9 W {2 P: x) e/ M; B6 M {* V // 全部绘制过程在nSubItem == 0 时完成! i# l' h N5 g9 @. q/ a$ {. J // 分布完成会有小问题,我无法解决 8 U+ S- m' V9 C. q *pResult = CDRF_SKIPDEFAULT;9 H6 d- d9 A! @ return;% p2 j# D! D! T& L: Y/ {4 n: } } ! _: o. n* Y$ B9 ?- H6 k CDC *pDC = CDC::FromHandle(pLVCD->nmcd.hdc); * R& j. Q. o7 C int nColCount = pThis->GetHeaderCtrl()->GetItemCount(); I* r$ u# E! S CRect rectSubItem;$ o" |; ]7 V) u0 o; Z0 ~/ P7 D CString strItemText; 7 P! ]7 Y! a6 v pDC->SetTextColor(((ItemDataEx*)pThis->GetItemData(nItem))->clrItemTextColor); 3 g7 \# o' y) l9 O- q6 K8 N) S pDC->SetBkMode(TRANSPARENT);0 ?' Y9 ~9 z4 K2 ] N! C // 相当于橡皮 " v: [, T* |& b6 w0 W( n CBrush brEraser(GetItemBackColor(nItem));

// int nFirstWidth = GetColumnWidth(0);

, J$ ^5 T. @% [3 x6 l' ] for (int i = 0;i < nColCount;i++)0 x" T) A- f7 [0 l5 `) Z9 `& p8 O {3 p, a6 C1 o3 B% ~ strItemText = GetItemText(nItem,i);

if (i == 0)) u) G" @: G' Z7 J5 r2 g# u {9 A) l. L, ?% J2 w. P, F pThis->GetSubItemRect(nItem,i,LVIR_BOUNDS,rectSubItem); - \" f g4 K+ t' J/ N // 擦掉原来的图形' P/ i# Z0 t, s pDC->FillRect(rectSubItem,&brEraser); : ^* w5 h$ a6 X; o if (GetExtendedStyle() & LVS_EX_CHECKBOXES)1 C5 ]2 @ \- D* U4 ? { , @7 c. p* O% P, D3 c- ? CRect rectCheckBox;5 I$ J$ M5 A- r. Y( K GetSubItemRect(nItem,0,LVIR_ICON,rectCheckBox);# D. @0 W9 d6 y/ w! ] CRect::SwapLeftRight(rectCheckBox); 4 a5 }* g0 E9 c6 h" s2 ^ rectCheckBox.left = rectCheckBox.right - rectCheckBox.Height();

rectCheckBox -= CRect(2,2,2,2);5 _" k ?2 S) L8 N# [ pDC->Draw3dRect(rectCheckBox,0x81521D,0x81521D); ) A4 x2 h: X: l" [ //rectCheckBox += CRect(3,3,3,3);% P2 `2 b0 q' w8 D+ v if (GetCheck(nItem)); l% _2 L ~: g/ I" n$ D { 1 V* R/ x' X) V // 绘制一个CHECKBOX ' k1 v( ^$ {0 v1 } CPen penCheck(PS_SOLID, 1, 0x0021A221);' }9 m2 P' [6 ? P4 e2 E. `* { CPen *pOldpen = pDC->SelectObject(&penCheck); + H' ~- h8 R S* Q for (int j = 3; j < 10; j++)" P* r! ^' d* Q2 Q! X; v* ?( P {& X1 T/ b+ f+ m# L pDC->MoveTo(rectCheckBox.left + j, rectCheckBox.top + rectCheckBox.Height() /2 - 3 + ((j < 6) ? j - 1 : (9 - j))); 3 c- R$ x5 Y/ Q" x1 q. b2 d" I% O8 |5 R pDC->LineTo(rectCheckBox.left + j, rectCheckBox.top + rectCheckBox.Height() /2 - 3 + ((j < 6) ? j + 2 : (12 - j))); I; c# w* Y; M/ v" M, ~ }9 F' \) j& M5 ~ ; J F7 Y1 O5 @# a7 S" f+ A1 K pDC->SelectObject(pOldpen);. w3 P) t7 j6 J9 t' J2 [' d }1 R% e- j& H( c3 t } 7 n- j: x+ I" B5 R2 t- M# T1 _ p1 L // 根据需要画选中框8 S6 _ O. j# P- Q4 {' u if ( (GetFocus() == this || GetStyle() & LVS_SHOWSELALWAYS)&&(GetItemState(nItem,LVIS_SELECTED) == LVIS_SELECTED))4 ~- E" l! O6 o* C { ' [4 L( s0 _* ^; M n+ L if (GetExtendedStyle() & LVS_EX_CHECKBOXES) # ]! m- M' f( V" M. }& |0 b {// 存在检查框风格的时候需要对左坐标进行修正: R2 x& e% F$ f% b( U( ~7 G/ d CRect rectLeft;: F& u) R u4 W GetItemRect(nItem,rectLeft,LVIR_ICON);6 \0 @4 ?* c3 @7 ^+ y7 ~. v5 w% ^ rectSubItem.left = rectLeft.left; * |/ i0 f/ F+ `; i2 x3 e# p } . Z! {( m( z' I+ H if (!(GetExtendedStyle() & LVS_EX_FULLROWSELECT)) & y! M$ F1 W. e7 O5 @ {// 非全行选择时右坐标需要修正 1 Y' S5 I+ |; q, I% A# Z+ [1 N CRect rectRight;% Y) P: `& `. S$ T# f: p! } GetSubItemRect(nItem,nSubItem,LVIR_LABEL,rectRight); 3 a& D3 W% ^; m rectSubItem.right = rectRight.right; 0 ` @- `/ V4 F4 i }1 A! V+ Q; C& a CBrush brHighLight; 3 U0 N, x3 Y) d W) @4 W CPen penHighLightEdge;7 F- O! c( ?( ?3 A( b1 b if (GetFocus() == this) 5 J" j f# r& ?- R* K5 N, S { 2 a Q/ p9 J% e! x% f" @ brHighLight.CreateSolidBrush(m_clrHighLight);& U2 k5 @ v5 ?+ t penHighLightEdge.CreatePen(PS_SOLID,1,m_clrHighLightEdge); ; d/ r3 R' m$ ?+ o" H } 0 q; Z8 g" Z. B0 Z y else 9 U; _- y m% W6 @- i {, S5 B" e$ g( ^: t // 不是焦点时,颜色70%不透明度 6 b3 e! H0 Z0 |( i0 o2 c) ^ COLORREF clrBack = GetItemBackColor(nItem);) u4 |* ^7 O( x6 T WORD red = GetRValue(m_clrHighLight)*0.7 + GetRValue(clrBack)*0.3;/ u! o5 G' r0 i3 [/ s WORD green = GetGValue(m_clrHighLight)*0.7 + GetGValue(clrBack)*0.3;0 H8 w, {; q4 Q' B' _# Z! X WORD blue = GetBValue(m_clrHighLight)*0.7 + GetBValue(clrBack)*0.3;" a/ H' f5 I8 h" r" l0 o& J0 [ Y brHighLight.CreateSolidBrush(RGB(red,green,blue));" i& S1 z2 x9 e red = GetRValue(m_clrHighLightEdge)*0.7 + GetRValue(clrBack)*0.3; 5 ^: z1 A3 l* X7 J3 t" r. W5 ? green = GetGValue(m_clrHighLightEdge)*0.7 + GetGValue(clrBack)*0.3;) n% _- G* n9 l* X2 N; G# r blue = GetBValue(m_clrHighLightEdge)*0.7 + GetBValue(clrBack)*0.3; 8 ~' Q; V4 V4 G5 R/ n5 { penHighLightEdge.CreatePen(PS_SOLID,1,RGB(red,green,blue)); 6 [0 Q2 i5 h3 V0 j5 Q } 6 f/ m1 {1 G- _6 e) \' Z CBrush *pbrOld = pDC->SelectObject(&brHighLight);9 a( G' G& T( d4 x2 V CPen *ppenHighLightEdge = pDC->SelectObject(&penHighLightEdge); 7 j$ `* `; Q) ]6 s' I6 Q pDC->Rectangle(rectSubItem); 6 m: E" v3 P4 u9 j pDC->SelectObject(ppenHighLightEdge); ' y/ h8 S# p+ @, x B pDC->SelectObject(pbrOld);) X- m, C% x6 ]* _" s0 U) |5 }7 A' G }4 t- \, c3 ]1 W+ z D5 @. { }' f2 J) J" Q v pThis->GetSubItemRect(nItem,i,LVIR_LABEL,rectSubItem); . u+ S# M( P( a" z UINT uFormat = DT_SINGLELINE|DT_VCENTER|DT_END_ELLIPSIS; , ?- O3 q6 z+ G7 K HDITEM hdi;$ b& Q o: k/ L s hdi.mask = HDI_FORMAT;- o* A$ r' F- ]* U0 a( u2 K pThis->GetHeaderCtrl()->GetItem(i,&hdi);) o5 h+ q. @% k if (hdi.fmt & LVCFMT_RIGHT)+ e% `# Z, W5 w {2 W9 L7 c4 n6 F9 |( b/ w* [ uFormat |= DT_RIGHT; . k2 |9 T9 t6 v& N6 s5 T, k } : P- _& f% V, x. y! L/ d! p else if (hdi.fmt & LVCFMT_CENTER)/ C8 C. e2 l. ]3 q. V2 d { 3 I- `8 ~$ y2 t* y uFormat |= DT_CENTER; ! s' d! [1 b$ l5 P) q$ l; ~( ^$ \ }2 ^8 I8 i6 B6 A5 F pDC->DrawText(strItemText,rectSubItem,uFormat);

CImageList *pImageList = pThis->GetImageList(LVSIL_SMALL); / G/ o3 z# S5 j) W' w0 g2 ~9 d if (pImageList) ; e# {) y) {' Y { ; w& j8 H' h$ C8 Q; U CRect rectIcon;0 Q+ g* [1 ?* E$ s$ c2 D GetItemRect(nItem,rectIcon,LVIR_ICON); 7 s$ P3 H' C( Y3 h! U5 e pDC->DrawIcon(rectIcon.left,rectIcon.top,pImageList->ExtractIcon(GetItemImage(nItem))); / W. g2 ]* K' F- O/ A/ r4 ~$ m+ W } , U$ o! }% w, k# A3 i# S }% N% I% _' ^% p0 T *pResult = CDRF_SKIPDEFAULT; ! T W8 A. m; G: g( `, T }

}

该用户从未签到

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

本版积分规则

关闭

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

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