Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ Windows MFC
I recently ran our software on a Windows 8 machine and encountered an issue where the custom draw functionality is simply not working. The custom draw works fine on windows XP and windows 7. We are changing background colours and some text to bold in a ctreectrl depending on each items data and state.
 
I knocked up a simple bare bones app with a ctreectrl and custom draw, but still no luck. This is a code snippet used in the test app.
 
...
DDX_Control(pDX, IDC_TREE1,m_TreeCtrl);
...
ON_NOTIFY(TVN_GETDISPINFO, IDC_TREE1, &CMyTreeView::OnTvnGetdispinfoTree1)
...
void CMyTreeView::OnNMCustomdrawTree1(NMHDR *pNMHDR, LRESULT *pResult)
{
   LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
 
   LPNMTVCUSTOMDRAW tvcd = (LPNMTVCUSTOMDRAW) pNMHDR;
   
   // Get the tree control item
   HTREEITEM hItem = (HTREEITEM) (pNMCD->dwItemSpec);
   *pResult = 0;
   if(hItem == NULL)
   {
      return;
   }
 
   switch(tvcd->nmcd.dwDrawStage)
   {
      case CDDS_PREPAINT:
      {
	 *pResult = (CDRF_NOTIFYPOSTPAINT | CDRF_NOTIFYITEMDRAW);
	 break;
      }
      case CDDS_ITEMPREPAINT:
      {
         if ((tvcd->nmcd.uItemState & CDIS_SELECTED) == CDIS_SELECTED)
         {// Set item text to bold if selected
            m_TreeCtrl.SetItemState(hItem,TVIS_BOLD,TVIS_BOLD);
         }
         else
            m_TreeCtrl.SetItemState(hItem,0,TVIS_BOLD);
         // Set Item background color
         tvcd->clrTextBk = Items.GetAt(
                                    (POSITION)(m_TreeCtrl.GetItemData(hItem))).col;
 
         *pResult = (CDRF_NOTIFYPOSTPAINT | CDRF_NEWFONT);
	 break;
      }
      case CDDS_ITEMPOSTPAINT:
      {
         *pResult = 0;
         break;
      }
   }
}
 
This code displays the colours when run under Win XP and 7, but not win 8. I've searched through Google and MSDN with not much luck either. Any thoughts or possible fixes would be greatly appreciated.
Posted 21-Apr-13 21:24pm
Edited 22-Apr-13 16:33pm
v3
Comments
Sergey Alexandrovich Kryukov at 22-Apr-13 2:30am
   
A fix of some code is only possible if the code is available.
—SA
Member 9499256 at 12-May-13 21:56pm
   
Sample code has been provided.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I posted this question on the MSDN forums and the suggestion was to adjust the hItem == NULL check since hItem now is actually NULL during the CDDS_PREPAINT stage on Windows 8 (i'm assuming) so the draw function gets skipped.
 
Changed the following code from
if(hItem == NULL)
{
   return;
}
 
to
 
if(hItem == NULL && tvcd->nmcd.dwDrawStage != CDDS_PREPAINT)
{
   return;
}
 
It now draws fine on windows 8.
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I think in the CDDS_ITEMPREPAINT stage you need to CDRF_DODEFAULT to *pResult. This is what I usually do. CDRF_NEWFONT is only used when you specified a new font (via SelectObject())
  Permalink  
Comments
Member 9499256 at 23-Apr-13 0:08am
   
I removed the CDRF_NEWFONT and changed the *pResult to CDRF_DODEFAULT but it still doesn't work. My boss has built it under VS2012 in the hope it might make a difference but it runs just the same.

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

  Print Answers RSS
0 OriginalGriff 406
1 /\jmot 180
2 Suraj Sahoo | Coding Passion 170
3 BillWoodruff 159
4 Afzaal Ahmad Zeeshan 154
0 OriginalGriff 8,344
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,634
3 Maciej Los 5,024
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 10 Jun 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100