下沙论坛

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

QQ登录

QQ登录

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

又是垃圾,CListCtrlEx Base CListCtrl

[复制链接]

该用户从未签到

跳转到指定楼层
1
发表于 2005-2-23 21:29:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
// ---------------------------------------------% r/ }4 q1 ^# k# G% S! b
// ListCtrlEx.h
$ b# T+ d2 a8 Y& {$ W/ B" f3 G// ---------------------------------------------5 I4 b* Z8 o, J' y
#pragma once
) t2 r1 X7 c$ _. J" E# D# P# j4 w4 Z/ w
// CListCtrlEx0 c" d& S( i9 b9 a6 }
class CListCtrlEx : public CListCtrl* w/ X7 H$ J" v4 X) J3 }
{+ t% E$ }! z9 `; U
    DECLARE_DYNAMIC(CListCtrlEx)
! F" x: q; ~4 y6 opublic:& [7 Z& i% w$ \; i$ ?
    CListCtrlEx();* {; g; z0 F* L6 J* j$ L2 D/ W" \7 E
    virtual ~CListCtrlEx();
  ~3 C3 f, E( c& Q5 A& k+ J& d$ gprotected:" m1 O# K0 V2 Q; r1 {
    DECLARE_MESSAGE_MAP()9 T2 Z+ e% X- \) O# ]
    CListCtrl* pThis;
9 @2 F! Z  c6 [3 F1 j, R4 c    virtual BOOL PreCreateWindow(CREATESTRUCT& cs);4 q8 B% S1 F: ]
private:
5 m" t, X* e7 _3 f1 R: ^- W6 A    struct ItemDataEx {. q5 z. z- ?2 [* j. ]
        LPARAM SrclParam;* T7 X( l% E  j
        COLORREF clrItemTextColor;
) C: g! u: ?. P. m$ f        COLORREF clrItemBackColor;$ s; P7 e3 K# A
    };
0 n8 E' m0 y/ b0 `/ r7 e    COLORREF m_clrHighLight;
3 z4 A) r) Y3 P( x/ e% a; A    COLORREF m_clrHighLightEdge;, O) q+ Y, E8 d' {. a
public:- g4 w  \9 d5 B' i9 @* T. i
    // 新增方法
; Z+ N* ^& u& B  I" g- G+ @    COLORREF SetItemTextColor(int nItem,COLORREF clrText);) u) |0 N3 f# i* M6 B3 {* y
    COLORREF GetItemTextColor(int nItem) const;2 }+ w+ C+ q8 |; w/ F: b- U" _
    COLORREF SetItemBackColor(int nItem, COLORREF clrBack);
) O2 O/ S/ A/ e8 }1 D) i    COLORREF GetItemBackColor(int nItem) const;
% D! a  C  M  p' f    COLORREF SetHighLightColor(COLORREF clrHighLight);
: R7 J1 b& {0 }' p4 d    COLORREF SetHighLightEdgeColor(COLORREF clrHighLightEdge);3 u& ?$ l# {, U+ S4 z1 @! @/ b
    COLORREF GetHighLightColor(void) const;; n: X# P. V* P1 B
    COLORREF GetHighLightEdgeColor(void) const;( e& L& p# P& z% T
    int GetItemImage(int nItem);4 R1 E! s, P0 S. M
    BOOL SwitchItem(int nItem1, int nItem2);* o$ @5 ^- O3 a
    BOOL MoveItemUp(int nItem);9 I. y) s' Q' o- D
    BOOL MoveItemDown(int nItem);8 e. Y/ _; k- M  b1 V/ |
    // 重写原来的方法
# M$ U' ?& v, I! D    DWORD_PTR GetItemData (int nItem) const;% N9 r7 K- Q& Y
    BOOL SetItemData (int nItem, DWORD_PTR dwData);
