- 人气:
- 放大
- 缩小
- 二维码
- 赞赏
delphi ListView1 加载文件列表用法
//OpenDialog1 设置位文件多选 GetFileSize 本站可以搜索到
var
i, numExisting, fileSize, fileHandle: Cardinal;
item: TListItem;
filePath: string;
begin
if OpenDialog1.Execute then
begin
numExisting := ListView1.Items.Count;
for i := 0 to OpenDialog1.Files.Count - 1 do
begin
filePath := OpenDialog1.Files[i];
fileHandle := FileOpen(filePath, fmOpenRead);
fileSize := GetFileSize(fileHandle, nil);
FileClose(fileHandle);
item := ListView1.Items.Add;
item.Caption := IntToStr(i + numExisting + 1);
item.SubItems.Add(filePath);
item.SubItems.Append(IntToStr(fileSize));
end;
end;
//ListView1 移除选中的文件
var
i, j, k: Cardinal;
removalIndices: array of Cardinal;
begin
if ListView1.SelCount > 0 then
begin
j := 0;
k := 1;
SetLength(removalIndices, ListView1.SelCount);
for i := 0 to ListView1.Items.Count - 1 do
begin
if ListView1.Items[i].Selected then
begin
removalIndices[j] := i;
Inc(j);
end
else
begin
ListView1.Items[i].Caption := IntToStr(k);
Inc(k);
end;
end;
for i := Length(removalIndices) - 1 downto 0 do
ListView1.Items[removalIndices[i]].Delete;
end;
//
ListView1 清空
ListView1.Clear;
//
for i := 0 to ListView1.Items.Count - 1 do
begin
ListView1.Items[i].SubItems[1]
end;
//收藏
for i := 1 to ListView1.Items.Count - 1 do
begin
ListView1.Items[i].SubItems[0]
end;