如何防止列表视图在 item.count 更改时跳转到 selected/focused 行?

How to prevent listview from jumping to selected/focused row on item.count change?

我有一个虚拟列表视图,我打算用它来显示一个相当大的日志文件中的内容。

每当添加或删除一行并且我在列表框中选择或聚焦(或两者)一行时,它会自动滚动回它,这非常烦人。

感觉当项目计数被修改时,某些东西会调用 MakeVisible(或做同样事情的东西)。

重现它的非常简单的示例:

procedure TForm1.FormCreate(Sender: TObject);
var
  Col: TListColumn;
begin
  ListView1.OwnerData := True;
  ListView1.ViewStyle := vsReport;
  ListView1.RowSelect := True;

  Col := ListView1.Columns.Add;
  Col.Caption := 'LineNum';
  Col.Alignment := taLeftJustify;
  Col.Width := 70;
end;
// listview onData event
procedure TForm1.ListView1Data(Sender: TObject; Item: TListItem);
begin
  Item.Caption := IntToStr(Item.Index+1);
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  ListView1.Items.Count := ListView1.Items.Count + 10;
end;

编辑:测试不同的 ViewStyles,这只发生在 vsReport 和 vsList 上

问题是 TListItems.Count 属性 setter 在没有 LVSICF_NOSCROLL 标志的情况下调用 ListView_SetItemCountEx()

The list-view control will not change the scroll position when the item count changes.

procedure TListItems.SetCount(Value: Integer);
begin
  if Value <> 0 then
    ListView_SetItemCountEx(Handle, Value, LVSICF_NOINVALIDATEALL)
  else
    ListView_SetItemCountEx(Handle, Value, 0);
end;

这就是每当 Count 更改时 ListView 滚动的原因。您必须自己直接调用 ListView_SetItemCountEx() 才能指定 LVSICF_NOSCROLL 标志。

uses
  ..., CommCtrl;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  //ListView1.Items.Count := ListView1.Items.Count + 10;
  ListView_SetItemCountEx(ListView1.Handle, ListView1.Items.Count + 10, LVSICF_NOINVALIDATEALL or LVSICF_NOSCROLL);
end;