vc listctr失去焦点,仍然高亮


VC改变list颜色例子

http://download.csdn.net/source/1183845


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 CHighLightDlg::OnNMCustomdrawList2(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 =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;


    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值