Seite 1 von 1
[gelöst] Icons von Dateien laden (Qt4)
Verfasst: 19. September 2007 15:33
von michams
Hi,
gibt es in Qt4 eine Möglichkeit, anhand eines beliebigen Dateinamen das zugehörige Icon zu laden, sodaß man es zB in einer Tabelle anzeigen kann?
Also nicht falsch verstehen, die Datei ist selber KEIN Icon, sondern eine Text Datei, ein PDF oder sonstwas! Ich möchte an das Icon, daß in Datei Explorern oder im Open Dialog benutzt wird.
Verfasst: 19. September 2007 15:45
von FlorianBecker
Das musst du mit der Windows API lösen. Falls du eigene Icons benutzen willst, musst das ganze einfach selber schreiben.
Verfasst: 19. September 2007 15:48
von CaptnChaos
Verfasst: 19. September 2007 16:53
von michams
Danke für die Tips,
Ich hatte zwar gehofft, etwas direktes in Qt zu finden, aber das Thema ist halt plattformabhängig. Den Weg über das HICON bin ich bei der MFC Anwendugn auch gegangen, allerdings war mir nicht ganz klar, wie man das in was Qt-geräuchliches umwandeln kann. Aber auch dafür ist mir inzwischen eine Lösung in die Hände gefallen, also wen es interessiert, hier die gesamte Funktion:
Code: Alles auswählen
QPixmap LoadIconFromSystem(QString qstr_FileName)
{
bool foundAlpha = false;
HDC screenDevice = qt_win_display_dc();
HDC hdc = CreateCompatibleDC(screenDevice);
SHFILEINFO shfi;
memset(&shfi,0,sizeof(shfi));
SHGetFileInfo(qstr_FileName.toAscii(),
FILE_ATTRIBUTE_NORMAL,
&shfi,
sizeof(shfi),
SHGFI_ICON|SHGFI_USEFILEATTRIBUTES);
ICONINFO iconinfo;
GetIconInfo(shfi.hIcon, &iconinfo); //x and y Hotspot describes the icon center
BITMAPINFOHEADER bitmapInfo;
bitmapInfo.biSize = sizeof(BITMAPINFOHEADER);
bitmapInfo.biWidth = iconinfo.xHotspot * 2;
bitmapInfo.biHeight = iconinfo.yHotspot * 2;
bitmapInfo.biPlanes = 1;
bitmapInfo.biBitCount = 32;
bitmapInfo.biCompression = BI_RGB;
bitmapInfo.biSizeImage = 0;
bitmapInfo.biXPelsPerMeter = 0;
bitmapInfo.biYPelsPerMeter = 0;
bitmapInfo.biClrUsed = 0;
bitmapInfo.biClrImportant = 0;
DWORD* bits;
HBITMAP winBitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bitmapInfo, DIB_RGB_COLORS, (VOID**)&bits, NULL, 0);
HGDIOBJ oldhdc = (HBITMAP)SelectObject(hdc, winBitmap);
DrawIconEx( hdc, 0, 0, shfi.hIcon, iconinfo.xHotspot * 2, iconinfo.yHotspot * 2, 0, 0, DI_NORMAL);
QPixmap::HBitmapFormat alphaType = QPixmap::PremultipliedAlpha;
QPixmap iconpixmap = QPixmap::fromWinHBITMAP(winBitmap, alphaType);
QImage img = iconpixmap.toImage();
for (int y = 0 ; y < iconpixmap.height() && !foundAlpha ; y++) {
QRgb *scanLine= reinterpret_cast<QRgb *>(img.scanLine(y));
for (int x = 0; x < img.width() ; x++) {
if (qAlpha(scanLine[x]) != 0) {
foundAlpha = true;
break;
}
}
}
if (!foundAlpha) {
//If no alpha was found, we use the mask to set alpha values
DrawIconEx( hdc, 0, 0, shfi.hIcon, iconinfo.xHotspot * 2, iconinfo.yHotspot * 2, 0, 0, DI_MASK);
QPixmap maskPixmap = QPixmap::fromWinHBITMAP(winBitmap, alphaType);
QImage mask = maskPixmap.toImage();
for (int y = 0 ; y< iconpixmap.height() ; y++){
QRgb *scanlineImage = reinterpret_cast<QRgb *>(img.scanLine(y));
QRgb *scanlineMask = reinterpret_cast<QRgb *>(mask.scanLine(y));
for (int x = 0; x < img.width() ; x++){
if (qRed(scanlineMask[x]) != 0)
scanlineImage[x] = 0; //mask out this pixel
else
scanlineImage[x] |= 0xff000000; // set the alpha channel to 255
}
}
}
//dispose resources created by iconinfo call
DeleteObject(iconinfo.hbmMask);
DeleteObject(iconinfo.hbmColor);
SelectObject(hdc, oldhdc); //restore state
DeleteObject(winBitmap);
DeleteDC(hdc);
return QPixmap::fromImage(img);
}