下沙论坛

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

QQ登录

QQ登录

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

又是垃圾,CListCtrlEx Base CListCtrl

[复制链接]

该用户从未签到

跳转到指定楼层
1
发表于 2005-2-23 21:29:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
// ---------------------------------------------2 J0 |) c: e: v* H$ \1 F
// ListCtrlEx.h) u9 u$ I# Y: [/ N7 Y( V
// ---------------------------------------------9 c2 J. D: a; Z: p4 @8 M
#pragma once( N7 s: I# K# B+ L6 O& g
& C& f; E" l) `3 T8 e
// CListCtrlEx
6 r' ~. [( V' e* }! E7 p- vclass CListCtrlEx : public CListCtrl
) o6 w$ R7 O/ \( N* u6 }9 }) \! K( G{3 S  Q$ G. Q& }) ?& C
    DECLARE_DYNAMIC(CListCtrlEx)' |* i! o' l7 F; h! k3 t
public:
( V" f4 X. r' O2 x8 X; z& e    CListCtrlEx();3 s" B5 {" `5 a! j& D: F7 |! s- f: I
    virtual ~CListCtrlEx();- A6 T& I. x: h! O) @
protected:
' B( ~2 m# R, a5 u- x& s# K    DECLARE_MESSAGE_MAP()/ v# [% F0 {- n
    CListCtrl* pThis;0 m6 b" ?: U8 y/ x
    virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
, S# T, ?$ o: Y4 Qprivate:
( [2 F3 {" B5 d+ k1 B& L' h1 m) m    struct ItemDataEx {
& k, @; o  V; K+ I& z5 C        LPARAM SrclParam;6 P8 C2 H. d- H8 f$ _
        COLORREF clrItemTextColor;
' k, ?7 O" e) X3 c5 r8 r        COLORREF clrItemBackColor;
2 J& s- O1 ^. l* i( ]" V    };* s2 H  h9 z7 u" w. U
    COLORREF m_clrHighLight;
8 _& S* }$ r2 X* y/ {+ M    COLORREF m_clrHighLightEdge;1 ~) j/ R) m0 O# X6 r! a9 T
public:
8 l5 r' W4 w) i" A7 _    // 新增方法+ ^0 S' Q9 r  X3 [, m  E0 c' f- b
    COLORREF SetItemTextColor(int nItem,COLORREF clrText);
$ ]: e" v: B' l6 }    COLORREF GetItemTextColor(int nItem) const;
- ~9 E  ]2 T9 |: \9 e. p' v    COLORREF SetItemBackColor(int nItem, COLORREF clrBack);6 R. W) r" x+ r3 ~. w- w# i7 K+ Q8 K
    COLORREF GetItemBackColor(int nItem) const;6 `2 @( p* g7 l1 [
    COLORREF SetHighLightColor(COLORREF clrHighLight);) `" {9 n* t5 ~7 R# P0 U$ U
    COLORREF SetHighLightEdgeColor(COLORREF clrHighLightEdge);
( b  i1 T7 F' B0 q    COLORREF GetHighLightColor(void) const;
. W% ~  `* m% e% }    COLORREF GetHighLightEdgeColor(void) const;) y6 l- `) u  C) ?/ K; L
    int GetItemImage(int nItem);
+ R7 i  u1 q- g' N' o. k: w    BOOL SwitchItem(int nItem1, int nItem2);( p7 p6 |$ e) {/ E
    BOOL MoveItemUp(int nItem);$ d+ l1 l! A) \! ^8 l- L' C5 g
    BOOL MoveItemDown(int nItem);# I% w* B0 J+ Y4 M! C6 ]
    // 重写原来的方法4 b2 |0 L3 }( H! {. I
    DWORD_PTR GetItemData (int nItem) const;. Y5 L7 ~. l. O# O; P
    BOOL SetItemData (int nItem, DWORD_PTR dwData);7 O/ ^) n4 h5 C2 f" C
    int InsertItem (const LVITEM* pItem);
$ }$ z9 p% T2 S7 _7 g/ F2 L4 e  u    int InsertItem (int nItem, LPCTSTR lpszItem);
- d" J! F$ v" s: _& O# S! H. j    int InsertItem (int nItem, LPCTSTR lpszItem, int nImage);* X2 ]$ v2 H7 f- N
    int InsertItem (UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam);