5 p3 W2 O  n# O6 J- o$ W$ ^    int InsertItem (const LVITEM* pItem);  [3 y) v. ]: o
    int InsertItem (int nItem, LPCTSTR lpszItem);
) _( s3 W$ K8 N( h    int InsertItem (int nItem, LPCTSTR lpszItem, int nImage);
3 F/ @, J2 b& u1 T" L    int InsertItem (UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam);8 M/ V, O3 ~7 o, d: n" f
    BOOL DeleteItem (int nItem);6 A# w$ `5 `; B; U" C* B3 ?; H
    BOOL DeleteAllItems ();" r7 B! b; {3 p* y+ p
    BOOL GetItem(LVITEM* pItem);
6 M5 q5 n3 {- C" [    afx_msg void OnDestroy();1 C( v0 H' B6 s; Y( i4 H
    afx_msg void OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult);
+ U4 R& O, {" I' L6 y};
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 顶 踩

该用户从未签到

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

// --------------------------------------------- / R: ~: A" c5 B& H// ListCtrlEx.cpp& ?& z4 {3 V3 y5 {3 i8 ]6 q // ---------------------------------------------7 ]# b7 o8 F0 h, o // ListCtrlEx.cpp : 实现文件 ' [& ^# W6 X+ s3 d- U//

#include "stdafx.h" K v. Y) T1 e #include "CreateNewClass.h" 3 N% r! Z! r, m, i) l#include "ListCtrlEx.h" - i& Q2 Z" H8 U) G, E#include ".\listctrlex.h"

; m8 V5 s4 u7 f& j, @// CListCtrlEx

IMPLEMENT_DYNAMIC(CListCtrlEx, CListCtrl)+ E' x6 R; z# b CListCtrlEx::CListCtrlEx() @+ o r, s& B ` {) H% S9 s6 x$ } // 用于调用基本的方法9 ?- o7 d% L% F7 ]+ [. r pThis = (CListCtrl*)this; 7 O c* x( W1 j( N // 默认的选中框颜色 0 Y9 i1 `1 X; X6 P4 L, Y8 h; l m_clrHighLight = 0xF1DACC; + G9 L$ U u+ @( c m_clrHighLightEdge = 0xC56A31;# z2 R5 C4 p0 C }

CListCtrlEx::~CListCtrlEx()/ f: s, w/ e& C0 o+ Q i {

}

8 w1 U- K: v% lBEGIN_MESSAGE_MAP(CListCtrlEx, CListCtrl). r5 G/ x7 n. n ON_WM_DESTROY()1 z. h O7 U: d: e ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnNMCustomdraw) + Y' `" e) j: R- `! DEND_MESSAGE_MAP()

// CListCtrlEx 消息处理程序

% a0 D6 z1 ?1 Y! pCOLORREF CListCtrlEx::SetItemTextColor(int nItem,COLORREF clrText) X6 q- m. i# f* ?3 e7 Q { ' y: Z1 o4 {3 B COLORREF clr = GetItemTextColor(nItem);) l) m# x" i: H; t ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem); * ]3 t) T+ ?) W. p0 g1 u pData->clrItemTextColor = clrText; ( E0 `- s( N- m5 P return clr; 9 r4 K- d" e* ^+ w}

COLORREF CListCtrlEx::GetItemTextColor(int nItem) const8 o. D8 `. E! j% F7 D% D# F5 U { " `3 I1 a! {( m ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem); + B% C, F! v: t* ` return pData->clrItemTextColor;/ x& i& e$ {# M% P }

COLORREF CListCtrlEx::SetItemBackColor(int nItem, COLORREF clrBack) ' a% }2 [! O! q3 O& `" r{ " \# k$ ~& b. d, O: Z7 T7 w COLORREF clr = GetItemBackColor(nItem);7 z6 n8 v, X) a ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);" g2 ~% X. q* O# |* v1 N pData->clrItemBackColor = clrBack; , @1 x1 a v x/ Y0 Q" a return clr;: k( j! A. i" S3 n }

COLORREF CListCtrlEx::GetItemBackColor(int nItem) const$ B+ w& }# b, F. |7 W' k7 u$ r& J { U8 m& q( {6 U ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);& x ?+ x5 y6 N0 k/ z( ` return pData->clrItemBackColor; 6 c. F B$ ]: y) W" z}& c, s; v$ h* m* b& V1 O$ Z/ E! C DWORD_PTR CListCtrlEx::GetItemData (int nItem) const- }7 T: w$ ^* Y5 _" b { " @2 W0 v: E5 A" X if (nItem >= GetItemCount()) 7 ~* M! \4 k/ |" z8 M) p { " Z6 O- T1 K+ W3 i1 `3 I return 0; # q: f$ J$ d2 N$ f4 v2 V. C } 2 K$ n, V, ]; m CListCtrl* pThis = (CListCtrl*)this; 3 r5 H9 o* I# K6 Y DWORD_PTR Data = pThis->GetItemData(nItem); v+ x% R4 w! `7 S* [ return ((ItemDataEx*)Data)->SrclParam;, d+ U: p6 p: ~3 S# F, m( ~ } " o8 V% ^ `; w9 k/ F& B1 U- RBOOL CListCtrlEx::SetItemData (int nItem, DWORD_PTR dwData)/ s; Y1 i* b# o* Y1 l' v/ j: K {4 g! c7 _- R9 F if (nItem >= GetItemCount()) 2 f3 q: ]+ Y4 _! L0 L' n { 5 _& y0 T5 A+ U6 b4 z return 0; * \' j/ Y% P2 M5 O7 U9 a; B9 y }7 t6 T0 ]5 X7 ^" n1 q3 }. m: ^ CListCtrl* pThis = (CListCtrl*)this; 1 D- u/ ]4 `8 r5 B DWORD_PTR Data = pThis->GetItemData(nItem);# i3 K1 g# ~7 P! T ASSERT(Data); * }/ F/ Q9 y* {$ {5 t+ ~ ((ItemDataEx*)Data)->SrclParam = dwData;# i. m: b/ F6 e+ U; j D' N, e$ N+ R return 1;/ l( P* e3 v/ Y m( J } 2 A9 D( B. n1 C, jint CListCtrlEx::InsertItem (const LVITEM* pItem)8 Q; \; i `9 y' U1 { { 7 c1 m! i% M# [" } int nReturn = pThis->InsertItem(pItem);! E8 [' Y# O1 n9 }" a$ Y( Q ItemDataEx *pData = new ItemDataEx;' d3 Q3 @* e+ y. [1 O& g // 设置默认颜色 7 a8 b' {, V2 B6 a pData->clrItemBackColor = 0xFFFFFF;//pThis->GetTextBkColor(); W3 C6 d, m& j/ M- I, Y2 Z pData->clrItemTextColor = 0x000000;//pThis->GetTextColor();1 |# k$ l6 g+ _2 w% @2 `' o pData->SrclParam = pItem->lParam;/ H. _6 a U& y! q) v" ] pThis->SetItemData(pItem->iItem,(DWORD_PTR)pData);6 g' U( |; G& K, S7 Q return nReturn;5 y+ l5 } f' U" A1 v }: \) m. a) @) l9 ] int CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem)( v; I7 R6 @- r5 J: | {. O1 n% ~9 I0 [/ |9 X+ [ LVITEM Item;; M) n5 d# d s4 L' g memset(&Item,0,sizeof(LVITEM));4 I) O" P& e4 B9 x# ^ Item.pszText = new TCHAR[strlen(lpszItem)+1]; 0 d% u" R# l1 ?' S% T! b$ [% ] strcpy(Item.pszText,lpszItem);, H1 K7 }. I5 k3 S% C Item.iItem = nItem;8 Y& |( V9 A' r7 `( n# T( j* K- Y Item.lParam = 0;) f5 x' P( X6 ^! W Item.mask = LVIF_TEXT |LVIF_PARAM; y+ `4 O9 g" J4 D; v3 W BOOL bReturn = InsertItem(&Item); 0 B/ {/ Z' o! `9 c9 g( n delete Item.pszText;% M2 l8 z- V7 N+ P& w' c& R return bReturn; ) z! ^1 @! o6 Z" n$ U! E}6 O6 Z% @" U9 W% H int CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem, int nImage) ; P5 X/ V1 b3 v* s{ % j ^6 V5 a6 R7 g8 s; u& _- n LVITEM Item;) v$ w" b) W* \& r memset(&Item,0,sizeof(LVITEM));4 A) Z/ \- \1 X/ O! `1 p5 n8 k/ ^ Item.pszText = new TCHAR[strlen(lpszItem)+1];& y0 V+ K% F# M* ?, Y8 s; N strcpy(Item.pszText,lpszItem);+ [: ~" _7 d5 Z Item.iItem = nItem;* F) j# r5 X' A) k2 M$ B Item.lParam = 0; p3 V) T; W! o1 h Item.iImage = nImage;5 ` i4 a- c. l: J, T8 l Item.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE; ; d& S' k$ z, K# _$ d6 @1 R* B) O BOOL bReturn = InsertItem(&Item); & z. P3 P; B* F" O% b1 t, |, ^ J- z delete Item.pszText;7 u" P! n W# m/ a+ F7 f* s return bReturn;3 ]/ h3 S( p1 {& X } 2 Y$ W" G2 M* K# d6 z, Aint CListCtrlEx::InsertItem (UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam)( i# ~: c! x4 l( U8 |% c" O9 f {: G' D( N" A6 u LVITEM Item;! Q+ Z0 E1 ]: y# d e/ D- N memset(&Item,0,sizeof(LVITEM));! u" ^0 G2 y. u% H& I Item.pszText = new TCHAR[strlen(lpszItem)+1]; 3 B. q; |. S; y/ K strcpy(Item.pszText,lpszItem); 8 x. a( Z# ]2 @" E, e: |' D Item.state = nState;# W. O, L. q Q" N Item.stateMask = nStateMask; , w* P9 r# a' q& {4 i Item.iItem = nItem; ! \$ X$ ~+ e: ^7 E% p6 A. L% h [! | Item.lParam = lParam;3 p+ C3 q4 ?: ^; p! G Item.iImage = nImage;% {' L* x; s" e8 R' i' K4 Y$ _% q" t Item.mask = nMask; % f; [" ^6 H+ c7 \. i' |. a7 A6 V BOOL bReturn = InsertItem(&Item); 7 W* }5 W a& t- p delete Item.pszText;* {2 h) K. c3 G return bReturn; # g" ]+ D6 e* E$ ]+ M3 W4 T} 2 g/ y) Q! P+ i2 H z0 r5 ?' _+ nBOOL CListCtrlEx:eleteItem (int nItem)! m4 I% ^0 [& N- s { ) v9 Y% r( t6 s) g5 [ void* pDataEx = (void*)pThis->GetItemData(nItem);8 g& X* o8 |, x5 t0 z8 h% a if(pDataEx != NULL)+ m8 L8 `( Y1 L6 w. ~ {# K. G! d6 J3 ~0 \ delete pDataEx;6 ]- E' Y9 w4 X$ }/ {$ n }6 D; ?1 \* {4 F, x4 Q( I# w2 i return pThis->DeleteItem(nItem);4 `$ i0 S' f; z/ e/ H } F2 u7 l5 B) ^! SBOOL CListCtrlEx:eleteAllItems ()! i0 S V7 m! ~2 B$ G& U { . E/ d. z) Y" T8 K1 S int nCount = GetItemCount();& b! {& k1 S. s: K, B7 N for (int i = 0;i<nCount;i++); H4 p5 a; C' h/ }, O9 L {) V) j& J' {) u/ [- z7 Q: v5 q if(!DeleteItem(0)) 8 x2 T0 Y5 C6 {4 M* z) J {+ c* P' b! A9 H X; S return 0; ; w& G) _/ ~: f4 t4 E }9 ^ j% S2 ]9 W& q% k7 R0 d }0 [5 h5 o* h" f$ y; Y- n return 1;9 V1 V& D3 o5 r+ ]$ E( u: z } * T' l6 e h/ [! O6 ^6 A+ KBOOL CListCtrlEx::GetItem(LVITEM* pItem) " ~3 `0 v6 B0 n9 U( ^8 ~{" m& @- ~; T9 G BOOL bReturn = pThis->GetItem(pItem);4 L/ S1 r. n8 i7 v; ` if (pItem->lParam): V1 d& ]. t. q1 Z {' T0 U; p7 [/ D pItem->lParam = ((ItemDataEx*)pItem->lParam)->SrclParam; 2 N" [/ M/ g+ S G7 { }

return bReturn;, ~) C _5 P+ L } 4 F z7 o, z7 ]$ L) [ Vvoid CListCtrlEx::OnDestroy() , }' }, x! L9 S4 x{8 y, |, E! p0 w$ [' _ // 关键是清除申请的内存空间" R5 y$ X$ m3 s7 ^/ t2 S DeleteAllItems();# g" Z$ [% a' b; I$ V7 x2 u! D+ M! O CListCtrl::OnDestroy();, ]" E: q& P6 p* D# O }

