下沙论坛

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

QQ登录

QQ登录

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

又是垃圾,CListCtrlEx Base CListCtrl

[复制链接]

该用户从未签到

跳转到指定楼层
1
发表于 2005-2-23 21:29:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
// ---------------------------------------------
7 S# N+ H3 H2 F9 Y// ListCtrlEx.h8 Q6 g/ T; w5 j8 ?& s1 @9 @
// ---------------------------------------------
# ~: A  L6 y/ R#pragma once
/ \4 P+ X- m% N: Y
7 R' o% a! \' `/ p// CListCtrlEx
; i8 J6 O5 G2 n# U4 vclass CListCtrlEx : public CListCtrl/ O$ Q/ X, `8 E' y6 i
{
7 F9 V+ J- U: B2 F( j% Z2 D* @/ y    DECLARE_DYNAMIC(CListCtrlEx)
+ G5 O+ e$ ?6 G. E; l8 Kpublic:% @9 h+ _3 j5 q& a. L2 ?. n9 _
    CListCtrlEx();
, [7 |# H. m, G% T    virtual ~CListCtrlEx();& u0 m2 V5 E- V! ^0 Q. e! e& m/ v
protected:: j. g. N  L: Y" ^: b
    DECLARE_MESSAGE_MAP()* H- M8 z6 G3 z% T- M
    CListCtrl* pThis;
" {) Y6 M1 a2 q9 P7 i! q    virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
# O7 e; a- Q5 M& aprivate:: `) B- X/ Q, V# m  v
    struct ItemDataEx {
& k: {3 r0 O' s) i  I        LPARAM SrclParam;
: K- _/ W1 M0 l" N5 H4 B8 L0 V        COLORREF clrItemTextColor;
4 g3 y( l* i1 j1 K6 l" e        COLORREF clrItemBackColor;
3 n- R' \: k4 I    };, p1 n  L5 S) K: V" m% q
    COLORREF m_clrHighLight;
" q' F( _9 {' T0 j8 _0 L0 e    COLORREF m_clrHighLightEdge;
: X/ P* J8 |/ epublic:: ]. w, y! J; J* H! z: z0 k7 I) h
    // 新增方法0 X6 t& R1 G: c+ D
    COLORREF SetItemTextColor(int nItem,COLORREF clrText);
2 q9 H% x6 R) x/ C3 d( v6 W) {- S    COLORREF GetItemTextColor(int nItem) const;
: A, ~& l6 b+ W1 t    COLORREF SetItemBackColor(int nItem, COLORREF clrBack);# `& V- o; f- u- ]
    COLORREF GetItemBackColor(int nItem) const;( I- R! z- R4 L
    COLORREF SetHighLightColor(COLORREF clrHighLight);/ O% H; V0 F/ ?" J( m7 }4 ~
    COLORREF SetHighLightEdgeColor(COLORREF clrHighLightEdge);" f# P2 c- i; q: \5 J- i. V
    COLORREF GetHighLightColor(void) const;& S$ J* G! y9 [, Y% [
    COLORREF GetHighLightEdgeColor(void) const;
* D5 Y( s* q, C' l3 }. `    int GetItemImage(int nItem);$ ~$ X! r6 a1 r& P5 Q
    BOOL SwitchItem(int nItem1, int nItem2);
% h) ]  M3 p( H4 N7 {    BOOL MoveItemUp(int nItem);$ J. x/ I0 j) d* W2 T
    BOOL MoveItemDown(int nItem);1 X- K0 n: ]5 x* P! x
    // 重写原来的方法
+ V6 `/ L( M% h- E' T2 i    DWORD_PTR GetItemData (int nItem) const;
0 W; B8 f9 a3 N# j( c/ r    BOOL SetItemData (int nItem, DWORD_PTR dwData);
+ T0 ?+ M1 c: K" J    int InsertItem (const LVITEM* pItem);4 C. N, ~1 W  y. @
    int InsertItem (int nItem, LPCTSTR lpszItem);: s4 u( j9 @' I; x3 M
    int InsertItem (int nItem, LPCTSTR lpszItem, int nImage);
  J1 E# n, X; P  |& r! J7 H    int InsertItem (UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam);
' @8 l0 M8 J+ Y0 ], M8 M    BOOL DeleteItem (int nItem);; O% }( Z9 k0 u4 o
    BOOL DeleteAllItems ();
6 T" w  o* H  A# ]5 g2 F3 ~- s/ _    BOOL GetItem(LVITEM* pItem);" ?1 b: H( C: W" H* t9 J# f7 M, w
    afx_msg void OnDestroy();& M8 Y3 t, w# d7 I
    afx_msg void OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult);
  |) S2 H0 T9 M3 }};
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 顶 踩

该用户从未签到

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

// --------------------------------------------- . h7 o& G4 b) J4 `0 t) R4 [// ListCtrlEx.cpp* R B; ^: V2 x" X // --------------------------------------------- l' j$ K8 G, d( M" o t// ListCtrlEx.cpp : 实现文件 3 ]8 v$ n/ X5 p8 m//

#include "stdafx.h"% o% L9 ~: b4 X. B #include "CreateNewClass.h"7 ^+ S w2 ]+ g v #include "ListCtrlEx.h" - a6 b4 B( A2 |# ]2 q#include ".\listctrlex.h"

3 Q1 ^9 y5 I) C9 r4 d$ c# @8 A5 q// CListCtrlEx

IMPLEMENT_DYNAMIC(CListCtrlEx, CListCtrl)! h7 x2 n* U$ M ^ CListCtrlEx::CListCtrlEx(). Z. _; R2 Y- a, ` { % S, \3 W% b1 ~% _4 ~1 _7 c // 用于调用基本的方法( m5 |$ n, e3 U* K5 g, k pThis = (CListCtrl*)this; - w8 \3 f* l9 ^% L7 l // 默认的选中框颜色- I4 b7 v" l+ q8 v3 r m_clrHighLight = 0xF1DACC; . ?" P! y% i5 |7 H3 H m_clrHighLightEdge = 0xC56A31;* a5 c7 a9 i+ A$ p9 N# c1 I }

CListCtrlEx::~CListCtrlEx()8 y0 z" }. l7 _1 W0 c' { {

}

" ~% T2 x& W4 n& K- k% s( iBEGIN_MESSAGE_MAP(CListCtrlEx, CListCtrl)/ I/ v2 |+ G- C% V ON_WM_DESTROY() ! g7 d6 D; x* s2 A ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnNMCustomdraw)$ a, X" W# v* Y0 J# X: _; | END_MESSAGE_MAP()

// CListCtrlEx 消息处理程序

! G3 v" p ?+ k; }) P* Y% S' a, _ COLORREF CListCtrlEx::SetItemTextColor(int nItem,COLORREF clrText); ^0 b' L$ n( g" O7 m! m7 \ {( O5 v' j0 `# Y: V COLORREF clr = GetItemTextColor(nItem);4 ?( R) Q3 N; y5 Y* _- z ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem); 3 i6 d3 ^& L& X+ Z pData->clrItemTextColor = clrText;4 c# ~* ]+ {/ S6 b; Y0 R3 | return clr;! b+ C9 d% \+ _2 ?" ]8 i }

COLORREF CListCtrlEx::GetItemTextColor(int nItem) const ! K! K7 ?$ I3 S& k8 ~8 i j{2 _' N7 R- \" t/ y4 G ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);0 K* z7 N9 C! v/ Z$ Y8 Y return pData->clrItemTextColor;7 @. Q+ M5 b. e# Q5 j }

COLORREF CListCtrlEx::SetItemBackColor(int nItem, COLORREF clrBack) 0 b) f5 e" S r T{ ' R8 S5 v, o! d5 F8 [! t1 r& r COLORREF clr = GetItemBackColor(nItem); 7 E& L+ x9 u4 O+ d/ G# U* { ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);7 G; k6 J6 J/ @9 ]' { pData->clrItemBackColor = clrBack;) D/ M# x% [$ ~ return clr; 6 }" A! T; A) D) M3 F. Z$ `}

COLORREF CListCtrlEx::GetItemBackColor(int nItem) const3 L8 m; j' F$ x; { {( x1 ?, N/ w! n6 u ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem); : [$ n% J9 X! j' E/ t$ B3 \ return pData->clrItemBackColor;* V5 O# A6 h1 V } 0 E! x' A9 T0 T5 `: f3 DDWORD_PTR CListCtrlEx::GetItemData (int nItem) const6 E$ f& k! f% c$ m W x {4 t, V4 y* k6 f6 W3 L. y, a8 u2 a$ } if (nItem >= GetItemCount()) $ m' A8 _0 ^0 D3 n5 \ { 9 ?* U) v% Q/ Q6 Q return 0; % | `# E' c% W9 J _! S$ s$ V+ U1 Q- y }1 P1 e; T1 [3 L* n CListCtrl* pThis = (CListCtrl*)this; 2 E& ?, ^+ E- z0 D, R! _ DWORD_PTR Data = pThis->GetItemData(nItem); / L! b2 ?: ^5 F; {, P return ((ItemDataEx*)Data)->SrclParam;5 Q* G, v6 h/ A0 m% d9 F }! J6 ]( P/ u1 \ BOOL CListCtrlEx::SetItemData (int nItem, DWORD_PTR dwData)7 L4 _ w1 Q4 J8 v( C6 W0 G+ `# ^2 L2 J {# x& x# ~$ t' F% A if (nItem >= GetItemCount())2 J+ D4 J* ^" j$ ~ {* X# T( J) t" j" D0 A return 0; , {) T8 i+ b" Z4 J& c }' _( j/ @8 O) N s, \ CListCtrl* pThis = (CListCtrl*)this; y/ A" A1 ~0 h. r0 D DWORD_PTR Data = pThis->GetItemData(nItem);) r, ^. W/ S6 X/ g ASSERT(Data); * A3 y5 |5 ]) S' Y" G. E+ `: M ((ItemDataEx*)Data)->SrclParam = dwData; ' _% n. J' D+ O( j" `: w return 1; : [4 L! m% H2 v8 Z} V; o% F5 Q1 y% [" qint CListCtrlEx::InsertItem (const LVITEM* pItem) 0 T* t$ Y5 r" e# u{& n8 ]& G5 ^) h- d int nReturn = pThis->InsertItem(pItem); 1 w1 \6 i7 a& t# V. O. H ItemDataEx *pData = new ItemDataEx; - m6 ^' j0 `/ ]) ^. K# h" M // 设置默认颜色6 Y0 n0 _/ S% y' Q+ o pData->clrItemBackColor = 0xFFFFFF;//pThis->GetTextBkColor(); & O5 I7 ~: a( r7 g8 H% c pData->clrItemTextColor = 0x000000;//pThis->GetTextColor(); * W+ J9 }. G( p, T4 u1 \ pData->SrclParam = pItem->lParam; . P# e7 I6 y% t pThis->SetItemData(pItem->iItem,(DWORD_PTR)pData); ) O; x+ ?1 j2 Y; z6 k% D% o return nReturn;) V' O0 j d9 @% Q( B" \0 ? }1 S: f! `5 ?, s9 `( h- v) M7 M2 } int CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem) 7 V B4 ?' Q* E: F{6 A& M u- n: l- N LVITEM Item;# T4 G+ Q5 |; \% e memset(&Item,0,sizeof(LVITEM));' g6 b3 i. ?$ X3 s4 |2 q Item.pszText = new TCHAR[strlen(lpszItem)+1];/ ~2 \ U, m* D6 n* K k! b strcpy(Item.pszText,lpszItem);2 z1 m) W- a2 |3 q% c U Item.iItem = nItem;4 e) J. @( _% b# S+ L; f Item.lParam = 0; ( X8 w& D0 `2 b7 ?" ] Item.mask = LVIF_TEXT |LVIF_PARAM;% A0 c2 p8 |# C1 t; } BOOL bReturn = InsertItem(&Item);% P0 {4 r# Y0 k( ]# A4 _ z/ s7 D delete Item.pszText; 4 x4 |, k, f! K! W return bReturn;! D. _7 J: R3 g ]7 Q: z }2 ?% J% c/ d( n3 a' a" ?% S int CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem, int nImage)! H, j: i) d% |- B* l; e { , U* x* _$ B! R8 F. a! J LVITEM Item; % P# O/ }3 [* c, b7 f! K M, j2 h memset(&Item,0,sizeof(LVITEM)); ; w; {2 B8 q) ? [' u Item.pszText = new TCHAR[strlen(lpszItem)+1];: t6 J! q3 G1 c2 B* m* w strcpy(Item.pszText,lpszItem);; n. a; h9 p) v5 |) P' e* K Item.iItem = nItem; + X. g, v- n" ~ l6 k E Item.lParam = 0; " @2 m" K" F V Item.iImage = nImage; * }, g4 n( ] d! J# _6 I Item.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE; + K1 l" g% ?) x6 |; v8 E' T BOOL bReturn = InsertItem(&Item); 2 | A1 A1 T5 G delete Item.pszText;/ s& T( e* k2 {) A' r. ^ return bReturn; ( h) J+ t: b7 u/ k" I4 D0 v3 i* [}* z5 M7 L0 ], j/ { int CListCtrlEx::InsertItem (UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam)8 s# d8 N! q' j7 C { ( C1 o. t4 e& K& m: o4 Q LVITEM Item; , z# B( z2 `0 f8 [% O1 A memset(&Item,0,sizeof(LVITEM));1 F$ b9 g3 a1 d' F Item.pszText = new TCHAR[strlen(lpszItem)+1];8 u2 w& N! N* z" { strcpy(Item.pszText,lpszItem);) S1 ~4 @+ L2 ~, A Item.state = nState;: c2 Y) Y5 }$ n+ M; ]7 }5 i' L Item.stateMask = nStateMask; : U0 Q# k- Q1 u; H Item.iItem = nItem;6 g6 I$ [2 i; m Item.lParam = lParam;7 A L: ^& T' {: `. E8 K6 b5 V Item.iImage = nImage;. H' e1 D4 U7 E Item.mask = nMask;9 i% t% d$ k* a/ B8 P BOOL bReturn = InsertItem(&Item); / P {# ~# Q8 R7 P( Z/ U9 ?' P delete Item.pszText;; Y' o0 K4 m# W/ d% R: F return bReturn; + H& |7 t8 u- m) H) a}+ U0 V, L/ z: K2 L7 E9 p BOOL CListCtrlEx:eleteItem (int nItem)6 S5 v) X: `" ^4 F { a( h- }9 r( G void* pDataEx = (void*)pThis->GetItemData(nItem); & Y- ?: p4 p# c, W& j if(pDataEx != NULL)& c h* K. O3 w% C3 I { 4 Y. S* G: s" V: _' v1 W delete pDataEx; - `$ q0 ^- b2 B2 {, M# d }9 U/ C8 Q4 N' `+ C4 Y- f0 y& S- { return pThis->DeleteItem(nItem); 4 M, z( c" w7 ?* @5 P3 h} : Z8 U) L' k$ Y& w9 y& R4 l( l YBOOL CListCtrlEx:eleteAllItems ()7 R! T! t+ X; h; m1 H { ! h0 ]; t1 A4 M% z' _" R5 n int nCount = GetItemCount(); - {4 ~# d- o% I" M; u for (int i = 0;i<nCount;i++)$ S2 E# n& c0 R3 [* W5 W+ S { ' d- @/ d* ]- J3 k; M1 @ if(!DeleteItem(0)) 2 y/ _% G2 Y% i. {/ r {2 l, O, I2 J$ T0 y! x" q% F return 0;1 v5 u: I& N6 g- r2 Q) ]% A7 q } ' R. K8 ~6 ?$ k" u* A } ( s% }6 K- k7 [* e return 1; + _% k& z3 w( U% P}/ n& X4 l( A; g9 t5 S, t1 i9 Q BOOL CListCtrlEx::GetItem(LVITEM* pItem)( Y! m% Y, V: w6 r1 @) y2 [2 l { . i6 f4 v5 {: W* L BOOL bReturn = pThis->GetItem(pItem);4 A& U, i1 l/ B2 r if (pItem->lParam), Y+ O( ?0 C- \ { ( z" c V1 u/ m. U) `4 D$ L pItem->lParam = ((ItemDataEx*)pItem->lParam)->SrclParam;, p$ L6 c( _. ]/ W4 ]% C* k& D; M" m }

return bReturn;. W; M/ V$ g0 }: d* }4 G- Z2 p; \ }: k4 @% t- m! d) S5 [8 m, o, n void CListCtrlEx::OnDestroy()! Y( r: U: y% y' h5 I, O { # t' o8 n; ]: _- O. }4 ?0 [ // 关键是清除申请的内存空间 1 u; K) K( @/ ~2 X+ l h* K DeleteAllItems(); , ]; X9 S% L8 ^ CListCtrl::OnDestroy(); ' b7 {& |* e# X}

BOOL CListCtrlEx::SwitchItem(int nItem1, int nItem2) 2 U2 V+ _1 D* _{4 L: T2 n6 ^4 \3 T7 w6 P9 ^5 n4 ^ if (nItem1 == nItem2 || nItem1 >= GetItemCount() || nItem2 >= GetItemCount() || nItem1 < 0 || nItem2 < 0) " Z+ {/ y) x. G( v { 9 x# p) S+ P2 X! Y2 v* j9 Z# g return 0;4 \6 G" c( i* h$ F% a* |, b# { }% p) Q9 _" w$ Q h LVITEM ItemData1,ItemData2; 0 C6 C' r2 \; i9 t& }+ { int ItemLength1 = pThis->GetItemText(nItem1,0).GetLength();1 U) a: ~! O6 E7 E1 a( m int ItemLength2 = pThis->GetItemText(nItem2,0).GetLength();# c5 C/ q( z3 y" ?+ I* Q! g% O ItemData1.pszText = new TCHAR[ItemLength1+1]; 5 f \8 X2 V9 r$ v( \0 I) C ItemData2.pszText = new TCHAR[ItemLength2+1];0 M M+ u* f! b k s" u ItemData1.cchTextMax = ItemLength1+1; 8 E: j: K# }' R ItemData2.cchTextMax = ItemLength2+1;( H9 s0 y% g8 j ItemData1.iItem = nItem1; 2 z, @- j' ?+ D ItemData2.iItem = nItem2; + G# G$ Q3 R; s, S ItemData1.iSubItem = ItemData2.iSubItem = 0; 3 g3 t1 [& Y' @$ v) o3 D ItemData1.mask = ItemData2.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE|LVIF_STATE;

pThis->GetItem(&ItemData1); % V8 Z& K$ U- y; j2 H2 R; f pThis->GetItem(&ItemData2);' F) H8 B* w+ u1 Q // 交换数据& V# `7 l/ A$ N3 h ItemData1.iItem = nItem2; . S( w1 H- m" Q ItemData2.iItem = nItem1; ' @; v1 Q7 p4 Y2 ]" h: C pThis->SetItem(&ItemData1); # _5 I( b: w; I/ L- G pThis->SetItem(&ItemData2);

delete ItemData1.pszText; : V; \7 \# b, _- b. t delete ItemData2.pszText;1 `3 V: \ E4 O* P // 得到报表模式的列数 / a8 R2 V9 T5 [! @: t int nColCount = pThis->GetHeaderCtrl()->GetItemCount(); 4 F- z9 `5 Y( o. j CString strBuffer; % S6 K1 O& F) Z2 D$ ` for (int iSubItem = 1;iSubItem < nColCount;iSubItem++)1 X c$ n3 _5 a: c9 h { 2 n# ~) O% b0 z6 V strBuffer = pThis->GetItemText(nItem1,iSubItem);. z# [2 x8 u' ` pThis->SetItemText(nItem1,iSubItem,pThis->GetItemText(nItem2,iSubItem)); 8 A4 l5 k4 |2 g5 { pThis->SetItemText(nItem2,iSubItem,strBuffer);9 L7 j5 u, i" }) n: B" P }# X, U& E8 ?. x- d // 把原来的选中状态也转移过去( D: a2 }' O, z4 U* C! M // 不知道为什么这个属性没有体现在LVITEM结构中……: t( M" d7 C4 r' u bool bItemSelected1 = GetItemState(nItem1,LVIS_SELECTED) == LVIS_SELECTED;- V7 m! z* ~" y( w4 K bool bItemSelected2 = GetItemState(nItem2,LVIS_SELECTED) == LVIS_SELECTED;

SetItemState(nItem1,bItemSelected2?LVIS_SELECTED:0,LVIS_SELECTED);+ @4 m1 d7 t" z1 D6 O SetItemState(nItem2,bItemSelected1?LVIS_SELECTED:0,LVIS_SELECTED); & E$ U- u1 f$ n" Q" z" \9 K( p+ ] return 1;+ s" W1 _5 X; X }

BOOL CListCtrlEx:reCreateWindow(CREATESTRUCT& cs) 8 }' S, P+ z: T. S* i{ 1 p* E: U$ _, }: ^$ e // 本类报表模式专用 6 c; @0 I% Q# D cs.style |= LVS_REPORT ; 5 f2 L/ A. ^9 e# G) i/ h // cs.dwExStyle |= LVS_EX_FULLROWSELECT;: N3 C% L3 D# D: K# D1 } return CListCtrl:reCreateWindow(cs); , ^5 Y. H, W: p; F& A' ?6 J) d2 i}

