下沙论坛

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

QQ登录

QQ登录

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

又是垃圾,CListCtrlEx Base CListCtrl

[复制链接]

该用户从未签到

跳转到指定楼层
1
发表于 2005-2-23 21:29:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
// ---------------------------------------------% I; X# d& J" B9 w
// ListCtrlEx.h
& u  g; z% v. ^) R7 w' w// ---------------------------------------------4 X, H/ k* A7 g: m9 m5 V. n+ X
#pragma once
0 j. h( V! [/ q+ p; Y8 _! y1 d7 Q0 Y8 z1 |" ?, f+ W/ `0 W+ r
// CListCtrlEx+ f1 r3 J/ k# V  h% i, i8 J
class CListCtrlEx : public CListCtrl& y% h7 P4 T1 y  g+ D0 J: N3 Z; i
{- ?# X) y. S5 h$ |) m- n
    DECLARE_DYNAMIC(CListCtrlEx)$ P# ^9 j! I; ]+ Q- U9 W0 ^
public:
8 ], x- U' h) R% G0 k    CListCtrlEx();( X6 A$ f6 K$ {& N
    virtual ~CListCtrlEx();7 g: V- O) r  O7 t: P$ P$ }- ~
protected:# v5 m# l- d+ X1 i
    DECLARE_MESSAGE_MAP()
" h5 N4 J8 y% M% r. i5 A    CListCtrl* pThis;% m1 a; k1 L, F  a6 ?. e3 g# ^* x
    virtual BOOL PreCreateWindow(CREATESTRUCT& cs);  q9 Y- h+ s6 H. M4 y# X( `9 X# W' q
private:
6 a! S$ h: i3 @) C' C1 B. t1 F    struct ItemDataEx {3 B  H9 D6 v4 l, `0 W
        LPARAM SrclParam;2 w+ M9 b7 G& H- B* W
        COLORREF clrItemTextColor;( N- ?" I) f; X1 u' f& @/ p* D
        COLORREF clrItemBackColor;4 y1 N" R7 |* N5 Z" x- W
    };7 F$ i3 T. A1 T& S4 f8 v# @1 E
    COLORREF m_clrHighLight;! d& p# J+ X. ~
    COLORREF m_clrHighLightEdge;
" ]5 b1 ^, X: x. q  fpublic:4 {3 p9 C) ^# B
    // 新增方法7 s5 f1 ]0 i. e; f! `
    COLORREF SetItemTextColor(int nItem,COLORREF clrText);
