4

I need to get the number of columns in a list control in report mode.

Right now I'm sending a LVM_GETCOLUMN with increasing column number until SendMessage returns FALSE:

int col;
for (col = 0;; col++)
{ 
  LVCOLUMN Column;
  Column.mask = LVCF_WIDTH;
  if (!::SendMessage(hWnd, LVM_GETCOLUMN, col, (LPARAM)Column)
    break;
}

But this is rather awkward.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115

1 Answers1

18

You can retrieve the number of columns from the header control of the list control.

HWND hWndHdr = (HWND)::SendMessage(hWnd, LVM_GETHEADER, 0, 0);
int count = (int)::SendMessage(hWndHdr, HDM_GETITEMCOUNT, 0, 0L);
Werner Henze
  • 16,404
  • 12
  • 44
  • 69
  • 3
    On the other hand, columns have to added to the ListView using [`LVM_INSERTCOLUMN`](https://msdn.microsoft.com/en-us/library/windows/desktop/bb761101.aspx), so you could just keep track of how many columns your code has added. Unless you are querying a ListView that you do not have control over... – Remy Lebeau Oct 22 '15 at 20:20