BOOL CListCtrlEx::SwitchItem(int nItem1, int nItem2)3 E8 g+ A" ?/ |$ \- ^9 d { 8 g" f) }/ _# Y' s" T D/ w% K if (nItem1 == nItem2 || nItem1 >= GetItemCount() || nItem2 >= GetItemCount() || nItem1 < 0 || nItem2 < 0) : x6 S+ G0 r, q. |0 p3 U4 x5 I. h+ k {# A7 J8 g2 @: ]2 q% l' i return 0; 7 g* ], j# i% H' S }9 z$ U4 ^$ q/ F! J LVITEM ItemData1,ItemData2;6 L, A0 X L- S; j8 S int ItemLength1 = pThis->GetItemText(nItem1,0).GetLength(); + W* `/ s8 E0 ]& l8 m. _ int ItemLength2 = pThis->GetItemText(nItem2,0).GetLength();* Y7 W# l9 j6 F0 p+ y ItemData1.pszText = new TCHAR[ItemLength1+1];1 f% b0 K0 o5 u o/ D4 U/ E ItemData2.pszText = new TCHAR[ItemLength2+1]; 6 l# W7 n7 e1 `2 K& @, }3 J/ e' E ItemData1.cchTextMax = ItemLength1+1; ! p( d% k: d. L+ m: E [; B5 \ ItemData2.cchTextMax = ItemLength2+1; l" M1 N! g, J. x, k$ b ItemData1.iItem = nItem1; + d# D9 Q0 U* P3 c* Z! s1 M ItemData2.iItem = nItem2;, j* x5 I" `1 x# _ U6 Q$ x- f3 P ItemData1.iSubItem = ItemData2.iSubItem = 0; & L! {6 X' r0 p9 {$ U* d ItemData1.mask = ItemData2.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE|LVIF_STATE;

