Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
500 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi







Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (0)


Getting the associated Icon of any fileFormat this article printer-friendly!Bookmark function is only available for registered users!
Using the Shell Functions provided by MS Windows
Product:
Delphi 3.x (or higher)
Category:
Shell API
Skill Level:
Scoring:
Last Update:
04/25/2005
Search Keys:
delphi delphi3000 article borland vcl code-snippet extract associated file icon SHGetFileInfo image list handle
Times Scored:
7
Visits:
4837
Uploader: Daniel Wischnewski
Company: Delphi-PRAXiS
Reference: Delphi-PRAXiS
 
Question/Problem/Abstract:
This article shows you how to extract the associated icon of any file. Including files that use shell icon handlers such as ICO and Corel Draw files.
Answer:



Just a few days ago, I published an article on creating an shell icon handler allowing you to manually create icons for every file of a specific file type.

This article shows you how to get such icons without much work. Using the function SHGetFileInfo from the ShellAPI unit it is a rather simple task.

This article will simply provide you with the steps you have to take in order to get the icon.

Starting with creating a new application drop a PaintBox, a Button and a OpenDialog on the form. Fill the Button OnClick with the following procedure:

procedure TForm1.Button1Click(Sender: TObject);
begin
  if
OpenDialog1.Execute then
    
DrawFile(OpenDialog1.FileName);
end;

Next you need the DrawFile method. The lines are commented.

procedure TForm1.DrawFile(Name: String);
var
  
FileInfo: TSHFileInfo;
  ImageListHandle: THandle;
  aIcon: TIcon;
begin
  
// clear the memory
  
FillChar(FileInfo, SizeOf(FileInfo), #0);
  // get a handle to the ImageList for the file selected
  
ImageListHandle := SHGetFileInfo(
    PChar(Name), 0, FileInfo, SizeOf(FileInfo),
    // we want an icon in LARGE
    
SHGFI_ICON or SHGFI_LARGEICON
  );
  try
    
// create a icon class
    
aIcon := TIcon.Create;
    try
      
// assign the handle of the icon returned
      
aIcon.Handle := FileInfo.hIcon;
      // lets paint it transparent
      
aIcon.Transparent := True;
      with PaintBox1 do begin
        
// resize the paint box to icon size
        
Width := aIcon.Width;
         Height := aIcon.Height;
         // tell our app to first resize the paint box before we draw the icon
        
Application.ProcessMessages;
         // clear the paintbox
        
Canvas.Rectangle(-1, -1, Succ(Width), Succ(Height));
         // copy the icon
        
Canvas.Draw(0, 0, aIcon);
      end;
    finally
      
// free our icon class
      
FreeAndNil(aIcon);
    end;
  finally
    
// !!! FREE THE ICON PROVIDED BY THE SHELL
    
DestroyIcon(FileInfo.hIcon);
    // free the image list handle
    
ImageList_Destroy(ImageListHandle);
  end;
end;


I hope this will help you.

Daniel Wischnewski.





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment













 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
A. B. Talal
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)