下沙论坛

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

QQ登录

QQ登录

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

又是垃圾,CListCtrlEx Base CListCtrl

[复制链接]

该用户从未签到

跳转到指定楼层
1
发表于 2005-2-23 21:29:00 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
// ---------------------------------------------
' I( A  e- T& H. _// ListCtrlEx.h
' j5 a0 r$ g" e8 q8 }// ---------------------------------------------+ y. i" F# q" v3 Y& f! @
#pragma once
* v# P4 ]1 v9 z0 ?" V5 K- s+ j: p7 @  ]( L, U; ?
// CListCtrlEx
, S5 b2 @: x& M$ T6 _+ M5 jclass CListCtrlEx : public CListCtrl; |- C# [& d" z4 P' w2 S
{/ k7 N* x( s0 c' I
    DECLARE_DYNAMIC(CListCtrlEx)  f  x8 @9 k0 s/ v* C6 ]
public:+ E5 O! y: R6 |+ b5 G! O
    CListCtrlEx();
0 W7 F+ E+ p* G/ }; r5 P# |# k    virtual ~CListCtrlEx();
  E  J" Y% P+ o6 }protected:6 P! S* r: w  J
    DECLARE_MESSAGE_MAP()
  M! k8 F1 [2 `9 l8 K: y) x    CListCtrl* pThis;
1 f6 e  W0 k& @3 [: d' ~    virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
, k, I% w! s& u$ ~private:
3 J4 C3 E2 `" B5 f+ h    struct ItemDataEx {/ P% ]* h. l. {( U! T
        LPARAM SrclParam;8 n! T! N7 o' i! w5 T6 }
        COLORREF clrItemTextColor;4 w) V* L; F: b( o
        COLORREF clrItemBackColor;
$ I& p9 X$ c8 Z, m1 W# t, o    };  Q8 T3 g* Z' E2 s& R4 |# o
    COLORREF m_clrHighLight;. h  U4 g1 K( a& @2 R& x
    COLORREF m_clrHighLightEdge;
8 {8 b+ L! ~+ x' Qpublic:
% j7 h: A: \0 }8 J7 r4 y    // 新增方法% Q% _& F# |8 T# }! d# w8 }
    COLORREF SetItemTextColor(int nItem,COLORREF clrText);( o- C5 Q# @/ @4 N5 y
    COLORREF GetItemTextColor(int nItem) const;
$ l. Q7 N% _  d* j6 K! {    COLORREF SetItemBackColor(int nItem, COLORREF clrBack);
7 G! h9 |1 V+ x! A    COLORREF GetItemBackColor(int nItem) const;$ `) L# G/ e; `) d0 t: Y; h
    COLORREF SetHighLightColor(COLORREF clrHighLight);
* o5 d5 H/ y; c- u5 j  R    COLORREF SetHighLightEdgeColor(COLORREF clrHighLightEdge);
/ W2 }+ R5 }2 W    COLORREF GetHighLightColor(void) const;5 L# y! c3 X- M8 B% B  r
    COLORREF GetHighLightEdgeColor(void) const;
% U  ^6 T2 c+ [8 B  U    int GetItemImage(int nItem);; V. J# I6 c0 D: ]  ~; ]& J
    BOOL SwitchItem(int nItem1, int nItem2);/ j" n" ^: E% r- w% U- p
    BOOL MoveItemUp(int nItem);$ u' _6 Z% O7 U
    BOOL MoveItemDown(int nItem);/ a* r) J8 D" w& P- i) t, o
    // 重写原来的方法* i: {$ n6 h7 f8 s$ k
    DWORD_PTR GetItemData (int nItem) const;
8 ^) K5 ~0 ?) Q4 o+ P" K9 G1 W! N    BOOL SetItemData (int nItem, DWORD_PTR dwData);
4 I7 S7 C- Q) n    int InsertItem (const LVITEM* pItem);( W( Z% h! _/ t# o) E; u
    int InsertItem (int nItem, LPCTSTR lpszItem);/ H3 s. E3 ?; ?: p& O2 y, }% F
    int InsertItem (int nItem, LPCTSTR lpszItem, int nImage);
$ m0 H0 Z5 d4 ~& s- m- Z8 ^% V4 ]    int InsertItem (UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam);
7 i+ }4 X  o1 ~, Q    BOOL DeleteItem (int nItem);
5 Z8 R9 U5 N6 M3 V8 ^3 K    BOOL DeleteAllItems ();5 B0 H4 y! V# {2 V- N0 Z
    BOOL GetItem(LVITEM* pItem);
$ ~2 e) w. z. u+ {6 ]' O    afx_msg void OnDestroy();  _( Q( E6 A  |7 q' e9 c
    afx_msg void OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult);" ]& y* b$ f/ g- x
};
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 顶 踩

