防止调整 ListCtrl 中第一列的大小(ReportView)

Prevent resizing first column in ListCtrl (ReportView)

我来自this LVCFMT_FIXED_WIDTH 这种格式样式有效,但仅在第一列之后。第一列项目始终是可移动的。为什么?我是不是漏了什么?

for(int i = 0; i<NUM_COLUMNS; i++)
{
    m_ListCtrl.InsertColumn(i,_gszColumnLabel[i], _gnColumnFmt[i] |  LVCFMT_FIXED_WIDTH, _gnColumnWidth[i], -1);
}   

第一列is special出于某种原因:

If a column is added to a list-view control with index 0 (the leftmost column), it is always LVCFMT_LEFT. Setting other flags on column 0 does not override that alignment. Therefore if you keep inserting columns with index 0, the text in all columns are left-aligned. If you want the first column to be right-aligned or centered you can make a dummy column, then insert one or more columns with index 1 or higher and specify the alignment you require. Finally delete the dummy column.

也许虚拟列技巧也适用于您的情况?

LVCFMT_FIXED_WIDTH 的一个问题是无法以编程方式更改列宽(使用 SetColumnWidth)。如果您想在 header 控件构造之后设置列宽(例如在 OnSize 中),您必须暂时删除 LVCFMT_FIXED_WIDTH 标志。

这是一个实现,它既有无法修改第一列的解决方法,也有无法通过 LVCFMT_FIXED_WIDTH:

以编程方式修改列的解决方法
void
MyTreeeCtrl::SetFixedColumnWidth(int col, int width)
{
  int colToModify = col;
  // Workaround 1: Insert a dummy column to be able to modify the attributes
  // of the first column.
  if (col == 0)
  {
    InsertColumn(0, L"");
    ++colToModify;
  }

  // Workaround 2: Temporarily remove the fixed width attribute to be able to
  // modify the width.
  LVCOLUMN colInfo;
  colInfo.mask = LVCF_FMT;
  GetColumn(colToModify, &colInfo);
  colInfo.fmt = colInfo.fmt & ~LVCFMT_FIXED_WIDTH;
  SetColumn(colToModify, &colInfo);

  // Set the column width
  SetColumnWidth(colToModify, width);

  // Restore the fixed width attribute
  colInfo.fmt = colInfo.fmt | LVCFMT_FIXED_WIDTH;
  SetColumn(colToModify, &colInfo);

  // Remove the dummy column
  if (col == 0)
  {
    DeleteColumn(0);
  }
}