Delphi TListview CustomDrawItem更改字体

Delphi TListview CustomDrawItem更改字体,delphi,delphi-7,vcl,Delphi,Delphi 7,Vcl,我有一个listview,如果用户双击它,我想将项目的字体颜色更改为clRed。但是,如果用户双击另一个项目,所有其他项目都应返回黑色字体颜色,新双击的项目将更改为CLRD,依此类推 我这里有这个代码: var CurrentProfile : String; // Global var that stores the caption of the double clicked item. procedure TForm1.ListView1DblClick(Sender: TObject

我有一个listview,如果用户双击它,我想将项目的字体颜色更改为clRed。但是,如果用户双击另一个项目,所有其他项目都应返回黑色字体颜色,新双击的项目将更改为CLRD,依此类推

我这里有这个代码:

var
 CurrentProfile : String; // Global var that stores the caption of the double clicked item.


procedure TForm1.ListView1DblClick(Sender: TObject);
begin
 if ListView1.Selected <> NIL then CurrentProfile := ListView1.Selected.Caption;
end;

procedure TForm1.ListView1CustomDrawItem(
Sender: TCustomListView; Item: TListItem; State: TCustomDrawState;
var DefaultDraw: Boolean);
begin
 if item.Caption = CurrentProfile then begin
  Sender.Canvas.Font.Color := clRed;
 end else begin 
  Sender.Canvas.Font.Color := clBlack; // if not change it back to black
 end;
end;
var
CurrentProfile:String;//存储双击项标题的全局变量。
程序TForm1.ListView1DblClick(发送方:ToObject);
开始
如果ListView1.Selected为零,则CurrentProfile:=ListView1.Selected.Caption;
结束;
程序TForm1.ListView1CustomDrawItem(
发件人:TCustomListView;项:TListItem;状态:TCustomDrawState;
var DefaultDraw:Boolean);
开始
如果item.Caption=CurrentProfile,则开始
Sender.Canvas.Font.Color:=clRed;
结束,否则开始
Sender.Canvas.Font.Color:=clBlack;//如果没有,请将其改回黑色
结束;
结束;
使用此代码,每个双击的项都将保留在CLED中。为什么不改回黑色?请帮忙。先谢谢你


注:我使用delphi7。

双击事件处理程序需要强制重新绘制。调用
ListView1。在该处理程序的右端使
无效。这将强制列表视图上的绘制循环

procedure TForm1.ListView1DblClick(Sender: TObject);
begin
  if ListView1.Selected <> NIL then 
  begin
    CurrentProfile := ListView1.Selected.Caption; 
    ListView1.Invalidate;
  end;
end;
过程TForm1.ListView1DblClick(发送方:TObject);
开始
如果ListView1.选择了NIL,则
开始
CurrentProfile:=ListView1.Selected.Caption;
ListView1.无效;
结束;
结束;