下沙论坛

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

QQ登录

QQ登录

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

又是垃圾,CListCtrlEx Base CListCtrl

[复制链接]

该用户从未签到

跳转到指定楼层
1
发表于 2005-2-23 21:29:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
// ---------------------------------------------
  O: n+ B" v  _  @. {// ListCtrlEx.h
8 B2 b6 ]- E6 ]& W6 c1 A6 c  {0 {' W# o// ---------------------------------------------
9 _+ s' X6 J) U7 S" B  u#pragma once
9 u0 ~9 Y+ i8 f. B) j: c5 Q, N& d7 \- ?) B# m8 p1 [
// CListCtrlEx
/ T: d) A8 I* a; g, B  ]: C. Sclass CListCtrlEx : public CListCtrl# Z* q! s( C+ y  U
{
% E& G& g4 S( |' X9 m$ b    DECLARE_DYNAMIC(CListCtrlEx)
: r4 S8 o- n$ t4 \public:  n9 y7 k7 u6 q
    CListCtrlEx();. P$ g% G0 G& j
    virtual ~CListCtrlEx();
. m% n; W) d, `1 o$ _9 g. rprotected:' \# g# B6 h; {0 \
    DECLARE_MESSAGE_MAP(), P2 m4 `6 k7 j: K6 l4 V# W3 w
    CListCtrl* pThis;
8 Z: s3 J/ e* V- l  f  N; s- {    virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
+ y; c. l8 b4 Z+ X% sprivate:
+ v# Y# N) g( x3 b    struct ItemDataEx {* v& q0 R; f7 y  f+ x( J
        LPARAM SrclParam;; S3 r# f2 ^0 R
        COLORREF clrItemTextColor;
9 P+ }2 s" e! E7 u# z' c        COLORREF clrItemBackColor;1 }# G: \: A; F7 D% X9 x
    };
4 c" {: D' G% V" T2 o1 p9 ]( B    COLORREF m_clrHighLight;
5 e& s9 U) ~' w, t$ Z- e    COLORREF m_clrHighLightEdge;1 G2 B! J/ R6 ~: R
public:# H2 ?; m& t  h9 T/ M  Z
    // 新增方法6 G; h, K8 |  B% P+ D  }
    COLORREF SetItemTextColor(int nItem,COLORREF clrText);0 W( U( L4 g( R  X  O! ]! D+ ~8 W  K
    COLORREF GetItemTextColor(int nItem) const;8 ?7 ~* D/ m/ Q# c
    COLORREF SetItemBackColor(int nItem, COLORREF clrBack);
6 O8 n3 e1 l% ]2 ~$ P, _/ }: u" }    COLORREF GetItemBackColor(int nItem) const;5 J5 ]- Y; S. D& D! M/ B* Q5 E# p
    COLORREF SetHighLightColor(COLORREF clrHighLight);& c" y" l7 n' x
    COLORREF SetHighLightEdgeColor(COLORREF clrHighLightEdge);! p) f: [* N: T) ^/ F* N# _8 F7 E
    COLORREF GetHighLightColor(void) const;
