📄 icons.cpp
字号:
if( (lpIR = (LPICONRESOURCE)malloc( sizeof(ICONRESOURCE) + ((lpIcon->idCount-1) * sizeof(ICONIMAGE)) )) == NULL )
{
MessageBox( AfxGetMainWnd()->m_hWnd, "内存分配出错!", szFileName, MB_OK );
FreeLibrary( hLibrary );
return NULL;
}
// Fill in local struct members
lpIR->nNumImages = lpIcon->idCount;
lstrcpy( lpIR->szOriginalDLLFileName, szFileName );
lstrcpy( lpIR->szOriginalICOFileName, "" );
// Loop through the images
for( i = 0; i < lpIR->nNumImages; i++ )
{
// Get the individual image
if( (hRsrc = FindResource( hLibrary, MAKEINTRESOURCE(lpIcon->idEntries[i].nID), RT_ICON )) == NULL )
{
free( lpIR );
FreeLibrary( hLibrary );
return NULL;
}
if( (hGlobal = LoadResource( hLibrary, hRsrc )) == NULL )
{
free( lpIR );
FreeLibrary( hLibrary );
return NULL;
}
// Store a copy of the resource locally
lpIR->IconImages[i].dwNumBytes =SizeofResource( hLibrary, hRsrc );
lpIR->IconImages[i].lpBits = (LPBYTE)malloc( lpIR->IconImages[i].dwNumBytes );
memcpy( lpIR->IconImages[i].lpBits, LockResource( hGlobal ), lpIR->IconImages[i].dwNumBytes );
// Adjust internal pointers
if( ! AdjustIconImagePointers( &(lpIR->IconImages[i]) ) )
{
MessageBox(AfxGetMainWnd()->m_hWnd, "转换成图标内部格式时出错!", szFileName, MB_OK );
free( lpIR );
FreeLibrary( hLibrary );
return NULL;
}
}
}
FreeLibrary( hLibrary );
return lpIR;
}
/* End ReadIconFromEXEFile() ************************************************/
/****************************************************************************
*
* FUNCTION: WriteICOHeader
*
* PURPOSE: Writes the header to an ICO file
*
* PARAMS: HANDLE hFile - handle to the file
* UINT nNumEntries - Number of images in file
*
* RETURNS: BOOL - TRUE for success, FALSE for failure
*
*
\****************************************************************************/
BOOL CIcons::WriteICOHeader( HANDLE hFile, UINT nNumEntries )
{
WORD Output;
DWORD dwBytesWritten;
// Write 'reserved' WORD
Output = 0;
if( ! WriteFile( hFile, &Output, sizeof( WORD ), &dwBytesWritten, NULL ) )
return FALSE;
// Did we write a WORD?
if( dwBytesWritten != sizeof( WORD ) )
return FALSE;
// Write 'type' WORD (1)
Output = 1;
if( ! WriteFile( hFile, &Output, sizeof( WORD ), &dwBytesWritten, NULL ) )
return FALSE;
// Did we write a WORD?
if( dwBytesWritten != sizeof( WORD ) )
return FALSE;
// Write Number of Entries
Output = (WORD)nNumEntries;
if( ! WriteFile( hFile, &Output, sizeof( WORD ), &dwBytesWritten, NULL ) )
return FALSE;
// Did we write a WORD?
if( dwBytesWritten != sizeof( WORD ) )
return FALSE;
return TRUE;
}
/* End WriteICOHeader() ****************************************************/
/****************************************************************************
*
* FUNCTION: CalculateImageOffset
*
* PURPOSE: Calculates the file offset for an icon image
*
* PARAMS: LPICONRESOURCE lpIR - pointer to icon resource
* UINT nIndex - which image?
*
* RETURNS: DWORD - the file offset for that image
*
*
\****************************************************************************/
DWORD CIcons::CalculateImageOffset( LPICONRESOURCE lpIR, UINT nIndex )
{
DWORD dwSize;
UINT i;
// Calculate the ICO header size
dwSize = 3 * sizeof(WORD);
// Add the ICONDIRENTRY's
dwSize += lpIR->nNumImages * sizeof(ICONDIRENTRY);
// Add the sizes of the previous images
for(i=0;i<nIndex;i++)
dwSize += lpIR->IconImages[i].dwNumBytes;
// we're there - return the number
return dwSize;
}
/* End CalculateImageOffset() ***********************************************/
/****************************************************************************
*
* FUNCTION: WriteIconToICOFile
*
* PURPOSE: Writes the icon resource data to an ICO file
*
* PARAMS: LPICONRESOURCE lpIR - pointer to icon resource
* LPCTSTR szFileName - name for the ICO file
*
* RETURNS: BOOL - TRUE for success, FALSE for failure
*
*
\****************************************************************************/
BOOL CIcons::WriteIconToICOFile( LPICONRESOURCE lpIR, LPCTSTR szFileName )
{
HANDLE hFile;
UINT i;
DWORD dwBytesWritten;
// open the file
if( (hFile = CreateFile( szFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL )) == INVALID_HANDLE_VALUE )
{
MessageBox( AfxGetMainWnd()->m_hWnd , "文件建立时出错!", szFileName, MB_OK );
return FALSE;
}
// Write the header
if( ! WriteICOHeader( hFile, lpIR->nNumImages ) )
{
MessageBox( AfxGetMainWnd()->m_hWnd , "写图标文件头时出错!", szFileName, MB_OK );
CloseHandle( hFile );
return FALSE;
}
// Write the ICONDIRENTRY's
for( i=0; i<lpIR->nNumImages; i++ )
{
ICONDIRENTRY ide;
// Convert internal format to ICONDIRENTRY
ide.bWidth = lpIR->IconImages[i].Width;
ide.bHeight = lpIR->IconImages[i].Height;
ide.bReserved = 0;
ide.wPlanes = lpIR->IconImages[i].lpbi->bmiHeader.biPlanes;
ide.wBitCount = lpIR->IconImages[i].lpbi->bmiHeader.biBitCount;
if( (ide.wPlanes * ide.wBitCount) >= 8 )
ide.bColorCount = 0;
else
ide.bColorCount = 1 << (ide.wPlanes * ide.wBitCount);
ide.dwBytesInRes = lpIR->IconImages[i].dwNumBytes;
ide.dwImageOffset = CalculateImageOffset( lpIR, i );
// Write the ICONDIRENTRY out to disk
if( ! WriteFile( hFile, &ide, sizeof( ICONDIRENTRY ), &dwBytesWritten, NULL ) )
return FALSE;
// Did we write a full ICONDIRENTRY ?
if( dwBytesWritten != sizeof( ICONDIRENTRY ) )
return FALSE;
}
// Write the image bits for each image
for( i=0; i<lpIR->nNumImages; i++ )
{
DWORD dwTemp = lpIR->IconImages[i].lpbi->bmiHeader.biSizeImage;
// Set the sizeimage member to zero
lpIR->IconImages[i].lpbi->bmiHeader.biSizeImage = 0;
// Write the image bits to file
if( ! WriteFile( hFile, lpIR->IconImages[i].lpBits, lpIR->IconImages[i].dwNumBytes, &dwBytesWritten, NULL ) )
return FALSE;
if( dwBytesWritten != lpIR->IconImages[i].dwNumBytes )
return FALSE;
// set it back
lpIR->IconImages[i].lpbi->bmiHeader.biSizeImage = dwTemp;
}
CloseHandle( hFile );
return FALSE;
}
/* End WriteIconToICOFile() **************************************************/
/****************************************************************************
*
* FUNCTION: IconImageToClipBoard
*
* PURPOSE: Copies an icon image to the clipboard in CF_DIB format
*
* PARAMS: LPICONIMAGE lpii - pointer to icon image data
*
* RETURNS: BOOL - TRUE for success, FALSE for failure
*
*
\****************************************************************************/
BOOL CIcons::IconImageToClipBoard( LPICONIMAGE lpii )
{
HANDLE hGlobal;
LPSTR lpBits;
// Open the clipboard
if( OpenClipboard(AfxGetMainWnd()->m_hWnd ) )
{
// empty it
if( EmptyClipboard() )
{
if(lpii->dwNumBytes ==NULL)
return false;
// Make a buffer to send to clipboard
hGlobal = GlobalAlloc( GMEM_MOVEABLE | GMEM_DDESHARE, lpii->dwNumBytes );
lpBits = (LPSTR)GlobalLock( hGlobal );
// Copy the bits to the buffer
memcpy( lpBits, lpii->lpBits, lpii->dwNumBytes );
// Adjust for funky height*2 thing
((LPBITMAPINFOHEADER)lpBits)->biHeight /= 2;
GlobalUnlock( hGlobal );
// Send it to the clipboard
SetClipboardData( CF_DIB, hGlobal );
CloseClipboard();
return TRUE;
}
}
return FALSE;
}
/* End IconImageToClipBoard() ***********************************************/
/****************************************************************************
*
* FUNCTION: IconImageFromClipBoard
*
* PURPOSE: Creates an icon image from the CF_DIB clipboard entry
*
* PARAMS: LPICONIMAGE lpii - pointer to icon image data
* BOOL bStretchToFit - TRUE to stretch, FALSE to take
* the upper left corner of the DIB
*
* RETURNS: BOOL - TRUE for success, FALSE for failure
*
*
\****************************************************************************/
BOOL CIcons::IconImageFromClipBoard( LPICONIMAGE lpii, BOOL bStretchToFit )
{
LPBITMAPINFO lpbi;
HANDLE hClipGlobal;
BOOL bRet = FALSE;
// Open the clipboard
if( OpenClipboard( AfxGetMainWnd()->m_hWnd ) )
{
// Get the CF_DIB data from it
if( (hClipGlobal = GetClipboardData( CF_DIB )) != NULL )
{
// Lock it down
if( (lpbi=(LPBITMAPINFO)GlobalLock(hClipGlobal)) != NULL )
{
// Convert it to an icon image
bRet = DIBToIconImage( lpii, (LPBYTE)lpbi, bStretchToFit );
GlobalUnlock( hClipGlobal );
}
}
CloseClipboard();
}
return bRet;
}
/* End IconImageFromClipBoard() ********************************************/
/****************************************************************************
*
* FUNCTION: DIBToIconImage
*
* PURPOSE: Converts a CF_DIB memory block to an icon image
*
* PARAMS: LPICONIMAGE lpii - pointer to icon image data
* LPBYTE lpDIB - a pointer to the CF_DIB block
* BOOL bStretchToFit - TRUE to stretch, FALSE to take
* the upper left corner of the DIB
*
* RETURNS: BOOL - TRUE for success, FALSE for failure
*
*
\****************************************************************************/
BOOL CIcons::DIBToIconImage( LPICONIMAGE lpii, LPBYTE lpDIB, BOOL bStretch )
{
LPBYTE lpNewDIB;
// Sanity check
if( lpDIB == NULL )
return FALSE;
// Let the DIB engine convert color depths if need be
lpNewDIB = pDib->ConvertDIBFormat( (LPBITMAPINFO)lpDIB, lpii->Width, lpii->Height, lpii->Colors, bStretch );
// Now we have a cool new DIB of the proper size/color depth
// Lets poke it into our data structures and be done with it
// How big is it?
lpii->dwNumBytes = sizeof( BITMAPINFOHEADER ) // Header
+ pDib->PaletteSize( (LPSTR)lpNewDIB ) // Palette
+ lpii->Height * pDib->BytesPerLine( (LPBITMAPINFOHEADER)lpNewDIB ) // XOR mask
+ lpii->Height * WIDTHBYTES( lpii->Width ); // AND mask
// If there was already an image here, free it
if( lpii->lpBits != NULL )
free( lpii->lpBits );
// Allocate enough room for the new image
if( (lpii->lpBits = (LPBYTE)malloc( lpii->dwNumBytes )) == NULL )
{
free( lpii );
return FALSE;
}
// Copy the bits
memcpy( lpii->lpBits, lpNewDIB, sizeof( BITMAPINFOHEADER ) + pDib->PaletteSize( (LPSTR)lpNewDIB ) );
// Adjust internal pointers/variables for new image
lpii->lpbi = (LPBITMAPINFO)(lpii->lpBits);
lpii->lpbi->bmiHeader.biHeight *= 2;
lpii->lpXOR =(LPBYTE) pDib->FindDIBBits( (LPSTR)(lpii->lpBits) );
memcpy( lpii->lpXOR, pDib->FindDIBBits((LPSTR)lpNewDIB), lpii->Height * pDib->BytesPerLine( (LPBITMAPINFOHEADER)lpNewDIB ) );
lpii->lpAND = lpii->lpXOR + lpii->Height * pDib->BytesPerLine( (LPBITMAPINFOHEADER)lpNewDIB );
memset( lpii->lpAND, 0, lpii->Height * WIDTHBYTES( lpii->Width ) );
// Free the source
free( lpNewDIB );
return TRUE;
}
/* End DIBToIconImage() ***************************************************/
/****************************************************************************
*
* FUNCTION: IconImageFromBMPFile
*
* PURPOSE: Creates an icon image from a BMP file
*
* PARAMS: LPCTSTR szFileName - Filename for BMP file
* LPICONIMAGE lpii - pointer to icon image data
* BOOL bStretchToFit - TRUE to stretch, FALSE to take
* the upper left corner of the DIB
*
* RETURNS: BOOL - TRUE for success, FALSE for failure
*
*
\****************************************************************************/
BOOL CIcons::IconImageFromBMPFile( LPCTSTR szFileName, LPICONIMAGE lpii, BOOL bStretchToFit )
{
LPBYTE lpDIB = NULL;
BOOL bRet = FALSE;
if( (lpDIB=pDib->ReadBMPFile(szFileName)) == NULL )
return FALSE;
// Convert it to an icon image
bRet = DIBToIconImage( lpii, lpDIB, bStretchToFit );
free( lpDIB );
return bRet;
}
/* End IconImageFromBMPFile() ********************************************/
/****************************************************************************
*
* FUNCTION: IconImageToBMPFile
*
* PURPOSE: Creates BMP file from an icon image
*
* PARAMS: LPCTSTR szFileName - Filename for BMP file
* LPICONIMAGE lpii - pointer to icon image data
*
* RETURNS: BOOL - TRUE for success, FALSE for failure
*
*
\****************************************************************************/
BOOL CIcons::IconImageToBMPFile( LPCTSTR szFileName, LPICONIMAGE lpii )
{
return pDib->WriteBMPFile( szFileName, (LPBYTE)lpii->lpbi );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -