Question

I am trying to implement a custom drawn listview in WTL, this is the code for the listview:

class CTaskListCtrl:  public CWindowImpl<CTaskListCtrl, CListViewCtrl>,
                   public CCustomDraw<CTaskListCtrl>   
{
public:
    BOOL SubclassWindow(HWND hWnd)
    {
        ATLASSERT(m_hWnd==NULL);
        ATLASSERT(::IsWindow(hWnd));
        BOOL bRet = CWindowImpl<CTaskListCtrl, CListViewCtrl>::SubclassWindow(hWnd);
          return bRet;
   }
   BEGIN_MSG_MAP(CTaskListCtrl)
      CHAIN_MSG_MAP(CCustomDraw<CTaskListCtrl>)
   END_MSG_MAP()

   DWORD OnPrePaint(int /*idCtrl*/, LPNMCUSTOMDRAW /*lpNMCustomDraw*/)
   {        
      return  CDRF_NOTIFYITEMDRAW;
   }

   DWORD OnItemPrePaint(int /*idCtrl*/, LPNMCUSTOMDRAW lpNMCustomDraw)
   {
      NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>( lpNMCustomDraw );

      COLORREF crText;

      if ( (pLVCD->nmcd.dwItemSpec % 2) == 0 )
         crText = RGB(200,200,255);
      else 
         crText = RGB(255,255,255);   

      pLVCD->clrTextBk = crText;

      return CDRF_DODEFAULT;
    }
};   

And this is how I use it: IDC_LIST_TASKS is the ListVieew resource that I have created in the resource wizard.

CTaskListCtrl m_taskList;
m_taskList.SubclassWindow(GetDlgItem(IDC_LIST_TASKS));

m_taskList.AddColumn( TEXT("Name"),0);
m_taskList.SetColumnWidth(0,100);

m_taskList.AddColumn( TEXT("Algorithm"), 1);
m_taskList.SetColumnWidth(1,100);

m_taskList.AddColumn( TEXT("Status"), 2);
m_taskList.SetColumnWidth(2,100);

m_taskList.AddItem(0,0,L"ASDASD");
m_taskList.AddItem(0,1,L"ASDASD");
m_taskList.AddItem(0,2,L"ASDASD");

and the color of items text doesn't change. Why?

Was it helpful?

Solution

Without sorting out previous questions you keep posting new with the same issues.

There is nothing to fix in this code snippet, too many things are wrong.

You can start with working sample Lazy Grid WTL implementation, Custom Drawn Controls using WTL and figure out how things work.

  • you don't need to subclass to custom draw
  • custom draw notifications come to control's parent, not to control itself - you need to handle them right there on parent window, or REFLECT_NOTIFICATIONS and in this case you have an option to handle them on the control (and subclassing makes sense)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top