! J/ }2 B& K- n! Q0 G  n+ Q; k    COLORREF GetHighLightEdgeColor(void) const;% v4 F; G: `' ^% R$ f& Q
    int GetItemImage(int nItem);$ x- y( ~- \9 C& O/ F# W5 w
    BOOL SwitchItem(int nItem1, int nItem2);
6 V6 s! _- e- ]- B    BOOL MoveItemUp(int nItem);! H  o8 l9 O0 m1 D) n
    BOOL MoveItemDown(int nItem);
: Z2 t6 ~! B! l$ S- n    // 重写原来的方法
! k, h; W7 i6 |- u& a4 X    DWORD_PTR GetItemData (int nItem) const;
& M0 R( D3 t6 y    BOOL SetItemData (int nItem, DWORD_PTR dwData);4 z4 C% i5 [* r0 P0 B
    int InsertItem (const LVITEM* pItem);
3 i$ ^# w* E5 E% ?9 q    int InsertItem (int nItem, LPCTSTR lpszItem);2 m/ ^. k) B/ v" F9 ]
    int InsertItem (int nItem, LPCTSTR lpszItem, int nImage);
0 Z7 N) Z' W3 u: v3 x, k' u    int InsertItem (UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam);9 a2 J6 A( o, P9 J) N3 @5 ^, }
    BOOL DeleteItem (int nItem);/ \& e9 x- P! c3 V+ \1 b: F, U
    BOOL DeleteAllItems ();' u* [0 o4 ~2 M' H! t% j
    BOOL GetItem(LVITEM* pItem);' x; ~  w/ d8 p# K$ X9 X$ ^
    afx_msg void OnDestroy();  q4 E2 g$ p, c( G8 A
    afx_msg void OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult);
) m7 }9 v9 ?; W' S, [};
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 顶 踩

该用户从未签到

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

// --------------------------------------------- + p# D' Z e+ O6 \$ g( H% {$ S& _// ListCtrlEx.cpp ]5 C7 V5 t" Q" a# { // --------------------------------------------- ?& H3 {- K. s) E$ o" O! }, P // ListCtrlEx.cpp : 实现文件 / d- i1 X; x9 T8 |//

#include "stdafx.h" & c) H$ K( D3 {2 L6 b9 i& I* w# i/ W( g#include "CreateNewClass.h"1 c0 U) V! U! }6 ~' f) i, l+ _ #include "ListCtrlEx.h"' E& e( b. k6 k' }1 C5 L #include ".\listctrlex.h"

/ F7 O0 k: Q8 m$ b* W: f) J // CListCtrlEx

IMPLEMENT_DYNAMIC(CListCtrlEx, CListCtrl) 6 d# i8 J8 `8 i( SCListCtrlEx::CListCtrlEx() ) j4 K4 D0 c6 H2 ~0 s{ ! b8 G( i& W' _# B4 ^ // 用于调用基本的方法 {+ G' a& e+ O# _3 A/ M Z pThis = (CListCtrl*)this;7 P3 ]6 y7 G7 m0 h- _; L // 默认的选中框颜色 6 D4 Y3 D) ?1 ? m_clrHighLight = 0xF1DACC; $ _( Z( I, b6 p$ O- J4 D2 v m_clrHighLightEdge = 0xC56A31;7 f9 v/ Y2 | B9 ~+ G }

CListCtrlEx::~CListCtrlEx() # K% J) Q, g( d$ \ j% ~{

}

, ^( i3 }# N4 y& C9 N9 E# T BEGIN_MESSAGE_MAP(CListCtrlEx, CListCtrl) " l9 i: U5 t1 t. Q/ U# A ON_WM_DESTROY() 5 S+ m. G5 n9 g* X/ m; A ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnNMCustomdraw)+ j( v. Z. t% g. V7 D% L END_MESSAGE_MAP()

// CListCtrlEx 消息处理程序

) V2 w0 i' u8 lCOLORREF CListCtrlEx::SetItemTextColor(int nItem,COLORREF clrText)3 g5 X- c7 Q. j7 F { $ F' N/ V [1 A: Q COLORREF clr = GetItemTextColor(nItem);* ~/ _; f! b# F1 P0 @- V* c ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem); 3 R$ K( t" [* Z$ Q( i pData->clrItemTextColor = clrText; 0 c. u0 @3 Z, v9 Z7 A4 I1 K( N% V return clr;7 j, p& \; X9 v* @0 [5 |8 C }

COLORREF CListCtrlEx::GetItemTextColor(int nItem) const 1 x$ A: r ]1 Z% S{ U9 `9 B) D+ T. Y0 m# _) Z! j2 e ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem); 6 o+ I9 @+ H% @( X& ^ return pData->clrItemTextColor;; @- T' Q$ F, k6 m7 r }

COLORREF CListCtrlEx::SetItemBackColor(int nItem, COLORREF clrBack)+ `: R, g8 t- U9 e; T3 Z9 ] { ' G% `1 q0 n+ b* T, F( _ COLORREF clr = GetItemBackColor(nItem); ) G: J: b* l5 K$ K% M0 f! w6 E5 C ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem); / C% J3 \( _* k pData->clrItemBackColor = clrBack; - U! h- a$ c+ s return clr;3 t" A+ K+ F5 r- c1 n: O+ I }

COLORREF CListCtrlEx::GetItemBackColor(int nItem) const- ^/ O N8 ~7 Q- a7 ` {3 O0 Y3 s6 W5 ?: u ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem); ) r8 q( c" {9 n3 w3 w3 V return pData->clrItemBackColor; # v/ \ ` N, y, Z! b+ X} ! x9 m+ d/ p* v9 dDWORD_PTR CListCtrlEx::GetItemData (int nItem) const: Z/ p" I t6 {% s {; e0 Y9 z7 i& j6 X if (nItem >= GetItemCount())5 t. G0 Q6 z* G. e { 2 U/ H5 c0 d7 y$ W/ j; O return 0; . `5 f- t' P {4 k# d } . c5 ^: L, a& T5 ?5 y- o* v7 Y+ n4 N CListCtrl* pThis = (CListCtrl*)this; % a" h: _0 b) X2 Q7 @. | DWORD_PTR Data = pThis->GetItemData(nItem);$ J2 q! s2 Y- @6 C. L8 H# ~ return ((ItemDataEx*)Data)->SrclParam;8 ` e1 o3 s! k8 k$ | } . P0 z" J+ s9 F# VBOOL CListCtrlEx::SetItemData (int nItem, DWORD_PTR dwData) E v3 Y6 ^# u4 Z { - e1 I+ D/ P. Q( b( n0 J! R if (nItem >= GetItemCount())( X8 m( K+ u* m8 e {/ ~* i9 Y. R9 Q# {6 j: K6 J return 0; % h; N ?* Y4 |8 m5 Z }6 O& W4 C0 E) g3 B0 F; j. Y CListCtrl* pThis = (CListCtrl*)this;9 s9 [5 A; V) X DWORD_PTR Data = pThis->GetItemData(nItem); " r$ v1 U0 h0 e" X2 J ASSERT(Data);: f7 g1 b2 o9 O+ { ((ItemDataEx*)Data)->SrclParam = dwData; 5 y$ Q5 W. Q/ j8 C4 _ return 1;5 c0 W& i. g; C m: s } 2 T2 ]1 T4 E. A" \1 a1 [int CListCtrlEx::InsertItem (const LVITEM* pItem)1 s0 E. S M; c! @" Q5 ` {5 w: Q( z8 _* m2 s8 u& Y int nReturn = pThis->InsertItem(pItem); ' A1 A4 C4 {$ y2 A( v/ w# ^3 N ItemDataEx *pData = new ItemDataEx;5 A3 U( N: D; |; y } // 设置默认颜色 + d1 b: F# }! d1 W$ k2 c2 ^ pData->clrItemBackColor = 0xFFFFFF;//pThis->GetTextBkColor(); / B1 r! f( \) H' w pData->clrItemTextColor = 0x000000;//pThis->GetTextColor();& C) D0 y: v+ C, a: J pData->SrclParam = pItem->lParam; , `% j' Z; k1 D1 b; j pThis->SetItemData(pItem->iItem,(DWORD_PTR)pData); ( ]& [( Y: j+ y) ]* a; P- K I return nReturn; / g9 l: M A% r: o3 i, V} & d9 |% N9 r' ~- z: l! g3 h+ rint CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem) 6 l7 [, [8 E4 [0 s. d! Y, {{ * C. d1 g+ A8 K LVITEM Item;, }/ V0 x; G& l$ H: U* S memset(&Item,0,sizeof(LVITEM));4 _& F7 I$ R8 B' F) ` [ S Item.pszText = new TCHAR[strlen(lpszItem)+1]; + X- f% }5 V* M strcpy(Item.pszText,lpszItem);6 P7 y3 Z5 E, D% ^ @; a Item.iItem = nItem;6 C7 M1 A5 m3 d3 Z& l Item.lParam = 0; 5 h( n0 `) o! U7 D, I3 r7 v Item.mask = LVIF_TEXT |LVIF_PARAM;' M) r; F4 C7 P$ l$ V2 P BOOL bReturn = InsertItem(&Item); 2 t; O; Z' h/ t# W delete Item.pszText;( D2 _7 P. i' K" h return bReturn;# e1 _ d! t. m. U } * b! z% F; t4 R6 |int CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem, int nImage) + E4 Q$ N T: d4 O* x{ D: I2 v6 P, f4 y- W5 U LVITEM Item;( N6 b3 g( ]" M! ]: F memset(&Item,0,sizeof(LVITEM));: _; [2 T# ?& k9 U @7 _' h% A Item.pszText = new TCHAR[strlen(lpszItem)+1]; " W0 G6 F' B) W# C strcpy(Item.pszText,lpszItem); / Y. f& e2 b9 ?4 ?6 _ Item.iItem = nItem; ; c7 h7 ^. k4 i {: K( |, u$ k Item.lParam = 0; 3 x3 ?* C: M" ]/ l Item.iImage = nImage;. A; H$ ^1 s Z2 L0 V) }! L Item.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE; % D* Q* y7 i K' E/ j6 W) g8 N BOOL bReturn = InsertItem(&Item); 1 a5 r% m4 ~+ C+ d& p delete Item.pszText; s$ Y0 e- H* u. W return bReturn;% a! l8 p% t* u" x; @6 I }9 [ _/ E! ~8 R ?' ?4 j* N4 {7 U4 o4 e int CListCtrlEx::InsertItem (UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam)( Q0 x0 x- L$ \% u/ F/ I }! y) a; I" n- P {- G. B* G8 g& B: g( a LVITEM Item;5 W/ `+ C& C5 m2 a memset(&Item,0,sizeof(LVITEM));3 o: I# v8 g6 k3 { C+ I Item.pszText = new TCHAR[strlen(lpszItem)+1]; 8 w4 j& f8 `; M strcpy(Item.pszText,lpszItem); 0 c- d# P+ |6 ^, a; E1 r! u" l Item.state = nState;3 x3 c1 w) X6 b% \ Item.stateMask = nStateMask; 6 z' t9 o8 @. v, ^ Item.iItem = nItem;# T' q6 ~; K& a: ~ v* B1 @5 R Item.lParam = lParam;; ^7 y* I1 {% G. J/ m8 E Item.iImage = nImage; 4 h7 k- K& u; l5 U; Q Item.mask = nMask;! r" X' n1 \& B7 f9 B V6 Z BOOL bReturn = InsertItem(&Item); , x& a' {/ m1 |! r8 h8 f delete Item.pszText;7 I: l8 E! [% C# @% o( c' F return bReturn; # f9 Y" I- w( Y) e} 8 V/ a( N6 I2 a& m$ Z: e* P/ B2 bBOOL CListCtrlEx:eleteItem (int nItem) & Q1 t( o( c0 J( D/ p# o% ^{ 2 V* J- R1 }# H( G& n( g void* pDataEx = (void*)pThis->GetItemData(nItem);5 C' T3 N$ {" ^* h if(pDataEx != NULL)* _4 }& {* P& C4 q$ Y7 a {2 t, O, P2 _" W delete pDataEx;/ f8 }) A+ q3 G1 {" w }% d' L l- U6 @* g: s1 V2 ^ return pThis->DeleteItem(nItem);8 H; l, ~( v9 n3 Q% d# s }5 w$ B( x& i4 b- x& E% z0 r2 ? BOOL CListCtrlEx:eleteAllItems () 8 U. C& y" `0 M3 _( D{( k$ @* p, [( p1 n9 _, N4 q int nCount = GetItemCount(); 8 k7 [! }: t# Y4 y( w5 D for (int i = 0;i<nCount;i++)0 t- O2 s# O" M* x {# |! a0 Z4 U, e- a$ } if(!DeleteItem(0))9 ~6 U4 v3 y* k3 @+ c {5 p5 b3 B1 D- e+ }2 x return 0;1 K* R: C* f; g) O) i } 4 f$ U S. e0 v7 T }; r1 l) T W! j) k0 a. P2 s return 1;1 n' b7 c4 S$ {) n) _: V' Y5 Y }# S: L; f. L7 H0 P1 A: }' Z BOOL CListCtrlEx::GetItem(LVITEM* pItem)( n- a0 s' A e0 w& e, z { 7 R; n) V) C6 W% H8 g BOOL bReturn = pThis->GetItem(pItem); / `+ M' k( Y+ X+ o8 v& J if (pItem->lParam)" k( v$ ` B2 r; V { 7 ~! b7 j; f$ N* I+ r pItem->lParam = ((ItemDataEx*)pItem->lParam)->SrclParam;9 R1 B. H0 g. H: M8 j }

