下沙论坛

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

QQ登录

QQ登录

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

又是垃圾,CListCtrlEx Base CListCtrl

[复制链接]

该用户从未签到

跳转到指定楼层
1
发表于 2005-2-23 21:29:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
// ---------------------------------------------
7 b+ c& [* k; z2 Y3 i// ListCtrlEx.h
8 w1 V0 u. y3 v6 S' b; j% {# b// ---------------------------------------------
4 `- K9 h0 T( N0 D, ?# b3 F3 |0 [#pragma once" C7 a+ A1 x* p6 v1 }* T

2 H, v9 p, }  B8 X+ v' }// CListCtrlEx
9 X, H  ~4 W$ r' r4 Y2 jclass CListCtrlEx : public CListCtrl
; H& A: v  v1 l$ d" @  E2 u{5 ~; A/ Y4 p% w  L
    DECLARE_DYNAMIC(CListCtrlEx)) B, M/ l' O% Y4 X' B: b
public:
: |/ s$ `' J) S8 d; L    CListCtrlEx();
  n( l8 B, w9 D, w+ ~    virtual ~CListCtrlEx();
" W; }- }' g4 `, V* `6 R! uprotected:$ u  p1 O/ X+ M  M
    DECLARE_MESSAGE_MAP()
) N$ ~& V# ^% {2 a6 a    CListCtrl* pThis;5 Z" D% }3 g& U' z8 b
    virtual BOOL PreCreateWindow(CREATESTRUCT& cs);# P/ F3 L' K# D8 i7 L
private:
, n" a9 |# i6 P5 ~3 t/ }    struct ItemDataEx {
: O1 T8 y* R- ]0 M        LPARAM SrclParam;' e2 z# i1 R/ t" p' z
        COLORREF clrItemTextColor;
