设置CTreeCtrl的Always Show Selection:TrueCListCtrl的Always Show Selection:False在NM_CUSTOMDRAW事务中添加如下代码:
void CHighLightDlg::OnNMCustomdrawTree1(NMHDR pNMHDR, LRESULT pResult)
{
LPNMCUSTOMDRAW pNMCD = reinterpret_cast(pNMHDR);
//TODO: 在此添加控件通知处理惩罚法度代码 pResult = 0;
LPNMTVCUSTOMDRAW pDraw=(LPNMTVCUSTOMDRAW)pNMHDR;
DWORD dwDrawStage=pDraw->nmcd.dwDrawStage;
UINT uItemState =pDraw->nmcd.uItemState;
// pResult=CDRF_NOTIFYITEMDRAW;
//|CDRF_NOTIFYPOSTPAINT|CDRF_NOTIFYSUBITEMDRAW|CDRF_NOTIFYPOSTERASE; CDC pdc=CDC::FromHandle(pDraw->nmcd.hdc);
CRect rc;
HTREEITEM hItem=(HTREEITEM) pDraw->nmcd.dwItemSpec;
m_tree.GetItemRect(hItem,&rc,TRUE);//FALSE);text only CString txt=m_tree.GetItemText(hItem);
if((dwDrawStage & CDDS_ITEM) && (uItemState & CDIS_SELECTED))
{// pdc->FillSolidRect(&rc,RGB(49,106,197));//clr);// pdc->SetTextColor(RGB(255,255,255));//white pdc->SetBkColor(RGB(49,106,197));//clr);//
CFont pfnt=pdc->GetCurrentFont();
// pdc->TextOut(rc.left+2,rc.top+2,txt);
pdc->SelectObject(pfnt);
pResult |= CDRF_SKIPDEFAULT;
//afxDump <
else //without these ,1st blue ! {
pdc->FillSolidRect(&rc, GetSysColor(COLOR_WINDOW));
pdc->SetTextColor(GetSysColor(COLOR_WINDOWTEXT));
pdc->TextOut(rc.left+2, rc.top+2, txt);
//afxDump <
}
void CHighLightDlg::OnNMCustomdrawList2(NMHDR pNMHDR, LRESULT pResult)
{
LPNMCUSTOMDRAW pNMCD = reinterpret_cast(pNMHDR);
//TODO: 在此添加控件通知处理惩罚法度代码 pResult = 0;
//This code based on Michael Dunns excellent article on//list control custom draw athttp://www.codeproject.com/listctrl/lvcustomdraw.asp
NMLVCUSTOMDRAW pLVCD = reinterpret_cast( pNMHDR );
//Take the default processing unless we set this to something else below. pResult = CDRF_DODEFAULT;
//First thing - check the draw stage. If its the controls prepaint//stage, then tell Windows we want messages for every item. if ( CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage )
{
pResult = CDRF_NOTIFYITEMDRAW;
}
else if ( CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage )
{
//This is the notification message for an item. Well request//notifications before each subitems prepaint stage.
pResult = CDRF_NOTIFYSUBITEMDRAW;
}
else if ( (CDDS_ITEMPREPAINT | CDDS_SUBITEM) == pLVCD->nmcd.dwDrawStage )
{
COLORREF clrNewTextColor, clrNewBkColor;
int nItem = static_cast( pLVCD->nmcd.dwItemSpec );
BOOL bSelect = FALSE;
POSITION pos =m_lst.GetFirstSelectedItemPosition();
while(pos)
{
int index = m_lst.GetNextSelectedItem(pos);
if(index==nItem)
{
bSelect = TRUE;
break;
}
}
if(bSelect)
{
//clrNewTextColor = RGB(255,0,0);//Set the text to red clrNewTextColor = RGB(0,255,0); //Set the text to red clrNewBkColor = RGB(0,0,255); //Set the bkgrnd color to blue }
else
{
clrNewTextColor = RGB(0,0,0); //Leave the text black clrNewBkColor = RGB(255,255,255); //leave the bkgrnd color white }
pLVCD->clrText = clrNewTextColor;
pLVCD->clrTextBk = clrNewBkColor;
//Tell Windows to paint the control itself. pResult = CDRF_DODEFAULT;
}
}
这个比来斗劲常用,记录一下。须要源码的留下邮箱。url:http://greatverve.cnblogs.com/p/ctreectrl-clistctrl-highlight.html
以下未