Alpha_AI
asked on
Getting the images from all file extensions. such as pdf, .doc and so on
How do I get the icons that have assigned to all the file types
such as .pdf, .doc, .mpg
and assign those to TImage
I am after the small images and big images.
I know how to get the small images for executable.
TheIcon := FOrm1.GetSmallIconFromFile (Trim(Data ^.Type1),0 );
if TheIcon <> nil then
begin
TImage(((Form1.FindCompone nt('Edit'+ Data^.Mean ing2) as TEdit).Parent).Controls[i] ).Picture. Assign(The Icon); // assign the icon to the TImage that exists on the panel
end;
function TForm1.GetSmallIconFromFil e(FName: String; idx: Word): TIcon;
var
LargeIcon : Hicon;
SmallIcon : Hicon;
Icon : TIcon;
begin
Icon := TIcon.Create();
ExtractIconEx(PChar(FName) , idx, LargeIcon, SmallIcon, 1);
if SmallIcon <= 1 then
begin
Icon.Handle := SmallIcon;
Result := Icon;
end
else begin
Icon.Handle := LargeIcon;
Result := Icon;
end;
ive tried to pull a image from many of the other file extension other than .exe and i cant rip it out.
Whats the solution?
such as .pdf, .doc, .mpg
and assign those to TImage
I am after the small images and big images.
I know how to get the small images for executable.
TheIcon := FOrm1.GetSmallIconFromFile
if TheIcon <> nil then
begin
TImage(((Form1.FindCompone
end;
function TForm1.GetSmallIconFromFil
var
LargeIcon : Hicon;
SmallIcon : Hicon;
Icon : TIcon;
begin
Icon := TIcon.Create();
ExtractIconEx(PChar(FName)
if SmallIcon <= 1 then
begin
Icon.Handle := SmallIcon;
Result := Icon;
end
else begin
Icon.Handle := LargeIcon;
Result := Icon;
end;
ive tried to pull a image from many of the other file extension other than .exe and i cant rip it out.
Whats the solution?
ASKER
Thanks TomasHelg
This one http://www.swissdelphicenter.ch/torry/showcode.php?id=421 works and output the images into a listview.
This one http://www.swissdelphicenter.ch/torry/showcode.php?id=1108 can output the image to a TImage object
however the first one http://www.swissdelphicenter.ch/torry/showcode.php?id=421 for some reason has a better image quality (brighter and more crisp)
Here is the code for that one, I have some problem trying to output the image to a TImage object. I have modified it so it can only find one find to put into the Timage object
You will have to change the file to match your computer.
uses
ShellApi;
procedure LV_InsertFiles(strPath: string; ListView: TListView; ImageList: TImageList);
var
i: Integer;
Icon: TIcon;
SearchRec: TSearchRec;
ListItem: TListItem;
FileInfo: SHFILEINFO;
begin
// Create a temporary TIcon
Icon := TIcon.Create;
ListView.Items.BeginUpdate ;
try
// search for the first file
i := FindFirst(strPath + 'twain.dll', faAnyFile, SearchRec);
while i = 0 do
begin
with ListView do
begin
// On directories and volumes
if ((SearchRec.Attr and FaDirectory <> FaDirectory) and
(SearchRec.Attr and FaVolumeId <> FaVolumeID)) then
begin
ListItem := ListView.Items.Add;
//Get The DisplayName
SHGetFileInfo(PChar(strPat h + SearchRec.Name), 0, FileInfo,
SizeOf(FileInfo), SHGFI_DISPLAYNAME);
Listitem.Caption := FileInfo.szDisplayName;
// Get The TypeName
SHGetFileInfo(PChar(strPat h + SearchRec.Name), 0, FileInfo,
SizeOf(FileInfo), SHGFI_TYPENAME);
ListItem.SubItems.Add(File Info.szTyp eName);
//Get The Icon That Represents The File
SHGetFileInfo(PChar(strPat h + SearchRec.Name), 0, FileInfo,
SizeOf(FileInfo), SHGFI_ICON or SHGFI_SMALLICON);
icon.Handle := FileInfo.hIcon;
ListItem.ImageIndex := ImageList.AddIcon(Icon);
// this is where i add in my little code
Image1.Picture.Assign(Icon );
// Destroy the Icon
DestroyIcon(FileInfo.hIcon );
end;
end;
i := FindNext(SearchRec);
end;
finally
Icon.Free;
ListView.Items.EndUpdate;
end;
end;
procedure TForm1.Button1Click(Sender : TObject);
begin
// Assign a Imagelist to the ListView
ListView1.SmallImages := ImageList1;
// Show Listview in Report Style and add 2 Columns
ListView1.ViewStyle := vsReport;
ListView1.Columns.Add;
ListView1.Columns.Add;
LV_InsertFiles('C:\Windows \', ListView1, ImageList1);
end;
I dont know why it doesnt work. Any clues?
Ben
This one http://www.swissdelphicenter.ch/torry/showcode.php?id=421 works and output the images into a listview.
This one http://www.swissdelphicenter.ch/torry/showcode.php?id=1108 can output the image to a TImage object
however the first one http://www.swissdelphicenter.ch/torry/showcode.php?id=421 for some reason has a better image quality (brighter and more crisp)
Here is the code for that one, I have some problem trying to output the image to a TImage object. I have modified it so it can only find one find to put into the Timage object
You will have to change the file to match your computer.
uses
ShellApi;
procedure LV_InsertFiles(strPath: string; ListView: TListView; ImageList: TImageList);
var
i: Integer;
Icon: TIcon;
SearchRec: TSearchRec;
ListItem: TListItem;
FileInfo: SHFILEINFO;
begin
// Create a temporary TIcon
Icon := TIcon.Create;
ListView.Items.BeginUpdate
try
// search for the first file
i := FindFirst(strPath + 'twain.dll', faAnyFile, SearchRec);
while i = 0 do
begin
with ListView do
begin
// On directories and volumes
if ((SearchRec.Attr and FaDirectory <> FaDirectory) and
(SearchRec.Attr and FaVolumeId <> FaVolumeID)) then
begin
ListItem := ListView.Items.Add;
//Get The DisplayName
SHGetFileInfo(PChar(strPat
SizeOf(FileInfo), SHGFI_DISPLAYNAME);
Listitem.Caption := FileInfo.szDisplayName;
// Get The TypeName
SHGetFileInfo(PChar(strPat
SizeOf(FileInfo), SHGFI_TYPENAME);
ListItem.SubItems.Add(File
//Get The Icon That Represents The File
SHGetFileInfo(PChar(strPat
SizeOf(FileInfo), SHGFI_ICON or SHGFI_SMALLICON);
icon.Handle := FileInfo.hIcon;
ListItem.ImageIndex := ImageList.AddIcon(Icon);
// this is where i add in my little code
Image1.Picture.Assign(Icon
// Destroy the Icon
DestroyIcon(FileInfo.hIcon
end;
end;
i := FindNext(SearchRec);
end;
finally
Icon.Free;
ListView.Items.EndUpdate;
end;
end;
procedure TForm1.Button1Click(Sender
begin
// Assign a Imagelist to the ListView
ListView1.SmallImages := ImageList1;
// Show Listview in Report Style and add 2 Columns
ListView1.ViewStyle := vsReport;
ListView1.Columns.Add;
ListView1.Columns.Add;
LV_InsertFiles('C:\Windows
end;
I dont know why it doesnt work. Any clues?
Ben
ASKER
On this line
i := FindFirst(strPath + 'twain.dll', faAnyFile, SearchRec);
I modified it so it only bring up the twain.dll file along with its icon.
For some reason i cannot output the image out to a TImage object
Ben
i := FindFirst(strPath + 'twain.dll', faAnyFile, SearchRec);
I modified it so it only bring up the twain.dll file along with its icon.
For some reason i cannot output the image out to a TImage object
Ben
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I found a really simple way somewhere online
begin
InfoSize := SizeOf(TSHFileInfo);
FillChar(Info, InfoSize, 0);
SHGetFileInfo(PChar(Trim(D ata^.Type1 )), 0, Info, InfoSize, SHGFI_ICON or SHGFI_SMALLICON);
TImage(((Form1.FindCompone nt('Edit'+ Data^.Mean ing2) as TEdit).Parent).Controls[i] ).Picture. icon.Handl e := Info.hIcon;
end;
with this code its so easy to do.
Ben
begin
InfoSize := SizeOf(TSHFileInfo);
FillChar(Info, InfoSize, 0);
SHGetFileInfo(PChar(Trim(D
TImage(((Form1.FindCompone
end;
with this code its so easy to do.
Ben
ASKER
well the code i posted is not the correct code, its my modified code for my program :-)
Ben
Ben
ASKER
Thanks again.
Ben
Ben
Check out these examples:
http://www.swissdelphicenter.ch/torry/showcode.php?id=421
http://www.swissdelphicenter.ch/torry/showcode.php?id=218
http://www.swissdelphicenter.ch/torry/showcode.php?id=1108
Hope they help.
Regards,
Tomas Helgi