記得你是不是爲不文本匹配列表項重置ListView的Canvas.Font
參數的值。
procedure TMainForm.ListCustomDrawItem(Sender: TCustomListView;
Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
begin
if Edit2.Text = Item.Caption then
begin
Sender.Canvas.Font.Color := Font.Font.Color;
Sender.Canvas.Font.Style := Font.Font.Style;
end else begin
// add this...
Sender.Canvas.Font.Color := Sender.Font.Color;
Sender.Canvas.Font.Style := Sender.Font.Style;
end;
end;
話雖這麼說,如果你知道你想提前使用時間的顏色,以不同的方式來設置每個項目的顏色是從TListItem
派生新類和你自己的Font
屬性添加到它,那麼你可以在繪圖時使用它。
type
TMyListItem = class(TListItem)
private
fFont: TFont;
procedure FontChanged(Sender: TObject);
procedure SetFont(AValue: TFont);
public
constructor Create(AOwner: TListItems); override;
destructor Destroy; override;
property Font: TFont read fFont write SetFont;
end;
constructor TMyListItem.Create(AOwner: TListItems);
begin
inherited;
fFont := TFont.Create;
fFont.OnChange := FontChanged;
end;
destructor TMyListItem.Destroy;
begin
fFont.Free;
inherited;
end;
procedure TMyListItem.FontChanged(Sender: TObject);
begin
Update;
end;
procedure TMyListItem.SetFont(AValue: TFont);
begin
fFont.Assign(AValue);
end;
// OnCreateItemClass event handler
procedure TMainForm.ListCreateItemClass(Sender: TCustomListView; var ItemClass: TListItemClass);
begin
ItemClass := TMyListItem;
end;
procedure TMainForm.ListCustomDrawItem(Sender: TCustomListView;
Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
begin
Sender.Canvas.Font := TMyListItem(Item).Font;
end;
...
var
Item: TMyListItem;
begin
...
Item := TMyListItem(List.Items.Add);
Item.Caption := ...;
if Edit2.Text = Item.Caption then
Item.Font := Font.Font // assign from font dialogue
else
Item.Font := List.Font; // assign from listview
...
end;
感謝更正雷米:) –