Click here to Skip to main content
15,867,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
<pre>
void CMyTrre::OnCustomDraw(NMHDR *pNMHDR, LRESULT *pResult)
{
	NMLVCUSTOMDRAW* pCD = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR);

	*pResult = 0;

	// If this is the beginning of the control's paint cycle, request
	// notifications for each item.

	if (CDDS_PREPAINT == pCD->nmcd.dwDrawStage)
	{
		*pResult = CDRF_NOTIFYITEMDRAW;
	}
	else if (CDDS_ITEMPREPAINT == pCD->nmcd.dwDrawStage)
	{
		// This is the pre-paint stage for an item.  We need to make another
		// request to be notified during the post-paint stage.
		

		*pResult = CDRF_NOTIFYPOSTPAINT;

			
	}

	else if (CDDS_ITEMPOSTPAINT == pCD->nmcd.dwDrawStage)
	{
		// If this item is selected, re-draw the icon in its normal
		// color (not blended with the highlight color).

		LVITEM rItem;
		int    nItem = static_cast<int>(pCD->nmcd.dwItemSpec);

		// Get the image index and state of this item.  Note that we need to
		// check the selected state manually.  The docs _say_ that the
		// item's state is in pLVCD->nmcd.uItemState, but during my testing
		// it was always equal to 0x0201, which doesn't make sense, since
		// the max CDIS_* constant in commctrl.h is 0x0100.
		HTREEITEM   hItem = (HTREEITEM)pCD->nmcd.dwItemSpec;

		

		ZeroMemory(&rItem, sizeof(LVITEM));
		rItem.mask = LVIF_IMAGE | LVIF_STATE;
		rItem.iItem = nItem;
		rItem.stateMask = LVIS_SELECTED;
		CString giorno;
		giorno = GetItemText(hItem);//ottengo il mese o giorno
		int n_pos;
		int n_pos1;
		int n_pos2;
		n_pos = giorno.Find(_T("a"));
		n_pos1 = giorno.Find(_T("e"));
		n_pos2 = giorno.Find(_T("i"));

		//se il parent è o oggi o today metto il testo in un certo modo		
		if (((n_pos >= 0)|| (n_pos1 >= 0) ||(n_pos2 >= 0))&&(giorno!= _T("Oggi"))&&(giorno != _T("Today")))
		{		
			COLORREF myColor = RGB(255, 0, 0);
		    HBRUSH myBrush = CreateSolidBrush(myColor);
			//FillRect(pCD->nmcd.hdc, &pCD->nmcd.rc, myBrush);
			RECT tt;
			tt.bottom = pCD->nmcd.rc.bottom;
			tt.top = pCD->nmcd.rc.top+23;
			tt.right = 478;
			tt.left = pCD->nmcd.rc.left;
					
			//COLORREF blu = RGB(0, 0, 255);
			//pCD->clrTextBk = RGB(0, 0, 255);

			HTREEITEM giorno = GetParentItem(hItem);
			CString str_giorno;
			str_giorno = GetItemText(giorno);

			HTREEITEM mese = GetParentItem(giorno);
			CString str_mese;
			str_mese = GetItemText(mese);

			HTREEITEM anno = GetParentItem(mese);
			CString str_anno;
			str_anno = GetItemText(anno);
		
			CString text;
			text = GetItemText(hItem);

			int numero = GetItemData(hItem);

			CString orario;

			//COLORREF		colSfondoTXT = dc.SetBkColor(RGB(255, 255, 255));
			COLORREF		colTextOLD = (RGB(0, 0, 255));

			if (m_vItemF.size() > 0)
			{
				for (int ii = 0; ii < m_vItemF.size(); ii++)
				{
					if ((m_vItemF[ii].numero == numero) && (m_vItemF[ii].str == text) && (m_vItemF[ii].giorno == str_giorno) && (m_vItemF[ii].mese == str_mese) && (m_vItemF[ii].anno == str_anno))
					{
						orario = m_vItemF[ii].ora;
					}
				}
			}		
			    
				SetBkMode(pCD->nmcd.hdc, TRANSPARENT);

				//COLORREF		colTextOLD1 = SetTextColor(RGB(0, 0, 255));
				DrawText(pCD->nmcd.hdc, orario, 5, &tt, DT_CENTER);

				//SetTextColor(RGB(25, 55, 200));
				//SetTextColor(colTextOLD);
				//SetTextColor(colTextOLD1);
			    tt = pCD->nmcd.rc;	
	  }
			*pResult = CDRF_SKIPDEFAULT;
	}
}






how can I change color of DrawText when I select on treeView

What I have tried:

I tried:

//SetTextColor(RGB(25, 55, 200));


but if I use it in ITEMPOSTPAINT text of treeView blinks
Posted
Updated 28-Feb-22 3:21am

I think you need to select correct HDC for SetTextColor:

::SetTextColor(pCD->nmcd.hdc, RGB(0, 0, 255));
 
Share this answer
 
Comments
Member 14594285 28-Feb-22 8:46am    
Thanks, it works
See bold code. Replace
OnGetCellBkColor
and
OnGetCellTextColor
with your RGB values.

C++
void CMyListView::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult)
{
   ENSURE(pNMHDR != NULL);
   LPNMLVCUSTOMDRAW lplvcd = (LPNMLVCUSTOMDRAW)pNMHDR;

   switch (lplvcd->nmcd.dwDrawStage)
   {
   case CDDS_PREPAINT:
      *pResult = CDRF_NOTIFYITEMDRAW;
      break;

   case CDDS_ITEMPREPAINT:
      *pResult = CDRF_NOTIFYSUBITEMDRAW;
      break;

   case CDDS_ITEMPREPAINT | CDDS_SUBITEM:
      {
         int iColumn = lplvcd->iSubItem;
         int iRow = (int) lplvcd->nmcd.dwItemSpec;

         lplvcd->clrTextBk = OnGetCellBkColor(iRow, iColumn, lplvcd->nmcd.lItemlParam);
         lplvcd->clrText = OnGetCellTextColor(iRow, iColumn, lplvcd->nmcd.lItemlParam);

         if (iColumn == m_iSortedColumn && m_bMarkSortedColumn && lplvcd->clrTextBk == GetListCtrl().GetBkColor())
         {
            lplvcd->clrTextBk = m_clrSortedColumn;
         }

         HFONT hFont = OnGetCellFont( iRow, iColumn, lplvcd->nmcd.lItemlParam);
         if (hFont != NULL)
         {
            m_hOldFont = (HFONT) SelectObject(lplvcd->nmcd.hdc, hFont);
            ENSURE(m_hOldFont != NULL);

            *pResult = CDRF_NEWFONT | CDRF_NOTIFYPOSTPAINT;
         }
         else
         {
            *pResult = CDRF_DODEFAULT;
         }
      }
      break;

   case CDDS_ITEMPOSTPAINT | CDDS_SUBITEM:
      if (m_hOldFont != NULL)
      {
         SelectObject(lplvcd->nmcd.hdc, m_hOldFont);
         m_hOldFont = NULL;
      }

      *pResult = CDRF_DODEFAULT;
      break;
   }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900