CListCtrl, 重载DrawItem函数

<!-- [if !mso]> <style> v/:* {behavior:url(#default#VML);} o/:* {behavior:url(#default#VML);} w/:* {behavior:url(#default#VML);} .shape {behavior:url(#default#VML);} </style> <![endif]--><!-- [if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning/> <w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing> <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:SpaceForUL/> <w:BalanceSingleByteDoubleByteWidth/> <w:DoNotLeaveBackslashAlone/> <w:ULTrailSpace/> <w:DoNotExpandShiftReturn/> <w:AdjustLineHeightInTable/> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> <w:UseFELayout/> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!-- [if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles> </xml><![endif]--><!-- [if !mso]> <object classid="clsid:38481807-CA0E-42D2-BF39-B33AF135CC4D" id=ieooui> </object> <style> st1/:*{behavior:url(#ieooui) } </style> <![endif]--><!-- [if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} </style> <![endif]-->

CListCtrl, 重载DrawItem 函数

定义你自己的类,继承CListCtrl
类,然后重载DrawItem
函数,并保证在属性中有LVS_OWNERDRAWFIXED
风格.



exp:

CListCtrlExt m_ListCtrl;

m_ListCtrl.ModifyStyle(0,LVS_OWNERDRAWFIXED,0);



void CListCtrlExt::DrawItem(LPDRAWITEMSTRUCT lpDIS)

{

int nItem=lpDIS->itemID;

if(nItem == -1)

return ;

CRect rcCol = lpDIS->rcItem;

CString sText;



CDC* pDC=CDC::FromHandle(lpDIS->hDC);

int nOldDCMode=pDC->SaveDC();



LVITEM item;

item.iItem = nItem;

item.iSubItem = 0;

item.mask = LVIF_IMAGE|LVIF_STATE;

item.stateMask = 0XFFFF;

GetItem(&item);

BOOL bSelected = item.state&LVIS_SELECTED;



COLORREF color=::GetSysColor(COLOR_WINDOW);

if(bSelected )

{

pDC->SetBkColor(::GetSysColor(COLOR_HIGHLIGHT));

pDC->SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));

color=::GetSysColor(COLOR_HIGHLIGHT);

}

else

{

pDC->SetBkColor(color);

pDC->SetTextColor(::GetSysColor(COLOR_WINDOWTEXT));

}



LV_COLUMN lvc;

lvc.mask=LVCF_FMT|LVCF_WIDTH;



rcCol.right = rcCol.left;

for(int nCol=0; GetColumn(nCol,&lvc); nCol++)

{

rcCol.left = rcCol.right;

rcCol.right = rcCol.left + GetColumnWidth(nCol);

HPEN hOldPen = (HPEN)::SelectObject(lpDIS->hDC, ::CreatePen(PS_SOLID, 1, RGB(0xc0,0xc0,0xc0)));

HBRUSH hOldBrush = (HBRUSH)::SelectObject(lpDIS->hDC, ::CreateSolidBrush(color));

::Rectangle(lpDIS->hDC, rcCol.left-1, rcCol.top-1, rcCol.right, rcCol.bottom);

::DeleteObject(SelectObject(lpDIS->hDC, hOldBrush));

::DeleteObject(SelectObject(lpDIS->hDC, hOldPen));



sText=MakeShortString(pDC,GetItemText(nItem,nCol),rcCol.Width());

pDC->DrawText(sText, -1, CRect::CRect(rcCol.left+3,rcCol.top,rcCol.right,rcCol.bottom-2), DT_LEFT);

}

pDC->RestoreDC(nOldDCMode);

}

CListView实现自画功能

在实现自画功能的时候, 注意要画的时候只能对一个CRect 里面来描画, 也就是说对一个item 所占的格子进行描画, 不能同时画这个区域,DrawItem 函数当画面有什么变化时都会自动调用.

// 機能:DrawItem 関数のオーバーライドので、リストビューの自画ことができる。
void CDiameterLogWndOne::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CListCtrl& m_ListCtrl = GetListCtrl();
CRect rcItem(lpDrawItemStruct->rcItem);
CRect RectOne,RectTwo,RectThr,RectFour,RectFive,r;
CString str,sTextTwo;
int nCount = m_RecvIP.size();
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
int nSavedDC = pDC->SaveDC();

/*
CPen Pen(PS_SOLID, 1, RGB(0,0,0)), *pOldPen;
CPen Pen2(PS_SOLID, 1, RGB(255,0,0));
pOldPen = pDC->SelectObject(&Pen);
pDC->SelectStockObject(NULL_BRUSH);*/

int nItem = lpDrawItemStruct->itemID;

LVITEM item;
ZeroMemory ( &item, sizeof(LVITEM) );
item.iItem = nItem;
item.iSubItem = 0;
item.mask = LVIF_IMAGE|LVIF_STATE;
item.stateMask = 0XFFFF;