该用户从未签到

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

该用户从未签到

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

COLORREF CListCtrlEx::GetHighLightColor(void) const) z% p1 C" u; R4 K { ; K! }6 x' w: E2 N. i5 y return m_clrHighLight; ; o6 ~: B8 |" E}

COLORREF CListCtrlEx::GetHighLightEdgeColor(void) const& L* D* K1 ]5 b3 J' ~3 x1 S {+ I) W% h- d* q) [# a return m_clrHighLightEdge;) @ j* A& G8 Q' W9 p7 N# x }

int CListCtrlEx::GetItemImage(int nItem) ) {$ b& e4 \! y- E. G2 j{ & `9 S& Z& h( e7 m5 D8 u LVITEM ItemData; 2 X( N& I t0 K" E, B3 r; `5 }' B ItemData.iItem = nItem;. C1 e& E( V5 O/ D0 v. W+ M ItemData.iSubItem = 0;( v8 n8 z3 H+ k3 B1 q T+ K+ N ItemData.mask = LVIF_IMAGE; 5 m7 a9 Y) F/ }+ p( S: ?9 c pThis->GetItem(&ItemData);- n) J: M; s' D- w$ k2 X return ItemData.iImage; & K+ s# Q k: c' p0 `- D; s: U}

BOOL CListCtrlEx::MoveItemUp(int nItem)3 i* L% T( O5 I { ) a/ f: d' a# w, F. x+ V* l if (nItem < 1 || nItem >= GetItemCount()) 4 U/ G7 F% q2 _- f3 U! ~ { - ~$ w; y9 o5 E5 s4 l, Z7 B+ b return 0; 8 o- g" `2 G ?1 n) x( V* K4 M } 1 o, l6 b T0 |! A1 m# n I$ }# { return SwitchItem(nItem,nItem - 1);% Y/ d1 d+ ^' X X/ K% L/ r7 H }

BOOL CListCtrlEx::MoveItemDown(int nItem), Z' P$ e1 m- e/ B( D {: t* Z, `$ M! l1 M if (nItem < 0 || nItem >= GetItemCount() - 1) 9 p; `2 t4 [! B {$ z! r6 ~/ J' q0 ^8 J7 D0 P return 0;& e3 W. f! r: {. ^5 V, E: y, ? } F$ J! s, N3 r% p! S9 ^8 F8 q6 h6 i return SwitchItem(nItem,nItem + 1);5 q. M: J8 Y! \; l5 O }