: u# X! F5 a$ | o- \COLORREF CListCtrlEx::SetHighLightColor(COLORREF clrHighLight)# z% S* c2 n+ l5 p+ C { 4 L2 @9 t4 _) ^, ?1 x0 R COLORREF clr = GetHighLightColor(); " Y9 }+ V" V0 a2 j& [$ M9 `/ J. | m_clrHighLight = clrHighLight;: {5 t% Y8 e( P+ \5 E4 ~ return clr;4 k4 z# Y$ ?0 ]9 H; p, | }

COLORREF CListCtrlEx::SetHighLightEdgeColor(COLORREF clrHighLightEdge) ! f) @* k5 ^9 p, K* P, K) \3 b- I{- n& g4 T7 r* W COLORREF clr = GetHighLightEdgeColor();% H) n+ r1 X/ n: `. U* K m_clrHighLightEdge = clrHighLightEdge; : A k* B) v( Z5 H5 `) _ return clr;) v5 m$ g% R8 l; m( R" k% h }

该用户从未签到

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

COLORREF CListCtrlEx::GetHighLightColor(void) const ( Y. o$ I, [( B6 A& u{ * y- ?. C) `, } Q7 o/ E5 |( l return m_clrHighLight; , l( P* q9 W/ N% G# f! A}

COLORREF CListCtrlEx::GetHighLightEdgeColor(void) const W" _9 c0 z% w {6 m4 {# \- Q/ O# y$ x+ \/ q% v return m_clrHighLightEdge;5 \/ m2 Y, @$ E1 k1 v$ a2 C4 ~ }

int CListCtrlEx::GetItemImage(int nItem)& ^4 K- I, h0 O {8 J9 Q2 s3 ?$ z) J! ~3 J$ g9 ? LVITEM ItemData;) {2 R! H: h" X _5 j- f ItemData.iItem = nItem;5 S2 E0 ? T& x# X k0 H% x ItemData.iSubItem = 0; " O, [/ p3 B2 n ItemData.mask = LVIF_IMAGE; 7 q, {& P( A! j8 g9 { pThis->GetItem(&ItemData);& ?3 u/ [& R% }$ A! Q8 S return ItemData.iImage;% G: C; L$ w+ {# q3 k8 X6 w }

BOOL CListCtrlEx::MoveItemUp(int nItem)% b; l# B T2 c5 e { + H* Y. t8 y( K( g+ i( E* G if (nItem < 1 || nItem >= GetItemCount()) . H' a" q9 E/ f, R+ p! b; V { 5 z/ q( V c+ H/ ^3 }$ y4 t return 0;( F o0 x9 G0 E6 Z H5 P# B1 w } + p j: F1 v9 P I ` return SwitchItem(nItem,nItem - 1);# f3 C" G, c: X9 G3 n. k }

BOOL CListCtrlEx::MoveItemDown(int nItem) 2 D) F. y' S# |- }1 B{ ) k* D, ^8 u- d4 j1 N* u7 F if (nItem < 0 || nItem >= GetItemCount() - 1) : f/ a' s) K& Y- W {7 Y8 ?7 |, I& X$ [5 ~; P return 0;3 t& U, N ^ r" r4 \ }: V; h+ n, F' R7 z3 n return SwitchItem(nItem,nItem + 1); 7 y& _" Y7 E' z$ T8 W}

void CListCtrlEx::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult): y1 }: x' L4 [8 Q5 X3 I/ n { 3 w4 J6 ^) q' Z( H0 x8 b6 B NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR);

*pResult = CDRF_DODEFAULT;

if (pLVCD->nmcd.dwDrawStage == CDDS_PREPAINT): B! T P$ z2 B8 N) n) T { , W2 @0 X" P3 ~5 u% Z/ W *pResult = CDRF_NOTIFYITEMDRAW;" y# N! l% l, m& }0 t3 R } ( P3 @" s. |/ r else if (pLVCD->nmcd.dwDrawStage == CDDS_ITEMPREPAINT) / ~" _; ]# E5 J! H3 C { & Z0 a' Y/ c ~7 Z0 J Y& Q *pResult = CDRF_NOTIFYSUBITEMDRAW;4 d% O' }% J* W4 `. Z }7 U) ?; }, W' m" J- a ~4 k7 q else if (pLVCD->nmcd.dwDrawStage == (CDDS_ITEMPREPAINT | CDDS_SUBITEM))3 Z) N d- n# Y, L0 t, A {

int nItem = static_cast<int> (pLVCD->nmcd.dwItemSpec);2 z4 x! T$ J- ~$ F int nSubItem = pLVCD->iSubItem; : _( j: ?+ ^" m( G* A if (nSubItem != 0)6 y3 q$ Y* }+ l { $ j' l$ H2 j3 B: ]) P4 s // 全部绘制过程在nSubItem == 0 时完成! U1 u1 T1 f! c+ c' Q // 分布完成会有小问题,我无法解决 : |# }+ U/ w$ u) r8 d, M& M *pResult = CDRF_SKIPDEFAULT; 6 b) x# k! j" R6 Q! \* G return;0 E0 C4 f, L1 b }: q3 u! r3 o* s CDC *pDC = CDC::FromHandle(pLVCD->nmcd.hdc);; s+ ^9 p4 V. H int nColCount = pThis->GetHeaderCtrl()->GetItemCount(); 6 m5 x) X+ ^- j: l CRect rectSubItem; ! [9 h( J0 s) X/ S CString strItemText; 3 B) B& o+ g! O' K* ]5 L( c pDC->SetTextColor(((ItemDataEx*)pThis->GetItemData(nItem))->clrItemTextColor);1 j* T. `5 c0 M" u3 q2 u pDC->SetBkMode(TRANSPARENT);! G% T: s5 o& w6 r$ {% Z8 z // 相当于橡皮 . J& Y) @5 |& t! W M) l7 e CBrush brEraser(GetItemBackColor(nItem));

