2011-07-07 12 views
0

このコードを拡張する方法:ListView in vsReport mode colouring of Items and rows小さなアイコンを描画するには?Delphi:CustomDrawItemのリストビューで小さなアイコンを描画する方法

なぜ3つの列がある場合、 'List index of bounds(2)'というエラーが表示されますか?

ありがとうございます!

+1

を古いコードのバグで、3つのアイテムがある場合、最初のものは 'Caption'に格納され、残りの2つはそれぞれ' Subitems [0] 'と' Subitems [1] 'にあります。 –

+0

@アンドレアス・レイブランド、ありがとう!!!!!!!ありがとう – maxfax

答えて

5

アイコンを描画する方法はたくさんあります(ファイル、リソース、システムアイコンなど)、すべてのアイテムに単一のアイコンが必要かどうか、またはすべてのアイテム独自のアイコンを持っています。とにかく、一般的な考え方は、前の質問でこのコードの拡張バージョンから明らかである(と私はまた...範囲外のバグを修正した):約

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; 

Screenshot http://privat.rejbrand.se/TListViewCustomDrawIcon.png

+4

私はpbrushで作ったすばらしいアイコンの賞金を得るべきだと思います。 –

+3

'TImageList.Draw()'は、このコード –

+0

@Davidでイメージを描画する別の一般的な方法です。 –

関連する問題