( |- ^& ]: x/ {* q6 f- ~4 k4 \    COLORREF GetItemTextColor(int nItem) const;
; z& m1 I* W9 l- b$ v. Z$ B3 [9 ~( _    COLORREF SetItemBackColor(int nItem, COLORREF clrBack);
0 u% p7 v0 r) I/ b, d/ ^    COLORREF GetItemBackColor(int nItem) const;
6 ~1 P7 F% I+ A2 v9 H+ }. U* R3 L, _  }    COLORREF SetHighLightColor(COLORREF clrHighLight);1 l+ [% e% E6 b9 [
    COLORREF SetHighLightEdgeColor(COLORREF clrHighLightEdge);
  [2 Z# r* S, d9 {  D( T    COLORREF GetHighLightColor(void) const;7 I0 c$ V/ Z5 G0 z: O$ D) m8 v$ `
    COLORREF GetHighLightEdgeColor(void) const;" g3 E2 k8 K" _$ K* g2 T
    int GetItemImage(int nItem);
6 X' [. f2 D" S; ^' Y2 T  Y0 l# v4 e    BOOL SwitchItem(int nItem1, int nItem2);3 G: M1 a1 l) O  g6 ~; j
    BOOL MoveItemUp(int nItem);
7 Y, `/ t+ {% b$ c    BOOL MoveItemDown(int nItem);
# d  @; C2 m! k' V$ m6 a9 W    // 重写原来的方法0 l# n- G% K" V  I  R2 H' s
    DWORD_PTR GetItemData (int nItem) const;
  c# s8 r# U5 o5 J6 T0 B3 W    BOOL SetItemData (int nItem, DWORD_PTR dwData);
  K' v" t2 i0 H    int InsertItem (const LVITEM* pItem);
! t2 {6 ?9 }4 J0 V. I: n" |    int InsertItem (int nItem, LPCTSTR lpszItem);: `- f7 u+ |5 H- k2 t1 L$ V
    int InsertItem (int nItem, LPCTSTR lpszItem, int nImage);5 ~# D  Q& a0 l% F/ z2 M1 f. I# X
    int InsertItem (UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam);
" A) Z( o& m5 ?, {    BOOL DeleteItem (int nItem);
8 ~6 q5 o0 p% u! M/ Q$ n& G4 W1 K    BOOL DeleteAllItems ();
! H* r' b. W: p! G' K  @4 Z( Z    BOOL GetItem(LVITEM* pItem);
  n, F" \1 C; l( q; u  B$ l/ C    afx_msg void OnDestroy();! v, V' Y2 j# w8 E: X
    afx_msg void OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult);3 t( g# Q+ x0 v+ [  r9 h3 Z$ A4 P, X
};
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 顶 踩

该用户从未签到

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

// ---------------------------------------------& O/ I- I0 s5 s. L4 [8 d6 q+ j // ListCtrlEx.cpp: G$ _& Z x2 d // ---------------------------------------------' L4 m% y2 t! ^' e4 s // ListCtrlEx.cpp : 实现文件 : d# T; R$ T: T* ~/ g A//

#include "stdafx.h" 1 f' t, X$ k& Y& p9 `0 y9 y/ c7 a#include "CreateNewClass.h" 8 |9 O( v& ~7 p+ V' O9 H3 {#include "ListCtrlEx.h"- G% t. v, P0 v. y #include ".\listctrlex.h"

: c: _( t$ G+ [. L // CListCtrlEx

IMPLEMENT_DYNAMIC(CListCtrlEx, CListCtrl) 6 v/ { ]) C$ M9 nCListCtrlEx::CListCtrlEx()) r5 E4 c/ \7 j7 u7 l8 o- h {( W/ w% A$ Z [# Z+ m( d$ w/ t) U // 用于调用基本的方法 5 p$ H7 o# t- {7 ~ pThis = (CListCtrl*)this; 0 _ a7 k( y5 [! b% B k8 c // 默认的选中框颜色0 `4 e( j9 c8 A6 d m_clrHighLight = 0xF1DACC; " c' l* [6 u4 u/ i m_clrHighLightEdge = 0xC56A31; 8 G9 f+ L( V. @+ k" }}

CListCtrlEx::~CListCtrlEx()+ a& q$ s/ D! J2 K) `& j {

}

3 ^+ r2 F% x! d( F( ] BEGIN_MESSAGE_MAP(CListCtrlEx, CListCtrl)' l6 T. ]( ~* I1 @& `3 S7 _ ON_WM_DESTROY()7 F' n& m! A; X7 ^! k ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnNMCustomdraw)8 D1 h, ]5 r% C- L& D8 c END_MESSAGE_MAP()

// CListCtrlEx 消息处理程序

$ `) u( V, _4 a/ cCOLORREF CListCtrlEx::SetItemTextColor(int nItem,COLORREF clrText)& u0 Z% b( c! D! R0 Q7 o) i2 H! L$ ^ {+ ^+ R$ n# j$ E @$ q) ~( D COLORREF clr = GetItemTextColor(nItem); 7 s6 k, ?# |0 {3 F% b ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem); / X! [8 u d/ t2 `. G pData->clrItemTextColor = clrText; F/ `; V% H e0 u6 a2 g7 y return clr;. _8 I( T# u+ e9 @- B }

COLORREF CListCtrlEx::GetItemTextColor(int nItem) const ) P# r2 s) t# d/ @* t6 a* w{ ; b/ L ~% A6 g! Q ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);# l" W+ T& `3 N+ @ return pData->clrItemTextColor;+ Q Z9 x! f+ f1 B }

COLORREF CListCtrlEx::SetItemBackColor(int nItem, COLORREF clrBack)3 H+ G* r: L8 z2 D { 4 x; Y/ w r% a/ Y1 ~! B1 \/ n* e$ H: W COLORREF clr = GetItemBackColor(nItem);) q$ d3 A) L7 W3 M) a, k ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);3 [+ M% E+ f- L0 s( E3 t$ S# g pData->clrItemBackColor = clrBack; ; Q3 p: w6 c, k+ F& S return clr; 8 J) w" {7 l& A1 |' Q}

COLORREF CListCtrlEx::GetItemBackColor(int nItem) const . }- s, N* l# }8 n{" @4 @/ u( x: V: [ ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);) y2 D- F- n+ o1 P* \7 x2 u return pData->clrItemBackColor;2 h- [/ R6 D9 I2 L } 4 E% t4 |! P) UDWORD_PTR CListCtrlEx::GetItemData (int nItem) const6 U# ^- f5 G, @' X {5 I% _/ y. J6 \4 F if (nItem >= GetItemCount()) 4 t3 c3 U, }6 d3 G' t2 D {0 w7 S2 e z3 @& S8 s" [! v return 0; " X, Y" }6 u$ I# K" j+ {: f }1 Q- U7 L9 ]+ t5 U5 X CListCtrl* pThis = (CListCtrl*)this;- X/ O) d1 z0 C DWORD_PTR Data = pThis->GetItemData(nItem); 5 A# I# }/ R* d' ?; y1 N @ return ((ItemDataEx*)Data)->SrclParam;$ }9 W3 K4 ?( I- C/ e- g e } ! a& q6 \ z ~BOOL CListCtrlEx::SetItemData (int nItem, DWORD_PTR dwData)5 k4 I8 H r( Y. G) f: g0 _$ x% D3 K { 2 i, p& j! |' u) e6 s& c6 d if (nItem >= GetItemCount())$ U0 Z" O4 m7 D7 ~ { ! r( a' A+ \' j* f1 z return 0;# e+ U. I, }/ U+ ?) s } " C2 D1 g' @6 H1 x w/ J CListCtrl* pThis = (CListCtrl*)this;/ {: F& e6 _( }/ v2 ]" o8 p DWORD_PTR Data = pThis->GetItemData(nItem); * M8 H& Q( j$ O5 q" u! C+ K1 X! Y ASSERT(Data); 5 t" `4 M5 Y- Q2 i/ b7 y1 k ((ItemDataEx*)Data)->SrclParam = dwData; 7 G; H/ v2 l$ ~* J" k return 1; 3 |+ c5 U& h$ z5 r+ \}+ u3 |6 U- D, C. \- k8 r2 t int CListCtrlEx::InsertItem (const LVITEM* pItem) " g+ C3 b# K' l0 N{ 0 I! i0 o+ q: F" x. | int nReturn = pThis->InsertItem(pItem);! l# I+ z$ q; a ItemDataEx *pData = new ItemDataEx;+ h( A$ g) O; X/ y- o8 N) c // 设置默认颜色 4 X( f2 n8 `6 Q I pData->clrItemBackColor = 0xFFFFFF;//pThis->GetTextBkColor(); 9 P$ _1 L- k" p9 k/ e pData->clrItemTextColor = 0x000000;//pThis->GetTextColor();/ U9 I. K- g9 g2 Z6 p# Q pData->SrclParam = pItem->lParam;; t( p& y3 r" W+ G, ^( j pThis->SetItemData(pItem->iItem,(DWORD_PTR)pData); 8 q! Q* u) j( ? return nReturn;1 x" M9 V D/ }/ g9 m } . U- I4 U; r9 F9 z9 o4 qint CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem) 2 L% h. ~+ C1 Z" U: R8 o1 }; g$ L{ % G+ ]! }; t7 X& p LVITEM Item;5 X, n1 h) q2 `$ Z. R. g3 i1 R" P memset(&Item,0,sizeof(LVITEM)); 2 l. h! I! R) ^0 a, A" I1 \ Item.pszText = new TCHAR[strlen(lpszItem)+1]; ( y; g* |6 r8 W. A strcpy(Item.pszText,lpszItem); 8 _6 d7 R/ s, c+ c5 g! V Item.iItem = nItem;, _8 J$ h& J6 O8 a5 L# J9 ^) g Item.lParam = 0;8 C0 F( a# t& D& i7 W1 V Item.mask = LVIF_TEXT |LVIF_PARAM; - z* e) i" s$ L1 r4 w3 S BOOL bReturn = InsertItem(&Item);5 U% |; E. Y+ M1 w, W delete Item.pszText;" F* J/ n G7 s# c; { return bReturn; . h( q0 K- `. B1 [/ ?7 k8 ?}8 P; w" @0 {4 g/ I- e5 f1 _6 v int CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem, int nImage) ( k J7 Z4 n# R- k! l n3 m( x{ 5 O1 ?4 o& W5 f8 w' E! O$ x LVITEM Item; # P. {! b! x* f3 I; Y" L6 c9 r! f memset(&Item,0,sizeof(LVITEM));7 V0 `# j. S0 }- o( B Item.pszText = new TCHAR[strlen(lpszItem)+1];! u$ n+ i5 h; \. G# L. G6 r strcpy(Item.pszText,lpszItem);/ n. H! T- ]" C: U S Item.iItem = nItem;1 [$ n1 ?3 G0 u/ ]5 B Item.lParam = 0;& P' N* u% X" b: f: K Item.iImage = nImage;/ B5 J& P( c, y2 G" f1 X: z Item.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE; * U& t1 G, f$ d* N+ f& [& I/ I BOOL bReturn = InsertItem(&Item);; X. b+ {' r& I5 i8 b delete Item.pszText; ( y6 y3 y ?: Y+ e: A$ i return bReturn; 1 @3 x( \& F- j# h} 1 H0 _. ~$ S% m( Y" Dint CListCtrlEx::InsertItem (UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam)3 J( q3 L: R! b" G { / j( B% h/ r" S9 a/ L, ? LVITEM Item; - M9 |( x$ B; s, v5 w) D memset(&Item,0,sizeof(LVITEM)); - E0 }# k% A% N" Z! O( G Item.pszText = new TCHAR[strlen(lpszItem)+1]; . ~6 u+ R- _! \ strcpy(Item.pszText,lpszItem);8 j7 t) ~& }' v- H+ q Item.state = nState; 9 u8 p, l1 Q$ F# V/ Y9 r Item.stateMask = nStateMask; ' n( P, D0 s0 |6 {3 x Item.iItem = nItem;& f% J) `3 W5 W" ? Item.lParam = lParam;3 |# [4 _) G3 W Item.iImage = nImage;4 Y- n+ u* s7 q/ l1 B- }1 @" m Item.mask = nMask; - W2 a( B: i! p B( { BOOL bReturn = InsertItem(&Item);0 C$ o" t( S& ]3 P0 G- P8 | delete Item.pszText;1 B \# u1 x+ t$ l. Z9 M return bReturn; 9 i/ p8 Z8 b) Q} - y- F# |8 {+ u/ B# e8 l7 eBOOL CListCtrlEx:eleteItem (int nItem) 3 g2 G3 o5 h, ~! q: \{ 1 k3 d# z$ Y6 K D2 F) ]; p2 W void* pDataEx = (void*)pThis->GetItemData(nItem); * y1 u" z* o7 E7 O" Z$ T# B2 s6 o if(pDataEx != NULL)+ c6 P1 u p; F. s* Y7 p' y8 V! p { : l6 o% k# q5 b& }: R5 S, `& J delete pDataEx;1 q8 V) v, h! \7 t9 D' I) f }/ z7 T. e/ i' I1 y return pThis->DeleteItem(nItem);( C; d5 ~% v o! S" C+ n" S! h } D2 v6 P0 V3 c/ Z. u BOOL CListCtrlEx:eleteAllItems () $ c) X& |9 w1 }: K! j$ q3 V( B5 j* B$ N{ 3 y! N% B5 T4 B. I' E# O) O1 b9 ] int nCount = GetItemCount(); ; j/ i: B# w7 Q8 }! z" {5 q for (int i = 0;i<nCount;i++)7 V! H* |7 S4 }9 G* q/ ~5 H {6 C1 f( U" g4 v/ A; i if(!DeleteItem(0)) " E. |: c5 E+ c" P {8 T3 X/ T) L# P- J# P, Y return 0;" Y' I6 P7 @: p( ~7 ]& ? }2 A. O( R5 f0 k } # J* i5 f# {8 f+ C- L3 N# Z& h return 1; * J- `+ ~6 T- a} 0 F# ]5 k Q, o: IBOOL CListCtrlEx::GetItem(LVITEM* pItem) , U# ]9 c7 x3 q* Y{ 9 B' P& c% U8 H; d9 C4 `) M8 _" I BOOL bReturn = pThis->GetItem(pItem); + G6 b0 i C; r: D# P- N if (pItem->lParam) : C5 p$ c% B% G7 w7 B { " U4 ^, O, a( T5 d6 ~; V. [. Y pItem->lParam = ((ItemDataEx*)pItem->lParam)->SrclParam;* d Q- E0 R% w V. s- W- G }

return bReturn; + ?) R- ?7 U& O! k3 a7 d6 W6 P- a} : R% M# P1 q0 ~ B+ _& u9 dvoid CListCtrlEx::OnDestroy() 3 J/ F$ A& p' t{, x6 `5 M, l' M: B# u# J7 \ // 关键是清除申请的内存空间 $ E' C+ s, \2 u2 g) ?$ I! o7 r DeleteAllItems(); . m6 J8 u+ J- f+ m5 V: B# i3 F7 c5 { CListCtrl::OnDestroy();% }% z! @+ N) O L5 b }

