下沙论坛

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

QQ登录

QQ登录

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

又是垃圾,CListCtrlEx Base CListCtrl

[复制链接]

该用户从未签到

跳转到指定楼层
1
发表于 2005-2-23 21:29:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
// ---------------------------------------------
8 c; A1 u' |" u: d3 x// ListCtrlEx.h
/ B- c% N. I+ I// ---------------------------------------------
  {. }; P( `" ^% E#pragma once/ T% D1 o% {7 l5 _% q

* N9 `& t5 X* M  m6 Q- a" ?// CListCtrlEx
' c4 C$ H  j7 w7 u! V* c8 E$ Xclass CListCtrlEx : public CListCtrl
8 r# o/ r$ P5 y; T{$ B) ]; Y9 t1 k2 G' A' i
    DECLARE_DYNAMIC(CListCtrlEx)$ Q/ |6 E9 {6 F5 t6 e/ n
public:
9 F1 C, D5 h% j" s    CListCtrlEx();
$ C, ^' U3 Q$ I* ~    virtual ~CListCtrlEx();
0 L1 E5 p" y  z: Q: [3 t! e- iprotected:0 F. o9 ?: q' U% A* B4 b2 j
    DECLARE_MESSAGE_MAP()8 G/ k8 q, v: j; S0 |
    CListCtrl* pThis;
( W2 E2 Q0 \5 [/ ~# X* ~    virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
5 }9 Q, Q. s% G  x, ^8 c. R& D) aprivate:; f0 k! l/ j  \  j, g
    struct ItemDataEx {9 G' S+ R8 ~8 h# |+ |
        LPARAM SrclParam;
% E: }8 G" @/ T3 c% `        COLORREF clrItemTextColor;
, F; g$ }' I- k' p  N# ^        COLORREF clrItemBackColor;
1 T6 }6 Z. h! R; D  W+ [    };
3 T& N& H3 v+ F" N2 w# J- B9 l    COLORREF m_clrHighLight;9 r! ]" b1 N8 \. ]
    COLORREF m_clrHighLightEdge;
. }' k2 a. ]1 ~5 l$ l+ u# Hpublic:
* W0 E7 }" _$ ~: z1 q2 R    // 新增方法
& m  Z3 K3 J' W5 C- G    COLORREF SetItemTextColor(int nItem,COLORREF clrText);
/ ?( K8 x$ T5 z5 F0 T; @    COLORREF GetItemTextColor(int nItem) const;
# G5 e- `  o5 i) X  B    COLORREF SetItemBackColor(int nItem, COLORREF clrBack);
4 b7 |, O9 v1 b- Z! ^8 T. E    COLORREF GetItemBackColor(int nItem) const;
" K8 R/ a  h. D) D    COLORREF SetHighLightColor(COLORREF clrHighLight);
; a2 N0 s+ i  o    COLORREF SetHighLightEdgeColor(COLORREF clrHighLightEdge);' x" A& W4 _9 E% E8 E) E) X
    COLORREF GetHighLightColor(void) const;
- z; Z2 `6 A( Q; U9 j    COLORREF GetHighLightEdgeColor(void) const;
/ K6 B% y9 Z! ?) I    int GetItemImage(int nItem);& R1 R6 \7 Q: |5 @$ o# r3 y; z6 H
    BOOL SwitchItem(int nItem1, int nItem2);; z) i6 S) E; M* L2 w
    BOOL MoveItemUp(int nItem);+ B9 [/ j3 |$ ], Z$ t9 h; W
    BOOL MoveItemDown(int nItem);* b8 s  h6 z, L3 P! t
    // 重写原来的方法
  y7 S" M! U5 C7 M0 _    DWORD_PTR GetItemData (int nItem) const;2 ?; h6 R! E' h, c5 [
    BOOL SetItemData (int nItem, DWORD_PTR dwData);
3 o1 w6 s5 M- D4 T    int InsertItem (const LVITEM* pItem);
/ O0 R/ P  {7 \) f# s    int InsertItem (int nItem, LPCTSTR lpszItem);
' Y, @! S, B, {- E    int InsertItem (int nItem, LPCTSTR lpszItem, int nImage);" J- Z$ p3 B( c5 [, u1 w1 a
    int InsertItem (UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam);
% w+ }7 l7 q' U7 i5 p& B9 E    BOOL DeleteItem (int nItem);" [7 M: ^# `) F( j& u4 r. J+ z1 I6 H  _" [
    BOOL DeleteAllItems ();  v2 S$ A' }$ \
    BOOL GetItem(LVITEM* pItem);
& X' ~$ j5 e* t  M+ @$ y    afx_msg void OnDestroy();1 f1 J, U+ ~( O5 s4 T8 N; \
    afx_msg void OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult);
' d7 a% o& F  a% r};
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 顶 踩

该用户从未签到

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

// --------------------------------------------- % u7 [' L. c, x* X2 B// ListCtrlEx.cpp * V3 v+ d" j7 t' Z// ---------------------------------------------4 c8 F6 W' e3 s/ Q" M // ListCtrlEx.cpp : 实现文件 ; ^/ ?; J2 O0 o! W; a2 u//

#include "stdafx.h" ) S2 S$ r. p) T9 }#include "CreateNewClass.h" + X% n* R' Y0 A#include "ListCtrlEx.h" # C3 E) H A* K8 O: J3 [6 h5 ?) i#include ".\listctrlex.h"

O$ Z( v2 K- l5 N# g/ O// CListCtrlEx

IMPLEMENT_DYNAMIC(CListCtrlEx, CListCtrl) ; L) L, d0 ^0 D4 n6 }# wCListCtrlEx::CListCtrlEx() 9 G( f; T. ?* _0 e0 u! P3 a8 B* r/ A{ 4 x" z/ t' @) I // 用于调用基本的方法 0 s5 ]+ H& \' }& j; }1 ^ pThis = (CListCtrl*)this; & j+ N2 B) ` l2 K) g0 T* n" { // 默认的选中框颜色 * H# n& t: b1 }* g7 p7 e m_clrHighLight = 0xF1DACC; 0 N5 N( b* C# Z! E- I m_clrHighLightEdge = 0xC56A31;9 o6 _( y( |8 o# Z* { }

