Вопрос: Как для созданного списка файлов и папок получить системные иконки для отображения в списке?
Ответ :
Для одного файла это делается примерно так:
code:
--------------------------------------------------------------------------------
// calling parameters:
// filepath - path, name, and extension of file for which to retrieve the icon.
//
// return value:
// a handle to a Windows icon (an HICON ) - caller is responsible for
// deleting this handle with the Windows API DestroyIcon() call. failure
// to delete the icon will result in memory leakage.
//
// usage example:
// HICON hIcon = GetAssociatedIcon("c:\\autoexec.bat");
// [ ...use the icon... ]
// : [img]images/smiles/icon_biggrin.gif[/img]estroyIcon(hIcon);
//
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 -- nobody's paying
// for this anyway.
AnsiString s = filepath;
s.Unique();
if (s == "")
s = "nul.txt"; // changed to use ExtractAssociatedIcon()
WORD iconIndex = 0;
HICON hIcon = 0;
try {
char buf[MAX_PATH]; // executable path written back here
strncpy(buf, s.c_str(), sizeof(buf));
hIcon = ::ExtractAssociatedIcon(HInstance, buf, &iconIndex);
} catch (...) {
hIcon = 0;
}
return hIcon;
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
HICON hIcon = GetAssociatedIcon(ParamStr(0));
DrawIcon(Canvas->Handle, 10, 10, hIcon);
: [img]images/smiles/icon_biggrin.gif[/img]estroyIcon(hIcon);
}
--------------------------------------------------------------------------------
(c) 2003 Borland X Portal
|