下沙论坛

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

QQ登录

QQ登录

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

又是垃圾,CListCtrlEx Base CListCtrl

[复制链接]

该用户从未签到

跳转到指定楼层
1
发表于 2005-2-23 21:29:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
// ---------------------------------------------
8 i. I& z9 P% w5 [" W// ListCtrlEx.h% m6 ?6 K2 L9 M
// ---------------------------------------------8 Z8 y! L  P& M6 O! [7 h9 K- k5 g
#pragma once
( `* v8 W# h$ `( `& a; Q% E4 c; F8 v8 {: B5 ^( A2 N- ~. L. f$ v
// CListCtrlEx
- m3 }* h0 \1 Y: Q: \) Nclass CListCtrlEx : public CListCtrl
: Y0 @6 n; u" d( C{1 I+ L- O- Z  X: E& ?9 C- r* t5 F
    DECLARE_DYNAMIC(CListCtrlEx). z* g1 g2 L+ C& X, o$ H) ]1 @6 {* s1 h
public:
6 D: z: ^( ?9 o- |5 ^- K% }    CListCtrlEx();
: S' c4 |9 f9 p( x4 n) X- J    virtual ~CListCtrlEx();0 ?6 }8 ?7 a' @: o  j! @
protected:5 S5 l: j; k& [* F# _: E
    DECLARE_MESSAGE_MAP()7 y+ U1 m: g6 v
    CListCtrl* pThis;
* K  a1 U9 u- x; h$ q    virtual BOOL PreCreateWindow(CREATESTRUCT& cs);! [8 h0 e% S/ S2 R
private:
, N. L$ J( H0 B' N9 c; M    struct ItemDataEx {, Q+ j, P4 }1 Y7 t
        LPARAM SrclParam;
& b6 {  m4 @2 |1 f" L* ]        COLORREF clrItemTextColor;
# E* q& `" R8 _. o% o* M        COLORREF clrItemBackColor;: l- \, E/ n. Z* S5 J' i
    };' ?# _! \% C  `% K" S+ F4 s3 @* w
    COLORREF m_clrHighLight;
- a* [  `- G6 R9 _% O    COLORREF m_clrHighLightEdge;+ B3 R) C) M! E+ T3 d6 }
public:
* n  h/ A) G$ o  u    // 新增方法3 u# L  U* Z4 W) @/ j
    COLORREF SetItemTextColor(int nItem,COLORREF clrText);
) q; u. e" h8 G3 S    COLORREF GetItemTextColor(int nItem) const;
% T( k2 t& h( h8 {$ f% R: I    COLORREF SetItemBackColor(int nItem, COLORREF clrBack);% X5 S3 t; j7 Z- p0 n
    COLORREF GetItemBackColor(int nItem) const;% R, P3 w% a' ?; e- R. ]& M
    COLORREF SetHighLightColor(COLORREF clrHighLight);/ G- S) _0 [4 S
    COLORREF SetHighLightEdgeColor(COLORREF clrHighLightEdge);
" ?5 Y+ _2 S! I6 R0 x7 L4 |    COLORREF GetHighLightColor(void) const;7 F: e( n) R& v, q
    COLORREF GetHighLightEdgeColor(void) const;, v( [4 }: b% O- ?" Q3 S
    int GetItemImage(int nItem);# ~3 c' C! j& {4 u. A0 ^  i
    BOOL SwitchItem(int nItem1, int nItem2);
2 B" g$ S$ D- b/ ]5 z0 f    BOOL MoveItemUp(int nItem);
8 o& i& h* v- W0 X& I* y4 T: u    BOOL MoveItemDown(int nItem);
7 w- ?+ L- v; q" H* w" t& n    // 重写原来的方法
4 Z& w4 [- P+ {8 v0 O5 P. I    DWORD_PTR GetItemData (int nItem) const;
1 H! M+ Q( e# @; a    BOOL SetItemData (int nItem, DWORD_PTR dwData);* s+ l( q* W- [
    int InsertItem (const LVITEM* pItem);  V+ |& x8 Y  V: z
    int InsertItem (int nItem, LPCTSTR lpszItem);
# k  t9 I4 a9 J3 e4 q+ E    int InsertItem (int nItem, LPCTSTR lpszItem, int nImage);3 \& t; ?! T" l  J) @4 @% B& p
    int InsertItem (UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam);* f- E$ E: M! p0 ?
    BOOL DeleteItem (int nItem);
# P  R8 {4 t# P, |. W- l    BOOL DeleteAllItems ();7 ?) w, I: }" _) L
    BOOL GetItem(LVITEM* pItem);0 f% d7 E3 i, S3 a8 a" _3 ~
    afx_msg void OnDestroy();- i: m0 |! ]( Z$ |( }2 X
    afx_msg void OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult);/ ]# m; P  w% p' y/ F# {- X8 U, ~- _( ]
};
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 顶 踩

该用户从未签到

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

// --------------------------------------------- 7 Z6 C% h0 T) i// ListCtrlEx.cpp8 S: D) B+ g% ~6 W2 s1 F t // --------------------------------------------- C1 s# V# U+ g5 ~! d// ListCtrlEx.cpp : 实现文件 6 G7 E& Q" g% @5 M//

#include "stdafx.h" ; L9 m* T( M/ ?( \1 H c#include "CreateNewClass.h" - ^1 {% \5 t" x: I#include "ListCtrlEx.h"% c& K- S+ ~- S" S; { z #include ".\listctrlex.h"

) I% e4 |; A w; P% W# L // CListCtrlEx

IMPLEMENT_DYNAMIC(CListCtrlEx, CListCtrl) % U7 M% Q3 H4 Y# X1 t2 MCListCtrlEx::CListCtrlEx() 3 v5 u& \' q$ g, n{ ) R/ K& R- i: {6 y" | // 用于调用基本的方法 9 L3 q H, X' C, K$ A+ M pThis = (CListCtrl*)this;! e, g9 W( H/ Y! |6 U // 默认的选中框颜色 ( |0 w5 f) _! t7 ]7 O5 j& m& k7 B m_clrHighLight = 0xF1DACC; " S2 E3 @5 v& x* z m_clrHighLightEdge = 0xC56A31;/ ~+ r {: X9 Z& X4 Z }

CListCtrlEx::~CListCtrlEx() 5 g# f; s y y9 O0 s [9 q: W{

}

$ u$ d; Y1 O, E& j6 n; S7 QBEGIN_MESSAGE_MAP(CListCtrlEx, CListCtrl), ]! P# ~6 ?9 g- B9 i, o( I ON_WM_DESTROY() ) x& o; j: m/ [ ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnNMCustomdraw)- b: w' t: B7 d+ k+ e8 Q END_MESSAGE_MAP()

// CListCtrlEx 消息处理程序

% w4 S, J+ Q L6 b$ O* F) fCOLORREF CListCtrlEx::SetItemTextColor(int nItem,COLORREF clrText)4 |5 r$ _! J F0 Q) m9 M& x { & V) W' \( O& S: K3 c' J$ Y- R COLORREF clr = GetItemTextColor(nItem);! |" v* [' _. s) o# m9 v0 H ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);! @7 R% H( d% u3 H pData->clrItemTextColor = clrText; 2 c+ [4 S: ?2 d0 e return clr;, ]9 K- @0 j2 m: I" ^4 m9 X6 H, ]: [ }

COLORREF CListCtrlEx::GetItemTextColor(int nItem) const 9 j3 X# k1 s9 f) `{2 r* u8 ^. R) c ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);8 y2 A6 J5 u v8 v% e8 K7 L return pData->clrItemTextColor; # `; j. z# J% n}

COLORREF CListCtrlEx::SetItemBackColor(int nItem, COLORREF clrBack)# P" G5 P* z/ }0 M9 m! ^+ o { l6 T6 r0 [9 q COLORREF clr = GetItemBackColor(nItem); # p& {5 y, \, |! m5 l- W( j+ d. a ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem); ( g. i3 r0 G3 P9 B- { pData->clrItemBackColor = clrBack; 9 s: _1 B2 Y# H4 x) J# U return clr; N, E* A& m" M7 \* i/ f0 n( r}

COLORREF CListCtrlEx::GetItemBackColor(int nItem) const: ]5 g2 p& k( Y {/ R- C" u G+ e4 x; G6 s& k( V ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);8 R/ v$ u/ H l: P( ~& V) N return pData->clrItemBackColor;7 `9 {6 F, N: S+ k } " _7 a! i B6 gDWORD_PTR CListCtrlEx::GetItemData (int nItem) const $ ~" [5 U1 Z/ [. s{+ X0 A3 E; a) b" J2 Q+ I if (nItem >= GetItemCount())! B* f, D9 M3 p3 Q& x0 [4 G! x { " S9 C3 w, l$ i2 U2 P return 0; $ h( p( d; y" `5 u } : `* Q4 f& Q" Y; s1 N# U$ b CListCtrl* pThis = (CListCtrl*)this; : p) q& o; K; U3 p( U _ DWORD_PTR Data = pThis->GetItemData(nItem); 9 e$ x' \; _3 A8 ]6 u1 A return ((ItemDataEx*)Data)->SrclParam; 3 B' ~! Z) R5 W* R" T} , i: y6 ^& D2 w9 H0 T, o) T/ k. jBOOL CListCtrlEx::SetItemData (int nItem, DWORD_PTR dwData)' _* e8 U% W( Y. c7 m$ g% D { 7 Y7 o9 R' K" B if (nItem >= GetItemCount()) ^! u( _. Y( @5 A { 4 I( T4 t3 V- _( [( U return 0; - U: ]) e* @1 R. ?* C } , I0 d: {3 g# g7 f/ |; E+ l CListCtrl* pThis = (CListCtrl*)this;, S3 N r: ~$ F5 A$ H7 c DWORD_PTR Data = pThis->GetItemData(nItem);( {5 ?) ~! O$ ?+ q& v f ASSERT(Data);, k$ }* ]% _/ E$ H: e ((ItemDataEx*)Data)->SrclParam = dwData; # w+ ~. v4 g; b) g, B6 Y$ I3 J2 T return 1;4 w: B1 b, M/ S! m! |" O6 z }( _) V% {8 `9 S int CListCtrlEx::InsertItem (const LVITEM* pItem)8 D3 c% z7 g; z( B S { ' D. v5 X& w" [/ ^" ?3 U4 C int nReturn = pThis->InsertItem(pItem); 4 T) H" u: W5 R8 Z& C. z ItemDataEx *pData = new ItemDataEx; 0 O) _) R$ C9 s8 @ // 设置默认颜色4 R, Q: ?$ d' s c0 J pData->clrItemBackColor = 0xFFFFFF;//pThis->GetTextBkColor();+ |3 e% T3 I( b* I C o* j# y, U; K% L pData->clrItemTextColor = 0x000000;//pThis->GetTextColor(); 7 \& f K, P) c% ]: g pData->SrclParam = pItem->lParam; 0 J" b5 l. w2 J0 d" ^/ B: V pThis->SetItemData(pItem->iItem,(DWORD_PTR)pData); - u7 ], |# r7 Q$ @6 A return nReturn;' Z- K# e' c" {/ D* G7 B }2 ~. j2 f, z& N0 M int CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem) 1 {- B1 ~. B( T' C{( K, P5 f y8 }- S! { LVITEM Item;& D" G' V! q# z2 Y6 | memset(&Item,0,sizeof(LVITEM));: \8 @4 Z& `2 J$ V" ~! g Item.pszText = new TCHAR[strlen(lpszItem)+1];* d- K% k% z6 B' W strcpy(Item.pszText,lpszItem); 6 k4 K2 `( W( |8 ^5 m O Item.iItem = nItem;4 J) ]$ Q% \* h9 ]. G, ~ Item.lParam = 0;7 f! Q! H6 ]& V' s0 h& C Item.mask = LVIF_TEXT |LVIF_PARAM;) i' d% L/ y( A# k BOOL bReturn = InsertItem(&Item); $ j' Z) Y# g h2 S2 E$ s delete Item.pszText; & g h5 s7 D8 Z return bReturn;0 S* F2 \ l8 l+ K } * p; N. \5 d# N# G" P3 kint CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem, int nImage) D) w+ ?6 r2 x5 C { / @5 a0 n4 g L4 b. L LVITEM Item;. q1 L, B! K. `3 Q4 O | memset(&Item,0,sizeof(LVITEM));2 O* r1 Z8 Y9 ~* G/ w1 X Item.pszText = new TCHAR[strlen(lpszItem)+1];4 L; }& u% g8 j4 q7 s1 g- T strcpy(Item.pszText,lpszItem);: J6 e/ R4 I8 J: D4 T Item.iItem = nItem;4 Q, X2 f8 V) E: U( M) `; ` Item.lParam = 0;( Z' h" ^9 t' H9 S J Item.iImage = nImage;5 t6 a+ s* t5 q3 n Item.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE; * T7 {! v& \- s5 r1 P6 r BOOL bReturn = InsertItem(&Item); & k9 I: l# S) r, V, x/ r# C delete Item.pszText; p3 W' e; o* p2 v) |! j! X return bReturn; " q; o T( D# q$ Z7 h1 j5 h8 Y}$ ~* t0 F" b) w int CListCtrlEx::InsertItem (UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam) 2 T, f6 x S$ V3 U: m0 x{ 8 _' X7 d- p7 k* B+ z+ f F b# W LVITEM Item; * T& P+ p2 x$ g memset(&Item,0,sizeof(LVITEM)); 7 y( q% j- z) T+ }: j Item.pszText = new TCHAR[strlen(lpszItem)+1]; P' A* g: H" \3 U: V9 h* n: l: x* ]9 y strcpy(Item.pszText,lpszItem); 6 g$ f7 S3 P; } Item.state = nState; ) F+ P; P7 H+ @6 A( Q+ h Item.stateMask = nStateMask;+ C) i# {' J' c8 l7 O Item.iItem = nItem; 1 I% @0 J& p O' Y/ e Item.lParam = lParam; , m: h' ^. V' f" P# C5 b; V Item.iImage = nImage;; M: f5 R; p W9 |/ c0 K) L Item.mask = nMask;$ h& e3 Z& Z4 \. V3 a, I0 ?$ K BOOL bReturn = InsertItem(&Item);: O# f9 L$ T; }- G: t delete Item.pszText; : _! u0 X# ^ O" R return bReturn;& x. L7 Y5 `5 x; P } : G; A) u( M% v1 ~BOOL CListCtrlEx:eleteItem (int nItem)9 v! Y8 I4 ? q { 7 ?, @7 T A) _. Z7 o void* pDataEx = (void*)pThis->GetItemData(nItem); ; ]4 n R6 ?8 s' e if(pDataEx != NULL) A' w/ v# t h% M& B1 U% f; [1 Q" D {# B; O7 d2 a. g1 N# o delete pDataEx;; G+ }- y% w, D: O b }7 C* g/ H5 A7 C" Z- o( R" p1 O' ~ return pThis->DeleteItem(nItem); e) p+ o$ L3 x+ Q }" A P+ ~/ g5 @ BOOL CListCtrlEx:eleteAllItems ()) A. [) i# r& h, x6 s$ ~/ [2 L { 0 {. B0 r! S# e# f* x int nCount = GetItemCount();( t/ v, d8 c: r0 l9 n; \/ ?% x for (int i = 0;i<nCount;i++)% R/ }) H1 N1 B) \7 `& O { 4 ]0 v0 E7 d: S9 J! p if(!DeleteItem(0)) & _# r4 h, I0 j2 H9 c- K { , w8 b- M5 i' {5 @ return 0; - @8 u. b4 \$ d& f+ X9 Z }. L. f: X1 U; e$ c4 F } / m" {1 c* p" \8 t$ u* v. d return 1;, B" Q8 ~, p" z8 ~5 T, W, S* S }$ r0 @: ^: T" [ p' C1 v BOOL CListCtrlEx::GetItem(LVITEM* pItem)- w5 W' f4 k2 e+ u d& ] { ) Y- Q( ] E: X' V O, H. x, Y- L BOOL bReturn = pThis->GetItem(pItem);1 h8 C5 {$ P/ F" j6 _0 t' h% q if (pItem->lParam) 8 L- o$ L# i. q/ d+ | {. [1 d* ]- ~. b6 z' `, p pItem->lParam = ((ItemDataEx*)pItem->lParam)->SrclParam; / u6 \! Z" l# |$ z Q; ?) }9 K }