void CListCtrlEx::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult): C0 ^, ]' f1 e( q2 z2 O! M, a' h { ( I* i& Q5 D& h+ s NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR);

*pResult = CDRF_DODEFAULT;

if (pLVCD->nmcd.dwDrawStage == CDDS_PREPAINT) 7 P3 ]- X! ?6 H- N& } {$ e- X& l$ M* m2 i5 C: ^$ N *pResult = CDRF_NOTIFYITEMDRAW;3 i- C1 g% U; Y" _! P" @1 H }! c2 ^9 ^/ X4 |+ E else if (pLVCD->nmcd.dwDrawStage == CDDS_ITEMPREPAINT) - X7 r4 H) X% r( V {6 G Z+ B8 X( O# H6 }6 o- l5 P- J *pResult = CDRF_NOTIFYSUBITEMDRAW;4 M/ R* q' {1 v } % j1 n9 X8 c9 a# {3 d else if (pLVCD->nmcd.dwDrawStage == (CDDS_ITEMPREPAINT | CDDS_SUBITEM))/ I' `5 H9 b9 a" d# Z {

int nItem = static_cast<int> (pLVCD->nmcd.dwItemSpec);5 i% B+ B6 H& H# r8 Q) ~ int nSubItem = pLVCD->iSubItem;! t9 E5 T2 w- R T# @& l if (nSubItem != 0). H! G8 l$ _$ q: L% ^ {3 L0 i% I4 B4 H // 全部绘制过程在nSubItem == 0 时完成* f, J9 m( q% m3 m7 H+ V2 w, t // 分布完成会有小问题,我无法解决0 N/ j" m m+ G" V. X; C *pResult = CDRF_SKIPDEFAULT; / Y) Y9 K6 h$ B% d return; 5 W( L6 K' H! A }- n6 ~9 p& |# l& \$ |% E CDC *pDC = CDC::FromHandle(pLVCD->nmcd.hdc);' {, F0 K- P3 r7 P9 e5 i C int nColCount = pThis->GetHeaderCtrl()->GetItemCount(); & g; r" |3 X; J& q; ?- [ z% U CRect rectSubItem; ! A) I9 [0 Y% P( e CString strItemText;3 e$ V% o5 |7 E8 ?! t1 h! x4 h pDC->SetTextColor(((ItemDataEx*)pThis->GetItemData(nItem))->clrItemTextColor);2 _0 L4 J" Q7 L/ D! Y0 Y( [' @ pDC->SetBkMode(TRANSPARENT);3 P0 A0 B, \; A; T- K4 W1 f // 相当于橡皮$ Q" Z8 j+ R9 A' w9 W; v' A2 d. n CBrush brEraser(GetItemBackColor(nItem));

// int nFirstWidth = GetColumnWidth(0);

9 U$ p$ M! C2 c5 {* N+ r for (int i = 0;i < nColCount;i++) ' K# H. b" d2 m2 J* A {* H9 `7 `9 f2 c' F# z$ I strItemText = GetItemText(nItem,i);

if (i == 0)& Q3 D3 R* I- t; ]% L2 d2 Y+ B6 e { E6 a7 h7 j/ j; D; v* t pThis->GetSubItemRect(nItem,i,LVIR_BOUNDS,rectSubItem);3 f2 E) Z) y# b$ ] // 擦掉原来的图形 - ` l' X7 t) w, h pDC->FillRect(rectSubItem,&brEraser);8 P# e( M: O' B: y' n; h+ R m if (GetExtendedStyle() & LVS_EX_CHECKBOXES)2 y9 J( }4 c: k7 d+ a$ p' O {# W+ U3 S/ N2 m CRect rectCheckBox; 3 h9 f' s. w) u- R GetSubItemRect(nItem,0,LVIR_ICON,rectCheckBox); + P# i8 W/ M ?& B' `2 ` CRect::SwapLeftRight(rectCheckBox); + j9 R Q! W! o% R a2 R: C% I6 A rectCheckBox.left = rectCheckBox.right - rectCheckBox.Height();

rectCheckBox -= CRect(2,2,2,2);! T- Y+ D& w% ?! u. @ pDC->Draw3dRect(rectCheckBox,0x81521D,0x81521D);' K% E4 q6 d, X9 X) z! p( t //rectCheckBox += CRect(3,3,3,3); / q7 G* [3 [+ h3 {0 } if (GetCheck(nItem)) 7 c3 P) k9 x# N8 k& ] { " {. T e7 |# l! Y% {, T // 绘制一个CHECKBOX7 x Q& {& w# K. t CPen penCheck(PS_SOLID, 1, 0x0021A221); & Y& |5 y; N% U# P3 Q# w CPen *pOldpen = pDC->SelectObject(&penCheck); * H7 M& H- C% C5 V. ?- } for (int j = 3; j < 10; j++)+ i1 ? }4 N# u: L [0 p { R1 j6 L+ [3 R* U4 x* L' h pDC->MoveTo(rectCheckBox.left + j, rectCheckBox.top + rectCheckBox.Height() /2 - 3 + ((j < 6) ? j - 1 : (9 - j)));& c+ v {% Q" | R pDC->LineTo(rectCheckBox.left + j, rectCheckBox.top + rectCheckBox.Height() /2 - 3 + ((j < 6) ? j + 2 : (12 - j))); ' u0 X$ D7 m1 ~( f- { } 3 [- ]- m2 ~ \) O' e; k # N/ k( g# {* U' W% M pDC->SelectObject(pOldpen);+ d% n+ S) |0 b5 ?$ @$ t3 Q) Q } % y. E$ _' p2 n; D5 x* z0 Y } ! k4 K; | R' z! I5 V5 h // 根据需要画选中框 |' [, L$ G; [% t, ?. C if ( (GetFocus() == this || GetStyle() & LVS_SHOWSELALWAYS)&&(GetItemState(nItem,LVIS_SELECTED) == LVIS_SELECTED)) 6 y# Y$ ?, V- O { 1 A# E% C: Z6 R: l+ j: E( j, z if (GetExtendedStyle() & LVS_EX_CHECKBOXES)3 q0 C$ u, x" `* g/ m& r {// 存在检查框风格的时候需要对左坐标进行修正 0 X* j; V$ H1 a3 t" N% L6 L* u CRect rectLeft; ! I2 J' i) Y, L7 e% z# j GetItemRect(nItem,rectLeft,LVIR_ICON);( M, ? |7 X. _ rectSubItem.left = rectLeft.left; a' r- o* `+ g' J w9 v } % K3 H% i! U* w% [: L% w2 p0 t if (!(GetExtendedStyle() & LVS_EX_FULLROWSELECT)). }8 h+ k; ]' L2 ] {// 非全行选择时右坐标需要修正 . X1 C" l& d i CRect rectRight; 3 t% s. y) t r9 E GetSubItemRect(nItem,nSubItem,LVIR_LABEL,rectRight);2 C. Q, Y0 ]- ] rectSubItem.right = rectRight.right;# k2 a: g$ k' V7 m' @+ M } ) r6 z. o; ^, H# O1 M0 N; { CBrush brHighLight;) H j( f. ]7 u- V) b, E CPen penHighLightEdge;, X: Y& x3 |. E1 `, g6 h6 R if (GetFocus() == this)5 v, p! Q( j5 H% L {" v; b G3 }" ^0 g$ Y5 { brHighLight.CreateSolidBrush(m_clrHighLight); + d. T& r: R M penHighLightEdge.CreatePen(PS_SOLID,1,m_clrHighLightEdge); - l3 n2 O" q& A2 v5 y } - L' B e' V X9 l/ c1 r1 O% { else 3 \; N7 M! ~$ D9 k { . m. r" g0 W9 p9 Y" U" d // 不是焦点时,颜色70%不透明度 8 Y* l4 K% V7 P. n6 H/ @- f9 r# l COLORREF clrBack = GetItemBackColor(nItem);* G. l- O+ ?0 h6 n1 J WORD red = GetRValue(m_clrHighLight)*0.7 + GetRValue(clrBack)*0.3;. e7 ]9 [9 K" s5 h6 v WORD green = GetGValue(m_clrHighLight)*0.7 + GetGValue(clrBack)*0.3;; ]4 \1 R `7 N6 B# G! c WORD blue = GetBValue(m_clrHighLight)*0.7 + GetBValue(clrBack)*0.3; ' e8 R: Z2 h' W8 n* g; K brHighLight.CreateSolidBrush(RGB(red,green,blue)); e: W) |0 g, i6 a red = GetRValue(m_clrHighLightEdge)*0.7 + GetRValue(clrBack)*0.3; / F3 J1 Q( A" N( M `3 Q& _7 N green = GetGValue(m_clrHighLightEdge)*0.7 + GetGValue(clrBack)*0.3; _& H0 }( V' \ blue = GetBValue(m_clrHighLightEdge)*0.7 + GetBValue(clrBack)*0.3; - M5 z! o6 E: l+ z: Z- \ penHighLightEdge.CreatePen(PS_SOLID,1,RGB(red,green,blue));! k7 M" y' e9 u0 b } / `9 `% N* W) [7 _% W! R CBrush *pbrOld = pDC->SelectObject(&brHighLight); , m( d6 E- @( g- I1 d$ J) [) P8 m CPen *ppenHighLightEdge = pDC->SelectObject(&penHighLightEdge); 2 s9 n% M" B5 d- p; Z! Q' \$ D( I pDC->Rectangle(rectSubItem);) Q+ r3 k4 a' [% C* g5 {! L pDC->SelectObject(ppenHighLightEdge);, u4 C" J+ n" X( @' V pDC->SelectObject(pbrOld);/ d% o9 B% _; [' G: X }: r+ j* ]% b+ z9 |. r0 \ }5 I3 c( Q( L3 ~' i& g. R pThis->GetSubItemRect(nItem,i,LVIR_LABEL,rectSubItem); . Z2 R# ~! i" I8 c2 T: S; E UINT uFormat = DT_SINGLELINE|DT_VCENTER|DT_END_ELLIPSIS;( @ t5 ]2 n& `; Z, V: d6 k2 Z HDITEM hdi;' b7 }: m# `( Q5 B x hdi.mask = HDI_FORMAT; 2 n4 B! K! |( y! ^ pThis->GetHeaderCtrl()->GetItem(i,&hdi);7 a/ h; |' N5 p if (hdi.fmt & LVCFMT_RIGHT) . h8 K0 M$ {) a+ E0 K { 6 }+ p' l4 r% ]$ Y& j p/ Q: o uFormat |= DT_RIGHT; + @" r) d5 M" T7 m |! j2 j. r) a Z } 3 \, L# N3 p" T8 f4 w else if (hdi.fmt & LVCFMT_CENTER)0 B: m0 F G2 M- x0 Z% X6 b; B { 0 C: \! j6 w e% T6 m+ Q uFormat |= DT_CENTER; ' j I: P4 F6 \- g2 W6 q }1 d9 O! _% x) a pDC->DrawText(strItemText,rectSubItem,uFormat);

CImageList *pImageList = pThis->GetImageList(LVSIL_SMALL); $ v- ~! f) V o- A if (pImageList) + W: }) q/ ^/ C2 u {7 @! h! ?# Y# U/ b3 e+ _ CRect rectIcon;6 C+ N8 @7 \$ h9 }) E, b, j GetItemRect(nItem,rectIcon,LVIR_ICON);! u: Y8 m. K. E$ A9 W" p6 b2 G pDC->DrawIcon(rectIcon.left,rectIcon.top,pImageList->ExtractIcon(GetItemImage(nItem))); 6 V/ [+ \3 e4 r0 `* @ } / f+ [4 j; j5 F1 p }1 l8 V3 v$ Y$ e& Y' u *pResult = CDRF_SKIPDEFAULT; 9 E1 b- s3 k% g# U& K7 w( H }

}

