如何获取列表控件中的列数

How to get the number of columns in a list control

我需要在报表模式下获取列表控件的列数。

现在我正在发送一个 LVM_GETCOLUMN,其中列号不断增加,直到 SendMessage returns FALSE:

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

但这有点尴尬。

您可以从列表控件的 header 控件中检索列数。

HWND hWndHdr = (HWND)::SendMessage(hWnd, LVM_GETHEADER, 0, 0);
int count = (int)::SendMessage(hWndHdr, HDM_GETITEMCOUNT, 0, 0L);