How to extract and save ICON resources

Source: Internet
Author: User

What if you want to use beautiful icons in other applications? Some icon extraction tools may be used! So, do you know how they are implemented, and do not want to create a suitable icon Extraction Tool by yourself? Next, let me use a tool I used before to <easily grasp the icon> to tell you! It can extract ICON resources from various files and save them as icons (ICO) and bitmap (BMP. After running the program, the interface is as follows:

 

Figure 1

In the above routine, I integrated an icon class CIcons and a bitmap class CDib that matches it, including Icons. h. Icons. cpp, Dib. h and Dib. cpp files. After adding these four files to your project, you can use your own icon tool to call the classes.


 

Let's take a look at how to use them for implementation:

1. Extract and display the icon to the list box on the left.

The code is implemented as follows:

File: // readRetrieve various resource icons and display them in the list box on the left

Void CIconSnapDlg: OnOK ()
{
CFileDialog fileDialog (TRUE ,"*. ICO ", NULL, NULL," resource file (*. ICO ,*. BMP ,*. EXE ,*. DLL ,*. ICL) | *. ICO ;*. BMP ;*. EXE ;*. DLL ;*. ICL | ");
If (fileDialog. DoModal () = IDOK)
{
SzOpenFileName = fileDialog. GetPathName ();
SzOpenFileExtName = fileDialog. GetFileExt ();
SzOpenFileExtName. MakeLower ();

M_List.ResetContent ();File: // selectClear the icon list box on the left

File: // readRetrieve and display the ICON file
If (szOpenFileExtName = "ico ")
{
LpIR = pIcons-> ReadIconFromICOFile (szOpenFileName );
HICON hIcon;
HIcon = ExtractIcon (AfxGetInstanceHandle (), szOpenFileName, 0 );
If (hIcon! = NULL)
M_List.AddString (szOpenFileName );
CStatic * pStatic = (CStatic *) GetDlgItem (IDC_ICONS );
PStatic-> SetIcon (hIcon );
}
Else if (szOpenFileExtName = "bmp ")File: // readRetrieve and display BMP files
{
PIcons-> IconImageFromBMPFile (szOpenFileName, & lpIR-> IconImages [0], TRUE );
HICON hIcon;
HIcon = pIcons-> MakeIconFromResource (& lpIR-> IconImages [0]);
If (hIcon! = NULL)
M_List.AddString (szOpenFileName );
CStatic * pStatic = (CStatic *) GetDlgItem (IDC_ICONS );
PStatic-> SetIcon (hIcon );
}
ElseFile: // readObtain and display EXE, DLL, and other resource files
{
HINSTANCE hLibrary;

// Load the DLL/EXE-NOTE: must be a 32bit EXE/DLL for this to work
If (hLibrary = LoadLibraryEx (szOpenFileName, NULL, LOAD_LIBRARY_AS_DATAFILE) = NULL)
{
// Failed to load-abort
MessageBox (szOpenFileName + "file loading error, must be a WIN32 file! "," Error ", MB_ OK );
Return;
}
// Store the info
EDII. szFileName = szOpenFileName;
EDII. hInstance = hLibrary;

// Fill in the listbox with the icons available
If (! EnumResourceNames (EDII. hInstance, RT_GROUP_ICON, (ENUMRESNAMEPROC) MyEnumProcedure, (LPARAM) GetSafeHwnd ()))
{
MessageBox ("An error occurred while listing the icon Resource Name! "," Error ", MB_ OK );
Return;
}
}

M_List.SetCurSel (0 );
If (m_List.GetCount () = 0)
{
MessageBox ("No icon resource in this file! "," Error ", MB_ OK );
File: // noneIcon resource. The save and copy buttons are invalid.
M_Copy.EnableWindow (false );
M_SaveAs.EnableWindow (false );
Return;
}
File: // YesIcon resource, set the SAVE and copy buttons to valid
M_Copy.EnableWindow (true );
M_SaveAs.EnableWindow (true );

File: // click itNew OnPaint is called to display the icon
InvalidateRect (NULL, TRUE );
}
}

Add the following code to the OnPaint () Definition to display extracted icons or bitmap resources.
File: // RootUpdate the corresponding icon on the right using OnPaint () in the icon list on the left

LPTSTR lpIconID;
HICON hIcon;
If (lpIconID = (LPTSTR) m_List.GetItemData (m_List.GetCurSel ()))! = (LPTSTR) LB_ERR)
{
If (szOpenFileExtName = "exe" | szOpenFileExtName = "dll" | szOpenFileExtName = "icl ")
{
HIcon = pIcons-> GetIconFromInstance (EDII. hInstance, lpIconID );
CStatic * pStatic = (CStatic *) GetDlgItem (IDC_ICONS );
PStatic-> SetIcon (hIcon );
}
}

2. How to save extracted ICON resources in Ico or Bmp format.

File: // saveSave ICON resources as ICO or BMP files

Void CIconSnapDlg: OnButtonSaveas ()
{
LPTSTR lpIconID;

CFileDialog fileDialog (FALSE ,"*. ICO ", NULL, NULL," icon file (*. ICO) | *. ICO | bitmap file (*. BMP) | *. BMP | ");
If (fileDialog. DoModal () = IDOK)
{
SzSaveFileName = fileDialog. GetPathName ();
SzSaveFileExtName = fileDialog. GetFileExt ();
SzSaveFileExtName. MakeLower ();

If (szOpenFileExtName = "exe" | szOpenFileExtName = "dll" | szOpenFileExtName = "icl ")
If (lpIconID = (LPTSTR) m_List.GetItemData (m_List.GetCurSel ()))! = (LPTSTR) LB_ERR)
LpIR = pIcons-> ReadIconFromEXEFile (szOpenFileName, lpIconID );
If (szSaveFileExtName = "bmp ")
{
If (lpIR! = NULL & m_List.GetCount ()> 0)
{
BeginWaitCursor ();
PIcons-> IconImageToBMPFile (szSaveFileName, & lpIR-> IconImages [0]);
EndWaitCursor ();
}
Else
MessageBox ("No icon resource to save! "," Error ", MB_ OK );
}
Else if (szSaveFileExtName = "ico ")
{
If (lpIR! = NULL & m_List.GetCount ()> 0)
{
BeginWaitCursor ();
PIcons-> WriteIconToICOFile (lpIR, szSaveFileName );
EndWaitCursor ();
}
Else
MessageBox ("No icon resource to save! "," Error ", MB_ OK );
}
}

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.