How to extract and save icon resources

zhaozj2021-02-08  231

How to extract and save icon resources

Author: Xu Jing Zhou

Download sample source code (http://www.9cbs.net/cnshare/soft/9/9199.shtm)

What should I do when you want to use a beautiful icon in other applications? Maybe it will use some icon extraction tools! So, do you know how they do, I don't want to do it yourself, do it for your own icon extraction tool? Let me let me tell you if I use a tool I have done before! It can extract icon resources in various files and save it as an icon (ICO), bitmap (BMP) format. The interface after the program is running as follows:

In the routine above, I integrate a icon class CICONS and a bitmap class CDIB that is matching with it, including Icons.h, Icons.cpp, Dib.h and Dib.cpp four files. After adding these four files directly in your project, call its number of cullets, you can make your own icon tool.

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

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

The code is implemented as follows:

// Read various resource internal icons and display in the list box

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 ();

SzopenfireExtName = fileDialog.getfileext ();

SzopenfileextName.maker ();

M_List.resetContent (); // Select the empty left icon list box

// read 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") // reads and displays 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);

}

Else // Read and display resource files such as EXE, DLL

{

Hinstance hlibrary;

// load the DLL / EXE - NOTE: Must Be a 32bit EXE / DLL for this to workif ((Hlibrary = LoadLibraryEx (SzopenFileName, Null, Load_library_AS_DataFile) == NULL)

{

// failed to load - Abort

MessageBox (SzopenFileName "file load error, must be 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.HINSTANES (EDII.HINSTANCE, RT_GROUP_ICON, (EnumResNameProc) MyenumProcedure, (LPARAM) getsafehwnd ()))))

{

MessageBox ("Enumerates the icon resource name!", "Error", MB_OK;

Return;

}

}

M_List.Setcurseel (0);

IF (m_list.getcount () == 0)

{

MessageBox ("There is no icon resource in this file!", "Error", MB_OK;

// No icon resource, save and copy the button for invalidity

m_copy.enableWindow (FALSE);

m_saveas.enableWindow (FALSE);

Return;

}

// There is a icon resource, set the save and copy buttons for valid status.

m_copy.enableWindow;

m_saveas.enablewindow;

// Refresh the call onpaint to display the icon

INVALIDATERECT (NULL, TRUE);

}

}

The following code is added to the onpaint () mandate to specifically display the extracted icon or bitmap resources.

/ / According to the left icon list, use onPaint () to update the right icon

LPTSTR LPICONID;

Hicon Hicon;

IF ((lpiconid = (lptstr) m_list.getitemdata (m_list.getcurseel ()))))))))! = (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 the extracted icon resource as an ICO or BMP format.

/ / Save the icon resource for the ICO or BMP format file

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.getcurseel ()))))))))! = (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 saved icon resources!", "Error", MB_OK;

}

Else IF (szsavefileextname == "iCO")

{

IF (lpir! = null && m_list.getcount ()> 0)

{

Beginwaitcursor ();

Picons-> WriteicontOICofile (LPIR, SZSAVEFILENAME);

ENDWAITCURSOR ();

}

Else

MessageBox ("No saved icon resources!", "Error", MB_OK;

}

}

}

Detailed implementation of the above code, please take a closer look at the source code.

Contact information:

Email: jingzhou_xu@163.com

Future Studio (Future Studio)

转载请注明原文地址:https://www.9cbs.com/read-2104.html

New Post(0)