VC ListControl/TreeControl失去焦点后,所选择行仍然高亮显示

CTreeCtrl和CListCtrl失去焦点时高亮选中项


设置CTreeCtrl的Always Show Selection:True
CListCtrl的Always Show Selection:False
在NM_CUSTOMDRAW事件中添加如下代码:
void CHighLightDlg::OnNMCustomdrawTree1(NMHDR *pNMHDR, LRESULT *pResult)
{
    LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(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 << "1\n";
    }
    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 << "2\n";
    }
}


void CMFCApplicationDlg::OnNMCustomdrawlist(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
// TODO:  在此添加控件通知处理程序代码
*pResult = 0;

//This code based on Michael Dunn's excellent article on list control custom draw at http://www.codeproject.com/listctrl/lvcustomdraw.asp
NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR);
// Take the default processing unless we set this to something else below.
*pResult = CDRF_DODEFAULT;
// First thing - check the draw stage. If it's the control's 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.  We'll request notifications before each subitem's prepaint stage.
*pResult = CDRF_NOTIFYSUBITEMDRAW;
}
else if ((CDDS_ITEMPREPAINT | CDDS_SUBITEM) == pLVCD->nmcd.dwDrawStage)
{
COLORREF clrNewTextColor, clrNewBkColor;
int    nItem = static_cast<int>(pLVCD->nmcd.dwItemSpec);
BOOL bSelect = FALSE;
POSITION pos = ((CListCtrl*)(GetDlgItem(IDC_DeviceInfoDeviceList)))->GetFirstSelectedItemPosition();
while (pos)
{
int index = ((CListCtrl*)(GetDlgItem(IDC_DeviceInfoDeviceList)))->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;
}
}

猜你喜欢

转载自blog.csdn.net/cnicfhnui/article/details/54425798
vc