I am using Delphi XE3 and want to implement Windows Thumbnail style to show a list of images via TListView control.
What I need is like below:
The images are displayed as thumbnail style, there is a caption below each image. And when I click the image, the image together with the caption will be shown as selected...
To improve the performance, I do not want to load all the images into an image list beforehand, instead, I want to load the image when it is to be displayed. Therefore, I am thinking of using OnCustomDrawItem and OnAdvancedCustomDrawItem.
Below is a very simple version of my plan(I set the style of the list view to vsIcon):
procedure TForm1.FormCreate(Sender: TObject);
var
ListItem1: TListItem;
begin
ListItem1 := ListView1.Items.Add;
ListItem1.Caption := 'Chrysanthemum';
end;
procedure TForm1.ListView1AdvancedCustomDrawItem(Sender: TCustomListView;
Item: TListItem; State: TCustomDrawState; Stage: TCustomDrawStage;
var DefaultDraw: Boolean);
var
JPEG: TJPEGImage;
R: TRect;
begin
{
R := Item.DisplayRect(drBounds);
JPEG := TJPEGImage.Create;
JPEG.LoadFromFile('C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum (2).jpg');
Sender.Canvas.StretchDraw(R, JPEG);
}
end;
procedure TForm1.ListView1CustomDrawItem(Sender: TCustomListView;
Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
var
JPEG: TJPEGImage;
R: TRect;
begin
R := Item.DisplayRect(drBounds);
JPEG := TJPEGImage.Create;
JPEG.LoadFromFile('C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum (2).jpg');
Sender.Canvas.StretchDraw(R, JPEG);
end;
But the result is not satisfactory, as follows:
I cannot find a way to set the size of each icon. (All icon will have the same size).
I try to put the codes in OnCustomDrawItem and OnAdvancedCustomDrawItem. I cannot figure out much differences between these twos. The only main difference that in Advancedxxx version, the caption is editable. I cannot understand why.
The caption is not displayed under the image, instead, it is in the middle of the image, that is not desired. How to fix that?
Thanks