void CListCtrlPro::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
//得到dc
LPDRAWITEMSTRUCT lpDIS = lpDrawItemStruct;
CDC* pDC = CDC::FromHandle(lpDIS->hDC);
LVITEM lvi = {0};
lvi.mask = LVIF_STATE;//|LVIF_IMAGE;
lvi.stateMask = LVIS_FOCUSED | LVIS_SELECTED ;
lvi.iItem = lpDIS->itemID;
BOOL bGet = GetItem(&lvi);
BOOL bHighlight =((lvi.state & LVIS_DROPHILITED)||
( (lvi.state & LVIS_SELECTED) &&
(
(GetFocus() == this)|| (GetStyle() & LVS_SHOWSELALWAYS)
)
)
);
// 画文本背景
CRect rcBack = lpDIS->rcItem;
CMemDC dc(pDC,rcBack);
dc.SetBkMode(TRANSPARENT);
if( bHighlight )
{
dc.SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));
dc.SetBkColor(::GetSysColor(COLOR_HIGHLIGHT));
dc.FillRect(rcBack, &CBrush(::GetSysColor(COLOR_HIGHLIGHT)));
}
else
{
dc.SetTextColor(0x0C0F80);
if (lpDIS->itemID%2 ==0)
dc.FillRect(rcBack, &CBrush(0xEEF1E3));
else
dc.FillRect(rcBack, &CBrush(0xE3e3e3));
}
//设置字体颜色
CString str;
//得到焦点大小
CRect rcFocus = lpDIS->rcItem;
rcFocus.DeflateRect(1,1,1,1);
if (lpDIS->itemAction & ODA_DRAWENTIRE)
{
//写文本
CString szText;
//设置字体
//返回当前选择的CFont的指针,获得Windows默认的字体
CFont* font = CFont::FromHandle((HFONT)::GetStockObject(DEFAULT_GUI_FONT));
CFont* pOldFont = dc.SelectObject(font);
/*
font.DeleteObject();
LOGFONT lf={0};
pDC->GetCurrentFont()->GetLogFont(&lf);
lf.lfHeight =13;// -13;//14;
//与Windows字体关联
font.CreateFontIndirect(&lf);
*/
for (int i = 0; i < GetColumnCount(); i++)
{ //循环得到文本
CRect rcItem;
if ( !GetSubItemRect(lpDIS->itemID, i, LVIR_LABEL, rcItem ))
continue;
szText = GetItemText( lpDIS->itemID, i );
rcItem.left += 5; rcItem.right -= 1;
dc.DrawText(szText, lstrlen(szText), &rcItem, DT_VCENTER|DT_NOPREFIX|DT_SINGLELINE);
}
dc.SelectObject(pOldFont);
}
}
昨天晚上就把SSDT查看/恢复工具编写完毕,但美中不足的是,对于修改过的SSDT项在用CListCtrl列出来后,无法修改指定这行的颜色!郁闷中,今天下午起来又继续找方法,最后终于找到了,我的工具也可以说是圆满结束了!
首先是添加以下消息
ON_NOTIFY( NM_CUSTOMDRAW, IDC_LIST1, OnDrawColorForMyList ) //为改变颜色添加的消息
再添加类成员函数,就OK了:
//改变 m_List 控件单行的颜色
void CSSDTDlg::OnDrawColorForMyList( NMHDR *pNmHdr, LRESULT *pResult )
{
NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>( pNmHdr );
*pResult = CDRF_DODEFAULT;
if ( CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage )
{
*pResult = CDRF_NOTIFYITEMDRAW;
}
else if ( CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage )
{
*pResult = CDRF_NOTIFYSUBITEMDRAW;
}
else if ( (CDDS_ITEMPREPAINT | CDDS_SUBITEM) == pLVCD->nmcd.dwDrawStage )
{
COLORREF clrNewTextColor, clrNewBkColor;
int nItem = static_cast<int>( pLVCD->nmcd.dwItemSpec );
//如果当前SSDT地址与原始地址不一样,则显示字体为红色,否则为黑色
if( /
((pSSDTSaveTable)((ULONG)pSSDTST + nItem * sizeof(SSDTSaveTable)))->ulCurrentFunctionAddress /
!= ((pSSDTSaveTable)((ULONG)pSSDTST + nItem * sizeof(SSDTSaveTable)))->ulOriginalFunctionAddress /
) //设置为红色
{
clrNewTextColor = RGB( 255, 0, 0 );
}
else //设置为黑色
{
clrNewTextColor = RGB( 0, 0, 0 );
}
//设置背景色
if( nItem%2 ==0 )
{
clrNewBkColor = RGB( 240, 240, 240 ); //偶数行背景色为灰色
}
else
{
clrNewBkColor = RGB( 255, 255, 255 ); //奇数行背景色为白色
}
pLVCD->clrText = clrNewTextColor;
pLVCD->clrTextBk = clrNewBkColor;
*pResult = CDRF_DODEFAULT;
}
}
高亮及取消高亮
m_List.SetItemState(0,LVIS_DROPHILITED,LVIS_DROPHILITED);
UINT nState=m_List.GetItemState(1,LVIF_STATE);
nState&=!nState;
m_List.SetItemState(1,nState,LVIF_STATE);