Board index » cppbuilder » Getting files associated icon

Getting files associated icon

I want to fill a listview with a few files.  Next to the files I want the
associated icon.  I need the functionality of Outlook express and adding
file attachments.  ie.  If the file I select is bcb.exe is will put the
filename with the path and the icon of C++ and the blue buildings.  Thanks
 

Re:Getting files associated icon


Hi, Ryan.

Quote
Ryan Wade wrote:

> I want to fill a listview with a few files.  Next to the files I want the
> associated icon.  I need the functionality of Outlook express and adding
> file attachments.  ie.  If the file I select is bcb.exe is will put the
> filename with the path and the icon of C++ and the blue buildings.  Thanks

I use the following.

//---------------------------------------------------------------------------
// note: caller must call DestroyIcon()
//
HICON __fastcall GetAssociatedIcon(AnsiString filepath)
{
        // if the filepath is empty, we'll assume that we're looking for the
        // icon associated with the "txt" extension.  unfortunately,
        // SHGetFileInfo() apparently fails if the target file doesn't actually
        // exist, so we'll rely on an arcane feature present since the early
        // days of DOS... a filename for which the name portion matches a DOS
        // device actually points to the device... so "nul.txt" actually
        // references the NUL: device.  yeah, well, anyway, it works.  well,
        // sort of... apparently the extension is actually ignored and we
        // get back a default icon.  good enough for me....
        //
        AnsiString s = filepath;
        s.Unique();
        if (s == "") s = "nul.txt";

        // changed to use ExtractAssociatedIcon()
        WORD iconIndex = 0;
        HICON hIcon = 0;
        try {
                char buf[MAXPATH];      // executable path written back here
                strncpy(buf, s.c_str(), sizeof(buf));
                hIcon = ::ExtractAssociatedIcon(HInstance, buf, &iconIndex);
                }
        catch (...) {
                hIcon = 0;
                }
        return hIcon;

Quote
}

//---------------------------------------------------------------------------

Other Threads