BOOL CListCtrlEx::SwitchItem(int nItem1, int nItem2) - e, o5 N- ~8 j! B2 z; ?{* y# u, X1 u, c( l: _7 v: j' ` if (nItem1 == nItem2 || nItem1 >= GetItemCount() || nItem2 >= GetItemCount() || nItem1 < 0 || nItem2 < 0) . ^9 u! R) X8 E) F+ t% x5 a {0 K6 T6 j1 O! a return 0; 2 J0 G9 d: _& s, i* k2 ^2 w }8 a; g$ q+ q, z LVITEM ItemData1,ItemData2;# t% v4 E9 U8 f6 P& A" \ {. }8 Z0 F int ItemLength1 = pThis->GetItemText(nItem1,0).GetLength(); & Z& `4 Y* F2 U int ItemLength2 = pThis->GetItemText(nItem2,0).GetLength();8 Y' `; _6 x* F8 T+ A" Z4 X: B ItemData1.pszText = new TCHAR[ItemLength1+1];# T! y7 \ }4 K8 g6 p+ Y( i ItemData2.pszText = new TCHAR[ItemLength2+1];+ a# N. C8 ~- y [7 z6 c4 @7 f( _ ItemData1.cchTextMax = ItemLength1+1;7 D' _2 L8 z; S ItemData2.cchTextMax = ItemLength2+1; ) ?% i+ R4 G: Z. R; ~- c ItemData1.iItem = nItem1; 2 Z8 J4 H- W7 Q8 ~" _ ItemData2.iItem = nItem2; 5 d1 e% ?- P$ a8 f- P ItemData1.iSubItem = ItemData2.iSubItem = 0;4 ]7 x! X0 L7 |! W9 |6 P# o ItemData1.mask = ItemData2.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE|LVIF_STATE;

pThis->GetItem(&ItemData1); - q4 x' ?2 ^1 r3 z! K& S, v4 _ pThis->GetItem(&ItemData2); , M2 d% C- I( R6 r h2 K9 K0 a // 交换数据8 h1 [, y" d) [ ItemData1.iItem = nItem2; # ~& Q7 L- a+ c4 I7 x9 g ItemData2.iItem = nItem1;: y. l) b2 e( k9 }; E+ ~+ t pThis->SetItem(&ItemData1);: o4 t9 l9 i% d7 ^ pThis->SetItem(&ItemData2);

delete ItemData1.pszText; # X; B* B- J0 K. W' j8 U delete ItemData2.pszText; 7 y3 A1 ?( R# o4 f1 Z0 K. `, s // 得到报表模式的列数) I0 J! P$ e4 r$ F/ P2 h2 K8 G int nColCount = pThis->GetHeaderCtrl()->GetItemCount();. ]- y8 i1 d( p+ [! w- H: h$ Y. _ CString strBuffer;* D$ f& `3 | N" L for (int iSubItem = 1;iSubItem < nColCount;iSubItem++); C- ]2 r& e9 J' q$ Q { 7 b" @! ^- W2 z4 ]5 f" D2 _8 R strBuffer = pThis->GetItemText(nItem1,iSubItem); : ~: t9 B) {; f' P pThis->SetItemText(nItem1,iSubItem,pThis->GetItemText(nItem2,iSubItem)); + I) A# {" v( @ Y' B pThis->SetItemText(nItem2,iSubItem,strBuffer); . P) b: P3 D, c: H y: a } : z6 @6 O1 t* O- q+ r- \3 ] // 把原来的选中状态也转移过去 t" a N1 ^1 R( h // 不知道为什么这个属性没有体现在LVITEM结构中…… 4 o2 a% a! X( n/ L5 V9 d2 B bool bItemSelected1 = GetItemState(nItem1,LVIS_SELECTED) == LVIS_SELECTED; % k- Y" {+ H! s9 \: | bool bItemSelected2 = GetItemState(nItem2,LVIS_SELECTED) == LVIS_SELECTED;

SetItemState(nItem1,bItemSelected2?LVIS_SELECTED:0,LVIS_SELECTED);8 X/ y! T$ f1 z, I" m' k) t" w SetItemState(nItem2,bItemSelected1?LVIS_SELECTED:0,LVIS_SELECTED);) k* D- U/ Q3 K2 x, T' W3 G8 \ return 1; / d5 y7 F: ^8 i" P4 [# w}

