Programm Icons
-
Hallo,
ich weiß nicht ob meine Frage hier hin gehört aber ich progge mit Qt.
Wie kriege ich die Icons von Programmen, z.B. Skype, Firefox usw. Ich programmiere ein Schnell-Starter für Programme. Man kann da Programme hinzufüge, und in einem Fenster soll man dann die Programm-Buttons (mit den dazugehöriegen Icons) zum Starten sehen.
-
eine direkte Qt-Funktion gibt es wohl nicht, sieht natürlich bei jeder Plattform (Win, Linux, Mac) anders aus.
Unter Win gibt es die ExtractIcon oder ExtractIconEx, Qt nutzt diese Funktion auch hier://Auszug aus Qt-Source: bool RenderThemeWin::paintSearchFieldResultsDecoration(RenderObject* o, const RenderObject::PaintInfo& paintInfo, const IntRect& r) { IntRect bounds = r; bounds.setWidth(bounds.height()); TCHAR buffer[MAX_PATH]; UINT length = ::GetSystemDirectory(buffer, ARRAYSIZE(buffer)); if (!length) return 0; if (_tcscat_s(buffer, TEXT("\\shell32.dll"))) return 0; HICON hIcon; if (!::ExtractIconEx(buffer, shell32MagnifierIconIndex, 0, &hIcon, 1)) return 0; RefPtr<Icon> icon = Icon::create(hIcon); icon->paint(paintInfo.context, bounds); return false; }
aus der shell32.dll im Windows-Systemverzeichnis wird das Icon für shell32MagnifierIconIndex (= 22) geladen.
Das Applikationsicon müsste Index 1 haben (oder vielleicht 0).Hier siehst du wie Qt ein HICON in ein QPixmap wandelt:
//Auszug aus Qt-Source: QPixmap loadIconFromShell32( int resourceId, int size ) { HMODULE hmod = LoadLibraryA("shell32.dll"); if( hmod ) { HICON iconHandle = (HICON)LoadImage(hmod, MAKEINTRESOURCE(resourceId), IMAGE_ICON, size, size, 0); if( iconHandle ) { QPixmap iconpixmap = convertHIconToPixmap( iconHandle ); DestroyIcon(iconHandle); return iconpixmap; } } return QPixmap(); }
-
Danke für die Antwort, aber ich kriege da zwei komischen Fehler.
stert.obj:: error: Verweis auf nicht aufgelöstes externes Symbol "__imp__DestroyIcon@4" in Funktion ""private: class QPixmap __thiscall Stert::loadIconFromShell32(int,int)" (?loadIconFromShell32@Stert@@AAE?AVQPixmap@@HH@Z)".
stert.obj:: error: Verweis auf nicht aufgelöstes externes Symbol "__imp__LoadImageW@24" in Funktion ""private: class QPixmap __thiscall Stert::loadIconFromShell32(int,int)" (?loadIconFromShell32@Stert@@AAE?AVQPixmap@@HH@Z)".
Hab convertHIconToPixmap() durch fromWinHICON() ersetzt.
QPixmap Stert::loadIconFromShell32( int resourceId, int size ){ HMODULE hmod = LoadLibraryA("shell32.dll"); if( hmod ){ HICON iconHandle = (HICON)LoadImage(hmod, MAKEINTRESOURCE(resourceId), IMAGE_ICON, size, size, 0); if( iconHandle ) { QPixmap iconpixmap = QPixmap::fromWinHICON(iconHandle);// convertHIconToPixmap( iconHandle ); DestroyIcon(iconHandle); return iconpixmap; } } return QPixmap(); }
-
Du willst doch nicht die Icons aus der Shell32.dll haben oder?
Diese Funktion müsste Icon aus beliebiger exe oder dll laden können
QPixmap Stert::loadIconFromExecuteable( QString fullfilename ) { HICON hiconlarge; QPixmap ret; if (!::ExtractIconEx((LPCTSTR )fullfilename.toLocal8Bit().constData(), 1, &hiconlarge, 0, 1)); return ret; ret = QPixmap::fromWinHICON(hiconlarge); ::DestroyIcon(hiconlarge); return ret; }
musst Win-Header einbinden:
für DestroyIcon: Winuser.h
für ExtractIconEx: Shellapi.h
user32.lib und shell32.lib wird fürs Linken auch benötigt