pThis->GetItem(&ItemData1); 6 V, W# T1 o$ F! [: {! j5 V8 Z1 A pThis->GetItem(&ItemData2);) v* G( @& j% N0 f( F // 交换数据 ! |% e+ R/ e: H4 a( V. i6 {$ A ItemData1.iItem = nItem2; 9 L) L0 g3 c. w4 y' N& H ItemData2.iItem = nItem1;! `) ~4 ^& T- q( n8 ]4 n pThis->SetItem(&ItemData1);) Y% N. e/ Q. ~2 u* R: ~+ | pThis->SetItem(&ItemData2);

delete ItemData1.pszText; . a) K- J/ F7 a( _6 c0 D8 k delete ItemData2.pszText;! n( n9 K% f: U0 i, v# Q$ w // 得到报表模式的列数 / [' K( I z7 v( q$ \9 @ int nColCount = pThis->GetHeaderCtrl()->GetItemCount(); t! n0 [# t3 B c7 F9 {, z CString strBuffer; % _4 V3 [0 L4 M) g/ `, Q7 Z/ g" T for (int iSubItem = 1;iSubItem < nColCount;iSubItem++) $ h6 ]1 L" \: Q+ f2 b8 x! ?- B { 2 V4 q# i; |/ A4 V* M& x strBuffer = pThis->GetItemText(nItem1,iSubItem);- s/ A, q/ k2 R7 g5 B pThis->SetItemText(nItem1,iSubItem,pThis->GetItemText(nItem2,iSubItem));% y8 \' _3 G" e3 T* ~/ ? pThis->SetItemText(nItem2,iSubItem,strBuffer);6 [7 x' L' _! y! e } * W; }7 B4 w4 G# s; S$ F* L // 把原来的选中状态也转移过去- N& m3 I* v( m' h1 y- |& L // 不知道为什么这个属性没有体现在LVITEM结构中……) p1 ?& x3 f2 a7 W8 s6 z bool bItemSelected1 = GetItemState(nItem1,LVIS_SELECTED) == LVIS_SELECTED; 5 i- r7 Z) A1 I bool bItemSelected2 = GetItemState(nItem2,LVIS_SELECTED) == LVIS_SELECTED;

SetItemState(nItem1,bItemSelected2?LVIS_SELECTED:0,LVIS_SELECTED);. m, ^# _; _+ u SetItemState(nItem2,bItemSelected1?LVIS_SELECTED:0,LVIS_SELECTED);* }8 i# L3 x# n: W7 C( K. ~5 \$ A return 1; ) `6 t6 N7 G) ^5 t. C}

BOOL CListCtrlEx:reCreateWindow(CREATESTRUCT& cs)$ h; l" b' m) R { $ I0 b5 d/ d7 P3 w# F" O: @ // 本类报表模式专用 2 Q* y. Z% O- D7 A' H3 X7 g { cs.style |= LVS_REPORT ; ! D! k2 g) K+ f% ?9 h // cs.dwExStyle |= LVS_EX_FULLROWSELECT;- R) K: o3 z$ Q- J% n return CListCtrl:reCreateWindow(cs); ! n7 a/ X+ T1 s9 }! O4 L}

" g' G2 X' t. h: Q# f. TCOLORREF CListCtrlEx::SetHighLightColor(COLORREF clrHighLight)2 t0 N5 P; W H J! q3 U9 t { , }! f4 k6 o- a: e% J! { COLORREF clr = GetHighLightColor(); 9 w, ~" L& M Q$ J m_clrHighLight = clrHighLight; 1 v- I- C* } ]/ k; V3 ~ return clr; 3 T1 G+ W$ G* y4 Q" u- G" M/ I}