该用户从未签到

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

// --------------------------------------------- 0 x; O/ i0 n, ^; ]) R6 A// ListCtrlEx.cpp 8 |' v$ S5 e- P. R v// --------------------------------------------- $ x3 B7 {. x/ e1 \6 o// ListCtrlEx.cpp : 实现文件: l5 }8 _1 ] T" R //

#include "stdafx.h"' ~6 g& I% r/ T& S; j" Q& | #include "CreateNewClass.h"6 I+ w% n( F: T6 J, k( f5 ? #include "ListCtrlEx.h" + p- A# p5 M% o' n#include ".\listctrlex.h"

+ Z' o$ s+ e3 F4 M) I* t$ s // CListCtrlEx

IMPLEMENT_DYNAMIC(CListCtrlEx, CListCtrl) ) k* [9 y1 w* }5 d6 g! u+ ~+ b* H2 HCListCtrlEx::CListCtrlEx() 1 B4 j" Q/ K: ~6 F3 t* z{ $ T- g: o1 x* J' |' f // 用于调用基本的方法7 |1 i; @( Y3 H. J' u! g8 w# B/ f$ C/ ? pThis = (CListCtrl*)this; 0 S) l5 D A' \$ o# Z // 默认的选中框颜色 $ ]& B7 z* e7 k( s: U) S m_clrHighLight = 0xF1DACC; 9 O' k0 T3 i- ]* c m_clrHighLightEdge = 0xC56A31; " C9 l( M* T- B5 g& d}