CListCtrlEx::~CListCtrlEx() 2 \' O+ Y5 |- y/ I9 Y{

}

- t. f9 \" ]7 r0 Z; t BEGIN_MESSAGE_MAP(CListCtrlEx, CListCtrl) ' [) _, [, }7 P& H" X$ w ON_WM_DESTROY()& P! H( H- g2 t2 ? ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnNMCustomdraw) # m" U1 f5 {3 U, vEND_MESSAGE_MAP()

// CListCtrlEx 消息处理程序

" o! X& E: G) u* e& Q1 n) ?2 |COLORREF CListCtrlEx::SetItemTextColor(int nItem,COLORREF clrText) $ N7 V; Y4 Z/ P* {1 Q+ o5 [{ 4 e: y" _5 C! I1 a+ f; x0 N COLORREF clr = GetItemTextColor(nItem);$ q) V( g& `- y/ q [! _( I8 l ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem); ) {2 D" S; I" f$ \: x: |" s pData->clrItemTextColor = clrText;$ k- Z& w5 m* c6 E4 r$ R return clr;+ @3 T$ |, C! m6 L9 o }

COLORREF CListCtrlEx::GetItemTextColor(int nItem) const % \: P4 N: y# I2 R3 U; ]2 W{ ) a, t5 A6 H! ?! d) P) M. x2 I+ [ n ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem); , t2 f" `& Z U return pData->clrItemTextColor; 9 W( o% _5 c- j}

COLORREF CListCtrlEx::SetItemBackColor(int nItem, COLORREF clrBack) % c# ^) b6 m* O' W) e: ?: ?) K{- W, z& ]7 ^. @/ t# H; c COLORREF clr = GetItemBackColor(nItem); 2 _% b) n( J) B1 c5 j3 S# X ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem); + Q" x5 ]/ B4 u1 Q' O0 _) e pData->clrItemBackColor = clrBack; ( i: e R6 b' s9 R) `8 f0 l7 J return clr; ' J6 _) I2 s) |5 t0 D5 w}

COLORREF CListCtrlEx::GetItemBackColor(int nItem) const- i# x4 x9 c- e! O; k; s { 4 k. O& {) v8 [2 v ItemDataEx *pData = (ItemDataEx*)pThis->GetItemData(nItem);( A( ~2 L. u2 {/ ?( N7 `' F return pData->clrItemBackColor;% S' t1 l& d8 n4 U# W }- O6 H& z4 g: G1 u B' ^ DWORD_PTR CListCtrlEx::GetItemData (int nItem) const2 x* l; A& W7 q: U { , _8 ~! [; }! f4 t% L1 R6 |/ s5 z if (nItem >= GetItemCount()) Q: }% c0 m2 ]" r' z { $ b- A' C$ u* q! ~' j2 _* t4 a return 0; " a5 }; f% N) _7 f5 @ } 7 o0 j: ?' f. ?# Q CListCtrl* pThis = (CListCtrl*)this; 8 X% ]2 {: u8 s8 y$ O DWORD_PTR Data = pThis->GetItemData(nItem); " j# a Y# {) t% D return ((ItemDataEx*)Data)->SrclParam; 2 _. e! v: j( ^5 u! g$ V}% `2 W: V+ s) q BOOL CListCtrlEx::SetItemData (int nItem, DWORD_PTR dwData)! b7 _& ` ]2 U& x4 b; [2 i {2 N( ]2 n+ S6 U: t C) o1 p$ j: B if (nItem >= GetItemCount())! W" I1 m' T0 ` {4 A: ]3 S! F3 d0 s return 0; " W8 l+ q5 J: o } , f. Y1 j2 L4 O2 O' a7 @, B CListCtrl* pThis = (CListCtrl*)this; ! l2 [1 O+ ?- Z) v$ Y DWORD_PTR Data = pThis->GetItemData(nItem);1 C" n+ q- S# U/ k. o; @ ASSERT(Data); ' I% ]- o% Q Z/ j8 d ((ItemDataEx*)Data)->SrclParam = dwData;* A3 k& {7 r. R V9 _' e return 1; 9 _5 R0 Y9 v$ c* Y}! ?) f9 w! l' u# X int CListCtrlEx::InsertItem (const LVITEM* pItem) " _7 A$ d' r2 z& u u) O+ Z' d{ , o* j/ V3 b9 |. E z/ l" } int nReturn = pThis->InsertItem(pItem); 6 B/ L2 S2 o! n6 C) D ItemDataEx *pData = new ItemDataEx;9 I+ A' Z" h: G( H: O // 设置默认颜色7 m2 T' \0 ^$ B1 u' | pData->clrItemBackColor = 0xFFFFFF;//pThis->GetTextBkColor();% c, j/ B% G* [6 V: w. ]4 K pData->clrItemTextColor = 0x000000;//pThis->GetTextColor();1 g9 e+ U" n' I pData->SrclParam = pItem->lParam; ; |1 M3 M- n0 h& x4 G( ]$ a pThis->SetItemData(pItem->iItem,(DWORD_PTR)pData);) h3 p" k' m; C* N return nReturn;! P. d7 _, d; Z2 H, P& V# x% l } 0 Y: x$ d; f9 y5 U9 ^, ~7 c3 E) Fint CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem). J4 ]2 B2 |8 e* c {. o2 D" f5 c4 E LVITEM Item;; d% ~1 l3 \8 C memset(&Item,0,sizeof(LVITEM));' x, C" n |; o Item.pszText = new TCHAR[strlen(lpszItem)+1]; & X+ H% Z! _4 E; k6 h8 W1 s6 e" E1 V strcpy(Item.pszText,lpszItem);$ w b# ?" h0 V Item.iItem = nItem;; N: y2 G; w. U6 k7 o4 `5 i% F Item.lParam = 0;7 N6 q/ H5 B" @ Item.mask = LVIF_TEXT |LVIF_PARAM;! H6 q- Y `: b5 p/ D. F' W BOOL bReturn = InsertItem(&Item); ! q& e% M% Q" | delete Item.pszText;! R8 `3 K0 n1 B% `) x$ ~0 O return bReturn;; u4 |! _5 H0 N! L" i2 Z7 [ }: h+ f6 {1 @6 w. G+ P# v3 x. k int CListCtrlEx::InsertItem (int nItem, LPCTSTR lpszItem, int nImage). k7 L V& h) J3 ~: v! m { 7 L0 p- ]1 T2 l6 T) Q. `( P LVITEM Item; m. x) }; M! Y4 t+ U4 d memset(&Item,0,sizeof(LVITEM)); ; p' I2 e5 j" Z Item.pszText = new TCHAR[strlen(lpszItem)+1]; 9 M) D/ U" ~# s+ `1 S0 ~ H strcpy(Item.pszText,lpszItem); 0 |/ I5 a, D7 M+ |# ^ X) ]& v5 k, n Item.iItem = nItem; - j, @3 H t+ D# o) H% R Item.lParam = 0; j# k# R$ O0 M0 `0 p Item.iImage = nImage; ( r3 x0 w7 \" x: A7 C) @: E Item.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE;6 @ S7 x2 Z. f# N, e BOOL bReturn = InsertItem(&Item); e8 c: y: b% Z6 h3 D delete Item.pszText; 6 z8 e6 X' s' i% q# f2 A5 A! W return bReturn; " v; }! P5 w m/ Z} + H5 C0 ]6 i7 s: `/ dint CListCtrlEx::InsertItem (UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam) & I* N8 R: h; B# G{ ; _( N q ~. {- R LVITEM Item;* p% M& o) h' B7 t5 _ X U memset(&Item,0,sizeof(LVITEM)); + y# u9 B* Y& F Item.pszText = new TCHAR[strlen(lpszItem)+1];/ s3 z( q r* p) e. m4 ?/ ` strcpy(Item.pszText,lpszItem);5 G% ~; Y: X. m7 B: p) C, V Item.state = nState;# X7 v9 m. `+ l* @ g Item.stateMask = nStateMask;3 g0 S' Q8 Z2 v4 S9 u Item.iItem = nItem; + D: r3 p% S7 h4 n! s& `, Z7 \ Item.lParam = lParam;5 T3 i+ Y9 y8 C: c# d3 a1 l" A. s" |" C Item.iImage = nImage; 0 P. ^: e7 t0 U7 G: T Item.mask = nMask; ) x2 k* H8 w" ~9 X" D7 V: w; o BOOL bReturn = InsertItem(&Item);4 H, m. J1 l) [& A: [) J2 f delete Item.pszText;7 {3 S Q# I/ k, b return bReturn;3 d: a: M& u7 g0 r } 4 E* g/ N& X) R" [+ K/ A! ]BOOL CListCtrlEx:eleteItem (int nItem)+ F# { D" x& ?' A5 C6 ^0 z {; F8 E0 c' d. f void* pDataEx = (void*)pThis->GetItemData(nItem); 6 Q4 W6 a3 _+ v$ ` if(pDataEx != NULL)5 n3 t5 y2 ]% Y' g( a7 B9 U2 q/ I! A {( y) ]3 Y# y% t. L0 o delete pDataEx;$ h& T1 G4 N% ~' x7 z* D4 e }* s% G9 g+ D1 A" @; @# T return pThis->DeleteItem(nItem);2 C1 g6 H% O$ @/ T/ S+ b } & F# G9 H& c" k7 F/ Q; pBOOL CListCtrlEx:eleteAllItems () 6 g8 o; [& @( G1 c* g; a5 b. J{ T1 D; |8 s: l* f int nCount = GetItemCount();- O6 ` b3 s4 _, \9 } for (int i = 0;i<nCount;i++). F3 k1 u' Y( l& ~7 d { 2 t. S* y' Y5 s' b, i! N7 Q if(!DeleteItem(0))9 {" G- R/ a. e F# b- @ {3 G* V( K, F7 Y7 h* J* i return 0;4 x' U3 h, N/ V ~# a# p* H) C# z } " ] ~3 ] j0 O# G7 R }5 a* e& s4 e3 f. Y+ @' D8 b; R3 C4 `$ C return 1;9 M- U, R& r- X- E) x; Y4 ^2 O% Y }" L5 D- Y' T3 e9 P6 M7 L BOOL CListCtrlEx::GetItem(LVITEM* pItem)6 l/ R8 ?! ?* @- Z9 s8 s {9 C4 K% K# l' n5 ] BOOL bReturn = pThis->GetItem(pItem);- x+ N: h) j" P! ]" n if (pItem->lParam) * V- G+ _& T- ]% x/ X { 5 ~ J; n; ~8 U( e* i' q pItem->lParam = ((ItemDataEx*)pItem->lParam)->SrclParam; 2 y4 k. J" c( p7 D& H3 c* F }

return bReturn; 8 Q% ^9 l4 A2 U& _; @! J" e# b6 Z}3 h- i* P+ k1 L: b3 F; d! z void CListCtrlEx::OnDestroy()" ]- @& ^4 B5 U6 b0 I { # e+ u& z0 e; v0 S, ~/ g // 关键是清除申请的内存空间 2 |& `% _% V% I+ |- |% \ DeleteAllItems();3 n# {, f3 w, _% s, a7 f% ~7 _ CListCtrl::OnDestroy(); ( P( f# E h. g, @; O% T}

BOOL CListCtrlEx::SwitchItem(int nItem1, int nItem2)' X( R; P+ m2 l `3 E {9 X* P& u$ w' |# w1 @. m* j if (nItem1 == nItem2 || nItem1 >= GetItemCount() || nItem2 >= GetItemCount() || nItem1 < 0 || nItem2 < 0) 6 w& ~7 a% P( l ~1 ^ { 4 M6 D- v' u3 K' Y, k& g return 0; * E1 Q+ a% F7 d4 ?& E& F4 V$ U } 1 z; D& t% k/ m LVITEM ItemData1,ItemData2; / E5 T F% ]& L8 X0 D int ItemLength1 = pThis->GetItemText(nItem1,0).GetLength(); % R) k% q( l& q( h/ C int ItemLength2 = pThis->GetItemText(nItem2,0).GetLength(); ; e. t# \- O( ]. n ItemData1.pszText = new TCHAR[ItemLength1+1];# |/ x/ ~ k- J: r' o& V5 o4 C2 g ItemData2.pszText = new TCHAR[ItemLength2+1]; Z& d3 D% ?$ e1 H ItemData1.cchTextMax = ItemLength1+1;: E' ]" H: \# s: G( V) F ItemData2.cchTextMax = ItemLength2+1;! q4 B2 L* |6 @) M) e6 I; J6 |- A ItemData1.iItem = nItem1; ' |8 i/ k& u4 {% y0 k ItemData2.iItem = nItem2;/ P. d O1 N3 a1 r- P* L& ] ItemData1.iSubItem = ItemData2.iSubItem = 0; 7 n& g( N6 U+ ~3 K2 W: R. l/ X ItemData1.mask = ItemData2.mask = LVIF_TEXT |LVIF_PARAM|LVIF_IMAGE|LVIF_STATE;

pThis->GetItem(&ItemData1); 3 \1 x j' o$ ^; j% Y0 g, o# I1 Z pThis->GetItem(&ItemData2); 4 s( p: K% b+ P // 交换数据* M. G/ Z" d+ l7 d- M ItemData1.iItem = nItem2;/ ^2 ^5 f' T' s ItemData2.iItem = nItem1; 9 t+ O5 L3 Q$ S% z: X# \* t* j$ c pThis->SetItem(&ItemData1);/ a6 n, B$ p e! G4 V0 V pThis->SetItem(&ItemData2);

delete ItemData1.pszText;7 G. O5 c5 a: Z9 u' q delete ItemData2.pszText; 7 L3 w2 O& C% ^, u$ N, p- }) Q' E4 i) g // 得到报表模式的列数: N: o1 j) @1 k \: o7 r5 P int nColCount = pThis->GetHeaderCtrl()->GetItemCount(); , i: Y) O. l6 ~ A5 e) ]4 P CString strBuffer;+ j6 X9 N# r5 P4 q9 S% D; B" ? for (int iSubItem = 1;iSubItem < nColCount;iSubItem++) / Q2 m4 ]/ a# {+ C, F5 c$ h { % I: f8 k: d: A* f9 P strBuffer = pThis->GetItemText(nItem1,iSubItem); " _3 C6 M5 u0 V' g, w0 K7 x# `0 I pThis->SetItemText(nItem1,iSubItem,pThis->GetItemText(nItem2,iSubItem));! Z# ^! G# G) d7 F5 a0 D) G pThis->SetItemText(nItem2,iSubItem,strBuffer); * z3 t8 a! I$ a* e }7 ]2 E- n; Q/ z0 c // 把原来的选中状态也转移过去 ( {2 \5 D+ ~* b // 不知道为什么这个属性没有体现在LVITEM结构中……) n0 y- t% |5 A z0 B bool bItemSelected1 = GetItemState(nItem1,LVIS_SELECTED) == LVIS_SELECTED; " E) `3 c$ J8 e. n bool bItemSelected2 = GetItemState(nItem2,LVIS_SELECTED) == LVIS_SELECTED;