return bReturn; / C3 D4 a; J$ q7 f% ^" N} & K. S$ v- v/ c7 uvoid CListCtrlEx::OnDestroy()) v( e4 n7 `& ~6 N3 h& N$ B { / l8 |7 y0 V2 N" X8 o8 I // 关键是清除申请的内存空间* A8 g. {" c. u% ?8 v/ M' R& u DeleteAllItems(); ^& B0 [& R4 P P1 a CListCtrl::OnDestroy();/ V8 h0 Q* R- ^/ Q) p }

BOOL CListCtrlEx::SwitchItem(int nItem1, int nItem2)) ?5 ]6 d7 Y$ T' P! P {7 j% k& K }, L& j if (nItem1 == nItem2 || nItem1 >= GetItemCount() || nItem2 >= GetItemCount() || nItem1 < 0 || nItem2 < 0)& P6 S1 I1 T+ P2 T {; G" i5 ?( D: m' x- ?3 Y, X% M return 0;7 l/ Y j) Y; w }& I) ?' P1 t3 a& M1 O( I LVITEM ItemData1,ItemData2; " ~( E6 m' k5 X* T1 b9 Q9 t int ItemLength1 = pThis->GetItemText(nItem1,0).GetLength();2 R( E, A3 B7 L/ X' z1 y5 u2 C int ItemLength2 = pThis->GetItemText(nItem2,0).GetLength();7 k Q; O5 ` p# Q! \! L- u ItemData1.pszText = new TCHAR[ItemLength1+1];/ o3 _1 k6 N+ z% u2 P8 @ ItemData2.pszText = new TCHAR[ItemLength2+1];( Q; b, o3 q ]9 _ ItemData1.cchTextMax = ItemLength1+1;% [. j$ o* i3 {3 n. G9 ~ ItemData2.cchTextMax = ItemLength2+1;% Y4 X" c( d7 b" I# S ItemData1.iItem = nItem1; 0 L1 |' I* W4 \; e+ U ItemData2.iItem = nItem2;9 ]& L- A2 ^+ V$ g0 z, j; V+ u ItemData1.iSubItem = ItemData2.iSubItem = 0; 4 k7 x/ f* x# O4 F# Z ItemData1.mask = ItemData2.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE|LVIF_STATE;