CListCtrlEx::~CListCtrlEx() % ?7 i* t9 t1 i9 B, e# L{

}

x& ~8 X* Z3 Q5 I" U) j5 j BEGIN_MESSAGE_MAP(CListCtrlEx, CListCtrl); `& o/ @# t6 Z& s8 e% B ON_WM_DESTROY() / Z1 E" [. G" q3 L0 C: a( o ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnNMCustomdraw)% \- O2 @2 B! \8 R% F1 B- ]8 } END_MESSAGE_MAP()

// CListCtrlEx 消息处理程序

8 T" n( }+ o7 T G" W" P& @COLORREF CListCtrlEx::SetItemTextColor(int nItem,COLORREF clrText) : b: j' X) S; ^) j2 O" ?) ?3 q{6 A9 N$ O+ Y4 U9 V" x, Z8 N COLORREF clr = GetItemTextColor(nItem); & b: Y" m5 F2 N' t3 D ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);2 \+ o; |& Q5 T/ r) i pData->clrItemTextColor = clrText; / A3 \0 b0 _- ?+ |! K- N return clr; . h# T& t4 \0 q m+ }9 Q}

COLORREF CListCtrlEx::GetItemTextColor(int nItem) const 4 f' R5 f, _9 H" \- _1 l# Y{ , X4 a+ v' E- h6 D$ ~1 s6 T0 \ ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);5 p) b7 e5 L1 V# G z; B return pData->clrItemTextColor; % L$ u5 y' L% L0 Y& L}

COLORREF CListCtrlEx::SetItemBackColor(int nItem, COLORREF clrBack)3 m f- c, T( Q3 T/ Q+ A {5 H: x- X. ~2 G$ M COLORREF clr = GetItemBackColor(nItem); i0 q3 D9 G0 Q5 ^, a ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem); 4 j( _/ C) c- l2 }9 G. e+ q pData->clrItemBackColor = clrBack;. m q6 c' W3 D4 P3 { return clr;+ ^7 s; ~% O/ n% f }