SetItemState(nItem1,bItemSelected2?LVIS_SELECTED:0,LVIS_SELECTED);3 S' Y7 y; X q' b. o SetItemState(nItem2,bItemSelected1?LVIS_SELECTED:0,LVIS_SELECTED); 2 l9 K ^& w7 [& k( m' n return 1;* E( A6 f* t1 J6 @ }

BOOL CListCtrlEx:reCreateWindow(CREATESTRUCT& cs) # J5 s) Y' H) s6 \$ a2 r T{2 _6 b, B m- j3 d: u // 本类报表模式专用2 `0 B" W$ U4 U+ ^ W cs.style |= LVS_REPORT ;; e# |( i) _ L& S9 B1 a // cs.dwExStyle |= LVS_EX_FULLROWSELECT; , n8 h+ V/ T; E; S4 Z1 y4 J return CListCtrl:reCreateWindow(cs);- \2 e3 o2 E5 V' v0 F/ r a) V. Z+ \ }

& U. |7 g; k! E- QCOLORREF CListCtrlEx::SetHighLightColor(COLORREF clrHighLight)1 B% e" @. P" O3 v0 v { ( j8 g7 ~6 [: o& C COLORREF clr = GetHighLightColor();- t( `3 ?2 R2 Z m_clrHighLight = clrHighLight;+ k0 F+ B% w: p$ T1 S3 G; V; s return clr;9 l" |; W: z* D8 w4 W" J v$ \9 T9 \ }

COLORREF CListCtrlEx::SetHighLightEdgeColor(COLORREF clrHighLightEdge)3 i6 k, Z/ O" n! M; O& P {) z- k/ R3 e4 x* u2 O3 P6 Q" Q COLORREF clr = GetHighLightEdgeColor(); 0 T7 ^* w! F5 c m_clrHighLightEdge = clrHighLightEdge; ) d; ]7 s2 A" B- V& l5 a( }% k$ U return clr;9 t8 l7 d9 y7 f3 f- G) m }

该用户从未签到

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

COLORREF CListCtrlEx::GetHighLightColor(void) const 7 I: O" R& T8 ^* u* V1 ^/ Z& C{ - m( J& z* K6 z4 G1 r9 }/ ?# p1 ?4 U v return m_clrHighLight; - Q; @7 P5 f2 H7 `}

COLORREF CListCtrlEx::GetHighLightEdgeColor(void) const , \ Z! @* f6 v* a" x: c k$ X{7 `( d! x/ E- P/ k8 {3 f return m_clrHighLightEdge;: S: L/ A6 W8 j& W+ ^- h; | }

int CListCtrlEx::GetItemImage(int nItem) 8 ?, U. y* K* k% i# m" j, x4 V0 x{ z" Q1 v' \ a% C- b3 V LVITEM ItemData; D5 t5 A3 ~; d ItemData.iItem = nItem;3 S$ ~$ Y. S( W: s3 X* x' P% X _8 D( k ItemData.iSubItem = 0; [! T: K2 G3 q$ f4 G8 e ItemData.mask = LVIF_IMAGE;& U5 [* j% r/ s pThis->GetItem(&ItemData); 1 p" \$ o/ [" H2 D$ k return ItemData.iImage;' t- e S* W1 b. S0 F+ l }

BOOL CListCtrlEx::MoveItemUp(int nItem)6 A Q. r z2 w+ ]6 K { 0 R4 w6 W6 s! h if (nItem < 1 || nItem >= GetItemCount()) 8 {6 g4 U4 ~. s$ y! }/ I; u& h {1 l' ~- m5 X" \3 p" y1 M* b$ [) X return 0;& c' [- w. L. p0 V# Y& R } 0 ^: I& `8 c, V9 T8 P/ H$ m8 H return SwitchItem(nItem,nItem - 1);; a0 \; s, L1 @- X) [! `+ t }

BOOL CListCtrlEx::MoveItemDown(int nItem)6 {/ i* X% ^+ ^, G. Y { 3 P$ p$ v7 ~ u6 t if (nItem < 0 || nItem >= GetItemCount() - 1)5 D2 Z$ R6 i8 |0 Q; Y0 f; M5 g0 T {+ ]/ v7 U1 l- e2 ]! O( U return 0;+ X1 |+ A! L R3 F. f }4 Q' @ R2 F, ?6 d+ F/ o5 o return SwitchItem(nItem,nItem + 1);4 d3 R9 S, [( `2 k2 d }

void CListCtrlEx::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult) . R: i$ U9 x( w{, A b+ q) U% X) \& | NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR);

*pResult = CDRF_DODEFAULT;

if (pLVCD->nmcd.dwDrawStage == CDDS_PREPAINT)! v" m' i8 c; H- R1 r' J: | { 4 I' Y. K r$ I! M: L *pResult = CDRF_NOTIFYITEMDRAW;. _' R- z( x$ ~ @% J }7 n( i6 |$ `. b, R* J! X5 a1 H else if (pLVCD->nmcd.dwDrawStage == CDDS_ITEMPREPAINT) ( r: z9 {) u; J. S% Q0 ]) [1 M { ; h) W k; Q4 M3 a+ S *pResult = CDRF_NOTIFYSUBITEMDRAW; ; H5 X) Y9 @$ j. J$ c }" I: o& ^4 G4 h' N else if (pLVCD->nmcd.dwDrawStage == (CDDS_ITEMPREPAINT | CDDS_SUBITEM)) 9 I0 ~; y5 |& y) m {

int nItem = static_cast<int> (pLVCD->nmcd.dwItemSpec);" ]8 j1 J- H9 Z$ a* p int nSubItem = pLVCD->iSubItem; / b% b. Z9 F$ b) b, ~4 q) ?2 h/ s if (nSubItem != 0)3 F* d- P$ Q! O2 s6 o) T { : P5 u# d: d4 ~ Z' _# e5 L // 全部绘制过程在nSubItem == 0 时完成 " r% q" \$ i& o! G2 v- \% a$ A) y // 分布完成会有小问题,我无法解决 # u# G$ H& p2 ^& r *pResult = CDRF_SKIPDEFAULT;4 j, J5 H( Z' T2 S- w$ `7 e return; 2 k0 M- h) i# a; u& z } - m4 q( \" p0 q3 A6 Z/ x, U CDC *pDC = CDC::FromHandle(pLVCD->nmcd.hdc); , v# ~2 Z& o# `( B int nColCount = pThis->GetHeaderCtrl()->GetItemCount(); ( D$ E! k5 S' Y- g9 |5 z CRect rectSubItem;4 O- B# z6 W! T4 u+ ?6 ]- b2 G CString strItemText; # R6 d$ p4 v; v6 P pDC->SetTextColor(((ItemDataEx*)pThis->GetItemData(nItem))->clrItemTextColor); 3 u7 L) x" Z) J( }) y$ y pDC->SetBkMode(TRANSPARENT); & F3 X2 m* l* J! S( b( X7 O // 相当于橡皮% g* c$ o2 c* j L: \ CBrush brEraser(GetItemBackColor(nItem));

// int nFirstWidth = GetColumnWidth(0);

" L* K) V5 w8 j# v& E, z for (int i = 0;i < nColCount;i++) # J' I1 t3 z) z7 N1 C {3 v) H+ }$ ^) [8 K& @7 N& _% d3 N strItemText = GetItemText(nItem,i);

if (i == 0)% S1 m& \$ ] H0 _2 J6 Q3 [ { & m# ~6 V! Q- H; M pThis->GetSubItemRect(nItem,i,LVIR_BOUNDS,rectSubItem); 1 q8 G" s6 b) f% i3 W // 擦掉原来的图形 & Y7 y" s( P" N6 K pDC->FillRect(rectSubItem,&brEraser); . [# Q) J* c6 M& D3 g if (GetExtendedStyle() & LVS_EX_CHECKBOXES)" o6 m2 d4 d7 }* @6 N( X { # ?1 d5 U' H' s C) Z% e CRect rectCheckBox; : o2 y% L% o# z& ~* y GetSubItemRect(nItem,0,LVIR_ICON,rectCheckBox); 7 D8 C4 K0 m* N5 D$ Q0 r" d CRect::SwapLeftRight(rectCheckBox);5 b- R. a1 ^1 B; ^ rectCheckBox.left = rectCheckBox.right - rectCheckBox.Height();

rectCheckBox -= CRect(2,2,2,2); / v# J* c* M5 G' x pDC->Draw3dRect(rectCheckBox,0x81521D,0x81521D); 4 u2 Q, C- y5 z' f+ Y! Y9 N) j //rectCheckBox += CRect(3,3,3,3);4 A# T- s' l& A4 o4 Z if (GetCheck(nItem))! P E3 N! Y( F2 v' }: w! U {) i4 r/ u6 `. m# c3 P/ m. @$ X // 绘制一个CHECKBOX ) I2 g4 T% C; p1 w) J CPen penCheck(PS_SOLID, 1, 0x0021A221);2 Z0 [) P( J9 P CPen *pOldpen = pDC->SelectObject(&penCheck); 5 [- X1 d0 n* X) o! y for (int j = 3; j < 10; j++)8 s8 A, }$ i7 V3 \9 ]6 _& v" I$ f { $ |# {, B8 G% p% G8 i pDC->MoveTo(rectCheckBox.left + j, rectCheckBox.top + rectCheckBox.Height() /2 - 3 + ((j < 6) ? j - 1 : (9 - j))); ( J9 I, v7 w" F9 k# R6 A( g5 c pDC->LineTo(rectCheckBox.left + j, rectCheckBox.top + rectCheckBox.Height() /2 - 3 + ((j < 6) ? j + 2 : (12 - j))); : D( { R8 N2 f' V& _ } ; ~, v: N5 p- F( E8 k: i/ ? 9 o% w+ u( c1 ]2 z pDC->SelectObject(pOldpen); 8 W* \5 G' M2 j/ b( `& n, X6 S } 4 L( v/ s# p0 k/ Q, ^' S5 x& i- u } 7 w W2 A' F" q3 z- j( k l // 根据需要画选中框( H9 v! ^* l% I' B) W. j1 [ if ( (GetFocus() == this || GetStyle() & LVS_SHOWSELALWAYS)&&(GetItemState(nItem,LVIS_SELECTED) == LVIS_SELECTED)) q% V4 d6 n$ ? {" S( C% W D) K. f9 N if (GetExtendedStyle() & LVS_EX_CHECKBOXES) d( c# D N0 R {// 存在检查框风格的时候需要对左坐标进行修正7 A* W- U, }+ f. C1 w, {" ] CRect rectLeft; ( b) [* r" u) G% q6 | v4 L GetItemRect(nItem,rectLeft,LVIR_ICON);1 M5 f( d5 G- W& A0 B0 a; j } rectSubItem.left = rectLeft.left; ' C G% B/ [! Y( U3 q8 ]7 f9 z; S4 k } 6 R2 e; {$ |+ Y6 t if (!(GetExtendedStyle() & LVS_EX_FULLROWSELECT)) , B/ m: C' K e {// 非全行选择时右坐标需要修正5 R, [; z( I2 ^8 H) C+ X B0 p/ J8 q CRect rectRight;, {* ~- A5 e5 m9 W, f# `6 E* L. r GetSubItemRect(nItem,nSubItem,LVIR_LABEL,rectRight);# \" A5 w' \0 E& `5 u. i7 t rectSubItem.right = rectRight.right; 8 p9 t1 F% a" D0 U' Y; d }- b5 ^2 j8 \, ?' K; ?! ]9 J CBrush brHighLight; & n2 I* A$ W# {# ]3 R# P6 Q CPen penHighLightEdge;* F+ @9 o( Y" @. G6 I if (GetFocus() == this) / K' p7 A0 M; }5 @% Y7 J- ~/ [ { $ v; b7 k- c5 Q2 W4 m& B5 g brHighLight.CreateSolidBrush(m_clrHighLight);! q0 I0 t7 Y( i$ o penHighLightEdge.CreatePen(PS_SOLID,1,m_clrHighLightEdge);) V' ?( d6 J/ M } 3 ^1 I: @& [* ?' V9 A2 ^- E/ ? else- G& p/ W( ], f) P% Q { + O4 ^/ ?+ t4 n // 不是焦点时,颜色70%不透明度 ' _' s3 T9 J# s4 y% y' R COLORREF clrBack = GetItemBackColor(nItem); 8 X" h; [& b( \ WORD red = GetRValue(m_clrHighLight)*0.7 + GetRValue(clrBack)*0.3;2 g/ N- U9 e- J" |, v, p7 w WORD green = GetGValue(m_clrHighLight)*0.7 + GetGValue(clrBack)*0.3;* D$ T& S+ w* i3 g& e6 ? WORD blue = GetBValue(m_clrHighLight)*0.7 + GetBValue(clrBack)*0.3;4 _) q5 d3 t1 ^; x% [2 a brHighLight.CreateSolidBrush(RGB(red,green,blue));- l/ l* }) D) L z, s' F red = GetRValue(m_clrHighLightEdge)*0.7 + GetRValue(clrBack)*0.3;2 {: B: N2 Q4 z9 E2 z0 I9 z green = GetGValue(m_clrHighLightEdge)*0.7 + GetGValue(clrBack)*0.3; ! e( g" W. g9 o0 A6 n blue = GetBValue(m_clrHighLightEdge)*0.7 + GetBValue(clrBack)*0.3; 4 o+ d2 k* v" }5 ]: I penHighLightEdge.CreatePen(PS_SOLID,1,RGB(red,green,blue)); 5 O% I( M8 P1 ]3 P9 _- V } 5 b# ^) h6 i% ^8 X8 o3 j1 { CBrush *pbrOld = pDC->SelectObject(&brHighLight); ' U8 r; E7 Z, P, r CPen *ppenHighLightEdge = pDC->SelectObject(&penHighLightEdge); * G, _; J# A4 i" e; x pDC->Rectangle(rectSubItem); 6 ~" m. Z! y9 i: ?0 M+ _7 x pDC->SelectObject(ppenHighLightEdge); ' v7 s* |/ G/ N1 O5 D$ v pDC->SelectObject(pbrOld); 0 A+ K4 |. o4 H a! N0 x$ B }% F4 S1 X) q4 m# {9 x } % ^& F6 e# c6 H pThis->GetSubItemRect(nItem,i,LVIR_LABEL,rectSubItem); " T8 B6 C- I; R) Q* d& Q UINT uFormat = DT_SINGLELINE|DT_VCENTER|DT_END_ELLIPSIS; # n' B) D; V/ h2 ?8 g HDITEM hdi; 8 m; C$ Q5 b0 \$ Z `& H! ]( i) ?+ P1 Z hdi.mask = HDI_FORMAT;7 k* X6 a5 M0 U: x s; _- o) P8 ? pThis->GetHeaderCtrl()->GetItem(i,&hdi); , v( K1 E; M7 F; B; i5 w if (hdi.fmt & LVCFMT_RIGHT)$ r2 T1 C, ~7 H- b { $ C8 _5 I% ^0 B7 W$ y5 u% x uFormat |= DT_RIGHT;( c4 D* _6 H6 t; x$ J }9 S7 I4 w7 B! E7 H" N3 _8 ~ else if (hdi.fmt & LVCFMT_CENTER) ' `! n* T9 u2 b {0 i9 h% n( r: e* \6 u uFormat |= DT_CENTER; . C Z6 W% w e }1 o* b7 B5 ]6 V, E: J) Q3 k pDC->DrawText(strItemText,rectSubItem,uFormat);

CImageList *pImageList = pThis->GetImageList(LVSIL_SMALL);* l2 s4 v Z& h if (pImageList)& S4 F% @. i4 y) } { , h1 Q% M; b4 Z( J R CRect rectIcon;& l, ]( M& F+ m$ I4 M5 h GetItemRect(nItem,rectIcon,LVIR_ICON);7 u) c' b' A: C+ X; A) |9 i* L3 I pDC->DrawIcon(rectIcon.left,rectIcon.top,pImageList->ExtractIcon(GetItemImage(nItem)));) D( z& U: |2 M" P! ~! B }- ~; F) p6 y' _ } 5 O: i& W. e/ Y *pResult = CDRF_SKIPDEFAULT;' ?' j j0 Z$ d3 i0 a9 W }

}

该用户从未签到

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

本版积分规则

关闭

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

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