! q* t! m2 C; o0 z4 Z! v1 @8 h) _        COLORREF clrItemBackColor;
$ H" @% H: E% g    };7 M, u) R- N4 W
    COLORREF m_clrHighLight;7 Z- Q% B1 a- D' V/ z5 @6 H
    COLORREF m_clrHighLightEdge;6 {. {6 j4 {. [0 |
public:
- Q2 d6 j$ V* H1 n# q7 R$ v    // 新增方法" \6 g$ G- m* O, F: v5 M
    COLORREF SetItemTextColor(int nItem,COLORREF clrText);& m0 W1 X5 Q" J# P. ^: o
    COLORREF GetItemTextColor(int nItem) const;
9 Q# O6 K5 E9 C! `( W6 q8 _: ^4 m" y3 ?    COLORREF SetItemBackColor(int nItem, COLORREF clrBack);& r" c' b! F/ r* l' q5 \
    COLORREF GetItemBackColor(int nItem) const;1 E4 o" f" O9 B0 Y- N
    COLORREF SetHighLightColor(COLORREF clrHighLight);9 o0 l: \, }. I7 n
    COLORREF SetHighLightEdgeColor(COLORREF clrHighLightEdge);
) j* W+ [) ]+ ^8 {' s    COLORREF GetHighLightColor(void) const;
  B: d, p  q: z2 F: p7 U* i    COLORREF GetHighLightEdgeColor(void) const;# t3 X* {7 U" d0 `# J
    int GetItemImage(int nItem);
6 I- b/ Y; Y( l# f& D1 V    BOOL SwitchItem(int nItem1, int nItem2);8 y3 P( |/ O/ c: i+ H
    BOOL MoveItemUp(int nItem);
2 N( p3 I: a& O1 |4 v! B) I% A    BOOL MoveItemDown(int nItem);
& D# q& K  O/ U, ~! _, f    // 重写原来的方法
/ ^' C1 I) d: B( a  Q    DWORD_PTR GetItemData (int nItem) const;" D# n- H2 h9 Q' |! m1 |
    BOOL SetItemData (int nItem, DWORD_PTR dwData);
; v; p" O0 ~; k    int InsertItem (const LVITEM* pItem);
4 \) Y2 {2 z. W3 Y2 J' F6 V+ v    int InsertItem (int nItem, LPCTSTR lpszItem);
8 w( D4 `: Y( E4 S" C! X    int InsertItem (int nItem, LPCTSTR lpszItem, int nImage);
! f/ l) b1 a, V( G; ^4 b    int InsertItem (UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam);
' K$ {) S" T. |' u0 K    BOOL DeleteItem (int nItem);/ J& I$ v" T( O0 {( L5 T
    BOOL DeleteAllItems ();0 X. K% Y( Y  o- k8 A0 m
    BOOL GetItem(LVITEM* pItem);- s. c5 W& ]4 k  H, S8 g
    afx_msg void OnDestroy();
7 x2 y; n* J9 r$ L2 V9 N% e    afx_msg void OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult);) w) N6 m/ U& J& h: u0 y% T' l8 s
};
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 顶 踩

该用户从未签到

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

// ---------------------------------------------" E- Z7 m7 H; ^: ?5 m! w( v // ListCtrlEx.cpp0 z3 r* y0 B7 o# Q // --------------------------------------------- 3 C6 d2 _5 |. D) g0 J/ Q// ListCtrlEx.cpp : 实现文件: a1 c/ d7 K0 z. |3 ?8 R# V: e //

#include "stdafx.h"/ @9 y4 J4 I" b) R. u7 Q8 j #include "CreateNewClass.h"9 U" k: j% x+ x# k$ A+ P: r #include "ListCtrlEx.h"3 I( f3 `6 N% j! @& b% y. [ #include ".\listctrlex.h"

5 g2 W; u) m# u& ~9 n // CListCtrlEx

IMPLEMENT_DYNAMIC(CListCtrlEx, CListCtrl)$ u3 i; }' A% \: t4 A CListCtrlEx::CListCtrlEx() $ [( Q& w7 z! A# J# k{4 ^ I- {; y1 R. C' g( Q. k: H: b // 用于调用基本的方法 2 d8 v* \9 o, b3 I$ l7 {' |3 h pThis = (CListCtrl*)this;% s: T, Y! y4 L% g8 W) d0 E' a // 默认的选中框颜色 % n; ~! `3 c$ `( S, ^6 V" R m_clrHighLight = 0xF1DACC; ! G% \5 O: u2 _ m_clrHighLightEdge = 0xC56A31; , p! |/ O5 J2 L) k5 r}

CListCtrlEx::~CListCtrlEx() , n6 P9 s& V6 O8 X# C: j4 [ l/ T( d# |$ y{

}

' A6 d, S6 Z8 t$ s: ? BEGIN_MESSAGE_MAP(CListCtrlEx, CListCtrl) / l' w3 {3 P% N& E0 i$ @ ON_WM_DESTROY() : O1 [" i. o5 V ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnNMCustomdraw)* ]7 F7 z0 X/ k6 [; e0 G" e END_MESSAGE_MAP()

// CListCtrlEx 消息处理程序

/ O/ d( x, e5 @( v7 m2 TCOLORREF CListCtrlEx::SetItemTextColor(int nItem,COLORREF clrText) 2 U- W! y; w5 f8 k; K; H0 H{ 1 l5 w' E( ^" y# S; a$ O0 Z1 w COLORREF clr = GetItemTextColor(nItem); ! [( K% c/ ?: S2 f ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem); 4 S2 F6 m! p+ ]8 W pData->clrItemTextColor = clrText; ; X0 I0 f. D$ [. V9 {9 t. Y return clr;1 Z' \" E# Q/ |3 }& c& | }

COLORREF CListCtrlEx::GetItemTextColor(int nItem) const. q& D, H, y7 K: e' ]; S { ( [% R# w1 E' d0 D3 | ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem); : T( o- y2 ^. A, s; x9 q8 e0 T return pData->clrItemTextColor; * ~( o0 K. q! \! ~. D$ x g* Q}

COLORREF CListCtrlEx::SetItemBackColor(int nItem, COLORREF clrBack)* M$ ~' E7 b3 W6 P {$ {0 g- G( F* D2 w+ b! k' e7 U COLORREF clr = GetItemBackColor(nItem); 4 \- t' H- A" B. V! m+ t ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);7 N& F" ~) z ?. m2 }8 F2 h pData->clrItemBackColor = clrBack;* p2 }7 e! N$ u4 r return clr;$ \: n4 M" ], r$ H: m }

COLORREF CListCtrlEx::GetItemBackColor(int nItem) const 5 t; J! B6 O" m; S! {{ " X D/ u! r% S9 Y/ E( `* b1 n ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);0 |/ M& p2 A8 {' M1 O( t/ w return pData->clrItemBackColor;% }( p- u# j: |0 c% B7 N }, Z- V* i4 o }$ G" l8 y DWORD_PTR CListCtrlEx::GetItemData (int nItem) const " {" F- R) _8 D, c& A# l{ 4 t$ h2 ~ s0 Z$ |2 G if (nItem >= GetItemCount())$ ]: j# E$ l% c1 }) K { ; k/ r# a; x0 b- D7 O return 0; 6 I5 z7 |8 ?& x }5 q4 N& c) m+ \7 @ CListCtrl* pThis = (CListCtrl*)this; , j! I8 ~3 z' o, N9 e DWORD_PTR Data = pThis->GetItemData(nItem); - z6 F' l( X- _! P, U return ((ItemDataEx*)Data)->SrclParam;7 g6 _5 ~4 D) f4 ?; z% G }. f$ K5 R; H, |9 e2 ?5 I2 w% q( n/ q BOOL CListCtrlEx::SetItemData (int nItem, DWORD_PTR dwData) 5 c& u3 e9 Z! y ~! o{3 \+ z' X6 }; L7 a6 p if (nItem >= GetItemCount()) - `- C2 H3 A4 A) A0 O2 ^7 R8 ~ {: f* y6 l# I% x return 0;+ y2 J" Z4 h+ N/ s i }% D4 E: }3 W) g R% J5 L% I3 K CListCtrl* pThis = (CListCtrl*)this;4 F+ [2 F+ I5 }$ l% m0 T0 b1 r) P DWORD_PTR Data = pThis->GetItemData(nItem); U% l1 b% O2 e, O$ I3 W ASSERT(Data); 6 M/ ]& o: ~+ v1 a ((ItemDataEx*)Data)->SrclParam = dwData; / |+ Q, M: l' z; d0 s! r0 N return 1;: R3 K9 E% i4 F c8 Q } $ [' b& m1 z8 s3 p0 Eint CListCtrlEx::InsertItem (const LVITEM* pItem) 2 y0 e: {- z) r- _" p{ - ~/ f* y% |/ {1 {; _! L int nReturn = pThis->InsertItem(pItem); ! t9 b7 U2 h- c5 c0 c9 C+ e ItemDataEx *pData = new ItemDataEx; ) O: M7 T2 W( P: b2 C // 设置默认颜色 1 l6 \5 d/ \, \3 | J pData->clrItemBackColor = 0xFFFFFF;//pThis->GetTextBkColor(); ; h* V+ A; ^" e pData->clrItemTextColor = 0x000000;//pThis->GetTextColor();" w8 |( c3 e% L* r& b5 Q5 v pData->SrclParam = pItem->lParam; ' R7 V. I6 x8 R3 C8 h pThis->SetItemData(pItem->iItem,(DWORD_PTR)pData);0 E8 H2 B, i q& Q, \ return nReturn; " t& e. E+ u6 l# M3 {} ! A y' `/ n8 q5 q6 e" p0 Zint CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem) ! a9 }8 ^9 {7 C& n% w1 m1 o- O{6 o: S8 K4 |/ k% l* b' m% w$ U LVITEM Item;6 b1 y; t# e: b# h. ^$ U memset(&Item,0,sizeof(LVITEM));% c# G% N9 T7 j* ]( N Item.pszText = new TCHAR[strlen(lpszItem)+1];, f2 |2 w7 Q5 t9 S6 o' ^ strcpy(Item.pszText,lpszItem); 3 ]2 g; z; D9 i- k: C0 W Item.iItem = nItem; 4 Q) j( W1 x- X2 h o Item.lParam = 0;- F6 {) q' m1 x; i& V3 W$ ^# S Item.mask = LVIF_TEXT |LVIF_PARAM; 8 F6 J* G1 T1 U+ J$ B* M BOOL bReturn = InsertItem(&Item); . J- e: x! M w/ I1 e- r3 B! F delete Item.pszText; ' @! r$ e) a; d: R return bReturn; 7 j3 l5 Y. Y. Y7 B7 N}( T3 i: i+ J0 }% O0 X int CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem, int nImage). ~, y) t) o! O { , {# k0 H7 ^; h+ c7 |9 q) g LVITEM Item; 0 g3 o0 G% J9 ]" F memset(&Item,0,sizeof(LVITEM)); 1 N; d4 v' d$ s6 t- Y Item.pszText = new TCHAR[strlen(lpszItem)+1]; , ~4 P( e) [: ^' K7 n* ] strcpy(Item.pszText,lpszItem); 9 W8 W! j. i; }$ Z Item.iItem = nItem;* j8 e/ n! T" q1 u! Z( j8 ^ Item.lParam = 0; 6 r0 P9 @% W- P- x8 n; I Item.iImage = nImage;6 l8 Z! d2 U# h# i: d; k) M3 l Item.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE; + t. v, o O; Q. Z1 ~ BOOL bReturn = InsertItem(&Item);7 S+ M5 U2 \3 c9 Z- N1 ~ delete Item.pszText; : s5 w3 ~7 O# o; T4 X return bReturn;1 o7 y7 v# J5 M5 ?" f! o }0 Y9 i& e; }, U% K' @- A* p3 e int CListCtrlEx::InsertItem (UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam)) X7 r5 P6 H" Y. r) A. I! W9 r { 8 ]* m V; S( X2 A LVITEM Item; ; a0 K' X! k: \5 L1 s3 R) | memset(&Item,0,sizeof(LVITEM));: \- C7 F: O" q6 o: z# r7 X4 n Item.pszText = new TCHAR[strlen(lpszItem)+1]; 4 d3 I2 b' b- a& c1 Q strcpy(Item.pszText,lpszItem);( |# f; L) M, D6 L! O% s Item.state = nState; * A( F8 ^- M0 ~7 R Item.stateMask = nStateMask; ! r! [6 H2 m' |5 c, | Item.iItem = nItem;* B1 D3 A( L: l9 a% y6 ^ Item.lParam = lParam;& H$ h7 l4 |' y' l& n5 ~ Item.iImage = nImage;1 r ^- e& b- [" P) \# p# Q& ?2 D# m Item.mask = nMask;) a$ k7 e# p h& A& h3 Y$ Y BOOL bReturn = InsertItem(&Item);& F: L, i- K! Y8 A7 F$ H) E delete Item.pszText;+ ^% ~' H+ O1 ~) ~ return bReturn;) ~+ u: F% R5 M( l } 3 u+ t% M+ N& r- m/ Q& |BOOL CListCtrlEx:eleteItem (int nItem)/ M+ r6 Z D, v7 K! H W h {+ A- o2 O- y7 |1 t) K# w void* pDataEx = (void*)pThis->GetItemData(nItem);7 b& [! I0 W8 ~: S4 r/ v9 b if(pDataEx != NULL) 4 ^4 y. J/ e! L! D+ X {* \- J$ | H. p8 K1 I+ b delete pDataEx; 3 y& p5 E! ^ p+ j2 {1 T } . R4 J z2 M# J$ S K return pThis->DeleteItem(nItem); % |6 ~7 n7 v4 U9 h$ L}9 }3 l! f! P2 \( r BOOL CListCtrlEx:eleteAllItems () + L& R4 C7 [& |{8 {/ y/ N; Y" X& {) h' k int nCount = GetItemCount();( v7 a8 {) ` m* s for (int i = 0;i<nCount;i++). o$ I' K1 R, s; W7 Z { 0 R1 X: l7 S9 { if(!DeleteItem(0)) ; Q. V9 `# y& } n3 [) w+ | {2 `. d7 I+ f ~0 P5 n! B return 0; . ` O! u2 @1 Z }2 @8 F; A9 l1 q, B0 J! i4 m } . h& g0 ]5 k0 |- f4 C' c; L return 1; 2 r7 Z6 S: S8 Z! _$ C} 9 \( |1 E4 m0 a/ A: vBOOL CListCtrlEx::GetItem(LVITEM* pItem); C8 S* H2 G7 G/ r3 ` { ; x0 d" u- }, J! u- S2 s' G BOOL bReturn = pThis->GetItem(pItem); & X/ a7 h: {4 @0 x! P5 \& z if (pItem->lParam); _6 M5 P3 H+ b. L+ h# S) C {5 S: F& H2 W, K+ Z pItem->lParam = ((ItemDataEx*)pItem->lParam)->SrclParam; : Q. Y6 o" z$ K }

return bReturn;& |8 K" J! E$ r* I5 K Q }( ]* K: j1 l+ O. E9 g6 e% L4 R void CListCtrlEx::OnDestroy() 3 p# ^, m& c3 u{ 3 b, Z. t$ f, {1 Z( l }: l0 W // 关键是清除申请的内存空间' \! I/ w: W d& u4 u( h2 X DeleteAllItems();5 h" x: c* ~+ d9 H8 D" n" h9 P CListCtrl::OnDestroy(); , b1 d6 h: I- \- N}

BOOL CListCtrlEx::SwitchItem(int nItem1, int nItem2)# c7 c- ~& i7 q1 [& u. O F { / e; S( Q- S3 S- l8 U3 v if (nItem1 == nItem2 || nItem1 >= GetItemCount() || nItem2 >= GetItemCount() || nItem1 < 0 || nItem2 < 0)( y& C" c5 i. H+ k2 ? { 8 k1 h2 q4 m2 t return 0; / D) t/ x# I! U3 W }" H0 _& A& L. n) @& `6 q7 ^ LVITEM ItemData1,ItemData2; ; \/ u; }& l. q; W& O( J4 ~ int ItemLength1 = pThis->GetItemText(nItem1,0).GetLength();" @, ^6 I% Z0 p; y, q' Y% s2 R int ItemLength2 = pThis->GetItemText(nItem2,0).GetLength(); ' O( v& w6 j; B( t" G3 b! ` ItemData1.pszText = new TCHAR[ItemLength1+1]; ; P# p% t j% T4 i ItemData2.pszText = new TCHAR[ItemLength2+1];' A5 \8 M& p. d( F/ m ?+ v9 Q ItemData1.cchTextMax = ItemLength1+1; ! N' F( I2 _! L" m/ |+ v# P9 a ItemData2.cchTextMax = ItemLength2+1; 3 a' o7 N4 h. _; v7 D9 s# }9 u3 T, L ItemData1.iItem = nItem1;6 z2 E4 a2 p# H1 A; X ItemData2.iItem = nItem2; + A2 D; z8 L* C5 Q$ I1 d5 i' @4 d5 ] ItemData1.iSubItem = ItemData2.iSubItem = 0;) X5 b: [3 K7 O. s& K& _3 L/ E ItemData1.mask = ItemData2.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE|LVIF_STATE;

pThis->GetItem(&ItemData1); ' ^: I" c6 o% i: l; W j6 O pThis->GetItem(&ItemData2);% Y: U; j/ }3 _ T) ] // 交换数据$ S5 C7 {( S& i0 l, K" t- ]# w' u9 N ItemData1.iItem = nItem2; 6 C) P6 _2 N, ~0 ?% e6 [( S ItemData2.iItem = nItem1; 0 V3 a8 Z" a* d' \- G pThis->SetItem(&ItemData1); 2 K( V1 K8 [' T/ b3 c2 ` pThis->SetItem(&ItemData2);

delete ItemData1.pszText; & o* `9 A/ K3 t, Z9 @. I delete ItemData2.pszText; 6 H& h9 e6 O- F1 `! ^ p // 得到报表模式的列数 / Q6 b. a9 i4 h int nColCount = pThis->GetHeaderCtrl()->GetItemCount();- w; R% E! }, ?, l7 p/ X. a CString strBuffer; - U D" n) d* P4 p* U for (int iSubItem = 1;iSubItem < nColCount;iSubItem++) , E' f2 Y$ N7 u8 K) o, G1 G# L { 5 n: N& }+ E1 I% r- z; Q1 O2 r strBuffer = pThis->GetItemText(nItem1,iSubItem); * v/ Y" H8 X% F- U! n pThis->SetItemText(nItem1,iSubItem,pThis->GetItemText(nItem2,iSubItem));, G; B! _" D: H7 }/ ~ pThis->SetItemText(nItem2,iSubItem,strBuffer); + e/ g, R2 N* J. k* Q* n }3 Q. B0 _8 f K+ Q+ ^ // 把原来的选中状态也转移过去7 A7 Z+ }7 T' X" ^: w; p // 不知道为什么这个属性没有体现在LVITEM结构中…… 5 y/ F( b3 F3 E bool bItemSelected1 = GetItemState(nItem1,LVIS_SELECTED) == LVIS_SELECTED; - F, k- ]! j: n. h bool bItemSelected2 = GetItemState(nItem2,LVIS_SELECTED) == LVIS_SELECTED;

SetItemState(nItem1,bItemSelected2?LVIS_SELECTED:0,LVIS_SELECTED);7 W# v) G ^. b6 G/ J. g- [# ^ SetItemState(nItem2,bItemSelected1?LVIS_SELECTED:0,LVIS_SELECTED); K! c5 d- E3 k0 N return 1;& z. B8 U# h1 G3 f3 j/ ^ }

BOOL CListCtrlEx:reCreateWindow(CREATESTRUCT& cs)2 W7 Y0 ^3 l8 I: o3 A% x { " w; m9 p( K# V4 p // 本类报表模式专用4 x: \3 u' n( D/ G$ Y8 J cs.style |= LVS_REPORT ;+ |% m: y3 q4 {3 i8 A // cs.dwExStyle |= LVS_EX_FULLROWSELECT; F; l: r0 p! E* M6 ]# V, t return CListCtrl:reCreateWindow(cs); ' g8 u7 H3 d; K- y}

' E. P. _# L }: [( O0 P COLORREF CListCtrlEx::SetHighLightColor(COLORREF clrHighLight)1 C$ j# C& _' {/ P, u; s3 i {4 q( T+ L0 f6 ]6 c6 _9 n, T COLORREF clr = GetHighLightColor();1 \+ T' \/ X' [ m_clrHighLight = clrHighLight;# U7 q& ` F, h0 X, @2 S$ p return clr; & F( x R4 x' X+ F' t- `5 V& V v}

COLORREF CListCtrlEx::SetHighLightEdgeColor(COLORREF clrHighLightEdge) 8 {9 k3 F6 p6 w1 S{& I0 q+ k" w; f7 [: M$ ^! j COLORREF clr = GetHighLightEdgeColor();* L( g4 A" u& z c/ N m_clrHighLightEdge = clrHighLightEdge;# @& ]3 b- P' X% ? return clr;8 m* R, J8 c6 Z/ s, V }

该用户从未签到

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

COLORREF CListCtrlEx::GetHighLightColor(void) const, ^4 }5 r; S* X- \ T% I. I {- K4 |% J0 w5 ~ return m_clrHighLight; 2 h3 ]0 P, B9 ~}

COLORREF CListCtrlEx::GetHighLightEdgeColor(void) const7 V& k: [) I* d- ~ {5 `7 {( }# v4 R% Y" m return m_clrHighLightEdge; # J6 `; A5 H% \% p ?}

int CListCtrlEx::GetItemImage(int nItem) 0 n- O0 o3 f% f7 ?6 ?& R' l# N{2 P' b. g9 {( Y% N+ c8 ?! H7 } LVITEM ItemData;, G# x* k% j' g0 l' A3 H3 @ ItemData.iItem = nItem;. T& t+ T. }& `4 q P1 c" W ItemData.iSubItem = 0; ' w) [- f$ K' f ItemData.mask = LVIF_IMAGE;, ]* |; u4 m. S q( P" V* Q pThis->GetItem(&ItemData); ) o$ H( @) b. t0 @ return ItemData.iImage; * V3 Q1 R$ s& ^# d}

BOOL CListCtrlEx::MoveItemUp(int nItem)! _! d' e% V5 {4 |% {# f { $ k1 N" L5 }7 u3 ~: ?- D) X if (nItem < 1 || nItem >= GetItemCount())! M N3 Z& e, X$ R+ R' ~ {6 M O, L6 X' `- | return 0; # ]' Z9 H+ c8 P$ Q } . r' @" M J0 q return SwitchItem(nItem,nItem - 1); % M" x3 J* f. o7 _) x8 `$ } Y}

BOOL CListCtrlEx::MoveItemDown(int nItem) + g. ^& z) B. H+ `6 e{8 X% z" e) m _0 j9 o if (nItem < 0 || nItem >= GetItemCount() - 1) ; @$ u: E; H0 G2 G- @# c- K0 a/ Y {/ W* Z2 B7 l" O9 R* p! N return 0;% @0 T* v, g* j2 X$ P5 U f } . A/ H) M/ I) o ?- c c0 V: o return SwitchItem(nItem,nItem + 1); 2 e/ u, ]0 u& v9 i2 J R$ f" y}

void CListCtrlEx::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult)5 U* {8 Z$ V2 H! `+ U { 2 Q; C1 R2 X8 R6 f NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR);

*pResult = CDRF_DODEFAULT;

if (pLVCD->nmcd.dwDrawStage == CDDS_PREPAINT)8 N- V3 ?$ |: J% y4 U+ H { 9 w! s8 n& X( [+ S *pResult = CDRF_NOTIFYITEMDRAW; ( w% y1 W; `" X9 T } 6 C/ f; A8 T* g! R0 `5 g else if (pLVCD->nmcd.dwDrawStage == CDDS_ITEMPREPAINT) : X0 M8 K _ {. j) w {! g8 `; S* Q$ b0 V* i: x/ }* B *pResult = CDRF_NOTIFYSUBITEMDRAW;# T" \; o+ l, g, d; | } / p7 J# n# e: z8 u/ O else if (pLVCD->nmcd.dwDrawStage == (CDDS_ITEMPREPAINT | CDDS_SUBITEM))7 j& d8 y4 C. R: V# I {

int nItem = static_cast<int> (pLVCD->nmcd.dwItemSpec); k( u! e1 s4 O int nSubItem = pLVCD->iSubItem;3 r( b! w) @0 E" x6 N6 \ if (nSubItem != 0)- j" ]8 ]- Z9 Z { 2 @5 k$ A' g' u3 Y3 e" h |1 h // 全部绘制过程在nSubItem == 0 时完成 9 W+ P9 v9 _7 `6 ~9 U' D ]7 [4 \ // 分布完成会有小问题,我无法解决. X" }0 a+ c) A* T4 R8 @# V *pResult = CDRF_SKIPDEFAULT; 3 _) t. I# [* H& E8 D return; : t6 Y7 R" }( E8 R: `* A } 9 }# S' o& N( j2 v CDC *pDC = CDC::FromHandle(pLVCD->nmcd.hdc);6 U1 s1 {. l" D. l int nColCount = pThis->GetHeaderCtrl()->GetItemCount();, e! R9 k3 @9 u- y! o/ I/ Q/ j- d CRect rectSubItem;8 C& p& |- K, f+ X J. H. ?, r& L0 I CString strItemText; - N7 j7 q6 } \7 ?7 H pDC->SetTextColor(((ItemDataEx*)pThis->GetItemData(nItem))->clrItemTextColor);1 W" s: m) D1 f6 |+ D* m8 N+ G1 ` pDC->SetBkMode(TRANSPARENT);9 X& [0 ^3 h4 p) g& d D- y; Z- G // 相当于橡皮$ W7 `% L, V+ Y c2 N4 [2 d CBrush brEraser(GetItemBackColor(nItem));

// int nFirstWidth = GetColumnWidth(0);

" X; E* ?* X. X9 x6 H for (int i = 0;i < nColCount;i++) : @8 X, M* k1 e& c {: O5 y- \; A @$ G4 z/ u# P6 y strItemText = GetItemText(nItem,i);

if (i == 0) }7 {1 n$ o3 p {/ A7 v3 D( Q2 K pThis->GetSubItemRect(nItem,i,LVIR_BOUNDS,rectSubItem); , Y" I% _: K; J! X // 擦掉原来的图形 + Y) ?6 Z( ^3 @: B pDC->FillRect(rectSubItem,&brEraser); 6 | f7 b) W# `3 \5 }" k if (GetExtendedStyle() & LVS_EX_CHECKBOXES)7 Y/ c# P' P$ X5 Q/ n V" i { . `! o- g ~0 H( s CRect rectCheckBox; 8 o) B( g* A8 j/ v) i GetSubItemRect(nItem,0,LVIR_ICON,rectCheckBox);9 a) N! x$ \, I$ j o CRect::SwapLeftRight(rectCheckBox);% t- Y' H2 O9 R$ A rectCheckBox.left = rectCheckBox.right - rectCheckBox.Height();

rectCheckBox -= CRect(2,2,2,2); 6 e) g8 L1 k* s pDC->Draw3dRect(rectCheckBox,0x81521D,0x81521D); 7 G- l; s9 F- Q* V5 I1 I. t //rectCheckBox += CRect(3,3,3,3);; p- V ]6 H2 f/ h! Y* n if (GetCheck(nItem)) * w3 Z2 m$ W( {0 p5 R, z8 ` { - v N; F' q" ~) N // 绘制一个CHECKBOX 2 n+ i, j5 H" n( \ E CPen penCheck(PS_SOLID, 1, 0x0021A221); 1 q% ]# l/ p& C$ _) J% y) b CPen *pOldpen = pDC->SelectObject(&penCheck);+ @/ U/ F" F1 N# b, q- _* b @ for (int j = 3; j < 10; j++) 0 Y9 H4 ?& `5 f `. t; }6 ? {' ?6 y- c$ U# j/ C. @' ] s8 Q pDC->MoveTo(rectCheckBox.left + j, rectCheckBox.top + rectCheckBox.Height() /2 - 3 + ((j < 6) ? j - 1 : (9 - j))); a; J6 V3 I: E$ {, _ pDC->LineTo(rectCheckBox.left + j, rectCheckBox.top + rectCheckBox.Height() /2 - 3 + ((j < 6) ? j + 2 : (12 - j))); " d& w( y2 d8 | }& w% s0 m9 Y( }7 `9 `; e 7 @: }6 V4 O0 ]3 W) |2 S pDC->SelectObject(pOldpen); ! g8 [' K, x& U p% u } 6 Q$ p! o% N) v3 j/ Z } ; g; @$ S4 B5 b3 U4 ?1 s: b/ J6 h // 根据需要画选中框) f W) J' p! O. a! n if ( (GetFocus() == this || GetStyle() & LVS_SHOWSELALWAYS)&&(GetItemState(nItem,LVIS_SELECTED) == LVIS_SELECTED)) ; K, _' p! \- v5 @ { 3 d& f9 e6 h2 Z& K% Q& W if (GetExtendedStyle() & LVS_EX_CHECKBOXES) * D0 D9 v2 \! b! G: ~! L {// 存在检查框风格的时候需要对左坐标进行修正5 i3 K" c2 p u% x; U CRect rectLeft;4 K4 [2 \5 N( h8 ` v5 M6 K GetItemRect(nItem,rectLeft,LVIR_ICON);4 x3 ^# X5 K( x1 R rectSubItem.left = rectLeft.left;/ m _5 f7 i: ^2 c2 ]; w } 4 z. m! n# ?( z" ^- ~, O if (!(GetExtendedStyle() & LVS_EX_FULLROWSELECT)) . Q1 h7 S6 {5 E9 Z6 ?, U8 S7 O) [5 l {// 非全行选择时右坐标需要修正 L0 u5 e6 x: H/ R' C: j3 Y* X CRect rectRight;' E! m' N5 _$ G" e3 d' T& @$ K6 p GetSubItemRect(nItem,nSubItem,LVIR_LABEL,rectRight); # |! i9 I/ b- k" j/ B4 o9 ~6 E rectSubItem.right = rectRight.right;. Q/ F% c+ m0 w/ R2 V# v! \ } ) n4 @# f4 F: s. T CBrush brHighLight;9 q! [% [3 h0 O3 x CPen penHighLightEdge; # J9 z+ J5 k0 M x' i if (GetFocus() == this) 3 e+ c: E4 B5 W( F { 2 {% R6 D" ?9 }: Q6 y brHighLight.CreateSolidBrush(m_clrHighLight);$ W* q; h; o, Q4 X" H; f( ^ penHighLightEdge.CreatePen(PS_SOLID,1,m_clrHighLightEdge);; g7 r( u, K% ], A" H+ r% @ }6 L4 `% S/ s3 `, R0 D6 M3 g else E) Y$ }5 L+ `6 M7 x { . G* E6 j6 k" v // 不是焦点时,颜色70%不透明度7 W$ Z# L* c! m9 P COLORREF clrBack = GetItemBackColor(nItem);7 ?) t7 ?( B& T5 m. F; ?& L WORD red = GetRValue(m_clrHighLight)*0.7 + GetRValue(clrBack)*0.3; ( ], e( o$ q7 }) f6 C6 Z WORD green = GetGValue(m_clrHighLight)*0.7 + GetGValue(clrBack)*0.3;$ q5 i( I0 S/ s! E WORD blue = GetBValue(m_clrHighLight)*0.7 + GetBValue(clrBack)*0.3; 4 G. Z) f3 K7 j& ]/ u! ]) l brHighLight.CreateSolidBrush(RGB(red,green,blue));7 u2 C) J# `$ [3 w8 G red = GetRValue(m_clrHighLightEdge)*0.7 + GetRValue(clrBack)*0.3; ! p3 @6 I* b! I green = GetGValue(m_clrHighLightEdge)*0.7 + GetGValue(clrBack)*0.3; - b( x( h. @2 R2 o0 a blue = GetBValue(m_clrHighLightEdge)*0.7 + GetBValue(clrBack)*0.3; * r( }) u* j7 w- E: ] penHighLightEdge.CreatePen(PS_SOLID,1,RGB(red,green,blue)); {* \0 I' e# G' {1 Q }8 r: I8 m- e' G" X CBrush *pbrOld = pDC->SelectObject(&brHighLight);6 @* T/ j* y5 U+ O. b CPen *ppenHighLightEdge = pDC->SelectObject(&penHighLightEdge); ' K, u1 Y7 c# `' f% R0 {, B pDC->Rectangle(rectSubItem); : y' M- C& s$ k" v% L" X) y pDC->SelectObject(ppenHighLightEdge); v- m5 [* {; p/ i3 G4 o1 N pDC->SelectObject(pbrOld); ) \- o+ s9 L) e# J }" j. u }' o3 ^( t& y4 n& u Y }: t2 `! g& J; S" D, K) J pThis->GetSubItemRect(nItem,i,LVIR_LABEL,rectSubItem);& H' O$ z* Z# p! l$ V UINT uFormat = DT_SINGLELINE|DT_VCENTER|DT_END_ELLIPSIS; ( u% I4 r4 ]2 E HDITEM hdi; 3 E0 a6 }1 x: \3 T hdi.mask = HDI_FORMAT; O5 S X5 W) J3 l pThis->GetHeaderCtrl()->GetItem(i,&hdi);2 n7 c) n; ^, \6 C if (hdi.fmt & LVCFMT_RIGHT) " B* c+ Y' B) D9 [ {( L5 H9 s* Y3 ?- _4 D" Q- e uFormat |= DT_RIGHT; # O( s" s5 J; L$ J } 4 _+ V" X5 I3 h- j, y8 h else if (hdi.fmt & LVCFMT_CENTER) # k" p& I) P2 v" G0 w { 6 s! d0 u" ~7 G7 Q: x9 Q! @: u uFormat |= DT_CENTER;+ u1 n# C8 ]4 `7 @& V6 J" F5 I }) J3 g: n0 O5 L {- S( Y4 Z7 A pDC->DrawText(strItemText,rectSubItem,uFormat);

CImageList *pImageList = pThis->GetImageList(LVSIL_SMALL);& J$ }8 E# B, H3 d* h8 A5 c* c if (pImageList) 9 {/ `) @; L! z2 {, f { 4 |; u% n: h6 D CRect rectIcon; ! `. e3 C/ q# A7 s# g GetItemRect(nItem,rectIcon,LVIR_ICON);5 l2 ^' p9 g4 I pDC->DrawIcon(rectIcon.left,rectIcon.top,pImageList->ExtractIcon(GetItemImage(nItem))); ( [: T6 m, |1 W1 \0 j } / `4 _) n7 B# d! w7 z }4 E1 T" `! m$ [! X( a4 t' T/ Z4 }: c* @ *pResult = CDRF_SKIPDEFAULT;8 \- x' ?. B+ {& D! l' |- O/ q }

}

该用户从未签到

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

本版积分规则

关闭

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

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