How to combine two icon
Remember the shortcut in Windows, dragging your program to the desktop to build a shortcut, regardless of the program is icon, Microsoft will add a small arrow in the left of ICON, is it fun? How can Microsoft do this, let's discuss it.
Considering that ICON format can contain transparent pixels, we have a simple way to achieve this effect, and the specific practices are as follows:
1. Get its icon from the target program. This is easy to do, and Windows offers ready-made APIs to get icon from the EXE or DLL file ------- ExtracticOnex () See msdn.
2. The image to be added to the target ICON (small arrow or anything you want to add) makes an icon. Developing tools such as VC can be easily done.
3. Make the ICON we make with the ICON of the target program, and our ICON is covered. Ok, until this, we successfully realize the ICON effect of Microsoft shortcuts.
In the above 3 points, 1, 2 is easy to implement, and the 3rd point is relatively trouble, take into account the function of converting BMP into icon, considering the function of converting BMP to ICON, we can use this feature to write a function to implement third point ---- combine two different icon.
The list of procedures is as follows:
Void Combineicon (CDC * PDC, // ICON needs DC compatible with it (usually a window DC or desktop DC of the call program)
HiCon Hbaseicon, // Bottom ICON, put it below the one
Hicon htopicon, // upper Icon, covering the above
Hicon * phDesticon, // output ICON, after the merger
INT IWIDTH, // ICON width
INT IHEIGHT, // ICON's height
ColorRef colorKey / / transparent color
)
{
CBITMAP BMP;
CDC DC;
CBRUSH BR;
Rect RC;
// Establish a compatible DC for mapping
Dc.createCompatiPLEDC (PDC);
/ / Generate a white BMP map
Bmp.createCompaBitmap (PDC, iWidth, Iheight);
// Create a color brush
Br.createsolidbrush (colorKey);
/ / Generate a rectangle of ICON size
Rc.Left = 0;
rc.top = 0;
Rc.Bottom = IHEIGHT;
Rc.right = iWidth;
// Select the blank BMP in DC, prepare
CBITMAP * PBMPOLD = DC.SelectObject (& BMP);
// Fill transparency
Dc.FillRect (& RC, & Br);
/ / Picture on the underlying icon in BMP
Drawiconex (DC.M_HDC, 0, 0, HBaseicon, iWidth, Iheight, 0, NULL, DI_NORMAL);
/ / Painting on the upper layer icon in BMP
Drawiconex (dc.m_hdc, 0, 0, htopicon, iWidth, Iheight, 0, NULL, DI_NORMAL);
// Save painted BMP
Dc.selectObject (PBMPOLD);
/ / Create an Image List, pay attention to the parameters use ILC_COLORDDB, which can support high color icon (256 live 256 color) CIMAGELIST ImageList;
Imagelist.create (iWidth, Iheight, ILC_mask | ILC_COLORDDB, 1, 0);
// Put the painted BMP into Image List and set transparencies
ImageList.Add (& BMP, ColorKey);
// Get icon from Image List
* phDESTICON = imagelist.extracticon (0);
Return;
}
The above function is written by using the basic class of the MFC, or may be changed to standard WIN32, as long as the corresponding class call is changed to the API call. CIMAGELIST's Win32 API is imagelist_xxx (), please refer to MSDN.
October 2001