Click here to Skip to main content
15,881,092 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I use Draw Text to draw a text in a treeView

<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")))
		{
			//Rectangle(pcd->nmcd.hdc, pcd->nmcd.rc.left, pcd->nmcd.rc.top, pcd->nmcd.rc.right, pcd->nmcd.rc.bottom);
			pCD->nmcd.rc.right = pCD->nmcd.rc.right + 50;
			//	pcd->nmcd.rc.bottom = pcd->nmcd.rc.bottom + 20;
			pCD->nmcd.rc.top = pCD->nmcd.rc.top + 23;
			DrawText(pCD->nmcd.hdc, _T("10:31pm"), 7, &pCD->nmcd.rc, DT_CENTER);
		}

			*pResult = CDRF_SKIPDEFAULT;
		}
	}



but when item is select the text that I have drawn has a white backgroung, can I change it? thanks

What I have tried:

I tried with SetBkColor but if I use it all treeView has blu background and not only item, I tried also to draw a fill Rectangle but background of draw text is always white
Posted
Updated 28-Feb-22 12:09pm
v2
Comments
merano99 25-Feb-22 18:18pm    
Did you try SetBkMode(hdc,TRANSPARENT); ?
Member 14594285 28-Feb-22 2:54am    
yes, it works, Can I change also color of my written?
merano99 28-Feb-22 18:11pm    
OK. Didn't think the solution was that simple. I wrote this as a solution. I would be happy if the solution is accepted.

1 solution

If you do not want a Color in Background use
C++
SetBkMode(hdc,TRANSPARENT); ?


To set a Color for the Text use:
C++
SetTextColor(hdc, RGB(255, 0, 0));


Before I list all the features, take a look at this:
Font and Text Functions (Windows GDI) - Win32 apps | Microsoft Docs[^]
 
Share this answer
 

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