// int nFirstWidth = GetColumnWidth(0);

& D* s4 V; e7 R- q for (int i = 0;i < nColCount;i++) & N- y$ V @' X& L; U C- j& X { # A2 H+ _$ w- M& h strItemText = GetItemText(nItem,i);

if (i == 0) ; Q% V$ o" P) x+ g& a7 H* b {: f* r3 D% p) i pThis->GetSubItemRect(nItem,i,LVIR_BOUNDS,rectSubItem); 5 I' a( i2 e0 G$ s! g% N8 ` // 擦掉原来的图形 2 C8 ]. U( q' F0 v; D. g. D. i# q: Q pDC->FillRect(rectSubItem,&brEraser);+ t( S# b6 F$ f2 c8 ^* M& w) k" S if (GetExtendedStyle() & LVS_EX_CHECKBOXES)* Y8 l9 s/ O6 Q. }, S' W! R {6 ^6 M- j5 s% a2 h3 v( ^ CRect rectCheckBox; 1 e+ }* w9 U' D, g {. [$ ` GetSubItemRect(nItem,0,LVIR_ICON,rectCheckBox);! s8 b! i( z3 W CRect::SwapLeftRight(rectCheckBox);' Z. G: e7 l* `) y" p) @ rectCheckBox.left = rectCheckBox.right - rectCheckBox.Height();

rectCheckBox -= CRect(2,2,2,2); 7 X. o7 h7 }% s* [4 e0 v% z( D pDC->Draw3dRect(rectCheckBox,0x81521D,0x81521D); / X# `3 d: W: A) v& M //rectCheckBox += CRect(3,3,3,3);7 ^' J# y0 o8 p: C, v( g. D; h if (GetCheck(nItem)) 6 \& y1 d7 o/ t& L( n8 o { / j T. O5 F R // 绘制一个CHECKBOX# h) z. E) H) U CPen penCheck(PS_SOLID, 1, 0x0021A221); # \- o5 @5 Z6 Q/ ]! S CPen *pOldpen = pDC->SelectObject(&penCheck);& r2 M$ ]4 S& a for (int j = 3; j < 10; j++) 0 b/ }) \6 O: U { $ g p% r4 d7 Q& v pDC->MoveTo(rectCheckBox.left + j, rectCheckBox.top + rectCheckBox.Height() /2 - 3 + ((j < 6) ? j - 1 : (9 - j)));2 M, z- ^% b: l. G( v9 G6 n pDC->LineTo(rectCheckBox.left + j, rectCheckBox.top + rectCheckBox.Height() /2 - 3 + ((j < 6) ? j + 2 : (12 - j))); $ }% C1 r, g: [. w: A2 h: s }' V3 }; N5 j- @; \4 R1 U1 e 1 ?; |0 |5 {; L pDC->SelectObject(pOldpen); ! Y8 v. b" o! [' [8 f7 {7 h } ( A: B; c! n2 L" A8 _ }% j' [( @# }5 J) Z* W // 根据需要画选中框+ v* b# f+ L' r% s9 P if ( (GetFocus() == this || GetStyle() & LVS_SHOWSELALWAYS)&&(GetItemState(nItem,LVIS_SELECTED) == LVIS_SELECTED)) ' a; L/ f. `- y { 5 f" [" y0 k) H if (GetExtendedStyle() & LVS_EX_CHECKBOXES) 3 f3 U( F+ J$ o {// 存在检查框风格的时候需要对左坐标进行修正 : Z& P$ Q E- W8 _5 g4 O CRect rectLeft;* p: p3 ^' J4 U* j2 k' g5 A( U# | GetItemRect(nItem,rectLeft,LVIR_ICON);' z6 ]3 K, o# q4 X" \2 @ rectSubItem.left = rectLeft.left; ( g5 {6 d( x1 I! I9 p I3 s' M. d }: X# Y( Q9 H2 q# i if (!(GetExtendedStyle() & LVS_EX_FULLROWSELECT))/ T. b+ F+ C) O h {// 非全行选择时右坐标需要修正: D4 d; \* g, a# t5 L8 ? CRect rectRight;8 \' p, ?, Z ]0 e GetSubItemRect(nItem,nSubItem,LVIR_LABEL,rectRight);: N/ B9 l0 C/ T0 A" }% g ]9 v) F rectSubItem.right = rectRight.right; : A: ^1 v) e. l9 W, u }9 j2 \8 k4 S5 m; o CBrush brHighLight;6 R Y+ C# b% o! K0 [% U0 a( N# i! c CPen penHighLightEdge; 1 Y% n6 \1 p5 l" h' L f3 @0 R if (GetFocus() == this)$ k& M( Z& _2 w& [5 `9 q {6 Z( F. U& x) T5 k* `0 m brHighLight.CreateSolidBrush(m_clrHighLight); & G& x6 p5 q' p/ Y/ \ penHighLightEdge.CreatePen(PS_SOLID,1,m_clrHighLightEdge);9 r* S# C, Q7 d } & L2 t9 A" l S else * U0 u. s \& J7 G, | { ( n& h' K9 [! T$ M4 A8 ]4 `. | // 不是焦点时,颜色70%不透明度 8 {) M7 J1 ~: Y2 W N% ` COLORREF clrBack = GetItemBackColor(nItem); / y% b) u, r7 M# X& L WORD red = GetRValue(m_clrHighLight)*0.7 + GetRValue(clrBack)*0.3;7 V9 {" N) T: j WORD green = GetGValue(m_clrHighLight)*0.7 + GetGValue(clrBack)*0.3; + v% `8 a& Z9 u% q WORD blue = GetBValue(m_clrHighLight)*0.7 + GetBValue(clrBack)*0.3; & _: [' b' W8 E- h0 |5 Y' W8 \5 ] V$ {5 _ brHighLight.CreateSolidBrush(RGB(red,green,blue));- _( L6 O5 Y( |6 d6 H; z- V red = GetRValue(m_clrHighLightEdge)*0.7 + GetRValue(clrBack)*0.3; T6 C) o+ M# t( r/ G/ W* S) b green = GetGValue(m_clrHighLightEdge)*0.7 + GetGValue(clrBack)*0.3; 4 r" s5 |1 l' f- C# n! N blue = GetBValue(m_clrHighLightEdge)*0.7 + GetBValue(clrBack)*0.3;% O/ J, x0 }% a2 w) e penHighLightEdge.CreatePen(PS_SOLID,1,RGB(red,green,blue));# M4 R: [4 x; Q1 `+ Y } h* V3 m- ]+ G CBrush *pbrOld = pDC->SelectObject(&brHighLight); : G: r' Y! T2 T CPen *ppenHighLightEdge = pDC->SelectObject(&penHighLightEdge); + t2 D$ `; }/ p% o( I- n pDC->Rectangle(rectSubItem); 0 `$ T( R( ?/ Y. k: k. o, |" t pDC->SelectObject(ppenHighLightEdge); ( d# |; P# s: [: G; s% B1 o7 D& b pDC->SelectObject(pbrOld);9 r! n# x" N: R* p } ) d2 n) I/ ]8 t. H# E7 J }& M: g/ B: N. f2 n pThis->GetSubItemRect(nItem,i,LVIR_LABEL,rectSubItem); . R3 x2 f+ |' a; v5 B! x UINT uFormat = DT_SINGLELINE|DT_VCENTER|DT_END_ELLIPSIS; ( }% p) s8 i P, D+ u8 E% f2 A9 T% w* t2 e HDITEM hdi; ! K6 s2 X1 R3 F0 {3 v0 { hdi.mask = HDI_FORMAT;6 V3 q2 {8 _- V Y) U' { pThis->GetHeaderCtrl()->GetItem(i,&hdi);4 R, S+ `; [8 z7 W if (hdi.fmt & LVCFMT_RIGHT) " ~& ^6 n& ~2 Y0 `9 k { & K' E3 B/ y; b' a uFormat |= DT_RIGHT; " f* [5 m+ q1 e5 Z, c }5 r- ], ]0 b: o else if (hdi.fmt & LVCFMT_CENTER)* a/ f8 ` `/ @+ K7 s0 R {8 N# Q* W' Z5 ]5 B3 b uFormat |= DT_CENTER;( [/ M3 g+ K# U$ X }" H( z. @- w& P pDC->DrawText(strItemText,rectSubItem,uFormat);

CImageList *pImageList = pThis->GetImageList(LVSIL_SMALL); 1 K% X2 l, M# f; N& b5 J if (pImageList)% ]' j1 R5 r1 S& \, m7 k { * Q( l5 G1 D0 I: z2 M; c CRect rectIcon; ' U) p( i- d- s, _ GetItemRect(nItem,rectIcon,LVIR_ICON); ) Q: B* o9 u, X% A pDC->DrawIcon(rectIcon.left,rectIcon.top,pImageList->ExtractIcon(GetItemImage(nItem)));4 `! L& t: I2 Q% ~ } 1 G( V1 H7 O4 Y x. ~- [ } ! @* t% F t2 t# L4 I- ]1 I *pResult = CDRF_SKIPDEFAULT;4 D9 Q+ Q9 u; Q. A5 P }

}

该用户从未签到

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

本版积分规则

关闭

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

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