m_ListCtrl.GetItem(&item);// 得到当前的item.
BOOL bSelected = item.state&LVIS_SELECTED;// 是否被光标选中

COLORREF color=::GetSysColor(COLOR_WINDOW);

DWORD dw = m_ListCtrl.GetItemData(nItem) ;

DrawText(pDC,m_ListCtrl,nItem,nCount,dw);

DrawLine(pDC,m_ListCtrl,nItem,dw);

DrawClickColor(pDC,m_ListCtrl,nItem,bSelected);
/*
CBrush br1, br2, *pbrOld;
br1.CreateSolidBrush(RGB(0,0,0));
br2.CreateSolidBrush(RGB(255,0,0));
pbrOld = pDC->SelectObject(&br1);
*/
//
この箭頭の描画
ARROWSTRUCT a;

// この箭頭の属性
//setup arrows
a.nWidth = 10;
a.fTheta = 0.5f;
a.bFill = true;

PaintArrowColor(pDC,m_ListCtrl,a,nCount,nItem,dw);
//PaintArrow(pDC, m_ListCtrl,a,nCount);

//pOldPen = pDC->SelectObject(&Pen2);
//pbrOld = pDC->SelectObject(&br2);

//pDC->SelectObject(pOldPen);
}

// 機能:シーケンスウインドウには各信号の信号名と信号タイプを表示する。
void CDiameterLogWndOne::DrawText(CDC *pDC, CListCtrl &m_ListCtrl, int nItem, int nCount, int dw)
{
CRect RectOne,RectTwo,RectThr;
COLORREF color=::GetSysColor(COLOR_WINDOW);

// SendIPAddress
SendIPPort の描画領域計算
m_ListCtrl.GetItemRect ( nItem, RectOne, LVIR_LABEL );
CString sTextOne = m_ListCtrl.GetItemText ( nItem, 0);
//SendIPAddress
SendIPPorttext の描画
if(sTextOne.GetLength() != 0)
{
pDC->DrawText(sTextOne,RectOne,DT_END_ELLIPSIS | DT_LEFT );
}
else
{
pDC->DrawText(_T(""),RectOne,DT_END_ELLIPSIS | DT_LEFT );
}
//SendIPPort
の下縦線を描画する
if(nItem > 1)
{
pDC->MoveTo(RectOne.left + LINE_VERTLINE, RectOne.top);
pDC->LineTo(RectOne.left + LINE_VERTLINE, RectOne.bottom);
}

for(int i = 0 ; i < nCount ; i ++)
{
// RecVIPAddress
RecvIPPort の描画領域計算
m_ListCtrl.GetSubItemRect(nItem,i+2,LVIR_LABEL,RectTwo);
CString sText = m_ListCtrl.GetItemText ( nItem, i+2);
//
 RecvIPAddressRecvIPPorttext の描画
if(sText.GetLength() != 0)
{
pDC->DrawText(sText,RectTwo,DT_END_ELLIPSIS | DT_LEFT );
}
//RecvIPPort
の下縦線を描画する
if(nItem > 1)
{
pDC->MoveTo(RectTwo.left + LINE_VERTLINE, RectTwo.top);
pDC->LineTo(RectTwo.left + LINE_VERTLINE, RectTwo.bottom);
}
}
if(dw == ITEMCOLOR_CHANGE)
{
//
要求された色で色付きのアイテムを塗りつぶします
pDC->SetBkColor(RGB(255,255,255));
pDC->SetTextColor(RGB(255,0,0));
color=::GetSysColor(COLOR_HIGHLIGHT);
}
else
{
//
要求された色で色付きのアイテムを塗りつぶします
pDC->SetBkColor(color);
pDC->SetTextColor(RGB(0,0,0));
}

// CommandName
の描画領域計算
m_ListCtrl.GetSubItemRect(nItem,1,LVIR_LABEL,RectThr);
CString sTextTwo = m_ListCtrl.GetItemText ( nItem, 1);
//CommandName
の文本の描画
if(!sTextTwo.IsEmpty() && sTextTwo.GetLength() != 0)
{
pDC->DrawText(sTextTwo,RectThr,DT_END_ELLIPSIS | DT_CENTER );
}

pDC->SetBkColor(color);
pDC->SetTextColor(RGB(0,0,0));
}

