1

I need to get the column id will be drawn. This is my some of my code I try to get item id and column id to use ListView_GetItemText and set the correct color of the item to be drawn.

switch( ((LPNMLVCUSTOMDRAW)lParam)->nmcd.dwDrawStage){
case CDDS_PREPAINT:
    return CDRF_NOTIFYITEMDRAW;
    break;
case CDDS_ITEMPREPAINT:
   {
    LPNMLVCUSTOMDRAW customDraw = (LPNMLVCUSTOMDRAW)lParam;
    int itemid = (customDraw->nmcd).dwItemSpec //this is item id
    //column id is missing                                                                                          
    return CDRF_NEWFONT;
        break;
   }
default: return CDRF_DODEFAULT;
}
nKandel
  • 2,543
  • 1
  • 29
  • 47
Phat Tran
  • 307
  • 1
  • 3
  • 14

2 Answers2

2

if you include

case CDDS_ITEMPREPAINT | CDDS_SUBITEM:
     int iSubItem = ((LPNMLVCUSTOMDRAW)lParam)->iSubItem;
break;

this will get you the column. The reason why this isn't happening is you have to return the notifications you want to receive in the future through the LRESULT pointer passed in the function header, so for instance

If your function header looked like:

::OnNMCustomdraw(NMHDR* pNMHDR, LRESULT* pResult)

You'd need:

*pResult |= CDRF_NOTIFYITEMDRAW;
*pResult |= CDRF_NOTIFYSUBITEMDRAW;
*pResult |= CDRF_NOTIFYPOSTPAINT;
*pResult |= CDRF_NOTIFYPOSTERASE;

At the end of your function

8bitwide
  • 2,071
  • 1
  • 17
  • 24
1

NMLVCUSTOMDRAW contains a member called iSubItem, this will tell you which "column" is being drawn.

The documentation describes the member thus:

iSubItem

Type: int

... Index of the subitem that is being drawn. If the main item is being drawn, this member will be zero.

You should be able to refer to it via customDraw->iSubItem. If you can't then you will need to make sure you have _WIN32_IE defined (directly or indirectly) to be at least 0x0400.

Community
  • 1
  • 1
ta.speot.is
  • 26,914
  • 8
  • 68
  • 96
  • Can you have me with _WIN32_IE defined. – Phat Tran Sep 01 '12 at 05:17
  • @user1614028 The best guide is the [documentation](http://msdn.microsoft.com/en-us/library/windows/desktop/aa383745.aspx) – ta.speot.is Sep 01 '12 at 05:56
  • I try but the customDraw->iSubItem alway return 0. Any mistake here – Phat Tran Sep 01 '12 at 08:00
  • I had a look at this example [here](http://www.codeguru.com/cpp/controls/listview/customdrawing/article.php/c4199/Custom-Draw-ListView-Controls-Part-II.htm). It looks like you want to handle `case CDDS_ITEMPREPAINT|CDDS_SUBITEM` as well. – ta.speot.is Sep 01 '12 at 08:15
  • if i change from case CDDS_ITEMPREPAINT to case CDDS_ITEMPREPAINT|CDDS_SUBITEM the case is not run(not true) – Phat Tran Sep 01 '12 at 08:31