return bReturn;$ [2 U* }2 {7 P# I# o# Y1 f } ; Z2 }0 N# g/ u8 \5 mvoid CListCtrlEx::OnDestroy()9 U% L) J" P4 _3 N {, l! ?" {' `4 _& z7 N. ?1 w // 关键是清除申请的内存空间$ h$ U3 x4 x h$ o DeleteAllItems();! U. a/ `1 O7 P: N CListCtrl::OnDestroy();5 `9 O7 s) K8 w6 {. G" y, \ }

BOOL CListCtrlEx::SwitchItem(int nItem1, int nItem2). } C2 w0 t4 u M, [ {- c" [' h' \" R% |/ F/ B( n if (nItem1 == nItem2 || nItem1 >= GetItemCount() || nItem2 >= GetItemCount() || nItem1 < 0 || nItem2 < 0) 4 M; Q" z" s: v4 j" j { # D. n8 @' R% U$ U0 T return 0; 6 n9 }( s$ W( t } ( g+ L0 {+ B9 P LVITEM ItemData1,ItemData2; R% G& Y2 l4 l) Z- ^. { int ItemLength1 = pThis->GetItemText(nItem1,0).GetLength(); 9 @5 g; m0 t+ Y) c3 o6 w int ItemLength2 = pThis->GetItemText(nItem2,0).GetLength();6 k+ Z5 n T; v ItemData1.pszText = new TCHAR[ItemLength1+1];0 H: t# V: B5 q% _9 c" x ItemData2.pszText = new TCHAR[ItemLength2+1];* ~" _: b6 \$ H2 R ItemData1.cchTextMax = ItemLength1+1; % @# H8 E5 w' i0 g# q; P ItemData2.cchTextMax = ItemLength2+1; 3 Z/ ?/ r5 _# A* E5 T8 U ItemData1.iItem = nItem1;" o' s% G* B# X4 c ItemData2.iItem = nItem2;2 T% N/ M. n/ W! E- w ItemData1.iSubItem = ItemData2.iSubItem = 0;& q4 f, [7 x3 U7 u( h ItemData1.mask = ItemData2.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE|LVIF_STATE;

pThis->GetItem(&ItemData1); 8 r: I7 [- _+ M; Q pThis->GetItem(&ItemData2);; m4 `0 z+ I4 B; k( d1 _: s2 J // 交换数据: s% [7 E/ Z+ A# J4 u ItemData1.iItem = nItem2; 8 O% U* h4 |& |. |. A8 B/ ?8 s ItemData2.iItem = nItem1; 4 _8 a1 \: R) d pThis->SetItem(&ItemData1); & ]8 Y- F) F, d3 s: e) E! e pThis->SetItem(&ItemData2);

delete ItemData1.pszText;2 Y& r' J! t: z/ g delete ItemData2.pszText; & i2 s; ~4 o; b // 得到报表模式的列数 / I1 ]9 _' @6 M( y int nColCount = pThis->GetHeaderCtrl()->GetItemCount();, b* j% d1 V: s* V; p" X3 P+ I CString strBuffer; ( I/ B- ?8 T9 s. ^* L, g for (int iSubItem = 1;iSubItem < nColCount;iSubItem++) " ^! }0 s5 N8 _) y+ P( v& Z { & u0 S% {( S" c strBuffer = pThis->GetItemText(nItem1,iSubItem); * c. P* _5 _' A0 i- `+ @ pThis->SetItemText(nItem1,iSubItem,pThis->GetItemText(nItem2,iSubItem)); 8 |9 ^- v0 N1 A* s pThis->SetItemText(nItem2,iSubItem,strBuffer); - K+ @" B! N F4 y: T } * J2 P; d' S: Q9 U // 把原来的选中状态也转移过去 + p7 `" {: H# e# v. ?1 |, q% O // 不知道为什么这个属性没有体现在LVITEM结构中…… 8 F6 |7 L( a1 N/ E6 D bool bItemSelected1 = GetItemState(nItem1,LVIS_SELECTED) == LVIS_SELECTED;' d- G3 h7 C, p: k6 ?, H. \ bool bItemSelected2 = GetItemState(nItem2,LVIS_SELECTED) == LVIS_SELECTED;

SetItemState(nItem1,bItemSelected2?LVIS_SELECTED:0,LVIS_SELECTED); # e% S4 t% H. X* {# k SetItemState(nItem2,bItemSelected1?LVIS_SELECTED:0,LVIS_SELECTED);6 f; V( e7 c# P u8 j1 z return 1;/ c7 U+ m5 t9 m+ y, n2 `; X! V }

BOOL CListCtrlEx:reCreateWindow(CREATESTRUCT& cs). Z: h5 ~' G- d {6 z' M( b# z) T/ E1 x' j // 本类报表模式专用 0 }) Z1 t. a$ j1 z# i cs.style |= LVS_REPORT ;" I; Z1 @8 ^8 K // cs.dwExStyle |= LVS_EX_FULLROWSELECT;1 N5 P% ?0 ~ l* g$ c7 w return CListCtrl:reCreateWindow(cs); @3 |/ o' u- ` G% Z" H }

! s/ J% B! k: B9 A' JCOLORREF CListCtrlEx::SetHighLightColor(COLORREF clrHighLight)" o+ ?$ g: q2 a( b0 ~$ P# | { 9 z. o2 ?# v9 r9 Q; d6 ? COLORREF clr = GetHighLightColor(); & d7 i8 ^6 f' c. A9 @' T: o5 } m_clrHighLight = clrHighLight;( \# {* Q& l- x+ E. [ return clr;) x" b) u) m8 p, S- k5 H- E+ Q }

COLORREF CListCtrlEx::SetHighLightEdgeColor(COLORREF clrHighLightEdge) ; k) M/ }2 ~% e, q1 E5 h+ a{ : G1 `- O' A" z. | COLORREF clr = GetHighLightEdgeColor(); # E6 W6 k y' i6 `9 v1 p- I m_clrHighLightEdge = clrHighLightEdge;' i h3 b2 K" ^; ? return clr;1 r0 p& k! | O* t }

该用户从未签到

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

COLORREF CListCtrlEx::GetHighLightColor(void) const 4 z* s# A) B7 ^/ c* g: k+ Z{ 8 X6 w. s2 @4 N9 Q, ` return m_clrHighLight; 8 ~6 C" `7 T0 ^& v9 r' Y}

COLORREF CListCtrlEx::GetHighLightEdgeColor(void) const n# w" c& q. l' a3 w{ / G! U' n( y6 J, Z0 v' E return m_clrHighLightEdge;0 i" j2 }4 F# a1 C3 S L& I7 c }

int CListCtrlEx::GetItemImage(int nItem)9 ^& w* I8 H5 e. q5 i# ^% |% H8 Q { # ?: ]$ ?6 W8 O LVITEM ItemData;. k i1 y6 J4 V& m) j4 z ItemData.iItem = nItem; " K8 H# o I3 q. k0 q ItemData.iSubItem = 0;: Z: K/ z& N/ T* d; o ItemData.mask = LVIF_IMAGE; + m" W7 J$ N5 C1 Q6 \( B( Q pThis->GetItem(&ItemData);9 f5 t% |; ?5 D! S( ^ return ItemData.iImage;- }7 u3 i" v- @ }

BOOL CListCtrlEx::MoveItemUp(int nItem)( z- e) \4 ^/ \& t9 H) R% _9 E. o: L {& z# s, y& a. Z+ f1 Z if (nItem < 1 || nItem >= GetItemCount())4 a. S% D5 f; C! w T6 x { ' w% y g x# S; r return 0; 7 z. @7 C; x3 n3 M) g) [ } 6 z6 T* x8 r1 G4 x$ _* b! k return SwitchItem(nItem,nItem - 1); , i* K: l" F; c* m7 m: ]}

BOOL CListCtrlEx::MoveItemDown(int nItem)& `- p) a7 R# n* X5 }& a { ( |. u/ m) d& W: J if (nItem < 0 || nItem >= GetItemCount() - 1)2 i3 G: g7 J9 S7 E3 q# M {% @) S' J; R4 l8 ~) N return 0;1 v0 A0 O* O7 `4 @ } ) V: Y4 @0 }# _ return SwitchItem(nItem,nItem + 1); ) r( ~5 F3 T' U}

void CListCtrlEx::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult) 7 O. y: y! ^; R: g: J{' V: `4 q; }6 S/ F9 P4 Y% N, ^) }* O NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR);

*pResult = CDRF_DODEFAULT;

if (pLVCD->nmcd.dwDrawStage == CDDS_PREPAINT)0 o* {* r" i* |, g3 F8 f {' ]0 S. r; T7 \1 Q# L! l1 t7 J *pResult = CDRF_NOTIFYITEMDRAW;& {% [, B6 I; B! K } 4 v2 U% u) y8 I- P. u else if (pLVCD->nmcd.dwDrawStage == CDDS_ITEMPREPAINT) " |8 \: ?5 E: o* Z( x- ?2 U# ?' i { . ~4 m1 `2 e6 R7 z *pResult = CDRF_NOTIFYSUBITEMDRAW; 2 u* w- Q. h6 e( I& g; P }% B. l' k6 K2 @$ {* P" R else if (pLVCD->nmcd.dwDrawStage == (CDDS_ITEMPREPAINT | CDDS_SUBITEM)) 6 O* x+ E& E. | {

int nItem = static_cast<int> (pLVCD->nmcd.dwItemSpec);! J! ?% \- {8 x int nSubItem = pLVCD->iSubItem; 3 B' V4 Y+ L- x7 K if (nSubItem != 0) 3 j3 s& J/ n% f& P8 a2 e {+ @$ [6 {, m |/ N& O2 l1 E // 全部绘制过程在nSubItem == 0 时完成 * f! x. X5 ?5 \ // 分布完成会有小问题,我无法解决. z- F; A+ K# ?0 o8 J5 r *pResult = CDRF_SKIPDEFAULT; * w/ y0 |, B1 K2 m7 _8 p& I6 f, a( U return;# M5 \ ^ A; {5 q }- u1 K* v6 S; d+ D* J9 E CDC *pDC = CDC::FromHandle(pLVCD->nmcd.hdc); * O6 j$ w% U: U. \: t% m/ r# i2 R5 a: m int nColCount = pThis->GetHeaderCtrl()->GetItemCount();( C3 N! h% T: w$ h; S CRect rectSubItem; % P q) ?8 l5 K/ j CString strItemText; M A' I' `5 D* I% n* n& t pDC->SetTextColor(((ItemDataEx*)pThis->GetItemData(nItem))->clrItemTextColor);. ]/ m; h! z; }1 B" H. j pDC->SetBkMode(TRANSPARENT);: H5 M/ g4 p! u/ O# F! U // 相当于橡皮 # h) m( R# {# o/ j* w% P CBrush brEraser(GetItemBackColor(nItem));

// int nFirstWidth = GetColumnWidth(0);

# R5 j* X" D0 P% R- B. K) t5 [ for (int i = 0;i < nColCount;i++)6 v; n. Z1 t# G {- Z7 g( s, g& p5 b' R% F strItemText = GetItemText(nItem,i);

if (i == 0) 4 D& R* w/ M8 \' X4 k' t5 @: v { & ?' d, t1 E' A& L4 q9 J: }+ G pThis->GetSubItemRect(nItem,i,LVIR_BOUNDS,rectSubItem); / G- P7 c: ]" w8 l0 e) w5 b // 擦掉原来的图形8 E6 y4 N% `% Y" B+ E8 n" Q pDC->FillRect(rectSubItem,&brEraser); ) j( W9 `3 \8 U _3 w; M8 C% d) b if (GetExtendedStyle() & LVS_EX_CHECKBOXES)8 i5 |7 f5 {& q9 i {* T, [6 ^: \1 { CRect rectCheckBox;' a) X! d6 ?3 |1 }) C GetSubItemRect(nItem,0,LVIR_ICON,rectCheckBox); . } T9 B# f. a, ~3 D CRect::SwapLeftRight(rectCheckBox);/ g1 f, A3 \% U- k. H6 j( H rectCheckBox.left = rectCheckBox.right - rectCheckBox.Height();

rectCheckBox -= CRect(2,2,2,2);- F1 q& _/ g5 C( N) y pDC->Draw3dRect(rectCheckBox,0x81521D,0x81521D);' {. V( J: i+ ]" k8 z1 d8 d. i //rectCheckBox += CRect(3,3,3,3); 1 y( ~0 M; W: \3 |* o! t if (GetCheck(nItem)) : v" ~' x7 Z( [+ ? { 5 H5 J+ g* C7 h1 b- ` B // 绘制一个CHECKBOX 2 @9 ?" | @4 x0 Q$ ^9 s CPen penCheck(PS_SOLID, 1, 0x0021A221);, S3 g3 [* A3 C3 b' p9 Z. h CPen *pOldpen = pDC->SelectObject(&penCheck); 8 i8 M( }) S# G for (int j = 3; j < 10; j++) - n8 ^$ x# m0 w% m/ l {8 Y' j8 u8 D3 S+ K7 [9 K" I pDC->MoveTo(rectCheckBox.left + j, rectCheckBox.top + rectCheckBox.Height() /2 - 3 + ((j < 6) ? j - 1 : (9 - j)));& u8 C3 ~+ t4 L7 ~3 e pDC->LineTo(rectCheckBox.left + j, rectCheckBox.top + rectCheckBox.Height() /2 - 3 + ((j < 6) ? j + 2 : (12 - j)));9 `8 D* p4 j) m: d } . L, O- m7 T0 L* h. _/ ` 4 N' A. K. C! M. k' g& ?/ P/ _8 e pDC->SelectObject(pOldpen);& A8 k% P6 I! ?+ C5 }+ l$ _ } 9 N# O: d' Y8 |" v& o }8 k6 X3 P6 N ^# G- B( m: g // 根据需要画选中框+ T8 l% O+ C4 X, b( c& _' s; i' U if ( (GetFocus() == this || GetStyle() & LVS_SHOWSELALWAYS)&&(GetItemState(nItem,LVIS_SELECTED) == LVIS_SELECTED)) 7 I) f) C9 W4 _, D$ Z {5 }8 R$ k2 `" w. ] if (GetExtendedStyle() & LVS_EX_CHECKBOXES) : E, Z+ a7 R3 D/ h$ [6 b {// 存在检查框风格的时候需要对左坐标进行修正4 o2 s5 @% s$ n' \7 a* R CRect rectLeft;0 C0 c. `! a) K2 Y Y3 u GetItemRect(nItem,rectLeft,LVIR_ICON); 3 q0 ~6 p/ a' e' O: F4 A rectSubItem.left = rectLeft.left; & j- @& Q9 u' l } 1 `! u( ~4 P" j" U' p9 u" C if (!(GetExtendedStyle() & LVS_EX_FULLROWSELECT)); O' c" D3 y. ?6 G% Q/ b7 f {// 非全行选择时右坐标需要修正 % c8 J, _: W, S( B CRect rectRight; 4 l) d4 u4 D* z" l* ?2 T2 W GetSubItemRect(nItem,nSubItem,LVIR_LABEL,rectRight); ( f( V, C; k5 I" O7 L" W6 Z4 W+ k rectSubItem.right = rectRight.right; # [5 A9 H+ [7 O }& r2 V! [ k0 t' K CBrush brHighLight; # d c* b$ D9 D9 _1 n f CPen penHighLightEdge; 6 d5 p, E7 P, W' C) q7 V if (GetFocus() == this)# X" D- H' `* Y9 w1 C# [1 q {9 g! E. E; o$ `* j) `' s brHighLight.CreateSolidBrush(m_clrHighLight);- q' F" h# b1 c: Z- d. P, Z penHighLightEdge.CreatePen(PS_SOLID,1,m_clrHighLightEdge); , z& d K* I7 s! {3 K) j- { } 7 `( A2 F$ ?# \- x' A0 G! v else8 Q. j/ b+ e7 s: I {0 a# b& p, m. `0 e2 }$ F // 不是焦点时,颜色70%不透明度8 y7 h* X3 y4 I8 o) i) M COLORREF clrBack = GetItemBackColor(nItem);% a7 [4 J2 m1 ?0 w: b7 M WORD red = GetRValue(m_clrHighLight)*0.7 + GetRValue(clrBack)*0.3;* q4 H) u' P0 Z! m WORD green = GetGValue(m_clrHighLight)*0.7 + GetGValue(clrBack)*0.3; 5 O! g; b0 {6 y2 C, J% J WORD blue = GetBValue(m_clrHighLight)*0.7 + GetBValue(clrBack)*0.3;' d3 c2 Q s6 x! u' x! p- L brHighLight.CreateSolidBrush(RGB(red,green,blue)); * @0 g( ?' F: y" y" m* x, p0 E red = GetRValue(m_clrHighLightEdge)*0.7 + GetRValue(clrBack)*0.3;+ E' a: F V' \ green = GetGValue(m_clrHighLightEdge)*0.7 + GetGValue(clrBack)*0.3;3 V( P( X/ |( [5 P& U* ^4 W7 V/ G4 P6 T blue = GetBValue(m_clrHighLightEdge)*0.7 + GetBValue(clrBack)*0.3; 9 D& b: A' z- g6 B0 |' p penHighLightEdge.CreatePen(PS_SOLID,1,RGB(red,green,blue));' R3 t- J. M5 P: q6 O }( a' t8 _' n& F% ^- ^! X' D CBrush *pbrOld = pDC->SelectObject(&brHighLight); 9 d( ^1 ^: x+ n4 W( X+ q. s* l CPen *ppenHighLightEdge = pDC->SelectObject(&penHighLightEdge); . J" ], d3 y) h0 t7 z9 o, q1 } pDC->Rectangle(rectSubItem); & U/ I7 S g. f* t9 P2 G! d" H pDC->SelectObject(ppenHighLightEdge);2 K, r- a+ N: u# D h( ^; C9 O5 |! L/ D pDC->SelectObject(pbrOld);3 _- X( Y8 S, _6 ]$ a" t } , P& _/ c# s. L } 8 L$ T8 V* `. u4 K* u) @ pThis->GetSubItemRect(nItem,i,LVIR_LABEL,rectSubItem); ( |: H" `( e4 F2 S0 E) ]0 H' y UINT uFormat = DT_SINGLELINE|DT_VCENTER|DT_END_ELLIPSIS; , n3 I: W3 M, ~ HDITEM hdi; 7 P- _# K2 ~* Z hdi.mask = HDI_FORMAT; . b: y* f( y8 D7 w pThis->GetHeaderCtrl()->GetItem(i,&hdi); ! ?7 \0 W! O; N2 q2 m' g( O" v/ v' r if (hdi.fmt & LVCFMT_RIGHT)9 r' T, K& V/ U$ v. b {& V/ e& W" V3 T% ?1 a) t) h" d uFormat |= DT_RIGHT;4 ]! \& ^) Y) x8 a; u8 Q } . _+ f* y4 L* m: P else if (hdi.fmt & LVCFMT_CENTER) ! `4 b) p4 k& i { ( V+ ~% [! F. I' i9 l* l uFormat |= DT_CENTER; 6 I9 H! l9 R0 t }: P) I$ }9 {& {6 d3 O2 C6 | pDC->DrawText(strItemText,rectSubItem,uFormat);

CImageList *pImageList = pThis->GetImageList(LVSIL_SMALL);; k- _3 W6 H) L9 K1 V6 F if (pImageList) 8 k! }2 F: k# r) Z6 P {) c% ^) W v1 \7 V8 \: [. {( |1 H CRect rectIcon;- h! K& f# f) i d GetItemRect(nItem,rectIcon,LVIR_ICON); ; C. L+ n+ }" w4 z* ^) Y pDC->DrawIcon(rectIcon.left,rectIcon.top,pImageList->ExtractIcon(GetItemImage(nItem)));( K/ W7 z) S/ o d$ K: z }* X0 N& @6 m, g9 }# M! S* r" W } - m$ J: S5 V% p *pResult = CDRF_SKIPDEFAULT;, q$ N% v0 I2 M% x0 H }

}

该用户从未签到

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

本版积分规则

关闭

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

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