COLORREF CListCtrlEx::GetItemBackColor(int nItem) const* \9 q9 X! {# M! b0 N {9 G9 C2 P: C4 e% { ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem); ( K- f' J! ?1 e! j6 W+ j* D7 | return pData->clrItemBackColor;+ h+ `. \5 u# K }9 g# r4 r, n! R4 J* J* ?6 n" F DWORD_PTR CListCtrlEx::GetItemData (int nItem) const 7 Y0 ~) T& V9 _( p# w- n{" I r: U n) }& s7 l if (nItem >= GetItemCount()) ' k6 M6 j& o# { l { 1 D3 U% {; \* ?3 C6 ^) t return 0; : ?- V8 `$ l( W% s } 4 b4 _: G) [* K- u8 h2 ] CListCtrl* pThis = (CListCtrl*)this;1 V- }# P% ?# e% p5 b5 k* [ DWORD_PTR Data = pThis->GetItemData(nItem); - b# z3 W' [' ~- q# M return ((ItemDataEx*)Data)->SrclParam;8 I2 u& N) L; L6 }2 S( B }; w1 S2 ~. e6 O2 o. @- Q BOOL CListCtrlEx::SetItemData (int nItem, DWORD_PTR dwData)0 O# S+ D- K0 O1 k) | {7 }! ]! O G, w: @ if (nItem >= GetItemCount()) - n9 d9 p" K2 H# w# e' ^) k {/ m+ L4 u. A, v return 0;; {, H, h' ~3 {* T: s! {. H0 M }, m$ P2 l+ _- O: U: X( Y( T CListCtrl* pThis = (CListCtrl*)this;& v1 L5 R9 l$ m( p5 R# p0 D DWORD_PTR Data = pThis->GetItemData(nItem); ; u2 [4 i3 U: }4 a ASSERT(Data); ( |1 b) T+ n) O d( w ((ItemDataEx*)Data)->SrclParam = dwData; L# [ ?4 r- a5 F) @& k return 1; 9 ^2 M2 o! m' R" _}' B4 p ^! y1 \: X( B" q int CListCtrlEx::InsertItem (const LVITEM* pItem)) h# m7 p, j: ?3 x {. r$ b/ X2 i) i7 ]% z int nReturn = pThis->InsertItem(pItem);3 [# @1 Z; \7 {9 a4 O+ f- @ ItemDataEx *pData = new ItemDataEx; 2 s0 l. \) p' m" E3 M! u // 设置默认颜色 5 b8 n3 k% o, L+ M* q7 p pData->clrItemBackColor = 0xFFFFFF;//pThis->GetTextBkColor(); : C) T- |" R' ?. e* v, `" J pData->clrItemTextColor = 0x000000;//pThis->GetTextColor(); W; c L/ ^, z8 y8 q pData->SrclParam = pItem->lParam;4 j, P' a+ n# u pThis->SetItemData(pItem->iItem,(DWORD_PTR)pData);; g9 h3 q5 J O7 f& c1 C( q return nReturn;3 |$ |6 c8 U, T8 y. F } & r- U0 f9 _4 W& }6 D0 E1 vint CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem) I+ }: R8 t9 g$ s) p {$ W2 l; q6 f! {: h0 g. F# M LVITEM Item;0 e" @6 ?+ g+ j. n memset(&Item,0,sizeof(LVITEM)); ( _9 @' R$ B- c Item.pszText = new TCHAR[strlen(lpszItem)+1];% M& Q7 T* o+ ~4 @# \/ l. ?/ Y strcpy(Item.pszText,lpszItem);; O3 ^- K2 I2 v" Z1 N Item.iItem = nItem;! S, f& f) G' a4 v. m* a5 e Item.lParam = 0; e" @/ a2 W: U/ J2 A( _( J2 d Item.mask = LVIF_TEXT |LVIF_PARAM;0 X( S' z/ p- `" |5 y% w6 r BOOL bReturn = InsertItem(&Item);6 Q( e. B l- ^+ k# Z$ \4 I/ f delete Item.pszText;9 g/ N {" a9 ^9 X% a. A2 J return bReturn; 6 Z& k" Y: Q$ c- S9 h} & B2 l+ S, E5 n) j) c- Z+ Nint CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem, int nImage)( ?% X+ f' T6 _9 x0 s/ K { ; ]& J; }+ P1 E0 E LVITEM Item;- G2 @+ f" N5 p7 b, C6 G; U memset(&Item,0,sizeof(LVITEM)); 3 K3 }& s$ s- I, | Item.pszText = new TCHAR[strlen(lpszItem)+1];9 o/ x: E. ]( r2 i! ] strcpy(Item.pszText,lpszItem);, \" U( i, w* T9 c0 K Item.iItem = nItem; + G6 q* E8 `$ z& F Item.lParam = 0; : y% U4 p6 y U, n+ A& ~ O( @- Q Item.iImage = nImage; 4 T6 k; o: b/ S8 ~/ M( r {4 I3 o Item.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE;- N3 d: ^* q+ J$ B+ r BOOL bReturn = InsertItem(&Item);' [% P* i% ]' A0 M# Q: O, k delete Item.pszText;' F0 W$ _$ D9 k; R return bReturn;: g1 ~3 c7 f/ D) ]+ |& h0 C8 X4 L& { }- l" p$ Y& x9 R6 ^- @/ y! t) c int CListCtrlEx::InsertItem (UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam)3 ?" _& h+ Y0 X: y {& Q; B2 Y& p9 ]; ]/ n LVITEM Item;4 I8 ]9 m0 k5 M4 r memset(&Item,0,sizeof(LVITEM));0 C' J3 k( i. M0 }% {9 g4 ?$ ` Item.pszText = new TCHAR[strlen(lpszItem)+1];4 ^1 b4 S4 l- u, D strcpy(Item.pszText,lpszItem);3 r5 @1 W4 k {, J Item.state = nState; # h+ e1 k! A7 l' |! Z3 e2 k Item.stateMask = nStateMask; 3 \0 t, l9 q6 ?) u1 o [ Item.iItem = nItem;2 _+ \; T) E1 j0 m4 { Item.lParam = lParam; . q: h! A" Z: c4 h& Z Item.iImage = nImage;/ B7 C S6 X$ f3 S+ w/ p- C Item.mask = nMask;/ }' K& e( }7 _. w* y BOOL bReturn = InsertItem(&Item);# f6 m) v! q, V# u5 Q6 r delete Item.pszText; ' z/ }. p3 J8 G3 ?7 t- p+ q return bReturn; * G! G! U1 H8 N5 |1 F+ M}4 |, v) u6 N, _ a' j; f BOOL CListCtrlEx:eleteItem (int nItem) {+ s: ]2 y- e% y* n1 Y( x8 L{! |; `& ]2 z* I5 O. t$ G# d. _ void* pDataEx = (void*)pThis->GetItemData(nItem);9 w* \) a6 c$ \0 { if(pDataEx != NULL); Y' D0 o( V- J$ D+ F {- X% m' l$ _# k2 t* C$ Y8 x delete pDataEx; 8 u+ a1 ^6 W6 t0 g$ I } ) v2 R9 |: I& ~% L; t. i+ ~5 r return pThis->DeleteItem(nItem); 5 I* Y0 h$ G; K& {' R- s- L} ! }, q* F3 S" ?BOOL CListCtrlEx:eleteAllItems ()/ H% [: F, e( C0 x, ]2 Z {* W) T! C- ~1 R, \ int nCount = GetItemCount();2 J; P* F5 I; t1 O for (int i = 0;i<nCount;i++) " p* i& S% |3 o7 c! Z- s5 `3 x { W7 y/ Y# C% a if(!DeleteItem(0))3 y# d! Z# R- B; x; { { p( Q# ~' U+ q, r6 Y% t- G return 0; 4 H4 t7 \6 `/ m0 v7 S } ! X9 |. _2 m5 u7 o1 J) \& J } - E9 a, G$ n0 i, p6 t return 1;9 e9 u# J3 r2 Q' V e2 u0 Q } ' M$ l- }( K- c, |- ]: ZBOOL CListCtrlEx::GetItem(LVITEM* pItem)4 ~% R% a4 q. M' \/ g# i' I! ? { ) n# n6 x4 ~# r9 s D BOOL bReturn = pThis->GetItem(pItem); ; t; R# Z8 h# n' J) \; ] if (pItem->lParam) 3 @9 @3 I/ n( y& z {) c- {1 m w' u$ C+ M pItem->lParam = ((ItemDataEx*)pItem->lParam)->SrclParam;; r {7 E4 e5 c4 E! w }

return bReturn; 2 \+ p- M' H9 N8 H}5 w( Y3 ~/ s, Q3 W' o6 r4 ? void CListCtrlEx::OnDestroy() 6 h% R% R Z# M" j3 h. O9 i{ , i5 T! |3 b# |+ b8 T* s // 关键是清除申请的内存空间* X" b. l n/ G DeleteAllItems();: g; t/ E" v, X4 K CListCtrl::OnDestroy();3 v0 ^/ ]. I. p; k }

BOOL CListCtrlEx::SwitchItem(int nItem1, int nItem2) 5 \3 X. B5 ~( |7 c" Y{0 n+ q2 r% m t6 U* U if (nItem1 == nItem2 || nItem1 >= GetItemCount() || nItem2 >= GetItemCount() || nItem1 < 0 || nItem2 < 0) " |. k# U1 ~5 J/ P { , [$ B# o9 W* z7 L; E4 u; W5 N return 0; / T# Z8 |8 y( E0 L9 B+ w# z3 W! R }" c% Y" a* w& @# x5 V LVITEM ItemData1,ItemData2;- a: C& `. d, g9 j& I/ P/ d int ItemLength1 = pThis->GetItemText(nItem1,0).GetLength();* a( b2 Q" z4 w2 q2 C2 G4 d! V8 t8 u int ItemLength2 = pThis->GetItemText(nItem2,0).GetLength();% N0 N* K, b) N ItemData1.pszText = new TCHAR[ItemLength1+1]; ; V3 y9 z' m' _% {) f1 N& e8 A ItemData2.pszText = new TCHAR[ItemLength2+1]; G# o' R* x0 Q/ c ItemData1.cchTextMax = ItemLength1+1;3 Z7 R2 ?4 X. I! K+ K6 S' c ItemData2.cchTextMax = ItemLength2+1;- C; K; X9 Z/ [: y. Z& R ItemData1.iItem = nItem1;- X; N; [ ^9 d$ l; t+ I ItemData2.iItem = nItem2; ! ^$ j! S8 h+ m* f( {6 x: y! n ItemData1.iSubItem = ItemData2.iSubItem = 0; 3 t6 r3 i, {& W D) C ItemData1.mask = ItemData2.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE|LVIF_STATE;

pThis->GetItem(&ItemData1);4 G- o Y8 r- T; s0 @1 Z2 C pThis->GetItem(&ItemData2);5 t" I4 n( A( A2 f$ ? // 交换数据 - H6 X7 \! v( S) \ b ItemData1.iItem = nItem2;- c( \6 G% U* Z, L# v9 W) o ItemData2.iItem = nItem1; & v9 Y' t0 m& `8 _ pThis->SetItem(&ItemData1); - q3 |( ~0 k* t2 K pThis->SetItem(&ItemData2);

delete ItemData1.pszText; % V6 r5 m) J" b2 p0 R' h delete ItemData2.pszText;2 V1 P1 r) U8 ~/ U- \0 a // 得到报表模式的列数 . v# b3 i0 E/ ]- w9 _& U. r2 K7 X int nColCount = pThis->GetHeaderCtrl()->GetItemCount(); . x( x! A1 I; V! I- } CString strBuffer;1 E# I8 I8 G- w7 G% s8 C: c: g8 j2 l for (int iSubItem = 1;iSubItem < nColCount;iSubItem++) 2 |# F2 H: K$ V: ~6 V0 q { / H9 a# A9 U* w strBuffer = pThis->GetItemText(nItem1,iSubItem); # o) l8 G* P, I6 O& @; T pThis->SetItemText(nItem1,iSubItem,pThis->GetItemText(nItem2,iSubItem));- ~ J# B$ W W! @9 c( ? pThis->SetItemText(nItem2,iSubItem,strBuffer);4 t- ?. q8 C$ Y" B0 z& m }* D, m3 \' b) j9 X% H: W // 把原来的选中状态也转移过去 + F1 M& C$ I# b) P& A( v4 o // 不知道为什么这个属性没有体现在LVITEM结构中…… & X8 o. ]* ~; X- ?$ ~0 w1 u, I% O/ C bool bItemSelected1 = GetItemState(nItem1,LVIS_SELECTED) == LVIS_SELECTED; 6 l: F# m$ `; z1 S( r9 K bool bItemSelected2 = GetItemState(nItem2,LVIS_SELECTED) == LVIS_SELECTED;