// 機能:マウスにて左クリックした際に、パラメータウインドウには選択した信号名の内容を表示する。
void CDiameterLogWndOne::OnClick(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO:
この位置にコントロール通知ハンドラ用のコードを追加してください
NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>( pNMHDR );
*pResult = 0;

int nItem = static_cast<int>( pLVCD->nmcd.dwItemSpec );
CDC* pDC = CDC::FromHandle ( pLVCD->nmcd.hdc );

int flag = 0;
int nIndex = 0;
int nChange;

CRect r;

POINT pt;
int i;

GetCursorPos(&pt);
CRect rect;
CListCtrl& theCtrl = GetListCtrl();
theCtrl.GetWindowRect(rect);

CDC* pDCOne = theCtrl.GetDC();
/*
//
方法 一
int index;
int n,n0,n1;
n0=theCtrl.GetTopIndex();
n1=theCtrl.GetItemCount();
n1+=n0;
for(n=n0;n<=n1;n++)
{
//ListCtrl
中で選中Item を確認する
if(theCtrl.GetItemState(n,LVIS_SELECTED)==LVIS_SELECTED)
index=n;
}
*/
/*
//
方法 二
POSITION pos = theCtrl.GetFirstSelectedItemPosition();
int m_nIndex = theCtrl.GetNextSelectedItem(pos); // get the item be selected
*/

//
方法 三
pt.x-=rect.left;
pt.y-=rect.top;

//ListCtrl
中で選中Item を確認する
i= theCtrl.HitTest(CPoint(pt));

CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
CDiameterLogWndTwo *pView = (CDiameterLogWndTwo *)pFrame->m_wndSplitter.GetPane(1, 0);

int nCount = m_ALLCommandName.size();
for(int k = 0 ; k < nCount ; k ++)
{
if(i == (k+1) * 4)
{
for(int j = 0 ; j < m_ClickNumber.size() ; j ++)
{
if(i == m_ClickNumber[j] )
{
nIndex = i / 4 - 1;
TRACE(_T("%d/n"),nIndex);
pView->ShowFocus(nIndex,j);
flag = 1;
}
}
if(flag == 0)
{
TRACE(_T("%d/n"),k);
pView->ShowMessage(k,m_nClickCount);
m_nClickCount ++ ;
m_ClickNumber.push_back((k+1) * 4);
}
flag = 0;
//m_ClickNumber.push_back((k+1) * 4);
}
}

//
ソーティングする
for(k = 0 ; k < m_ClickNumber.size(); k ++)
{
for(int j = k ; j < m_ClickNumber.size() - 1 ; j ++)
{
if(m_ClickNumber[j] > m_ClickNumber[j + 1])
{
nChange = m_ClickNumber[j];
m_ClickNumber[j] = m_ClickNumber[j + 1];
m_ClickNumber[j + 1] = nChange;
}
}
}

}

最后在保存到html 里面, 注意<PRE></PRE> 语法是用于保存txt 里面的内容原封不动的放到html 里面, 还要注意用:<table width= "...."> 来控制画面的大小

通过处理NM_CUSTOMDRAW ,可以实现你的功能!但是NM_CUSTOMDRAWClass Wizard 中有可能看不到,不用管他,直接按照小面的方法添加处理过程即可!
1.
在消息映射表中
BEGIN_MESSAGE_MAP(CIHISSERVERView, CListView)
//{{AFX_MSG_MAP(CIHISSERVERView)
...
ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnCustomDraw)
...
//}}AFX_MSG_MAP
// Standard printing commands
END_MESSAGE_MAP()
2.
在头文件中
afx_msg void OnCustomDraw(NMHDR*, LRESULT*);
3.
cpp 文件中
void CIHISSERVERView::OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult)
{
// Contains information specific to an NM_CUSTOMDRAW
// notification message sent by a list-view control.
// mean:draw each item.set txt color,bkcolor....
NMLVCUSTOMDRAW* pLVCD = (NMLVCUSTOMDRAW*)(pNMHDR);
// Take the default processing unless we set this to something else below.
*pResult = CDRF_NEWFONT;
// First thing - check the draw stage. If it's the control's prepaint
// stage, then tell Windows we want messages for every item.
if ( pLVCD->nmcd.dwDrawStage==CDDS_PREPAINT)
{
*pResult = CDRF_NOTIFYITEMDRAW;
}
// This is the notification message for an item. We'll request
// notifications before each subitem's prepaint stage.
else if ( pLVCD->nmcd.dwDrawStage==CDDS_ITEMPREPAINT )
{
COLORREF m_crTextBk , m_clrText;
int nItem = static_cast (pLVCD->nmcd.dwItemSpec);


//
判断使ListCtrl 不同颜色现实的条件
CListCtrl &m_list = GetListCtrl();
CString str1 = m_list.GetItemText(nItem ,15);

bool bDBImplFail = false ;
if (str1 == "
信息不祥")
{
m_crTextBk = RGB(255, 255, 0) ;
m_clrText = RGB(128, 0, 128) ;
}
else
{
m_crTextBk = RGB(150, 255, 255);
m_clrText = RGB(12,26,234);
}

pLVCD->clrTextBk = m_crTextBk;
pLVCD->clrText = m_clrText;
*pResult = CDRF_DODEFAULT;
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值