I'm trying to create a listbox with images. The images will be fetched from a TImage component ; i don't want to use a TImageList because TImage can handle a lot of image types (png, gif, jpg) and i don't have to convert it to populate the Imagelist.
So i've set my listbox style to lbOwnerDrawVariable and i'm trying to paint the image from TImage into the listbox. I've set Image1 width and height to 50 because this is the size i want the images to have on the listbox.
Here is my code :
procedure TForm2.listbox1DrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState);
var
CenterText: Integer;
begin
listbox1.Canvas.FillRect(Rect);
listbox1.Canvas.draw(rect.Left 4,rect.Top 4,image1.Picture.graphic);
CenterText := (Rect.Bottom - Rect.top - listbox1.Canvas.TextHeight(text)) div 2;
listbox1.Canvas.TextOut(Rect.left 58, Rect.top CenterText, listbox1.Items.Strings[index]);
end;
But instead of putting the image in each listbox item, it's drawing a lot of images inside thel listbox itself, with its original size instead of 50... what's wrong with my code ?
CodePudding user response:
Image1.Width
and Image1.Height
are the dimensions of the TImage
control. They have nothing to do with the dimensions of the Image1.Picture
.