2011-07-07 68 views

回答

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。 –