MFC NM_CUSTOMDRAW - jues
一步一步教你实现CTreeCtrl 自绘
自绘控件的4种方法

MFC NM_CUSTOMDRAW

jues posted @ 2013年8月05日 18:09 in 笔记(转载) , 1105 阅读

继承类的方式

BEGIN_MESSAGE_MAP(CTreeCtrlEx, CTreeCtrl)
//{{AFX_MSG_MAP(CTreeCtrlEx)
    ON_NOTIFY_REFLECT( NM_CUSTOMDRAW, On_NmCustomDraw ) 
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()
void CTreeCtrlEx::On_NmCustomDraw(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
	
	LPNMTVCUSTOMDRAW lpnmcd = (LPNMTVCUSTOMDRAW) pNMCD;
	if (lpnmcd ->nmcd.dwDrawStage == CDDS_PREPAINT)
	{
		
		*pResult = CDRF_NOTIFYITEMDRAW;
		return;
	}
	else if (lpnmcd->nmcd.dwDrawStage == CDDS_ITEMPREPAINT ) 
	{
#if 1
		CRect rcItem(pNMCD->rc); // Item的区域
		POINT ptItem ;
		ptItem.x = rcItem.left  ;
		ptItem.y = rcItem.top  ;
		UINT uFlags; 
		HTREEITEM hItem = HitTest(ptItem, &uFlags); // Item 句柄
		CImageList  *img_list = this->GetImageList( TVSIL_NORMAL );
		int i,i_s;
		if(!hItem)
		{
			*pResult = CDRF_DODEFAULT;
			return;
		} 
		
		HDC hdcItem = pNMCD->hdc ; // Item的dc
		CDC *dc = CDC::FromHandle( hdcItem );
		CString strText;
		strText=GetItemText(hItem); 

		this->GetItemImage( hItem,i,i_s );
		dc->DrawIcon( rcItem.left,rcItem.top,img_list->ExtractIcon( i )  );
		rcItem.left += 32;
		dc->DrawText(strText,rcItem,DT_TOP );
		rcItem.top += 16;
		dc->DrawText("副标题",rcItem,DT_TOP );
		*pResult = CDRF_SKIPDEFAULT; 
		return; 
#else
		CRect rcItem(pNMCD->rc); // Item的区域
		POINT ptItem ;
		ptItem.x = rcItem.left + 1 ;
		ptItem.y = rcItem.top + 1 ;
		UINT uFlags; 
		HTREEITEM hItem = HitTest(ptItem, &uFlags); // Item 句柄
		if(!hItem)
		{
			*pResult = CDRF_DODEFAULT;
			return;
		} 
		
		HDC hdcItem = pNMCD->hdc ; // Item的dc
		CDC dc ;//= CDC::FromHandle( hdcItem );
		CString strText;
		strText=GetItemText(hItem); 
		dc.Attach(hdcItem); 
		 int nSave = dc.SaveDC(); 
		//dc.SetBkMode(TRANSPARENT);
		dc.DrawText( "111",rcItem,DT_TOP );
		 dc.RestoreDC(nSave);
		dc.Detach(); 
		*pResult = CDRF_SKIPDEFAULT; 
		return; 
#endif
	}
	*pResult = CDRF_DODEFAULT;
	return; 
	
}

 

	afx_msg void On_NmCustomDraw(NMHDR *pNMHDR, LRESULT *pResult);

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter