Qt get file-oversized icon

Source: Internet
Author: User

Recently I made a program to extract icons from EXE, DLL, or other files.
I collected some information from the Internet and found that only small icons can be extracted. The small icons are 16x16 and the large icons are 32x32, which far cannot meet the requirements. The following is a general practice:

QString filePath;
QFileInfo fileInfo (filePath );
QFileIconProvider fileIcon ();
QIcon icon = fileIcon (). icon (fileInfo );
QPixmap pixmap = icon. pixmap (128,128 );
Only 32x32 Icons can be obtained.

So I checked the implementation source file of QFileIconProvider and found that it was mainly implemented by the SHGetFileInfo function.
// Get the small icon
# Ifndef Q_ OS _WINCE
Val = SHGetFileInfo (const wchar_t *) QDir: toNativeSeparators (fileInfo. filePath (). utf16 (), 0, & info,
Sizeof (SHFILEINFO), SHGFI_ICON | SHGFI_SMALLICON | SHGFI_SYSICONINDEX | SHGFI_ADDOVERLAYS | SHGFI_OVERLAYINDEX );
 
 
// Get the big icon
# Ifndef Q_ OS _WINCE
Val = SHGetFileInfo (const wchar_t *) QDir: toNativeSeparators (fileInfo. filePath (). utf16 (), 0, & info,
Sizeof (SHFILEINFO), SHGFI_ICON | SHGFI_LARGEICON | SHGFI_SYSICONINDEX | SHGFI_ADDOVERLAYS | SHGFI_OVERLAYINDEX );
 
In this way, you can only get 16 small icons and 32 large icons.

Although QPixmap pixmap = icon. pixmap (128,128) is used above, only 32x32 Icons can be obtained.
 

So I figured out how to use the library under VS2010?
I thought it would be too low to come up with a solution, but I suddenly thought that I could use VS2010 to make a DLL for QT call.

Therefore, write an output function in VS2010:

# Include <ShellAPI. h>
# Include <CommCtrl. h>
# Include <commoncontrols. h>
# Include <windows. h>
EXTERN_C _ declspec (dllexport) HICON getJumbIcon (const tchar * filePath)
{
 
 
// Get the icon index using SHGetFileInfo
SHFILEINFOW sfi = {0 };
SHGetFileInfo (filePath,-1, & sfi, sizeof (sfi), SHGFI_SYSICONINDEX );
 
// Retrieve the system image list.
// To get the 48x48 icons, use SHIL_EXTRALARGE
// To get the 256x256 icons (Vista only), use SHIL_JUMBO
IImageList * imageList;
 
HRESULT hResult = SHGetImageList (SHIL_JUMBO, IID_IImageList, (void **) & imageList );
 
If (hResult = S_ OK ){
// Get the icon we need from the list. Note that the HIMAGELIST we retrieved
// Earlier needs to be casted to the IImageList interface before use.
HICON hIcon;
HResult = (imageList)-> GetIcon (sfi. iIcon, ILD_TRANSPARENT, & hIcon );
 
If (hResult = S_ OK ){
// Do something with the icon here.
Return hIcon;

}
}
}

 

Call the following in QT:

Typedef HICON (* getIcon) (const tchar * filePath); // defines the function pointer for future calls
 
HICON test (QString filePath)
{
QLibrary mylib ("./configure/dll/icon. dll"); // declare the dll file used
HICON jumbIcon;
If (mylib. load ())
{
 
GetIcon icon = (getIcon) mylib. resolve ("getJumbIcon"); // reference the add () function
If (icon) // whether the connection is successful add () function
{
 
FilePath. replace ("/","\\");
JumbIcon = icon (const tchar *) filePath. utf16 (); // The function pointer calls the add () function in dll.
 
Return jumbIcon;
 
 
}
Else
QMessageBox: information (NULL, "NO", "Linke to Function is not OK !!!! ");
}
Else
QMessageBox: information (NULL, "NO", "DLL is not loaded! ");
 
 
Return NULL;
 
}
 
HICON hicon = test (filePath );
QPixmap icon = QPixmap: fromWinHICON (hicon );

 

DestroyIcon (hicon );

 

The icon can display the 256X256 image.


You can change SHIL_JUMBO to determine the Icon size.


Finally, it is the DLL file.
 

This article is from the "leisurely and secluded" blog

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.