德尔福:如何绘制小图标的列表视图上CustomDrawItem [英] Delphi: how to draw small icons in List View on CustomDrawItem

查看:381
本文介绍了德尔福:如何绘制小图标的列表视图上CustomDrawItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何延长这一code:<一href=\"http://stackoverflow.com/questions/6294985/listview-in-vsreport-mode-colouring-of-items-and-rows\">ListView在项目和行vsReport模式的着色。,以画小图标?

how to extend this code: ListView in vsReport mode colouring of Items and rows. to draw small icons?

和为什么我有错误列表索引越界(2)如果我有3列?

and why do I have the error 'List index out of bounds (2)' if I have 3 columns?

谢谢!

推荐答案

有许多方法来绘制的图标,这取决于哪里来弗罗姆(一个文件,资源,系统的图标等),并根据是否应该对所有的项目,或者如果每个项目都有自己的图标一个图标。总之,总体思路应该是从code在previous问题这个加长版清楚(我也固定在了界外的错误...):

There are many ways to draw the icons, depending on where they come frome (a file, a resource, a system icon, etc.) and depending on if there should be a single icon for all items or if every item has its own icon. Anyhow, the general idea should be clear from this extended version of the code in the previous question (and I have also fixed the out-of-bounds bug...):

type
  TForm1 = class(TForm)
  ...
  private
    { Private declarations }
    bm: TBitmap;
  ...
  end;

...

implementation

...

procedure TForm1.FormCreate(Sender: TObject);
begin
  bm := TBitmap.Create;
  bm.LoadFromFile('C:\Users\Andreas Rejbrand\Desktop\img.bmp');
end;

procedure TForm1.ListView1DrawItem(Sender: TCustomListView; Item: TListItem;
  Rect: TRect; State: TOwnerDrawState);
var
  i: Integer;
  x1, x2: integer;
  r: TRect;
  S: string;
const
  DT_ALIGN: array[TAlignment] of integer = (DT_LEFT, DT_RIGHT, DT_CENTER);
begin
  if Odd(Item.Index) then
  begin
    Sender.Canvas.Font.Color := clBlack;
    Sender.Canvas.Brush.Color := $F6F6F6;
  end
  else
  begin
    Sender.Canvas.Font.Color := clBlack;
    Sender.Canvas.Brush.Color := clWhite;
  end;
  Sender.Canvas.Brush.Style := bsSolid;
  Sender.Canvas.FillRect(Rect);
  x1 := 0;
  x2 := 0;
  r := Rect;
  Sender.Canvas.Brush.Style := bsClear;
  Sender.Canvas.Draw(3, r.Top + (r.Bottom - r.Top - bm.Height) div 2, bm);
  for i := 0 to ListView1.Columns.Count - 1 do
  begin
    inc(x2, ListView1.Columns[i].Width);
    r.Left := x1;
    r.Right := x2;
    if i = 0 then
    begin
      S := Item.Caption;
      r.Left := bm.Width + 6;
    end
    else
      S := Item.SubItems[i - 1];
    DrawText(Sender.Canvas.Handle,
      S,
      length(S),
      r,
      DT_SINGLELINE or DT_ALIGN[ListView1.Columns[i].Alignment] or
        DT_VCENTER or DT_END_ELLIPSIS);
    x1 := x2;
  end;
end;

这篇关于德尔福:如何绘制小图标的列表视图上CustomDrawItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