pThis->GetItem(&ItemData1);# N `! v3 k) ^ pThis->GetItem(&ItemData2); ! j) U& M4 i0 I: i& L) ~7 ?6 o // 交换数据% O, M5 C/ i3 }* P* D* Q2 ~8 p) q ItemData1.iItem = nItem2;/ r4 O/ f& }3 x7 E& W- I C( Z ItemData2.iItem = nItem1;2 E+ j) W$ R9 W X pThis->SetItem(&ItemData1); & b! J6 K/ A0 S* w' f1 N' E pThis->SetItem(&ItemData2);

delete ItemData1.pszText; ( @5 J8 s+ m1 K9 D( {; U: ^' T: h delete ItemData2.pszText; : K5 l5 P* {$ D6 s6 k K1 n // 得到报表模式的列数" l l; N: m1 d4 m2 C int nColCount = pThis->GetHeaderCtrl()->GetItemCount(); / u e" R. L5 p CString strBuffer; " |* X( h$ {' P for (int iSubItem = 1;iSubItem < nColCount;iSubItem++) / w4 c0 x1 a9 ?2 c2 U { b& }# t1 l3 V! M* P5 o) v strBuffer = pThis->GetItemText(nItem1,iSubItem);) w/ z7 H' N3 R. E6 W pThis->SetItemText(nItem1,iSubItem,pThis->GetItemText(nItem2,iSubItem)); ; X5 E, \ Z# V$ ]2 H pThis->SetItemText(nItem2,iSubItem,strBuffer); 3 L' M% |# i; }7 Z1 N7 d } ' |) Y# D, @+ W( _ // 把原来的选中状态也转移过去, Y2 {: D$ F" x3 s$ | // 不知道为什么这个属性没有体现在LVITEM结构中…… ( |5 X: J/ s x bool bItemSelected1 = GetItemState(nItem1,LVIS_SELECTED) == LVIS_SELECTED; 5 `# M: ~$ }1 ~ bool bItemSelected2 = GetItemState(nItem2,LVIS_SELECTED) == LVIS_SELECTED;

SetItemState(nItem1,bItemSelected2?LVIS_SELECTED:0,LVIS_SELECTED); 2 n4 ]8 f0 M* ] SetItemState(nItem2,bItemSelected1?LVIS_SELECTED:0,LVIS_SELECTED);7 i; J5 L0 z3 O- [ return 1;7 x( W$ `* y" L; N l, |1 a% r/ w }

BOOL CListCtrlEx:reCreateWindow(CREATESTRUCT& cs)& P+ ?- e" V: E {: @0 [: M9 h. @. `0 _0 Z // 本类报表模式专用 c0 m6 C, h: @ o% J cs.style |= LVS_REPORT ; 5 Z) {6 k+ n' b1 {* D3 } // cs.dwExStyle |= LVS_EX_FULLROWSELECT; 9 l3 ~' O- c1 E# H T return CListCtrl:reCreateWindow(cs); & y7 r8 {1 u, R9 ^: T. C}

0 g- s; H7 r# F" Z7 ~5 \) L COLORREF CListCtrlEx::SetHighLightColor(COLORREF clrHighLight)0 P: I `3 k4 u& r" @ {: r; h4 U1 F4 S* ~8 O, h COLORREF clr = GetHighLightColor();6 d& Y+ e) s- s m_clrHighLight = clrHighLight; $ ~/ S6 t+ s9 O( w return clr;6 ~& t$ N% r0 `. Y' J }

COLORREF CListCtrlEx::SetHighLightEdgeColor(COLORREF clrHighLightEdge) 0 d# F" ?) K3 T, m{ . ]! ` {# r) o1 I( A5 l4 u COLORREF clr = GetHighLightEdgeColor(); ( }# v/ J& T3 l4 M5 T. g# E- t m_clrHighLightEdge = clrHighLightEdge; a) D: n( {' x return clr;. N& U: n( o# n; | }

该用户从未签到

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

COLORREF CListCtrlEx::GetHighLightColor(void) const& M2 Y' S3 I. o* G, P' T- H {) f4 H7 p- U! R5 ~ return m_clrHighLight;* o( H, y- {; K }

COLORREF CListCtrlEx::GetHighLightEdgeColor(void) const # t& N# w; t4 B D{' ~+ M$ f& {9 O* F1 s return m_clrHighLightEdge; 5 V1 s* q, C- l}

int CListCtrlEx::GetItemImage(int nItem) : |( C6 g7 c+ {, G1 H# @{" ?3 h( p4 ]! M7 r2 E% q% i LVITEM ItemData;0 g8 W% D/ n: N$ {: l; K ItemData.iItem = nItem;1 x; l F6 g, Z! y. k1 @ ItemData.iSubItem = 0; ! v9 N* N& j/ Z7 r4 b. o2 }' z ItemData.mask = LVIF_IMAGE;: m' Z9 p( k# p, Y- M# o( w pThis->GetItem(&ItemData); ' T% W U9 E7 u" S0 \ return ItemData.iImage;0 y- i/ N# M" b! F+ [6 C1 x }

BOOL CListCtrlEx::MoveItemUp(int nItem)1 s+ R8 U1 f. o( U) l4 ]2 \) K+ t {8 v# s& `9 z& x if (nItem < 1 || nItem >= GetItemCount()) + o0 D0 \1 w9 p: `: ] {1 }7 g* c' U- u$ w( T" ~" r8 b return 0;1 g! l1 ^* o2 U/ z } 7 I' }) j5 g! Z \7 o( z# q return SwitchItem(nItem,nItem - 1); X/ o5 Y% S, N/ o0 \1 N}

BOOL CListCtrlEx::MoveItemDown(int nItem) 0 f+ h& }) i5 b$ a0 f# c{ " S3 X5 T+ O& f& H U* \ if (nItem < 0 || nItem >= GetItemCount() - 1) " M/ m# s3 m/ ~$ \ {8 |6 L2 w3 F# |1 Z. \ return 0; 4 ]& t, N! l7 B7 b. r7 h& Q } " l+ X, k. T5 r* S1 y7 Q return SwitchItem(nItem,nItem + 1);( S. U6 M: i ~& g& h9 ] }

void CListCtrlEx::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult) 7 R9 w2 U8 [- G5 |{% ?% I. K$ e: V! \, u NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR);

*pResult = CDRF_DODEFAULT;

if (pLVCD->nmcd.dwDrawStage == CDDS_PREPAINT), o# F! o7 c0 B- V: t: ~ {/ G+ \7 C4 U/ ]. E, L *pResult = CDRF_NOTIFYITEMDRAW;) f+ M/ W8 n- V J8 @$ y& r }( h0 V* n$ B, j* A* X9 F6 K else if (pLVCD->nmcd.dwDrawStage == CDDS_ITEMPREPAINT)9 L9 f/ N8 m1 P$ X/ ]" l3 C9 X {, W& g& k' R2 Z6 b4 t2 q *pResult = CDRF_NOTIFYSUBITEMDRAW; 0 [; g( [$ ~- a+ g) s. Q: N- u } 1 L1 I, ^& D. a2 `9 ] else if (pLVCD->nmcd.dwDrawStage == (CDDS_ITEMPREPAINT | CDDS_SUBITEM)) % d7 s4 c- H/ J1 | {

int nItem = static_cast<int> (pLVCD->nmcd.dwItemSpec); 5 S/ r- X: R% D3 t, y7 I int nSubItem = pLVCD->iSubItem; ' x: ?) N$ B8 `+ j6 Y if (nSubItem != 0) 5 p5 z- ~2 P; V2 E- z1 ` { : e: ?2 _2 A; u7 n1 U. H# _ // 全部绘制过程在nSubItem == 0 时完成 7 P! d; |( z4 a. z T1 @) X- m // 分布完成会有小问题,我无法解决 9 G: _/ | ]3 Z- ^" E% V( r *pResult = CDRF_SKIPDEFAULT; - C6 a N' d1 O, c( a return; 5 e7 z- {( w" t: d- q8 P, ~ }$ k. j. N3 V; t% [6 i. v2 ] CDC *pDC = CDC::FromHandle(pLVCD->nmcd.hdc);: a! E0 ~! u9 J$ X int nColCount = pThis->GetHeaderCtrl()->GetItemCount();" @+ v' y W3 V. }2 t& Z5 P; N" L CRect rectSubItem;0 m4 I1 {( T7 z CString strItemText;8 }' K) A8 M- r pDC->SetTextColor(((ItemDataEx*)pThis->GetItemData(nItem))->clrItemTextColor);! ?8 U+ _2 t P pDC->SetBkMode(TRANSPARENT); # c( z3 x) h& x6 f5 M- a- G // 相当于橡皮) X, c* S W+ s CBrush brEraser(GetItemBackColor(nItem));

// int nFirstWidth = GetColumnWidth(0);

0 v/ y; N4 @, P t1 d" s( [5 Q/ S for (int i = 0;i < nColCount;i++)- O9 ^# Y, S- n+ Y6 ]# m* r5 ~ { ( s# C3 ]7 {3 B# O strItemText = GetItemText(nItem,i);

if (i == 0)- |% [4 l3 a7 s$ _, R {# R. B) H3 n* s pThis->GetSubItemRect(nItem,i,LVIR_BOUNDS,rectSubItem);+ N/ f) B; z* k% M3 [$ Z( i/ t // 擦掉原来的图形 4 c4 l. x! v# V/ W8 ]* c pDC->FillRect(rectSubItem,&brEraser);8 ^* t( G& x+ V2 m* ^ if (GetExtendedStyle() & LVS_EX_CHECKBOXES)! `3 \" F, V$ I, A* t { 5 a3 q5 i/ X* W) L7 x$ d CRect rectCheckBox; 7 d; f3 s7 v' w! T1 i7 m# y GetSubItemRect(nItem,0,LVIR_ICON,rectCheckBox); ( |. C- j5 ? I1 W5 s' }2 S CRect::SwapLeftRight(rectCheckBox);9 `2 q/ Q A# W1 u9 _( a! d4 @5 M rectCheckBox.left = rectCheckBox.right - rectCheckBox.Height();

rectCheckBox -= CRect(2,2,2,2);4 u- a/ j0 X0 m9 q pDC->Draw3dRect(rectCheckBox,0x81521D,0x81521D); 5 B1 W {& |, b //rectCheckBox += CRect(3,3,3,3);& \4 {0 H% K. G q( X if (GetCheck(nItem))& p8 m$ m0 n4 ?3 x. I { # V! v4 w- x& q1 T* r8 R2 k1 H // 绘制一个CHECKBOX % U6 z6 L/ N" y7 m% M9 g7 v% v CPen penCheck(PS_SOLID, 1, 0x0021A221);. U/ I. m: b m: i- q, w* m CPen *pOldpen = pDC->SelectObject(&penCheck); {; o u. [1 `+ N- m" y for (int j = 3; j < 10; j++)0 z6 y0 f6 r* l1 B" S { - ^% |4 G# t9 U& Y8 Y7 x9 @ pDC->MoveTo(rectCheckBox.left + j, rectCheckBox.top + rectCheckBox.Height() /2 - 3 + ((j < 6) ? j - 1 : (9 - j)));) N4 q% [% l# z* f5 | pDC->LineTo(rectCheckBox.left + j, rectCheckBox.top + rectCheckBox.Height() /2 - 3 + ((j < 6) ? j + 2 : (12 - j))); " L% N: J$ l7 v& q# N } # x( t1 E" N- |; s) E' s/ r 1 j9 s2 b2 [, {0 g# r6 { pDC->SelectObject(pOldpen); 9 `7 D% N$ [7 n M } ' ?: C5 {2 P* Y! b: o( U) J2 O' s }$ x) X8 y+ F+ F* c. j // 根据需要画选中框9 E% Z6 q3 n2 f% V+ K- G5 W4 d if ( (GetFocus() == this || GetStyle() & LVS_SHOWSELALWAYS)&&(GetItemState(nItem,LVIS_SELECTED) == LVIS_SELECTED)) 6 L! g, ?% a& o o1 k( ^; p6 X { . v Z1 E* x8 `' S1 K- `; v) I if (GetExtendedStyle() & LVS_EX_CHECKBOXES)8 n& p* d" P+ a0 Z2 w8 z {// 存在检查框风格的时候需要对左坐标进行修正 * L$ \1 D. E9 O" Z; c CRect rectLeft; / _' e$ A% x9 ]3 y7 Q GetItemRect(nItem,rectLeft,LVIR_ICON); * }) {& e$ V7 w; U! a- F rectSubItem.left = rectLeft.left; q% a" G& E4 p } 1 ^0 [% I2 z# f* Z# T8 p if (!(GetExtendedStyle() & LVS_EX_FULLROWSELECT)) % v" E9 H: R! ]3 F! p* p( X$ N {// 非全行选择时右坐标需要修正# I; X. A$ A3 P0 D2 X T2 p3 j! f CRect rectRight;3 N$ T9 U/ w0 J; B$ f7 q GetSubItemRect(nItem,nSubItem,LVIR_LABEL,rectRight); ' }7 V% V3 \2 U4 n. ^2 n rectSubItem.right = rectRight.right; % ?& p' L& ]! o3 N. F } " ]" E$ R* a" J m CBrush brHighLight; 2 Q/ P, i- I0 w2 k' _4 o; f CPen penHighLightEdge;& \" z; H5 j7 a% Y- B if (GetFocus() == this)1 p. K$ F+ M9 e' Y, t { 5 v9 t1 V! w% b( d( l brHighLight.CreateSolidBrush(m_clrHighLight);4 k0 w9 C$ }! V {4 R penHighLightEdge.CreatePen(PS_SOLID,1,m_clrHighLightEdge);( j4 c1 }/ b$ @% K" t2 |! {- V0 v }0 r" @( T/ X+ r else 1 r( Q% ~2 V. y3 Y4 n5 W+ r! @ { 9 P2 r. F7 K% w/ b' c3 l // 不是焦点时,颜色70%不透明度 7 x: r4 U, l' |0 E# [3 a COLORREF clrBack = GetItemBackColor(nItem); 1 f U5 ]! x5 e+ N, M( c0 j WORD red = GetRValue(m_clrHighLight)*0.7 + GetRValue(clrBack)*0.3; / y1 W( \ a% x, u7 H8 m WORD green = GetGValue(m_clrHighLight)*0.7 + GetGValue(clrBack)*0.3; . u- p3 g& e$ m L WORD blue = GetBValue(m_clrHighLight)*0.7 + GetBValue(clrBack)*0.3;% R: c* d j3 g6 B brHighLight.CreateSolidBrush(RGB(red,green,blue)); & O6 T9 f$ v) F& p% b' k red = GetRValue(m_clrHighLightEdge)*0.7 + GetRValue(clrBack)*0.3; 1 ~# n5 N9 _1 D7 K/ z0 D9 k% x) [ green = GetGValue(m_clrHighLightEdge)*0.7 + GetGValue(clrBack)*0.3;, I+ s; q" P' @- B6 A blue = GetBValue(m_clrHighLightEdge)*0.7 + GetBValue(clrBack)*0.3;- }- k* o$ g1 k) _% v penHighLightEdge.CreatePen(PS_SOLID,1,RGB(red,green,blue)); 9 n* O9 t t* J" }8 n: e9 w6 P9 Z } ( | U) ?( ] l: K. ] CBrush *pbrOld = pDC->SelectObject(&brHighLight); r+ M {0 \; s) A3 r% V) X CPen *ppenHighLightEdge = pDC->SelectObject(&penHighLightEdge); / z0 L$ `2 n- C' V0 K- h/ j pDC->Rectangle(rectSubItem); ' f8 f& _" ]: t" O& N pDC->SelectObject(ppenHighLightEdge);" v) c& d" D( `- B( j pDC->SelectObject(pbrOld);, V1 T+ _( Q' Y/ T! {1 x0 D; f } 8 ?: d4 n% P2 E8 X4 q }/ S: r+ D2 Y3 ?* K pThis->GetSubItemRect(nItem,i,LVIR_LABEL,rectSubItem);: C& u' h# x1 l' `( x3 n/ U UINT uFormat = DT_SINGLELINE|DT_VCENTER|DT_END_ELLIPSIS;% o8 S3 g- G* ?# [. v O9 E" k4 L HDITEM hdi; 5 U! R$ K' ?+ Z+ T! }. ^ hdi.mask = HDI_FORMAT; . d- I" \/ T2 J7 V* H pThis->GetHeaderCtrl()->GetItem(i,&hdi);5 I# D8 U1 a& b0 H. Z* [% \ if (hdi.fmt & LVCFMT_RIGHT) 8 p! A& s; A8 @, @! P+ y { 7 |( I1 V" @7 S: a- u7 D6 D uFormat |= DT_RIGHT;8 w0 f M2 b& P+ F } ) u5 M2 z4 n3 |! x: z' { P else if (hdi.fmt & LVCFMT_CENTER)! ~8 d ]0 `# D3 R: T' H {5 b% I3 \2 M; B1 |$ r uFormat |= DT_CENTER;# T# @1 J$ ]# l' [8 r5 {+ ~, P }& o0 ]+ c1 Y4 A8 H pDC->DrawText(strItemText,rectSubItem,uFormat);

CImageList *pImageList = pThis->GetImageList(LVSIL_SMALL); 8 j& ] L+ A7 W" p; E# K1 H8 Y H. D! I if (pImageList)' ~ |) ]5 F" V { ; _( n! @" N7 D' K+ v" m0 X CRect rectIcon; 7 J% W8 G6 ?7 A( Y) K# q, | GetItemRect(nItem,rectIcon,LVIR_ICON);; }& E3 f# Y% Q) X7 E pDC->DrawIcon(rectIcon.left,rectIcon.top,pImageList->ExtractIcon(GetItemImage(nItem))); ! ~* x8 ?. V+ j0 R) d4 A" Q }: d( M/ F$ w! U8 m: `$ x0 @ } ! _: i2 i5 T, w* ?+ D) O9 q *pResult = CDRF_SKIPDEFAULT; B6 ~% U/ {+ R4 ] }

}

该用户从未签到

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

本版积分规则

关闭

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

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