Главная страница | назад





Article #22091: How to get a file's associated icon.

Question

How can I programatically get a file's associated default icon without looking it up in the registry and loading it explicitly?

Answer

This is a lot easier than most people think. There is a relatively little-known function called ExtractAssociatedIcon. The prototype is as follows:

HICON ExtractAssociatedIcon(
    HINSTANCE hInst, // application instance handle
    LPTSTR lpIconPath, // path and filename of file for which icon is wanted
    LPWORD lpiIcon // pointer to icon index
);

Example:

//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
HICON ico;
WORD idx;
ico = ExtractAssociatedIcon(HInstance,"c:WINNT\\system32\\ntimage.gif",&idx);
Image1->Picture->Icon->Handle = ico;
}
//---------------------------------------------------------------------------

Last Modified: 27-OCT-00