Delphi TListView&TJvListView子项255字符限制突破方法咨询
突破Delphi TListView/TJvListView子项255字符限制的方法
Delphi原生TListView(及Jvcl的TJvListView)的SubItems默认存在255字符长度限制,根源是早期Windows控件API的ANSI缓冲区限制。以下是可靠的突破方案:
方法一:通过LVM_SETITEMTEXTW消息设置长文本
绕过Delphi封装层的限制,直接调用Unicode版本的Windows控件消息传递长文本。
辅助函数实现
uses Winapi.Windows, Winapi.Messages, Vcl.ComCtrls; procedure SetListViewSubItemText(ListView: TListView; ItemIndex, SubItemIndex: Integer; const Text: string); var LVItem: TLVItemW; begin if (ItemIndex < 0) or (ItemIndex >= ListView.Items.Count) then Exit; if (SubItemIndex < 0) or (SubItemIndex >= ListView.Columns.Count - 1) then Exit; FillChar(LVItem, SizeOf(LVItem), 0); LVItem.mask := LVIF_TEXT; LVItem.iItem := ItemIndex; LVItem.iSubItem := SubItemIndex + 1; // 子项索引从1开始(0对应主标题) LVItem.pszText := PWideChar(Text); SendMessageW(ListView.Handle, LVM_SETITEMTEXTW, WPARAM(ItemIndex), LPARAM(@LVItem)); end;
替换原有代码
将原代码中添加长文本的SubItems.Add调用替换为上述函数:
// 原代码:ListItem1.SubItems.Add('<RHINOSTRING ... />'); // 修改为: const LongText = '<RHINOSTRING English="Exploding this mesh will create %d individual meshes. This may be more than your system can safely manage using the available memory. You can use Weld to make the mesh explode into fewer pieces, or see Help for more information.'#10#10'Click OK to proceed with Explode, or Cancel to leave the mesh as is.[[24836]]" Localized="Exploding this mesh will create %d individual meshes. This may be more than your system can safely manage using the available memory. You can use Weld to make the mesh explode into fewer pieces, or see Help for more information.'#10#10'Click OK to proceed with Explode, or Cancel to leave the mesh as is.[[24836]]" />'; ListItem1 := ListView1.Items.Add; ListItem1.Caption := '22421 '; ListItem1.SubItems.Add('All Columns ' + IntToStr(ListView1.Columns.Count)); // 调用自定义函数设置第二个子项(索引1)的长文本 SetListViewSubItemText(ListView1, ListItem1.Index, 1, LongText);
方法二:确保Unicode编译支持(Delphi XE+)
Delphi XE及后续版本默认启用Unicode编译,若仍出现限制,需确认:
- ListView的
OwnerDraw属性设为False - 项目编译选项中“Use Unicode Strings”已勾选(默认开启)
此方法依赖Delphi封装的正确性,稳定性不如方法一。
TJvListView适配说明
TJvListView是原生TListView的扩展,上述方法完全适用,只需将函数参数中的TListView替换为TJvListView即可。
内容的提问来源于stack exchange,提问作者Gin
相关产品推荐
相关产品推荐

