#include <QFileInfo>
#include <QFileIconProvider>
...
QFileInfo file_info("D:\\Program Files (x86)\\Tencent\\QQ\\Bin\\QQ.exe");
QFileIconProvider icon_provider;
QIcon icon = icon_provider.icon(file_info);
QPixmap pixmap = icon.pixmap(28,28);
pixmap.save("D:\\test\\testqq.png", "png");
通过HWND来获取icon图标
QT += winextras
#include <QtWin>
/////////////////////////////////////小图标
HICON smallIcon;
if (!SendMessageTimeout(pWnd, WM_GETICON, 0, 0,SMTO_BLOCK | SMTO_ABORTIFHUNG, 1000, (PULONG_PTR) &smallIcon)
|| NULL == smallIcon)
{
smallIcon = (HICON)(LONG_PTR)GetClassLongPtr(pWnd, GCLP_HICONSM);
}
if(smallIcon)
{
QPixmap pix = QtWin::fromHICON(smallIcon);
pix.save("D:\\test\\" +winTitle+"_s.png", "png");
}
/////////////////////////////////////大图标
HICON largeIcon;
if (!SendMessageTimeout(pWnd, WM_GETICON, 1, 0, SMTO_BLOCK | SMTO_ABORTIFHUNG, 1000, (PULONG_PTR) &largeIcon)
|| NULL == largeIcon)
{
largeIcon = (HICON)(LONG_PTR)GetClassLongPtr(pWnd, GCLP_HICON);
}
if(largeIcon)
{
QPixmap pix = QtWin::fromHICON(largeIcon);
pix.save("D:\\test\\" +winTitle+"_l.png", "png");
}