好吧,我想出了一个解决方案。
但是有一个错误,如果我只删除一个项目,它不会调整列的大小。
case LVN_DELETEITEM:
{
LPNMLISTVIEW listView = (LPNMLISTVIEW) lParam;
// After an item is deleted,
// if there is not a vertical scroll bar and GWL_USERDATA is TRUE,
// resize the column back to normal.
if (!(GetWindowLong(listView->hdr.hwndFrom, GWL_STYLE) & WS_VSCROLL) &&
GetWindowLong(listView->hdr.hwndFrom, GWL_USERDATA) == TRUE)
{
const int ColWidth = ListView_GetColumnWidth(listView->hdr.hwndFrom, 0);
ListView_SetColumnWidth(listView->hdr.hwndFrom, 0, ColWidth + GetSystemMetrics(SM_CXVSCROLL));
SetWindowLong(listView->hdr.hwndFrom, GWL_USERDATA, FALSE);
}
break;
}
case LVN_ITEMCHANGED:
{
LPNMLISTVIEW listView = (LPNMLISTVIEW) lParam;
// After an item is added, if there is a horizontal scrollbar,
// resize the column and set GWL_USERDATA to TRUE.
if (GetWindowLong(listView->hdr.hwndFrom, GWL_STYLE) & WS_HSCROLL)
{
const int ColWidth = ListView_GetColumnWidth(listView->hdr.hwndFrom, 0);
ListView_SetColumnWidth(listView->hdr.hwndFrom, 0, ColWidth - GetSystemMetrics(SM_CXVSCROLL));
SetWindowLong(listView->hdr.hwndFrom, GWL_USERDATA, TRUE);
}
break;
}
我仍然希望看到一个更好的解决方案,但这目前有效。