4 H5 G! Z  N; S8 z    BOOL DeleteItem (int nItem);
! _9 {/ L  v9 R7 A    BOOL DeleteAllItems ();7 l' ^  D' @( m; G* y' `; G
    BOOL GetItem(LVITEM* pItem);
! A! ~6 t% @5 H+ N+ o1 a    afx_msg void OnDestroy();
, K# S% `5 E- `1 a    afx_msg void OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult);0 n9 r6 D6 p2 d6 q! G2 z& {
};
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 顶 踩

该用户从未签到

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

// ---------------------------------------------/ _' O9 s) Z( U1 S( h4 _" j // ListCtrlEx.cpp 0 [$ e* W# m- l; s/ P// --------------------------------------------- 7 Q# d) p2 [! k0 U// ListCtrlEx.cpp : 实现文件 9 n0 { L4 ]- d1 h. p//

#include "stdafx.h"& a! x" r- E$ r #include "CreateNewClass.h" 5 W9 B% _+ x/ M0 K1 s( N; G4 j, J#include "ListCtrlEx.h" ! s1 d' Z; [% R+ {' ?#include ".\listctrlex.h"

) r9 Q5 j' M7 v" ?3 L // CListCtrlEx

IMPLEMENT_DYNAMIC(CListCtrlEx, CListCtrl)' A: z7 h6 ?7 m. o2 d CListCtrlEx::CListCtrlEx() 2 ~, }# C; x- P, U) h{' ]. b' g* K* f; } j // 用于调用基本的方法( J& p0 m9 P" L2 V' S* o pThis = (CListCtrl*)this;1 f0 a# L. A+ I, k4 @ // 默认的选中框颜色) [0 i; }7 Y0 u) X' d1 r% q" v m_clrHighLight = 0xF1DACC; 5 H. T! W- d: N1 m/ g m_clrHighLightEdge = 0xC56A31; , @! G1 M/ Q7 J5 D4 K}

CListCtrlEx::~CListCtrlEx() 8 t! t! t& m* ]9 ?! g{

}

6 f+ E1 W! w9 W7 GBEGIN_MESSAGE_MAP(CListCtrlEx, CListCtrl) ( T6 W1 e0 K9 W* j6 H- {- W) h/ i/ Y ON_WM_DESTROY()# ^2 b3 ^1 p* [4 |& s9 O$ r. } ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnNMCustomdraw)" C& d9 A0 B! z" z( o4 T END_MESSAGE_MAP()

// CListCtrlEx 消息处理程序

) g! t% Z) }. m0 y! \+ R- E- E6 k/ ^ COLORREF CListCtrlEx::SetItemTextColor(int nItem,COLORREF clrText)! f; f0 |# e- l; o5 j { 6 ^- r1 J7 T+ u, w9 J COLORREF clr = GetItemTextColor(nItem); ( V. R" I- f. {0 p ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);- t9 I, s; O- Y- w3 x& D" u# o pData->clrItemTextColor = clrText;6 [& Z1 o) f: n8 O$ T% h+ V: o' z return clr; + v2 z$ u7 a9 d4 h# @}

COLORREF CListCtrlEx::GetItemTextColor(int nItem) const" u) u( D" R' W { / b/ E+ N% d; u" T" l' O0 O2 i ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem); & S; B }. M* Y! v: n* s3 y return pData->clrItemTextColor; # s% p( x6 S; G& r+ Q}

