在拉撒路中处理项目后,调整颜色 在 TListview 中选定的项目



在花了很多时间之后

'''

procedure Tf100.lvListCustomDrawItem(Sender: TCustomListView; Item: TListItem;
State: TCustomDrawState; var DefaultDraw: Boolean);
begin
Sender.Canvas.Font.Color := clRed;    // default was clBlack
end;
procedure Tf100.lvListCustomDrawSubItem(Sender: TCustomListView;
Item: TListItem; SubItem: Integer; State: TCustomDrawState;
var DefaultDraw: Boolean);
begin
Sender.Canvas.Font.Color := clRed;    // Default was clBlack;
end;

'''

我能够更改列表视图中项目的颜色。但是,我真正想要的是:

  1. 以黑色显示列表视图中的所有项目(完成,正常(
  2. 以编程方式遍历所有项目,一次选择 1 个项目(完成,好的(但是 将字体设置为红色(所选项目的颜色(。
  3. 阅读项目的标题(文件名(并处理该文件名(完成,好的(
  4. 如果处理成功,请将当前项目设为绿色,如果不成功,则将其保留为红色。
  5. 选择列表中的下一项,直到列表末尾(完成,确定(。

希望这是有道理的。总结一下,它的意思是:如何根据某个函数的结果更改 TListItem 的颜色。我什至不知道在什么情况下我应该调用这个函数(在列表视图的事件中?

现在,我有这个:

iIndex := 0;
repeat
pbProgress.Position := iIndex + 1;
try
lvList.SetFocus;                  // The color should become clRED
lvList.ItemIndex := iIndex;
lvList.Items[iIndex].Selected := true;
lvList.Selected.MakeVisible(True);
sFile := txtFolder.text + '' + lvList.Items[iIndex].Caption;
DisplayPicture(sFile);
application.processmessages ;
txtCurrent.Text := Format('%D of %D',[iIndex + 1, iMax]);
bOke := ProcessFile(sFile);
if (bOke) then begin
// Current index color should become clGREEN but I don't know how
end;
except
on E:Exception do begin
bOke := False;
msgbox(Format('Error at index %d',[iIndex]),'ooops',acError,[mbOk],['Press']);
end;
end;
if (bOke) then
Inc(FGood)
else
Inc(FWrong);
UpdateStats(Self);
Inc(iIndex);
until (iIndex = iMax);

希望有人能够回答我的问题。 此致敬意 马丁

我没有安装Lazarus,所以以下是Delphi完成的。我 99.9% 确定它也适合您。

您已经了解了如何在OnCustomDrawItem()OnCustomDrawSubItem()事件中定义项目的颜色。

在这里,重要的是要了解TListView不会将该颜色设置保存在任何地方。如果您考虑上下滚动列表,列表视图会根据需要调用每个项目的OnCustomDrawItem()OnCustomDrawSubItem()事件。自上次显示以来,项目的状态可能已更改。

因此,您需要将文件处理的结果保存在某个位置,以便您可以在OnCustomDrawItem()OnCustomDrawSubItem()事件中告诉列表视图对每个项目使用什么颜色。

我建议你创建一个数据结构,一个简单的类,用于保存文件名和处理结果。然后,在将文件添加到列表视图以进行显示时,还会添加对列表视图的每个文件对象的引用。

首先是一些类型和变量声明

type
// enum for the different processing states
TProcessEnum = (NotDone, Processing, DoneSucceeded, DoneFailed);
// class for holding file names and process state
TMyFileData = class
FileName: string;
ProcessState: TProcessEnum;
end;
// Array type for file data
TMyFileDataArray = array of TMyFileData;
const
// string representation for process states
ProcessStates: array of string = ['Not done', 'Processing', 'Succeeded', 'Failed'];
var
// data array
MyFileDataArray: TMyFileDataArray;

然后创建一些演示数据

procedure TForm21.FormCreate(Sender: TObject);
var
i: integer;
begin
SetLength(MyFileDataArray, 9);
for i := 0 to Length(MyFileDataArray)-1 do
begin
MyFileDataArray[i] := TMyFileData.Create;
MyFileDataArray[i].FileName := 'c:tmpexample'+IntToStr(i)+'.txt';
MyFileDataArray[i].ProcessState := NotDone;
end;
end;

然后将文件名和数据引用添加到 LV 项,并将进程状态添加到 LV 子项。数据引用是要AddItem()的第二个参数,稍后可以使用Item.DataOnCustomDrawItem()OnCustomDrawSubItem()事件中访问它

procedure TForm21.Button1Click(Sender: TObject);
var
i: integer;
begin
for i := 0 to Length(MyFileDataArray)-1 do
begin
ListView1.AddItem(MyFileDataArray[i].FileName, MyFileDataArray[i]);
ListView1.Items[i].SubItems.Add(ProcessStates[ord(MyFileDataArray[i].ProcessState)]);
end;
end;

并运行所有文件的处理

procedure TForm21.Button2Click(Sender: TObject);
var
i: integer;
begin
for i := 0 to Length(MyFileDataArray)-1 do
begin
MyFileDataArray[i].ProcessState := processing;
ListView1.Items[i].SubItems[0] := ProcessStates[ord(MyFileDataArray[i].ProcessState)];
ListView1.Repaint;  // force a visual update
sleep(500); // simulate processing delay 
MyFileDataArray[i].ProcessState := TProcessEnum(random(2) + 2);
ListView1.Items[i].SubItems[0] := ProcessStates[ord(MyFileDataArray[i].ProcessState)];
ListView1.Repaint;  // force a visual update
end;
end;

将进程状态转换为颜色的帮助程序函数

function TForm21.ItemColor(ProcessState: TProcessEnum): TColor;
begin
case ProcessState of
DoneSucceeded: Result := clGreen;
Processing,
DoneFailed: Result := clRed;
else
Result := clBlack;
end;
end;

OnCustomDrawItem()OnCustomDrawSubItem()都只有以下行

Sender.Canvas.Font.Color := ItemColor(TMyFileData(Item.Data).ProcessState);

但请注意,您可能希望根据State: TCustomDrawState值进一步修改背景或字体颜色。

最新更新