gpt4 book ai didi

delphi - 将图标从图像列表绘制到 ListView ?

转载 作者:行者123 更新时间:2023-12-02 07:02:25 29 4
gpt4 key购买 nike

如何将imagelist中的图标绘制到listview上?我使用此代码来更改 ListView 中的选择颜色,但它没有图像列表中的图标。

procedure TForm2.ListView1DrawItem(Sender: TCustomListView;
Item: TListItem; Rect: TRect; State: TOwnerDrawState);
var
x, y, i, w, h: integer;
begin
with ListView1, Canvas do
begin

if odSelected in State then
begin
Brush.Color := clRed;
Pen.Color := clWhite;
end else
begin
Brush.Color := Color;
Pen.Color := Font.Color;
end;
Brush.Style := bsSolid;
FillRect(rect);
h := Rect.Bottom - Rect.Top + 1;
x := Rect.Left + 1;
y := Rect.Top + (h - TextHeight('Hg')) div 2;
TextOut(x, y, Item.Caption);
inc(x, Columns[0].Width);
for i := 0 to Item.Subitems.Count - 1 do begin
TextOut(x, y, Item.SubItems[i]);
w := Columns[i + 1].Width;
inc(x, w);
end;
end;
end;

最佳答案

你也必须自己画图。

procedure DrawListViewItem(ListView: TListView; Item: TListItem; Rect: TRect; 
State: TOwnerDrawState; SelectedBrushColor, SelectedFontColor, BrushColor, FontColor: TColor);
var
x, y, i, w, h, iw, ih: integer;
begin
with ListView do
begin
if odSelected in State then
begin
Canvas.Brush.Color := SelectedBrushColor;
Canvas.Font.Color := SelectedFontColor;
end else
begin
Canvas.Brush.Color := BrushColor;
Canvas.Font.Color := FontColor;
end;
Canvas.Brush.Style := bsSolid;
Canvas.FillRect(rect);

h := Rect.Bottom - Rect.Top + 1;

if Assigned(SmallImages) then
begin
iw := SmallImages.Width;
ih := SmallImages.Height;
x := Rect.Left + 1;
if Item.ImageIndex >= 0 then
SmallImages.Draw(Canvas, Rect.Left + x, Rect.Top +(h - ih) div 2, Item.ImageIndex);
x := x + iw + 2;
end
else
begin
iw := 0;
ih := 0;
x := Rect.Left + 1;
end;

y := Rect.Top + (h - Canvas.TextHeight('Hg')) div 2;
Canvas.TextOut(x, y, Item.Caption);
inc(x, Columns[0].Width - iw);
for i := 0 to Item.Subitems.Count - 1 do begin
Canvas.TextOut(x, y, Item.SubItems[i]);
w := Columns[i + 1].Width;
inc(x, w);
end;
end;
end;

procedure TForm1.ListView1DrawItem(Sender: TCustomListView; Item: TListItem; Rect: TRect; State: TOwnerDrawState);
begin
DrawListViewItem(ListView1, Item, Rect, State, clRed, clWhite, ListView1.Color, ListView1.Font.Color);
end;

我已将绘图代码移至单独的函数中。这使得它可以重复使用并且更干净。直接在表单方法中使用 with 可能会产生不需要的副作用。对于双 with 子句也是如此,因此我只使用了一个(尽管我倾向于在代码中完全避免使用 with)。

我注意到您使用了Pen.Color,但我将其更改为Font.Color,因为设置Pen没有任何效果在您的代码中,我假设您实际上想更改文本的颜色。

关于delphi - 将图标从图像列表绘制到 ListView ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27738678/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com