COLORREF CListCtrlEx::SetItemBackColor(int nItem, COLORREF clrBack) 3 @: v$ i( q+ x$ u# p6 i{ 7 o% h' r: Z. J$ q& x$ C F, ^* r COLORREF clr = GetItemBackColor(nItem);0 m+ ^0 R2 ~* i9 m( n, T3 l6 W. q ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);8 P, l4 o' p- f# r( D pData->clrItemBackColor = clrBack; * Q5 {2 V2 j* s# Q return clr; 4 c( G9 O6 k; L; ^}

COLORREF CListCtrlEx::GetItemBackColor(int nItem) const 2 p6 c5 k2 H' f6 d/ l+ S H8 \{ 4 q. o. X4 ~" S9 ` ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);. L c& O* k9 I9 u2 B return pData->clrItemBackColor;- p4 W' V% E6 o6 ]0 l } 1 x. g' H; f l {" @/ ODWORD_PTR CListCtrlEx::GetItemData (int nItem) const. i' g- P0 t: w | {: u$ B1 | r p, K& G if (nItem >= GetItemCount())0 D. m& X* O) @- H8 p" [ { 9 K0 |1 ?! o& s% f return 0; ; s6 Q; i- D$ n" @ } * n% i' P# M. H6 \ k. q: c x CListCtrl* pThis = (CListCtrl*)this; 4 {- F- L% O% o0 f DWORD_PTR Data = pThis->GetItemData(nItem); 8 U7 f% g9 F+ P# f9 ]4 Q return ((ItemDataEx*)Data)->SrclParam; 4 o4 J# g7 a1 r6 B. D}" f, S' ]1 ^0 ^7 z0 V BOOL CListCtrlEx::SetItemData (int nItem, DWORD_PTR dwData)8 X0 X& y/ D5 j: @: V5 D! m5 K! i4 T- D { * I, }' D* f5 F+ Q! v5 u0 A if (nItem >= GetItemCount())' D9 |; y7 r9 g {( S) W: {, K+ ~+ S return 0;3 W L' T( x; B3 n8 l5 g }0 b; y) i; n+ ]4 s- a CListCtrl* pThis = (CListCtrl*)this; 0 y2 h# l0 L0 } D DWORD_PTR Data = pThis->GetItemData(nItem); w9 \4 x* h6 s* z) l' F5 \ ASSERT(Data);( t$ b/ K! P! h, U, f* O ((ItemDataEx*)Data)->SrclParam = dwData;1 \6 d5 g" k- B% b3 H3 d. A- X return 1;. e& X0 i1 D& w6 ]& Y0 }. \ } 0 O8 o6 f/ B1 `( \0 Oint CListCtrlEx::InsertItem (const LVITEM* pItem)' \+ v- I6 z. a6 @& E {& u' N. P0 B7 O9 l9 @2 X; G int nReturn = pThis->InsertItem(pItem);9 y( X% _* N+ P M ItemDataEx *pData = new ItemDataEx; & {" y1 l9 ?( ?/ l4 B3 d! O // 设置默认颜色 1 L6 W2 W4 E# V6 g pData->clrItemBackColor = 0xFFFFFF;//pThis->GetTextBkColor(); + t; e* l* ?7 Z2 J- a pData->clrItemTextColor = 0x000000;//pThis->GetTextColor();4 n' k# @( d/ J; b9 J0 P pData->SrclParam = pItem->lParam; & v" J. w" H/ C pThis->SetItemData(pItem->iItem,(DWORD_PTR)pData);9 d# o( _: j/ S1 K; H: P return nReturn; , o) D; C" C. X: D. p}! Q) q* I6 v7 m3 ~; A5 z int CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem)+ ^$ r# P4 V a5 V6 Q { # C2 \! p; P. G! n LVITEM Item;4 V2 L& {" h0 L a1 M1 u memset(&Item,0,sizeof(LVITEM)); 6 v$ _& z& M% ^ Item.pszText = new TCHAR[strlen(lpszItem)+1]; 2 U* q [* v/ Y8 r strcpy(Item.pszText,lpszItem);( y: X8 t+ n* r& Y& b Item.iItem = nItem; . w2 O, |/ w( \# C5 B! g& r Item.lParam = 0;6 r. m" J1 y- J5 P% B Item.mask = LVIF_TEXT |LVIF_PARAM; X: j3 T5 h( O% W" P: d5 q BOOL bReturn = InsertItem(&Item); 9 i* |/ H" o# z' _3 v7 I2 I delete Item.pszText;' l* T% h5 b1 z return bReturn;0 p- c) X9 |. f" n* i } 9 D3 l$ P4 {/ x; M2 Sint CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem, int nImage)* x/ X# L5 G R$ K) f/ m5 Z {% S2 M. ]5 Q2 k7 H LVITEM Item; 9 T2 E8 x& c, `; l" n4 S9 R2 f memset(&Item,0,sizeof(LVITEM));. h1 R! _! J; q* d5 R Item.pszText = new TCHAR[strlen(lpszItem)+1];! M- \! e, B) D" O1 C' n5 E: H0 n strcpy(Item.pszText,lpszItem);4 l( K! o0 F0 e Item.iItem = nItem; 8 X' U; q) C# a Item.lParam = 0; ( w) t: ]* {% `0 V; \ Item.iImage = nImage; # o4 X) v! Z9 F Item.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE; . a1 Y R. R; ^0 F) ]! \! d1 z; O, r8 M BOOL bReturn = InsertItem(&Item);" G, Q3 ^/ U& H; T R `- \0 A delete Item.pszText;8 Q) S+ a" a r0 F3 P# g. b0 D [ return bReturn;$ g& @) t2 l: j3 z) p7 [ } 9 ?5 |& s5 |) ^9 C# Vint CListCtrlEx::InsertItem (UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam) ; x) f x2 P$ h4 x" I; b{" M Q3 l) K, J3 j# s- y LVITEM Item;! E; q: v% ~& J; z% v4 y+ f memset(&Item,0,sizeof(LVITEM));4 Y8 \9 A) p | p Item.pszText = new TCHAR[strlen(lpszItem)+1]; - J5 G$ _0 {# \, \/ O# R/ m2 x. ~ strcpy(Item.pszText,lpszItem); j# v( v I' {! m Item.state = nState;, d, b8 G3 g) Q; ^1 a* X Item.stateMask = nStateMask;5 {2 X# P+ }( D' I+ {* s0 L+ F Item.iItem = nItem; 9 Y$ p, ]! C3 b6 R6 O7 [9 m H Item.lParam = lParam; % y8 k2 {! h$ R. r ^$ O, r Item.iImage = nImage; $ q/ e8 N: B8 L, N7 ]" i" i$ Y Item.mask = nMask;1 c% _) i' W( n v8 U1 }( g& m BOOL bReturn = InsertItem(&Item); 6 D5 U' {( X' F( B. \2 e delete Item.pszText; 1 Y: C) N. z. K6 R l( I% T return bReturn;0 s. }% Y7 N# d4 n8 k! l }+ \( m) U4 p! X2 H BOOL CListCtrlEx:eleteItem (int nItem)7 y# }- @+ o6 [- y. ~' y {' g, ~0 ^- w) o9 X% c void* pDataEx = (void*)pThis->GetItemData(nItem); : G, ^+ v; Q9 e+ T) \' \ if(pDataEx != NULL), M3 l" d% t1 x% _ { : \- S& {" ]: N( k delete pDataEx; ; T3 I0 A- Y4 `6 O4 e" [' ~ }/ o" z( I2 r# }. \; r return pThis->DeleteItem(nItem); 7 G5 k7 D. r8 W% j3 c} $ B, V" x" O' \$ \9 fBOOL CListCtrlEx:eleteAllItems () / G/ h' R0 z& x* `{ 5 n/ M5 j8 i* f: t; P% e* @% \ int nCount = GetItemCount(); # A" S9 D) P* g! _, J% z1 U5 {% x for (int i = 0;i<nCount;i++)* J) w0 B. w2 F0 @" Q0 }+ } { & w% Z1 p8 e1 u% |& P1 e/ c, u if(!DeleteItem(0)) 7 ~( N0 o+ D2 ] { 0 O( N: @ n" V% F( e% m return 0; & V* ]& {6 S6 ^% |5 H6 r6 f }' x# s; v1 q" N' J: k1 X } 1 K9 Q6 _( W+ p. {/ \) V return 1; 4 Z9 c" N) N. k% i. f% y} - U1 P! R9 v0 I I: e# W1 Y" mBOOL CListCtrlEx::GetItem(LVITEM* pItem) ; z- P- j! _9 q3 E$ s1 `0 v e{ & y' f! U9 V0 h, f! K BOOL bReturn = pThis->GetItem(pItem);7 A3 C4 h- u" g0 A7 S2 n- [ if (pItem->lParam)9 ~4 X# a( r7 D7 S8 h+ W {( `! d7 Y! v# V/ C* x, c pItem->lParam = ((ItemDataEx*)pItem->lParam)->SrclParam;- f" x o2 B& n0 u }

return bReturn; & a' z! q7 f# w0 K3 x: Q& H$ Y}/ m0 Z+ h: y; \5 v8 U, F void CListCtrlEx::OnDestroy() ' V' P* B l0 E) P6 G9 t{8 S; L' h; A3 p( d( _1 h( ~ // 关键是清除申请的内存空间/ n; o1 s; u0 N; ] DeleteAllItems(); 1 R& K; M1 p* o* M2 l/ J7 | CListCtrl::OnDestroy();8 [* I. Q/ r0 F) J9 Z }

BOOL CListCtrlEx::SwitchItem(int nItem1, int nItem2) / L! A6 m# N5 O. h/ [9 ?{* S9 B' g& }( f/ @ ^3 W. t if (nItem1 == nItem2 || nItem1 >= GetItemCount() || nItem2 >= GetItemCount() || nItem1 < 0 || nItem2 < 0) ) G9 L g% S7 s- O% d4 @2 x { 8 Z6 q/ u! F# [% [' Z2 L" X6 y return 0;$ @5 R" s9 s1 @0 f" e6 g }+ w" E' H0 i0 F: w; F* v LVITEM ItemData1,ItemData2; $ j8 \6 ^+ a) `- i$ ~, {7 S) [ int ItemLength1 = pThis->GetItemText(nItem1,0).GetLength();) H6 c7 y; k5 \1 k, H int ItemLength2 = pThis->GetItemText(nItem2,0).GetLength(); 4 F/ `# O, H4 A7 m- [ ItemData1.pszText = new TCHAR[ItemLength1+1]; {# X6 |: ^/ {8 k ItemData2.pszText = new TCHAR[ItemLength2+1]; / n! g9 Q o3 I7 R. d! W$ \7 Z' S ItemData1.cchTextMax = ItemLength1+1;8 V# R, a2 h+ t4 _. |- Y ItemData2.cchTextMax = ItemLength2+1; ]7 L5 z k0 G& ]; X% ] ItemData1.iItem = nItem1; * A, B4 V9 I$ T3 q c# `! `! I6 F( M ItemData2.iItem = nItem2; 7 |$ E2 ?5 p# k; J' x4 g- t, D7 g1 } ItemData1.iSubItem = ItemData2.iSubItem = 0;% H# p. U, }& t4 u+ L ItemData1.mask = ItemData2.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE|LVIF_STATE;

pThis->GetItem(&ItemData1); 8 o& _! l! Z3 |4 Q pThis->GetItem(&ItemData2); 4 w( x. ^5 _0 y // 交换数据 ; D- i: y; K4 I/ K& r ItemData1.iItem = nItem2; 2 ]1 M9 f4 E7 g9 f ItemData2.iItem = nItem1; 5 R/ C+ ?' m2 s9 {0 `3 G9 N0 F# G pThis->SetItem(&ItemData1); & r* R4 ] S9 V pThis->SetItem(&ItemData2);

delete ItemData1.pszText;7 ]; d. F9 s/ [9 [- O, b. I+ J" J% w! q delete ItemData2.pszText;1 e& G2 ~# E& x1 b5 T // 得到报表模式的列数* A c7 c6 e' A* z3 i& | int nColCount = pThis->GetHeaderCtrl()->GetItemCount();1 X0 C8 y0 Q( v4 K CString strBuffer;- k; Q4 ~* Z# f) |# j! \' B' s+ j7 w for (int iSubItem = 1;iSubItem < nColCount;iSubItem++)7 r/ b: U( I4 K1 O* I( u { & v# n* _: r% B( P5 e strBuffer = pThis->GetItemText(nItem1,iSubItem);$ G c' v9 `( [ p pThis->SetItemText(nItem1,iSubItem,pThis->GetItemText(nItem2,iSubItem)); & r" {, t- _ e5 E9 W" v pThis->SetItemText(nItem2,iSubItem,strBuffer);' I7 y2 e+ s9 s; n7 O. L5 a }& A( s9 }! ]$ h1 B // 把原来的选中状态也转移过去 " V6 p; I: m, E! I3 Q5 N" K // 不知道为什么这个属性没有体现在LVITEM结构中……3 u# s$ ^- B6 v8 J; u( {- N4 @ bool bItemSelected1 = GetItemState(nItem1,LVIS_SELECTED) == LVIS_SELECTED;; h3 Q. }3 w2 u" O7 z( x) ~ bool bItemSelected2 = GetItemState(nItem2,LVIS_SELECTED) == LVIS_SELECTED;

SetItemState(nItem1,bItemSelected2?LVIS_SELECTED:0,LVIS_SELECTED);; v# p9 G" P& j. ?% L; E SetItemState(nItem2,bItemSelected1?LVIS_SELECTED:0,LVIS_SELECTED); ; H4 v! D6 g: P7 S8 {% k% z return 1; & Z' I! `) @9 l' `}

BOOL CListCtrlEx:reCreateWindow(CREATESTRUCT& cs)1 _$ \3 ]' O* c7 u {& z) k& Q5 Y4 G* t7 ~- J/ q/ O1 A! n# ] // 本类报表模式专用 $ S+ L" h* O( F7 b, U$ Q1 ?1 y) R cs.style |= LVS_REPORT ; % m; Z8 m) r9 l L // cs.dwExStyle |= LVS_EX_FULLROWSELECT;5 |) L( m3 v8 L$ [0 ]- W return CListCtrl:reCreateWindow(cs);8 o5 A0 Y: w ]; V" j* | }

% [' D. J# J2 gCOLORREF CListCtrlEx::SetHighLightColor(COLORREF clrHighLight) 6 }. q: g; k! u. b{6 W$ X* d7 r8 ~& w5 \- z$ L/ l COLORREF clr = GetHighLightColor(); 3 D! J) V, {. d! m1 }9 Y m_clrHighLight = clrHighLight; + ? w7 u r) I% _5 g return clr;/ W6 F6 r& H2 E2 D5 U }

COLORREF CListCtrlEx::SetHighLightEdgeColor(COLORREF clrHighLightEdge)4 S, p) X7 Z9 w6 b3 f {: o { + |6 L0 r7 d6 j1 f COLORREF clr = GetHighLightEdgeColor();: s0 R# u& h. a5 g5 ^ m_clrHighLightEdge = clrHighLightEdge;4 w* h4 n' G, j' j$ l; ~ return clr; 0 m# z: F3 q( L}

该用户从未签到

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

COLORREF CListCtrlEx::GetHighLightColor(void) const. V6 s9 z* i- A+ Q# \1 r {! A2 D8 i4 G0 D2 g* ^ return m_clrHighLight; t4 i) N" b" y) @; X}

COLORREF CListCtrlEx::GetHighLightEdgeColor(void) const4 T% q0 O6 S2 z8 R {, Z9 P0 m, w7 u7 W4 | return m_clrHighLightEdge; & w' ^( t/ G- |3 c" {2 @3 T% o}

int CListCtrlEx::GetItemImage(int nItem). I/ j8 @& W2 W- a% ~# {* a' x { " u* P: b* _+ M; v2 J LVITEM ItemData; , f* a I" d6 T& ? ItemData.iItem = nItem;4 w# M$ V' V' Q8 W/ z. M ItemData.iSubItem = 0;) c% N6 Y n- \ ItemData.mask = LVIF_IMAGE;6 ^, Q5 s6 @& _3 R, H pThis->GetItem(&ItemData);0 g7 Z$ p; @! N* p3 P8 P return ItemData.iImage; 0 J* y7 L- `2 q' n( A( {; w( c2 w}

BOOL CListCtrlEx::MoveItemUp(int nItem)9 M- V2 [# \ s- b. h {( b$ b! I, P/ i# a if (nItem < 1 || nItem >= GetItemCount()) 4 f3 k" }/ q9 B- c. B$ S- b1 d- V ~ { G4 m4 U& E9 _6 @% Z7 y0 e7 I return 0; @& t5 W5 A; }9 u: H% V5 }5 o }( O1 M. R9 n3 j1 B return SwitchItem(nItem,nItem - 1); % y ]" z. \- H/ O1 J( J4 Q3 }}

BOOL CListCtrlEx::MoveItemDown(int nItem) - [9 e' S' c+ v1 S3 y2 Z2 e4 R/ O{ $ k8 a! o5 d+ h5 w* d% w if (nItem < 0 || nItem >= GetItemCount() - 1)& _! X+ P* P( v. j { + C4 ]8 C3 W: a: ]( p# F- C return 0;( L6 E Y; | M$ Z: R$ G }! K0 X8 E6 I9 V* c" X5 w- N return SwitchItem(nItem,nItem + 1);8 i8 L, X2 _$ J) A9 Z }

void CListCtrlEx::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult)! R; n: z T; S$ ~# a0 O { & c% c* |: r5 ~ NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR);

*pResult = CDRF_DODEFAULT;

if (pLVCD->nmcd.dwDrawStage == CDDS_PREPAINT)5 W) N+ X, j( E; H! R0 N0 n {( m& l0 W5 Y( D *pResult = CDRF_NOTIFYITEMDRAW;' `. c- Y! k; c/ @$ h) H } 5 v( U7 r1 X- k7 F+ v else if (pLVCD->nmcd.dwDrawStage == CDDS_ITEMPREPAINT) * e& B4 m3 N& Z3 d0 {3 b { % F8 I, j& a( S *pResult = CDRF_NOTIFYSUBITEMDRAW;+ e$ B% ^' W9 l) m. _ } 2 X- d* L# y' z0 x$ [ else if (pLVCD->nmcd.dwDrawStage == (CDDS_ITEMPREPAINT | CDDS_SUBITEM))) }. {" g9 m/ E! k& F {

int nItem = static_cast<int> (pLVCD->nmcd.dwItemSpec);5 v; t/ @2 W' @ K- T; b int nSubItem = pLVCD->iSubItem;) J+ ~2 E/ v v' m3 @9 I$ T' T if (nSubItem != 0) 8 g: S5 O5 z+ ^ { - A1 M e. K- r6 z6 }* U# Z' K // 全部绘制过程在nSubItem == 0 时完成1 r2 ~0 t6 q" G, I8 K! f1 m // 分布完成会有小问题,我无法解决 5 T# b) ]3 r7 L( Z# \0 T *pResult = CDRF_SKIPDEFAULT;5 O9 r4 o/ {7 O' \8 m9 c return; 8 V- u2 e& q) V/ Y( H6 F# c }3 J- ~5 g# B" u' I. Y CDC *pDC = CDC::FromHandle(pLVCD->nmcd.hdc); 0 v) e. T* R) z! u) O y int nColCount = pThis->GetHeaderCtrl()->GetItemCount(); 2 m% H! ^; O. R3 A- M7 f* T CRect rectSubItem;8 j. T% n, w* H0 B# Z9 z CString strItemText;) s% y. O0 o: t% [# t pDC->SetTextColor(((ItemDataEx*)pThis->GetItemData(nItem))->clrItemTextColor);' |' P& D4 j1 L pDC->SetBkMode(TRANSPARENT); ) j n0 Q" l0 z0 | // 相当于橡皮 ; H* \, k$ m- T1 v V) x9 Y CBrush brEraser(GetItemBackColor(nItem));

// int nFirstWidth = GetColumnWidth(0);

6 E( q( J9 y" S$ H' i9 i7 ~& ?; g for (int i = 0;i < nColCount;i++) " N) |8 V8 |3 e! I9 S) @1 z: S { l1 X0 K: E; t# L6 X1 v strItemText = GetItemText(nItem,i);

if (i == 0) 5 {* H# N, V7 f% T! o$ E8 g { & o$ q3 _5 t x pThis->GetSubItemRect(nItem,i,LVIR_BOUNDS,rectSubItem);" C, w/ q; C' R% U9 Y: ~- Y& T // 擦掉原来的图形 - k1 ~) ]6 j: h. f( P" }# m pDC->FillRect(rectSubItem,&brEraser);# { {/ S3 h# `6 Q if (GetExtendedStyle() & LVS_EX_CHECKBOXES)! A) b9 H: Q4 Y { 7 n8 d/ Y5 P9 b CRect rectCheckBox;7 N8 ?3 I3 \8 i" e GetSubItemRect(nItem,0,LVIR_ICON,rectCheckBox); 1 \2 ]' n W$ ^- F3 x2 |9 _ CRect::SwapLeftRight(rectCheckBox);. X& s9 c. W% Q rectCheckBox.left = rectCheckBox.right - rectCheckBox.Height();

rectCheckBox -= CRect(2,2,2,2); * B+ s& q5 a0 K5 x j7 \ pDC->Draw3dRect(rectCheckBox,0x81521D,0x81521D);/ r/ b, G0 _' H, G6 e; r //rectCheckBox += CRect(3,3,3,3);2 b3 S& @" D; t% r; T9 @6 p if (GetCheck(nItem)) , I. Y* y2 s5 a- Q7 O+ U { 6 }+ s5 i. ~8 h' T0 G2 N* b // 绘制一个CHECKBOX* g# L% R& e/ ~ n5 ~ CPen penCheck(PS_SOLID, 1, 0x0021A221);, @# D* A+ k4 x8 R9 z' _" d1 M2 L CPen *pOldpen = pDC->SelectObject(&penCheck); 2 X; F+ E+ L. ^, ^ for (int j = 3; j < 10; j++)/ g5 D3 U1 \3 s { - _8 p' Y) C: ~- z% E y pDC->MoveTo(rectCheckBox.left + j, rectCheckBox.top + rectCheckBox.Height() /2 - 3 + ((j < 6) ? j - 1 : (9 - j)));# B3 Y: o( Z, c, I pDC->LineTo(rectCheckBox.left + j, rectCheckBox.top + rectCheckBox.Height() /2 - 3 + ((j < 6) ? j + 2 : (12 - j))); ! @" t6 f+ q( ]1 q8 [" T- P1 p }0 a- h) T0 {& _! B3 U$ M! l / r7 t" ~3 p4 u0 u% ]$ P pDC->SelectObject(pOldpen); 1 O, c' a$ t9 d* N2 s& g } - F2 A+ z4 p0 E. D8 i3 a } / {0 d4 ~: e! j7 r5 O // 根据需要画选中框 : J5 |* U& M+ v. e' J3 o if ( (GetFocus() == this || GetStyle() & LVS_SHOWSELALWAYS)&&(GetItemState(nItem,LVIS_SELECTED) == LVIS_SELECTED)) 1 D3 R/ |( l K$ y# q {6 \5 g9 Z0 y) f4 f5 T4 ` if (GetExtendedStyle() & LVS_EX_CHECKBOXES)/ B4 x. P w9 p2 b# b) [8 ? {// 存在检查框风格的时候需要对左坐标进行修正 0 ~" ]8 E! g8 |; b8 k& P& M CRect rectLeft;# O4 z' o/ M- J6 J9 v GetItemRect(nItem,rectLeft,LVIR_ICON);7 U- t) M: o8 G z- C6 ? rectSubItem.left = rectLeft.left;) B, [$ A7 y* A3 V. @ } + x: \! U( R7 n3 [/ V, z% i- k* } if (!(GetExtendedStyle() & LVS_EX_FULLROWSELECT))7 H" w7 u. o7 c; J, |/ T' J; g {// 非全行选择时右坐标需要修正 ' k5 ?3 n) u" C/ B CRect rectRight; J+ b6 |1 Q- z GetSubItemRect(nItem,nSubItem,LVIR_LABEL,rectRight);# d5 K8 ~ a$ Q6 _% p7 ?4 {4 A# t rectSubItem.right = rectRight.right;) y h4 Q& M+ {* m0 z1 b6 G } ) O& y; {) i- d7 h: c1 R CBrush brHighLight; 4 I; |0 [# B- ^. ? CPen penHighLightEdge;- E8 s+ Q, l1 ]! U if (GetFocus() == this) - ?" U0 ^: c% Q8 {! s9 Y+ M { , d6 C* W! I/ a1 C d! ] brHighLight.CreateSolidBrush(m_clrHighLight);8 K( V; M8 w6 B9 ^6 i) ? penHighLightEdge.CreatePen(PS_SOLID,1,m_clrHighLightEdge); 1 i5 {0 I. H& A& u% V: G }7 I: ]1 R4 P% @* Y6 n. E4 W; D else# G+ L2 L5 U3 w3 @: G {% G0 Z, V+ M8 k // 不是焦点时,颜色70%不透明度8 ]3 \1 n# O/ o; c/ D* l COLORREF clrBack = GetItemBackColor(nItem); % g4 p# a6 ]/ [) ?) {+ l+ H WORD red = GetRValue(m_clrHighLight)*0.7 + GetRValue(clrBack)*0.3; + T/ ?5 L$ }& q* e6 |5 @# Y WORD green = GetGValue(m_clrHighLight)*0.7 + GetGValue(clrBack)*0.3;7 x0 a: q+ Y% q WORD blue = GetBValue(m_clrHighLight)*0.7 + GetBValue(clrBack)*0.3;+ f1 i. S3 e& t brHighLight.CreateSolidBrush(RGB(red,green,blue));$ b, B( z/ X0 E! i red = GetRValue(m_clrHighLightEdge)*0.7 + GetRValue(clrBack)*0.3;& U$ @, M+ x( s1 j: K n* t green = GetGValue(m_clrHighLightEdge)*0.7 + GetGValue(clrBack)*0.3;% P' S3 Q9 x6 w( { blue = GetBValue(m_clrHighLightEdge)*0.7 + GetBValue(clrBack)*0.3; ; q# O/ ?, h1 F- `% x" x7 s5 ` penHighLightEdge.CreatePen(PS_SOLID,1,RGB(red,green,blue)); % E# K1 n$ a# s2 T }/ q3 T8 U2 e8 ?+ N& K$ t) o$ b# F CBrush *pbrOld = pDC->SelectObject(&brHighLight); : k0 E$ ?: i4 u5 r9 R CPen *ppenHighLightEdge = pDC->SelectObject(&penHighLightEdge);4 f+ }: _5 @4 a* x. a pDC->Rectangle(rectSubItem);# J( Y: v; b% O. q6 ]% |2 l pDC->SelectObject(ppenHighLightEdge);( t3 c3 Q) r" y. H' S* l pDC->SelectObject(pbrOld);$ w: K# O# t/ G0 N0 [' X! t' _ }) J4 ~) d: R+ A: f' M$ k% _* v k }# X/ L2 g( M* [' G& f0 k, p pThis->GetSubItemRect(nItem,i,LVIR_LABEL,rectSubItem); * [. |8 M# j8 r4 U8 w# ]$ X# Y UINT uFormat = DT_SINGLELINE|DT_VCENTER|DT_END_ELLIPSIS;& F; ^* W3 a+ Q N HDITEM hdi; 0 K$ q; [2 z3 c. _$ ~ hdi.mask = HDI_FORMAT; , J; I: J% x3 O5 @( P, I# N pThis->GetHeaderCtrl()->GetItem(i,&hdi);5 {4 s- r, D# i0 L. @ if (hdi.fmt & LVCFMT_RIGHT) # |. t. j; P1 r1 f4 k { $ W$ ^5 V6 W2 w uFormat |= DT_RIGHT;/ s+ T4 a @" [( k0 m- ]4 [' M% | } ' X0 U3 m( h& L6 X else if (hdi.fmt & LVCFMT_CENTER) & y9 _) ?) f- k% ~ { 3 E8 _9 o. k- E* d/ q! y uFormat |= DT_CENTER; : Y1 Y# i/ K) B } 9 Z- Q+ p5 q) J1 s l- j8 G+ J pDC->DrawText(strItemText,rectSubItem,uFormat);

CImageList *pImageList = pThis->GetImageList(LVSIL_SMALL);& u/ d+ U; d* f; {5 e# T if (pImageList)1 }9 s/ p& e" c- |$ {+ a {0 P1 r- _* Y6 M5 M+ s CRect rectIcon; ' @' A4 v% j* i: Q8 L2 [- \ GetItemRect(nItem,rectIcon,LVIR_ICON);' z* F% v( u: P pDC->DrawIcon(rectIcon.left,rectIcon.top,pImageList->ExtractIcon(GetItemImage(nItem))); # ]! v6 S, H) }2 v1 X1 G" E } ( Y0 }1 T* A7 g } 9 n1 r R1 s1 G *pResult = CDRF_SKIPDEFAULT;4 S, x7 c1 c; g3 o }

}

该用户从未签到

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

本版积分规则

关闭

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

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