TListView在项目标题上添加600多个字符

时间:2013-11-09 18:20:20

标签: listview delphi ownerdrawn tlistview

我需要在Item.Caption和SubItems上添加600多个(或更多)字符,但我发现如果文本长于N个字符,TListView会完全剪切文本。

我试过了:

procedure TForm1.FormCreate(Sender: TObject);
var
 i1: Integer;
 s: String;
begin
 for i1 := 0 to 690 do
  s := s + IntToStr(i1) + '-';

 with ListView1.Items.Add do
 begin
   Caption := s;
   SubItems.Add(s);
 end;
end;

然后我启用了ListView1.OwnerDraw:= True;

从下图中可以看出,Column1中的文本遍历Column2:

enter image description here

任何人都可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:3)

Delphi 2007中的快速测试应用程序,使用以下(更合理的)代码,显示ListView停止在88-8显示Ansi字符,长度为259个字符。

procedure TForm4.FormCreate(Sender: TObject);
var
  s: string;
  i: Integer;
  Item: TListItem;
begin
  s := '';
  for i := 0 to 89 do
    s := s + '-' + IntToStr(i);

  // Set the width of the first column so there's room for all
  ListView1.Columns[0].Width := ListView1.Canvas.TextWidth(s) + 10;

  Item := ListView1.Items.Add;
  Item.Caption := s;
  Item.SubItems.Add(s);

  // Display length of string actually displayed, which
  // is one short of the total length (the final '9' in '89'
  // is truncated), in the form's caption.
  Caption := IntToStr(Length(s) - 1);
end;

为此添加空终止符(根据Windows API的要求)意味着它是260个字符,根据MSDN文档,它是显示文本的最大长度; LVITEM.pszText成员可以存储更多,但不会显示它。

(感谢@SertacAkyuz的链接,所以我没有找到它。)

您可以使用RegEdit自行验证。找到超出该限制的注册表值(例如,我很快找到了HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Arbiters\AllocationOrder)。无论您拖动Data列的宽度,RegEdit都会截断显示,但如果将鼠标悬停在其上,则会以单行提示显示全文。 (当然,除非你有多个宽显示器,否则不可能全部读取,因为你无法滚动提示窗口。)

你的OwnerDraw代码(如果你有的话)是不可能的,因为你没有发布它。仅仅设置OwnerDraw := True;没有提供任何事件来进行绘图。

正如评论:如果我是你,我会重新考虑你的设计。从UI的角度来看,这很糟糕,我可以证明原因。将上面的代码更改为原始690值并运行代码。您将看到第一列确实将其宽度设置为足以显示全部,即使文本停在同一点(88-8)。但是,请注意你必须在多大程度上滚动才能找到第二列?如果我使用你的软件,那会很臭。

在标题中显示少量文本会更好,如果用户点击它以表明他们确实想要全部阅读,则在标签或备忘录控件中显示全文,或者在弹出窗口中显示它。

相关问题