COLORREF CListCtrlEx::SetHighLightEdgeColor(COLORREF clrHighLightEdge)- l' u1 m6 I0 E4 A' `7 x, ` { 0 X) l) c& E. s* k* t COLORREF clr = GetHighLightEdgeColor();3 R1 ^! p* ?5 { m_clrHighLightEdge = clrHighLightEdge;8 O9 A7 ]! g/ ], R, d return clr;/ Y% i6 W" j9 W! o9 S& _! z: M }

该用户从未签到

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

COLORREF CListCtrlEx::GetHighLightColor(void) const 7 y% ~+ {' ~ _- _{ 0 ?* y; A9 g6 s) G+ @5 A return m_clrHighLight; - _/ H2 e' t# Z" }: a}

COLORREF CListCtrlEx::GetHighLightEdgeColor(void) const % | v6 r0 q% x# N" y0 Q' A{ w m& _: G1 t3 @: c& S5 D/ O return m_clrHighLightEdge;$ t9 b/ c! R! z3 n* |6 O7 k& z }

int CListCtrlEx::GetItemImage(int nItem)( Q( I6 L2 D$ d4 J3 w { & G4 M8 @$ a/ Y# w$ p- _ LVITEM ItemData;7 J# m0 v: W/ n' P% k# j ItemData.iItem = nItem;: q5 B) ~! S2 \/ L+ Q ItemData.iSubItem = 0; , W' n; Z# h9 r# ]9 T1 G$ I ItemData.mask = LVIF_IMAGE;; {0 W3 W; B& l G pThis->GetItem(&ItemData); % g" |& I2 e% A+ \4 O Q' D0 d return ItemData.iImage;# @8 S8 [. Y' I" z% x7 m' u }

BOOL CListCtrlEx::MoveItemUp(int nItem)4 i4 b6 G6 M' U$ o( k: M {- P( u0 K) [* c% r, O if (nItem < 1 || nItem >= GetItemCount()) . C+ P; e' d% v% J$ X1 k+ F {# ^7 } K2 a+ j3 r return 0; ) ?' i8 e1 a6 s4 Y } / y- s# \ t" L% M1 S1 ~# r return SwitchItem(nItem,nItem - 1); / N& H1 s* I) k& B7 Y* x1 |}

BOOL CListCtrlEx::MoveItemDown(int nItem) . |& x& w ?0 [3 k8 c$ C, n{ 9 Q; @& [+ B8 }/ V" a \5 y2 a if (nItem < 0 || nItem >= GetItemCount() - 1)) z5 d( W& G7 B4 e6 ]# k6 J { W, s2 G5 a# v return 0; , n; K# v i) x }# H# G% ~2 h, I4 L return SwitchItem(nItem,nItem + 1); ' `( v/ j' F: a: L}

void CListCtrlEx::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult)" |5 Z, u6 U" }/ a {7 p/ F' F [' ]7 M; B, R NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR);

*pResult = CDRF_DODEFAULT;

if (pLVCD->nmcd.dwDrawStage == CDDS_PREPAINT)" d3 |6 I4 G+ G" L6 D# C { - C/ H! U$ w) L6 `2 T% I *pResult = CDRF_NOTIFYITEMDRAW;5 K6 r7 O$ V7 H, y2 q }" R* ?5 x0 n6 }+ \ else if (pLVCD->nmcd.dwDrawStage == CDDS_ITEMPREPAINT) / x3 M) |+ {' a/ V/ p+ d" ]- L. s% t { ' }7 v$ o8 Q6 R: D) K *pResult = CDRF_NOTIFYSUBITEMDRAW; ( D; a3 g6 s% }9 J } + q* U I8 h; a% Z else if (pLVCD->nmcd.dwDrawStage == (CDDS_ITEMPREPAINT | CDDS_SUBITEM)) " I' ~$ E% W: y. |9 Z" {8 ]" u$ G4 F {

int nItem = static_cast<int> (pLVCD->nmcd.dwItemSpec);3 h9 S V$ m0 E4 e int nSubItem = pLVCD->iSubItem;: K! O( Y( Y2 k5 K if (nSubItem != 0)2 s4 ^, T- P2 i {5 k. Z9 K0 {' c2 ]7 ?& R9 E // 全部绘制过程在nSubItem == 0 时完成- o4 Y* u4 C+ @' Q! X0 j; S$ n4 }6 d // 分布完成会有小问题,我无法解决; |+ Y' Q, M! @ *pResult = CDRF_SKIPDEFAULT; 8 ?1 c7 T- D4 m$ g return;/ t1 O- W3 J+ v& S2 I }: Q: Y5 O4 ]6 V9 o' x9 H CDC *pDC = CDC::FromHandle(pLVCD->nmcd.hdc); 5 O+ }. g. v; B; c8 t: T b int nColCount = pThis->GetHeaderCtrl()->GetItemCount();7 v( ^, q, ?0 M5 q7 e CRect rectSubItem;7 f. e0 s5 U: M1 f' g7 n CString strItemText;$ y5 K" O* U" w* j% H4 K pDC->SetTextColor(((ItemDataEx*)pThis->GetItemData(nItem))->clrItemTextColor); & z+ s$ f; j- a g pDC->SetBkMode(TRANSPARENT); 5 r' i! M! x' u // 相当于橡皮 6 B; L. y0 P% `( A9 n1 K1 a* @ CBrush brEraser(GetItemBackColor(nItem));

// int nFirstWidth = GetColumnWidth(0);

" h8 V2 j$ e, I% ]$ ?7 g for (int i = 0;i < nColCount;i++); j/ m1 u+ f! c+ F: a {" Q( }; l g# _: |/ f; q strItemText = GetItemText(nItem,i);

if (i == 0) 8 e7 y5 n0 Z. q* E: V; B( R { ; [: D7 M9 D2 r! l) @2 ^ pThis->GetSubItemRect(nItem,i,LVIR_BOUNDS,rectSubItem); ( @& ], L" B& X // 擦掉原来的图形 ( h9 }+ w- s( w: {* ?0 i4 d pDC->FillRect(rectSubItem,&brEraser);3 ^8 L' _1 y( k. S- E if (GetExtendedStyle() & LVS_EX_CHECKBOXES)& i0 l; k$ c& X { ) Y; n% E1 m) B e" G0 t1 U+ `* O CRect rectCheckBox;& E: J- T5 o8 |4 J ^ GetSubItemRect(nItem,0,LVIR_ICON,rectCheckBox); ) i; h7 h/ `% @* X; _ CRect::SwapLeftRight(rectCheckBox);9 T9 Z+ n# @; j$ x rectCheckBox.left = rectCheckBox.right - rectCheckBox.Height();

rectCheckBox -= CRect(2,2,2,2);/ I P: y* a# y, J. {* l pDC->Draw3dRect(rectCheckBox,0x81521D,0x81521D);. J4 J9 h& Z) X. [5 E& s) v% J" a //rectCheckBox += CRect(3,3,3,3);) J4 s* \. ~ B if (GetCheck(nItem)) 4 a; B7 l! j+ {- a& q4 r* I+ ^+ h2 Z { 2 W) o/ F$ e$ [$ {2 v // 绘制一个CHECKBOX1 U) r( D& w% C0 F. @ CPen penCheck(PS_SOLID, 1, 0x0021A221); + |6 g6 D: u2 L5 u0 r% f CPen *pOldpen = pDC->SelectObject(&penCheck);# o2 u5 I" }7 }" h5 @1 ^) i for (int j = 3; j < 10; j++) " ]8 A( y4 Q6 {, M# b# N& E0 m {+ e a) {! p4 N6 A, n" Z) Y% r6 T3 p pDC->MoveTo(rectCheckBox.left + j, rectCheckBox.top + rectCheckBox.Height() /2 - 3 + ((j < 6) ? j - 1 : (9 - j)));& E0 H( a% K/ t pDC->LineTo(rectCheckBox.left + j, rectCheckBox.top + rectCheckBox.Height() /2 - 3 + ((j < 6) ? j + 2 : (12 - j))); 5 l% D# ]1 d* |8 n1 I9 D } * }( D$ g% n; I4 Y: W& X) V) } + y$ e7 f; o/ T pDC->SelectObject(pOldpen); 8 q b1 V# H# [( V% @1 P3 H3 C4 ]. A }2 y, g" A2 ~+ H6 N } + H; j, `7 o; i5 A // 根据需要画选中框: X& X! ?8 M+ x0 r! Y( m if ( (GetFocus() == this || GetStyle() & LVS_SHOWSELALWAYS)&&(GetItemState(nItem,LVIS_SELECTED) == LVIS_SELECTED)) ' A7 X4 p6 j, a6 F* ^ h {: ?. u& c- [* x, [; Q1 G if (GetExtendedStyle() & LVS_EX_CHECKBOXES)) ?! C) |' G& N0 d8 I {// 存在检查框风格的时候需要对左坐标进行修正! S# P6 W, M2 H, K& G7 G' P) X CRect rectLeft;6 w" Y% N* {7 R, P* p GetItemRect(nItem,rectLeft,LVIR_ICON);% C& v) z* Y' F8 d rectSubItem.left = rectLeft.left;2 f! P, k- u( g ? } ; G* ?$ @0 l" S0 B X3 G if (!(GetExtendedStyle() & LVS_EX_FULLROWSELECT))+ F1 P3 O ^6 S6 ` {// 非全行选择时右坐标需要修正 6 v: ` q8 w! Z$ I3 o CRect rectRight; 4 u3 L2 L" O1 k# B GetSubItemRect(nItem,nSubItem,LVIR_LABEL,rectRight); * E1 h, B' g/ r$ h6 @% J9 z rectSubItem.right = rectRight.right; 5 e) K3 Q% Y/ L: n } , O* Z* }! |* X$ ?& ~ CBrush brHighLight;2 \# d: k: [# ]$ ^ CPen penHighLightEdge;" A! m' W) C& j) n. a& P$ g$ ? if (GetFocus() == this) . n% U9 J7 I* R A( \8 G. z { 5 l V; n: Q/ Z0 T0 } brHighLight.CreateSolidBrush(m_clrHighLight); : Y# W9 e; i k6 i7 X# T/ m% B/ q penHighLightEdge.CreatePen(PS_SOLID,1,m_clrHighLightEdge);4 z2 Y9 s6 d- u6 F }- x# {; K+ H. }- e2 _ else4 K3 P: D8 `6 Z3 ~; |# l" s' y# u {! t+ a; R, E! N- w5 s // 不是焦点时,颜色70%不透明度- J; S# T$ D2 Z+ Z COLORREF clrBack = GetItemBackColor(nItem);& K o& J, \. b WORD red = GetRValue(m_clrHighLight)*0.7 + GetRValue(clrBack)*0.3; l5 s) Q3 ]0 _! s ` WORD green = GetGValue(m_clrHighLight)*0.7 + GetGValue(clrBack)*0.3;8 J$ w* A; c. L, Y4 X0 F WORD blue = GetBValue(m_clrHighLight)*0.7 + GetBValue(clrBack)*0.3; 8 }. l/ F1 J" N& ~8 L0 t0 d brHighLight.CreateSolidBrush(RGB(red,green,blue));# Z2 O3 q: E% ?; X# P red = GetRValue(m_clrHighLightEdge)*0.7 + GetRValue(clrBack)*0.3;. j/ f* }8 J- \$ t green = GetGValue(m_clrHighLightEdge)*0.7 + GetGValue(clrBack)*0.3; 1 z# O% I7 D1 b9 D8 ] blue = GetBValue(m_clrHighLightEdge)*0.7 + GetBValue(clrBack)*0.3; 6 K1 o- ]) Z4 Z4 O9 L penHighLightEdge.CreatePen(PS_SOLID,1,RGB(red,green,blue));: n9 l" \6 b: i2 t3 l } + j6 e* [3 o8 H9 }8 i CBrush *pbrOld = pDC->SelectObject(&brHighLight);# _0 L' o+ t p CPen *ppenHighLightEdge = pDC->SelectObject(&penHighLightEdge); * j& w2 h- N+ _9 i; ` pDC->Rectangle(rectSubItem);: F8 h( [0 o7 F. _ pDC->SelectObject(ppenHighLightEdge); . v1 X+ d' O' c# ^9 o5 B$ S3 s pDC->SelectObject(pbrOld);6 D& F% z+ i" s; v9 ^ } # R. O W( O7 O }: Q7 k% z. _# F pThis->GetSubItemRect(nItem,i,LVIR_LABEL,rectSubItem);6 g5 l( B; z9 \7 E% l UINT uFormat = DT_SINGLELINE|DT_VCENTER|DT_END_ELLIPSIS; ! Z; l L9 ^0 E2 ^& ^; h. d HDITEM hdi; - W& b3 _2 h$ h7 V6 X9 k hdi.mask = HDI_FORMAT;! o1 W$ n$ a3 j4 n& j* G/ T pThis->GetHeaderCtrl()->GetItem(i,&hdi); 7 O1 v8 I1 N/ G1 w3 Z& R# J! d if (hdi.fmt & LVCFMT_RIGHT)2 R0 v! ?; y' u( j { , [# U0 b& p8 Y# I uFormat |= DT_RIGHT;/ O' S4 C) u& V9 M$ [ } 3 l) a; d8 q. \$ E' H3 u% d else if (hdi.fmt & LVCFMT_CENTER) ) K- w+ P+ c- W# C) I { " @$ `4 ]' M$ R uFormat |= DT_CENTER; $ P# B+ T2 t8 A& K/ {& h I# P0 i9 g } - p% J7 n3 T2 e0 [0 X pDC->DrawText(strItemText,rectSubItem,uFormat);

CImageList *pImageList = pThis->GetImageList(LVSIL_SMALL); ' H$ X, Z. G: d- b+ \& n if (pImageList) 7 _0 \; B4 E$ {' p {- \' C, ^7 z2 I CRect rectIcon;! s: @/ S4 S0 s7 p GetItemRect(nItem,rectIcon,LVIR_ICON);8 C( ~- ]/ y) e0 q! u3 D4 k pDC->DrawIcon(rectIcon.left,rectIcon.top,pImageList->ExtractIcon(GetItemImage(nItem))); 8 M0 ?; p5 M6 V9 k3 A } 7 U3 H' a: J# Y3 A }# ]/ O- a5 P3 S% I# P: j; t0 W! j* Z* k6 B *pResult = CDRF_SKIPDEFAULT;* r% }3 z9 Q3 x' F% M }

}

该用户从未签到

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

本版积分规则

关闭

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

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