ctreectrl 设置选中_CTreeCtrl和CListCtrl落空核心时高亮选中项

设置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

以下未

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值