BOOL CListCtrlEx:reCreateWindow(CREATESTRUCT& cs)2 F3 @& J+ g8 \+ A9 u$ E {: I. `& p" T- u3 q3 {* K' }8 J; w" @ // 本类报表模式专用# u1 ~ G9 C( x cs.style |= LVS_REPORT ;9 \+ v: e/ n4 ~. i* j2 f // cs.dwExStyle |= LVS_EX_FULLROWSELECT; S( N$ h( x: E3 d" D: y return CListCtrl:reCreateWindow(cs); . p* C$ q/ b/ z, {- M4 s/ ]5 P$ i}

( E p2 o7 Z: B8 e+ @6 v COLORREF CListCtrlEx::SetHighLightColor(COLORREF clrHighLight)7 }$ @- z+ P! z/ C0 m( H; m1 G {: L' d: H+ S0 U2 X2 G COLORREF clr = GetHighLightColor();" y; X( |, V! ]7 t m_clrHighLight = clrHighLight;* O3 T( f6 h5 M return clr;+ Z8 `! x4 S+ _( W }

COLORREF CListCtrlEx::SetHighLightEdgeColor(COLORREF clrHighLightEdge) : {9 f. ], W) D) t{. g* E7 |8 m. l* v. B& G COLORREF clr = GetHighLightEdgeColor(); 9 v5 g& g. |3 t5 ~ x* O) a m_clrHighLightEdge = clrHighLightEdge;( H8 i8 @1 q& N6 G& v2 C4 | return clr; , R1 F" j- \. O' v+ {' `}

该用户从未签到

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

COLORREF CListCtrlEx::GetHighLightColor(void) const; D+ ]7 s) Q6 N, a7 }4 B7 [ { . [* u% c3 D$ `: Y& a3 f return m_clrHighLight; ( x% O. k9 N7 B) f}

COLORREF CListCtrlEx::GetHighLightEdgeColor(void) const , Z& l+ p0 W5 s6 k* U{% D9 ^* k8 e9 W) l, e& y. {8 W return m_clrHighLightEdge; - n; N9 w8 D& ]# o) q}

int CListCtrlEx::GetItemImage(int nItem)* D# c' n) M5 K4 `$ ~5 u$ K { + I* k* o" p0 b/ H& V% c" i LVITEM ItemData;5 r. w. j( v# Q( y; c$ Q/ x ItemData.iItem = nItem;4 L" G9 S- d- X! }7 R: F ItemData.iSubItem = 0;0 p; l8 R) Z2 i! I ItemData.mask = LVIF_IMAGE;. X( T7 a# N, A pThis->GetItem(&ItemData); 8 y/ U: [4 {3 D9 Y' g$ f return ItemData.iImage; . }0 c+ S) ]9 U6 J5 y9 p}

BOOL CListCtrlEx::MoveItemUp(int nItem)3 N8 j. v+ }: Y# K { 1 v$ Z. C( {8 K& B/ s1 V if (nItem < 1 || nItem >= GetItemCount()) ; i7 ?, ~" O+ w; k- N( T" f" o { . {/ L0 ]/ ^, Y- t$ } return 0; . @5 U1 H" w8 P4 O4 g" d }- l* P$ P( X( I+ T return SwitchItem(nItem,nItem - 1);$ }3 `$ d) Y C! R }

BOOL CListCtrlEx::MoveItemDown(int nItem) . A' N" b* M3 m; k# v8 N{7 e$ D9 l7 k% c* R. @) G if (nItem < 0 || nItem >= GetItemCount() - 1) 0 C+ l4 }! }& l5 |3 X, ^ {& r; L: E; u. [4 p return 0;0 }2 A0 w' Y" N9 V1 u( | }' ]) [& V* f+ b" |9 ^1 F return SwitchItem(nItem,nItem + 1); 7 N* ~) e* K- F}

void CListCtrlEx::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult)( D# \; Z" |6 i3 s9 D { / ~3 [7 c& V. r/ o NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR);

*pResult = CDRF_DODEFAULT;

if (pLVCD->nmcd.dwDrawStage == CDDS_PREPAINT)$ F2 b p6 t6 P+ n d% u { 6 l9 l0 Q, H" T( G *pResult = CDRF_NOTIFYITEMDRAW; $ m4 a5 M5 y) Q- i) P1 o }/ a7 \, Z5 U# Y; v else if (pLVCD->nmcd.dwDrawStage == CDDS_ITEMPREPAINT) ( b Q; s4 A7 m+ c( P { / L- q4 J1 {- `/ N- v *pResult = CDRF_NOTIFYSUBITEMDRAW;2 ^5 t5 C- N5 d2 B; x% Q3 ^ } 3 d" z, I' @( P8 \ else if (pLVCD->nmcd.dwDrawStage == (CDDS_ITEMPREPAINT | CDDS_SUBITEM))3 _' X* D5 J, W; p% d" S; J+ p. U {

int nItem = static_cast<int> (pLVCD->nmcd.dwItemSpec);" p9 h: W, K8 e# B int nSubItem = pLVCD->iSubItem;9 W N% O: b+ m# l if (nSubItem != 0)8 ]) @: V N$ a/ u( F- ` e2 { { / @ }/ X* J6 s6 w4 w // 全部绘制过程在nSubItem == 0 时完成& ^& b5 H. _9 b& a- f% @ // 分布完成会有小问题,我无法解决 ( h6 F# V$ q1 r/ N: W- [: } *pResult = CDRF_SKIPDEFAULT;$ V/ M( C7 t9 b0 Q" u+ Z- C return; 3 F4 [9 i) I& Q1 ?# M }5 Y) Y* C4 K: q! a/ ] CDC *pDC = CDC::FromHandle(pLVCD->nmcd.hdc);$ S. s1 L% B& O2 m& a: w c int nColCount = pThis->GetHeaderCtrl()->GetItemCount(); I% e0 E& Z: [6 E+ ?* D2 \' u8 ^ CRect rectSubItem;" q- A G( ~9 q0 W7 a, U CString strItemText;5 \7 y: d* X6 s3 d8 X( P3 a& r; H pDC->SetTextColor(((ItemDataEx*)pThis->GetItemData(nItem))->clrItemTextColor);% W& M. l f. \4 C" p7 ^ pDC->SetBkMode(TRANSPARENT); - u' ]; X' c! @1 A // 相当于橡皮. `9 ?, ?& D9 @# m; C0 b( b CBrush brEraser(GetItemBackColor(nItem));

// int nFirstWidth = GetColumnWidth(0);

# |& f' ?; w2 e, a# f. e% G for (int i = 0;i < nColCount;i++) 7 Q. k: [, l" r {5 ?/ c8 e! b# Z2 \# l; e1 i strItemText = GetItemText(nItem,i);

if (i == 0), T0 D# O4 T) Z- b( S { 8 w6 D5 _; T9 } pThis->GetSubItemRect(nItem,i,LVIR_BOUNDS,rectSubItem); 7 R$ |, }0 D# w // 擦掉原来的图形 4 }$ @9 H2 e( Q/ u6 | pDC->FillRect(rectSubItem,&brEraser); % ]' ]. r8 J) Q- m; S0 }8 y" W if (GetExtendedStyle() & LVS_EX_CHECKBOXES) 9 g: ?, X% U1 o1 d" A0 |9 {* o5 R { 9 K! {0 G9 f; o" S- w CRect rectCheckBox; 1 s* X8 ^( @/ Q7 u GetSubItemRect(nItem,0,LVIR_ICON,rectCheckBox); % |0 y j2 q: ^2 t0 N& K CRect::SwapLeftRight(rectCheckBox); $ x2 z3 G$ q3 n0 G# n; { rectCheckBox.left = rectCheckBox.right - rectCheckBox.Height();

rectCheckBox -= CRect(2,2,2,2); 5 s/ y. `/ B3 u, W- k4 \& @4 o pDC->Draw3dRect(rectCheckBox,0x81521D,0x81521D);% [8 Y3 i8 P% {$ Y //rectCheckBox += CRect(3,3,3,3); Z$ I8 y0 X d4 @ p. H0 D if (GetCheck(nItem))" x, q: N4 Z5 u8 k {- v5 A1 F. B" r4 O/ u( F // 绘制一个CHECKBOX $ J# O/ p' g5 k' Q7 N CPen penCheck(PS_SOLID, 1, 0x0021A221); 7 e4 N x& y4 Y! T5 ?- ~: } CPen *pOldpen = pDC->SelectObject(&penCheck); ) D0 {* g$ S3 F1 b& C2 b, Q7 m; R for (int j = 3; j < 10; j++) 7 j0 b; `1 n. @ {* f, F; h4 i0 F7 | K0 c6 V pDC->MoveTo(rectCheckBox.left + j, rectCheckBox.top + rectCheckBox.Height() /2 - 3 + ((j < 6) ? j - 1 : (9 - j)));# k2 N- X, y" Q8 i1 l pDC->LineTo(rectCheckBox.left + j, rectCheckBox.top + rectCheckBox.Height() /2 - 3 + ((j < 6) ? j + 2 : (12 - j)));# E$ c. K z( W } % e1 A0 i6 D8 Q6 W# }* j v C8 _. i' b5 K2 o5 k pDC->SelectObject(pOldpen); 7 p( A: }7 W, C1 {9 B: }( d } % h: h6 l6 Q2 z- l9 M7 T3 N } ; v e- D {9 F7 v2 G: _6 \9 P& a // 根据需要画选中框 9 [0 p/ o, X% K8 D( |7 q- G* x; [+ [ if ( (GetFocus() == this || GetStyle() & LVS_SHOWSELALWAYS)&&(GetItemState(nItem,LVIS_SELECTED) == LVIS_SELECTED))- H3 W- w H% v) H7 N) z, z& w {( N" ~0 a! j8 R* e, D$ o if (GetExtendedStyle() & LVS_EX_CHECKBOXES)+ h( u- K. c+ N/ b c% E+ k+ E {// 存在检查框风格的时候需要对左坐标进行修正 % [' V7 G. C" R) A e6 U CRect rectLeft; ! s' [$ c" _) H5 x6 L GetItemRect(nItem,rectLeft,LVIR_ICON); S' t4 Q1 |- a( ~ rectSubItem.left = rectLeft.left;- M, O3 ?5 l* g }# ?. c# o3 _$ L; d$ G- F( b if (!(GetExtendedStyle() & LVS_EX_FULLROWSELECT)) ( n" s! q# a9 @, Y3 q {// 非全行选择时右坐标需要修正 ' ?% k' V, k8 j# O. i. w2 d2 _ CRect rectRight; ( G- X- W9 @% x9 q, Z6 T GetSubItemRect(nItem,nSubItem,LVIR_LABEL,rectRight);. `4 I( }; ^9 n5 j0 p rectSubItem.right = rectRight.right;0 m, G8 R; p- u, B# n }" ?, P1 G* j6 W* |2 t CBrush brHighLight; ) [2 f( v/ ]: u# c CPen penHighLightEdge; 6 J! O/ o2 i; Z2 J$ W% g( K if (GetFocus() == this) , A; {7 R6 G9 D9 R7 ? { 7 s9 Y+ ?! }) M brHighLight.CreateSolidBrush(m_clrHighLight);4 o5 V' l, x% l# z0 o5 @+ z* G penHighLightEdge.CreatePen(PS_SOLID,1,m_clrHighLightEdge);% ~$ [: o* Z# U) N L3 d K }2 b( I. H7 e6 z else, _: o2 R2 ?' d; w& G { 6 l* j; _' S- i: Q8 n G // 不是焦点时,颜色70%不透明度 5 l1 r6 b7 l' \( ?& F3 O COLORREF clrBack = GetItemBackColor(nItem);2 F6 y# a2 J7 F# Y2 q% J WORD red = GetRValue(m_clrHighLight)*0.7 + GetRValue(clrBack)*0.3; * Z) c8 k0 k. R+ w8 G( ^ WORD green = GetGValue(m_clrHighLight)*0.7 + GetGValue(clrBack)*0.3; # q4 U- Q# u2 _ WORD blue = GetBValue(m_clrHighLight)*0.7 + GetBValue(clrBack)*0.3; 8 n+ m6 }: h. i- _# X/ Q brHighLight.CreateSolidBrush(RGB(red,green,blue)); 9 R+ i T& {: V red = GetRValue(m_clrHighLightEdge)*0.7 + GetRValue(clrBack)*0.3;! A+ C/ @- n5 R+ ^% p6 C green = GetGValue(m_clrHighLightEdge)*0.7 + GetGValue(clrBack)*0.3; 2 r$ R! j k" A n/ T blue = GetBValue(m_clrHighLightEdge)*0.7 + GetBValue(clrBack)*0.3; / A! Y$ ?% k! P8 a R& ^6 s( ]$ \" L penHighLightEdge.CreatePen(PS_SOLID,1,RGB(red,green,blue));: T, ~: x4 v8 `% u( b" V4 r$ a/ p } $ I! K0 g2 K/ I CBrush *pbrOld = pDC->SelectObject(&brHighLight);' z$ s- @6 D1 _! E/ a% T" o5 } CPen *ppenHighLightEdge = pDC->SelectObject(&penHighLightEdge); ) U# ~% Z/ X( a8 z4 M2 }2 N f2 N3 ?3 K! V pDC->Rectangle(rectSubItem);4 \% l% G# V. a% W6 K pDC->SelectObject(ppenHighLightEdge);& O% v* t# k; _: {% [ pDC->SelectObject(pbrOld);6 Q" Q# b6 m; h- t }9 b. `, ~& R g/ j$ x H }- N5 \; q9 d- x3 g0 D# ]9 ~" Y: D2 W pThis->GetSubItemRect(nItem,i,LVIR_LABEL,rectSubItem);4 V+ k7 j! w4 Y8 L; W' T UINT uFormat = DT_SINGLELINE|DT_VCENTER|DT_END_ELLIPSIS;5 s7 J' E" c% \4 ]7 A HDITEM hdi; ) {6 f; u+ Z+ Y+ x Y {/ v9 m1 y hdi.mask = HDI_FORMAT; " a' q K3 S8 z: F+ ]" R pThis->GetHeaderCtrl()->GetItem(i,&hdi);( L) @ j" N" O1 K7 z( Q, o' u if (hdi.fmt & LVCFMT_RIGHT)/ A8 H: E5 G0 R* C: V& x+ n9 O {) p: M, N# r7 V$ V9 W' p/ ] uFormat |= DT_RIGHT;! ?* o3 Y( ~$ y0 M( K }& Q# [! m2 l0 g5 ?8 k# z% ]& E else if (hdi.fmt & LVCFMT_CENTER)$ u( g; ~: A6 j! _8 F { ' ]% q Z1 a/ U uFormat |= DT_CENTER; + N( }. c3 u! M1 f }! r% i1 x& \$ o; k: V pDC->DrawText(strItemText,rectSubItem,uFormat);

CImageList *pImageList = pThis->GetImageList(LVSIL_SMALL);1 y$ u! |. G9 l- e: M5 ~6 C if (pImageList)" s8 q9 P" s$ h9 r4 X, v {4 i. `/ z0 u. F! V8 Q" t CRect rectIcon;8 @5 ^# L. K. n$ k+ j GetItemRect(nItem,rectIcon,LVIR_ICON); ; D5 |% E9 R$ N5 e pDC->DrawIcon(rectIcon.left,rectIcon.top,pImageList->ExtractIcon(GetItemImage(nItem))); 0 P) G; P7 j: N3 J( R1 j }$ \* |5 x$ N& p0 ^1 m } 6 G2 B/ E8 a' G5 L8 K% f0 u# I *pResult = CDRF_SKIPDEFAULT;' Y. H2 o0 ]( y! c7 I }

}

该用户从未签到

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

本版积分规则

关闭

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

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