SetItemState(nItem1,bItemSelected2?LVIS_SELECTED:0,LVIS_SELECTED);- t/ a# a3 d# {) |5 {! _6 ^ SetItemState(nItem2,bItemSelected1?LVIS_SELECTED:0,LVIS_SELECTED); ( S4 F8 O5 E6 d$ {9 W5 L5 g, [; ? return 1; ' p. L4 c- o! W) o* |}

BOOL CListCtrlEx:reCreateWindow(CREATESTRUCT& cs)7 h& j. D; [' W7 {# _) x {4 A7 U$ |4 }$ |4 ?0 M // 本类报表模式专用 # n0 k' w1 ~$ {+ d cs.style |= LVS_REPORT ; 5 W- ?$ ?3 w+ r6 z4 i/ J. O/ l! Q // cs.dwExStyle |= LVS_EX_FULLROWSELECT;1 ~( U8 ?' B' Z" I% ?) @ return CListCtrl:reCreateWindow(cs);& l2 B8 d- ]8 ?8 ] }

+ R9 ]& p: C" K7 T COLORREF CListCtrlEx::SetHighLightColor(COLORREF clrHighLight)2 F r+ N" ^& L8 f; P! `: o3 z {/ D* c6 H' i# X3 ^1 R7 t# x COLORREF clr = GetHighLightColor(); ' p& n( }* B% Y, e9 \ m_clrHighLight = clrHighLight; 9 D+ N* @# u1 O! \. ] return clr;2 i$ n6 A6 b# S }

COLORREF CListCtrlEx::SetHighLightEdgeColor(COLORREF clrHighLightEdge) " Q3 H+ c/ G% Q+ p1 n: i{ 4 v, o$ M" H! y COLORREF clr = GetHighLightEdgeColor(); ' \- x! [5 h1 e; N7 @4 \4 h m_clrHighLightEdge = clrHighLightEdge; 2 l- H9 O8 L8 U6 c return clr; ' V, I) a- `: q0 n! {4 I}

关闭

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

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