{"id":44513,"date":"2015-03-09T07:00:00","date_gmt":"2015-03-09T21:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2015\/03\/09\/how-can-i-programmatically-resize-a-listview-column-to-fit-its-contents\/"},"modified":"2019-03-13T12:13:38","modified_gmt":"2019-03-13T19:13:38","slug":"20150309-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20150309-00\/?p=44513","title":{"rendered":"How can I programmatically resize a listview column to fit its contents?"},"content":{"rendered":"<p>Sven wanted to know <a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2012\/10\/30\/10363822.aspx#10364873\">if there is a listview message to resize a column to fit its contents<\/a>. <\/p>\n<p>Sure there is. In fact, the default <kbd>Ctrl<\/kbd>+<kbd>Num+<\/kbd> handler uses that message. <\/p>\n<p>Take our <a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2003\/07\/23\/54576.aspx\">scratch program<\/a> and make these changes: <\/p>\n<pre>\nBOOL\nOnCreate(HWND hwnd, LPCREATESTRUCT lpcs)\n{\n  <font COLOR=\"blue\">g_hwndChild = CreateWindow(WC_LISTVIEW, NULL,\n        WS_CHILD | WS_VISIBLE | LVS_REPORT,\n        0, 0, 0, 0, hwnd, (HMENU)1, g_hinst, 0);\n  LVCOLUMN col;\n  col.mask = LVCF_TEXT | LVCF_WIDTH;\n  col.cx = 200;\n  col.pszText = TEXT(\"Name\");\n  ListView_InsertColumn(g_hwndChild, 0, &amp;col);\n  LVITEM item;\n  item.mask = LVIF_TEXT;\n  item.iSubItem = 0;\n  item.pszText = TEXT(\"Alpha\");\n  ListView_InsertItem(g_hwndChild, &amp;item);\n  item.pszText = TEXT(\"Beta\");\n  ListView_InsertItem(g_hwndChild, &amp;item);\n  item.pszText = TEXT(\"Gamma\");\n  ListView_InsertItem(g_hwndChild, &amp;item);\n  item.pszText = TEXT(\"Delta\");\n  ListView_InsertItem(g_hwndChild, &amp;item);\n\n  ListView_SetColumnWidth(g_hwndChild, 0, LVSCW_AUTOSIZE);<\/font>\n  return TRUE;\n}\n<\/pre>\n<p>The first part of the code just creates a listview control in report mode, inserts a column called &#8220;Name&#8221;, then fills it with some dummy data. <\/p>\n<p>The money is in the last line: <code>List&shy;View_Set&shy;Column&shy;Width<\/code> takes a column number and a width, and there are two special width values: <\/p>\n<ul>\n<li><code>LVSCW_AUTO&shy;SIZE<\/code>,     which sizes to content, \n<li><code>LVSCW_AUTO&shy;SIZE_USE&shy;HEADER<\/code>,     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. <\/ul>\n<p>The handler for the <kbd>Ctrl<\/kbd>+<kbd>Num+<\/kbd> keyboard shortcut simply loops through all the columns and uses <code>LVSCW_AUTO&shy;SIZE<\/code> for every column. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>ListView_SetColumnWidth.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-44513","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>ListView_SetColumnWidth.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/44513","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=44513"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/44513\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=44513"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=44513"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=44513"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}