[Back To Directory]

Extract An Icone From A File

1. Open Borland C++ Builder.
2. Place a Button on the form.
3. Place an Image on the form bellow Button1.
4. Place a Label on the form bellow image1.
5. Place a button on the form.
6. Place an ImageList on the form.
7. Place an OpenDialog on the form.
8. Select Form1 and go to the Object Inspector, click the Events tab and add the OnCreate function.

//This is what the FormCreate function should look like:

void __fastcall TForm1::FormCreate(TObject *Sender)
{
   SHFILEINFO file_info_def;
   DWORD ImageHandle = SHGetFileInfo("",
         0,
         &file_info_def,
         sizeof(file_info_def),
         SHGFI_ICON | SHGFI_ICON | SHGFI_SHELLICONSIZE | SHGFI_SYSICONINDEX);

   if (ImageHandle)
   {
      ImageList1->Handle = ImageHandle;
      ImageList1->ShareImages = true;
   }
}
//---------------------------------------------------------------------------


9. Double click the button.
10. Type the following red code in the button function:

//This is what the Button1Click function should look like:

void __fastcall TForm1::Button1Click(TObject *Sender)
{
   if(OpenDialog1->Execute())
   {
      SHFILEINFO file_info_def;
      DWORD result = SHGetFileInfo(OpenDialog1->FileName.c_str(),
         0,
         &file_info_def,
         sizeof(file_info_def),
         SHGFI_ICON | SHGFI_SHELLICONSIZE | SHGFI_SYSICONINDEX);

      Label1->Caption = OpenDialog1->FileName;

      if(result)
         ImageList1->GetIcon(file_info_def.iIcon,Image1->Picture->Icon);;
   }
}
//---------------------------------------------------------------------------


11. Run the program and click Button1. Browse to a file and the files icone will be displayed in Image1. You can also use Browse to Folder to get the icon of a folder.



[Back To Directory]