How can I programmatically resize a listview column to fit its contents?


Sven wanted to know if there is a listview message to resize a column to fit its contents.

Sure there is. In fact, the default Ctrl+Num+ handler uses that message.

Take our scratch program and make these changes:

BOOL
OnCreate(HWND hwnd, LPCREATESTRUCT lpcs)
{
  g_hwndChild = CreateWindow(WC_LISTVIEW, NULL,
        WS_CHILD | WS_VISIBLE | LVS_REPORT,
        0, 0, 0, 0, hwnd, (HMENU)1, g_hinst, 0);
  LVCOLUMN col;
  col.mask = LVCF_TEXT | LVCF_WIDTH;
  col.cx = 200;
  col.pszText = TEXT("Name");
  ListView_InsertColumn(g_hwndChild, 0, &col);
  LVITEM item;
  item.mask = LVIF_TEXT;
  item.iSubItem = 0;
  item.pszText = TEXT("Alpha");
  ListView_InsertItem(g_hwndChild, &item);
  item.pszText = TEXT("Beta");
  ListView_InsertItem(g_hwndChild, &item);
  item.pszText = TEXT("Gamma");
  ListView_InsertItem(g_hwndChild, &item);
  item.pszText = TEXT("Delta");
  ListView_InsertItem(g_hwndChild, &item);

  ListView_SetColumnWidth(g_hwndChild, 0, LVSCW_AUTOSIZE);
  return TRUE;
}

The first part of the code just creates a listview control in report mode, inserts a column called "Name", then fills it with some dummy data.

The money is in the last line: List­View_Set­Column­Width takes a column number and a width, and there are two special width values:

  • LVSCW_AUTO­SIZE, which sizes to content,

  • LVSCW_AUTO­SIZE_USE­HEADER, which sizes to content and the header, with the bonus feature that if you are adjusting the width of the last column, then it extends to the remaining width in the listview.

The handler for the Ctrl+Num+ keyboard shortcut simply loops through all the columns and uses LVSCW_AUTO­SIZE for every column.

Comments (13)
  1. SimonRev says:

    I love discovering hopelessly undiscoverable hotkeys that I could have known about for years :).

  2. Boris says:

    SimonRev: but I suppose they're mostly for people who need to resize listviews as part of their day jobs. Analyze your workflow, look up any optimizing hotkeys, learn to use them. I never needed to know about Win+L before I started working.

  3. Mordachai says:

    What key is "Num+"????

  4. Guest says:

    @Steve

    The + key on the number pad.

  5. Random User 20395714 says:

    I have sadly found that an increasing number of grid controls do not respond to that hotkey. It's particularly annoying in various database management products that used to, but suddenly stopped in later versions. Presumably when they switched to new UI Frameworks, because the overall looks changed at the same time.

  6. skSdnW says:

    I wish there was a LVSCW_*SMART*AUTO­SIZE. In Vista+ you can use LVS_EX_AUTOSIZECOLUMNS in some situations but I often end up having to implement my own version that sets the column size to something like "max(LVSCW_AUTO­SIZE, min(50, LVSCW_AUTO­SIZE_USE­HEADER))"

  7. Joshua says:

    > Presumably when they switched to new UI Frameworks

    And some things that should not have been forgotten were lost.

  8. Anon says:

    @Random User 20395714

    Yup. That's a consequence of "Always re-implement as much of the OS as possible" thinking, which seems to be cycling back in from decades ago.

    The corollaries for that are "Never bother attempting to understand any of what you're re-implementing," and "Never produce a set of requirements for a new control beyond basic functionality."

  9. Boris says:

    @Guest: Steve's question is obviously rhetorical, Num+ being fairly easy to bing.

  10. Gabe says:

    Since I use a normal-width laptop keyboard exclusively, my Ctrl-Num+ is really:

    Fn-ScrLk (to turn on Num Lock)

    Ctrl-/ (Num+ is on my / key)

    Fn-ScrLk (to turn off Num Lock)

    Needless to say, I never use that "shortcut". It would be nice if there were a laptop-friendly version of it.

  11. I don't recall wanting to know that!

    Wait, you're saying there could be other people called Sven? Preposterous!

  12. cheong00 says:

    @Boris: You don't even need to Bing it, it's been spelt out in the linked article.

  13. @Gabe says:

    Just use the free Autohotkey app to remap scroll lock to Ctrl + Numeric keypad  "+" key.

Comments are closed.

Skip to main content