?? icons.c
字號:
/****************************************************************************\
*
* FILE: ICONS.C
*
* PURPOSE: IconPro Project Icon handing Code C file
*
* COMMENTS: This file contains the icon handling code
*
* FUNCTIONS:
* EXPORTS:
* ReadIconFromICOFile - Reads Icon from ICO file
* WriteIconToICOFile - Writes Icon to ICO file
* MakeIconFromResource - Makes HICON from a resource
* ReadIconFromEXEFile - Reads Icon from a EXE or DLL file
* IconImageToClipBoard - Puts icon image on clipboard
* IconImageFromClipBoard - Gets icon image from clipboard
* CreateBlankNewFormatIcon - Makes a new, blank icon image
* DrawXORMask - Draws XOR mask using DIBs
* DrawANDMask - Draws AND mask using DIBs
* GetXORImageRect - Calculates XOR image position
* MakeNewANDMaskBasedOnPoint - Calculates new AND mask
* ConvertBMPFileToIcon - Converts BMP to Icon
* IconImageToBMPFile - Writes an icon image to BMP file
* LOCALS:
* ReadICOHeader - Reads ICO file header
* AdjustIconImagePointers - Adjusts internal pointers
* ExtractDlgProc - Dlg Proc for extract dialog
* MyEnumProcedure - For EnumResourceNames()
* GetIconFromInstance - Extracts Icon from Instance
* ChooseIconFromEXEFile - Gets a user's choice icon from file
* WriteICOHeader - Writes ICO file header
* CalculateImageOffset - Calcs offset in file of image
* DIBToIconImage - Converts DIB to icon image
*
* Copyright 1995 Microsoft Corp.
*
*
* History:
* July '95 - Created
*
\****************************************************************************/
#include <Windows.h>
#include "Resource.h"
#include "IconPro.h"
#include "Icons.H"
#include "Dib.H"
/****************************************************************************/
// Structs used locally (file scope)
// Resource Position info - size and offset of a resource in a file
typedef struct
{
DWORD dwBytes;
DWORD dwOffset;
} RESOURCEPOSINFO, *LPRESOURCEPOSINFO;
// EXE/DLL icon information - filename, instance handle and ID
typedef struct
{
LPCTSTR szFileName;
HINSTANCE hInstance;
LPTSTR lpID;
} EXEDLLICONINFO, *LPEXEDLLICONINFO;
/****************************************************************************/
/****************************************************************************/
// External Globals
extern HINSTANCE hInst;
extern HWND hWndMain;
/****************************************************************************/
/****************************************************************************/
// Prototypes for local functions
UINT ReadICOHeader( HANDLE hFile );
BOOL AdjustIconImagePointers( LPICONIMAGE lpImage );
BOOL CALLBACK ExtractDlgProc( HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam );
BOOL CALLBACK MyEnumProcedure( HANDLE hModule, LPCTSTR lpszType, LPTSTR lpszName, LONG lParam );
HICON GetIconFromInstance( HINSTANCE hInstance, LPTSTR nIndex );
LPTSTR ChooseIconFromEXEFile( LPEXEDLLICONINFO lpEDII );
BOOL WriteICOHeader( HANDLE hFile, UINT nNumEntries );
DWORD CalculateImageOffset( LPICONRESOURCE lpIR, UINT nIndex );
BOOL DIBToIconImage( LPICONIMAGE lpii, LPBYTE lpDIB, BOOL bStretch );
/****************************************************************************/
/****************************************************************************
*
* FUNCTION: MakeIconFromResource
*
* PURPOSE: Makes an HICON from an icon resource
*
* PARAMS: LPICONIMAGE lpIcon - pointer to the icon resource
*
* RETURNS: HICON - handle to the new icon, NULL for failure
*
* History:
* July '95 - Created
*
\****************************************************************************/
HICON MakeIconFromResource( LPICONIMAGE lpIcon )
{
HICON hIcon = NULL;
// Sanity Check
if( lpIcon == NULL )
return NULL;
if( lpIcon->lpBits == NULL )
return NULL;
// Let the OS do the real work :)
hIcon = CreateIconFromResourceEx( lpIcon->lpBits, lpIcon->dwNumBytes, TRUE, 0x00030000,
(*(LPBITMAPINFOHEADER)(lpIcon->lpBits)).biWidth, (*(LPBITMAPINFOHEADER)(lpIcon->lpBits)).biHeight/2, 0 );
// It failed, odds are good we're on NT so try the non-Ex way
if( hIcon == NULL )
{
// We would break on NT if we try with a 16bpp image
if(lpIcon->lpbi->bmiHeader.biBitCount != 16)
{
hIcon = CreateIconFromResource( lpIcon->lpBits, lpIcon->dwNumBytes, TRUE, 0x00030000 );
}
}
return hIcon;
}
/* End MakeIconFromResource() **********************************************/
/****************************************************************************
*
* FUNCTION: ReadIconFromICOFile
*
* PURPOSE: Reads an Icon Resource from an ICO file
*
* PARAMS: LPCTSTR szFileName - Name of the ICO file
*
* RETURNS: LPICONRESOURCE - pointer to the resource, NULL for failure
*
* History:
* July '95 - Created
*
\****************************************************************************/
LPICONRESOURCE ReadIconFromICOFile( LPCTSTR szFileName )
{
LPICONRESOURCE lpIR = NULL, lpNew = NULL;
HANDLE hFile = NULL;
LPRESOURCEPOSINFO lpRPI = NULL;
UINT i;
DWORD dwBytesRead;
LPICONDIRENTRY lpIDE = NULL;
// Open the file
if( (hFile = CreateFile( szFileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL )) == INVALID_HANDLE_VALUE )
{
MessageBox( hWndMain, "Error Opening File for Reading", szFileName, MB_OK );
return NULL;
}
// Allocate memory for the resource structure
if( (lpIR = malloc( sizeof(ICONRESOURCE) )) == NULL )
{
MessageBox( hWndMain, "Error Allocating Memory", szFileName, MB_OK );
CloseHandle( hFile );
return NULL;
}
// Read in the header
if( (lpIR->nNumImages = ReadICOHeader( hFile )) == (UINT)-1 )
{
MessageBox( hWndMain, "Error Reading File Header", szFileName, MB_OK );
CloseHandle( hFile );
free( lpIR );
return NULL;
}
// Adjust the size of the struct to account for the images
if( (lpNew = realloc( lpIR, sizeof(ICONRESOURCE) + ((lpIR->nNumImages-1) * sizeof(ICONIMAGE)) )) == NULL )
{
MessageBox( hWndMain, "Error Allocating Memory", szFileName, MB_OK );
CloseHandle( hFile );
free( lpIR );
return NULL;
}
lpIR = lpNew;
// Store the original name
lstrcpy( lpIR->szOriginalICOFileName, szFileName );
lstrcpy( lpIR->szOriginalDLLFileName, "" );
// Allocate enough memory for the icon directory entries
if( (lpIDE = malloc( lpIR->nNumImages * sizeof( ICONDIRENTRY ) ) ) == NULL )
{
MessageBox( hWndMain, "Error Allocating Memory", szFileName, MB_OK );
CloseHandle( hFile );
free( lpIR );
return NULL;
}
// Read in the icon directory entries
if( ! ReadFile( hFile, lpIDE, lpIR->nNumImages * sizeof( ICONDIRENTRY ), &dwBytesRead, NULL ) )
{
MessageBox( hWndMain, "Error Reading File", szFileName, MB_OK );
CloseHandle( hFile );
free( lpIR );
return NULL;
}
if( dwBytesRead != lpIR->nNumImages * sizeof( ICONDIRENTRY ) )
{
MessageBox( hWndMain, "Error Reading File", szFileName, MB_OK );
CloseHandle( hFile );
free( lpIR );
return NULL;
}
// Loop through and read in each image
for( i = 0; i < lpIR->nNumImages; i++ )
{
// Allocate memory for the resource
if( (lpIR->IconImages[i].lpBits = malloc(lpIDE[i].dwBytesInRes)) == NULL )
{
MessageBox( hWndMain, "Error Allocating Memory", szFileName, MB_OK );
CloseHandle( hFile );
free( lpIR );
free( lpIDE );
return NULL;
}
lpIR->IconImages[i].dwNumBytes = lpIDE[i].dwBytesInRes;
// Seek to beginning of this image
if( SetFilePointer( hFile, lpIDE[i].dwImageOffset, NULL, FILE_BEGIN ) == 0xFFFFFFFF )
{
MessageBox( hWndMain, "Error Seeking in File", szFileName, MB_OK );
CloseHandle( hFile );
free( lpIR );
free( lpIDE );
return NULL;
}
// Read it in
if( ! ReadFile( hFile, lpIR->IconImages[i].lpBits, lpIDE[i].dwBytesInRes, &dwBytesRead, NULL ) )
{
MessageBox( hWndMain, "Error Reading File", szFileName, MB_OK );
CloseHandle( hFile );
free( lpIR );
free( lpIDE );
return NULL;
}
if( dwBytesRead != lpIDE[i].dwBytesInRes )
{
MessageBox( hWndMain, "Error Reading File", szFileName, MB_OK );
CloseHandle( hFile );
free( lpIDE );
free( lpIR );
return NULL;
}
// Set the internal pointers appropriately
if( ! AdjustIconImagePointers( &(lpIR->IconImages[i]) ) )
{
MessageBox( hWndMain, "Error Converting to Internal Format", szFileName, MB_OK );
CloseHandle( hFile );
free( lpIDE );
free( lpIR );
return NULL;
}
}
// Clean up
free( lpIDE );
free( lpRPI );
CloseHandle( hFile );
return lpIR;
}
/* End ReadIconFromICOFile() **********************************************/
/****************************************************************************
*
* FUNCTION: AdjustIconImagePointers
*
* PURPOSE: Adjusts internal pointers in icon resource struct
*
* PARAMS: LPICONIMAGE lpImage - the resource to handle
*
* RETURNS: BOOL - TRUE for success, FALSE for failure
*
* History:
* July '95 - Created
*
\****************************************************************************/
BOOL AdjustIconImagePointers( LPICONIMAGE lpImage )
{
// Sanity check
if( lpImage==NULL )
return FALSE;
// BITMAPINFO is at beginning of bits
lpImage->lpbi = (LPBITMAPINFO)lpImage->lpBits;
// Width - simple enough
lpImage->Width = lpImage->lpbi->bmiHeader.biWidth;
// Icons are stored in funky format where height is doubled - account for it
lpImage->Height = (lpImage->lpbi->bmiHeader.biHeight)/2;
// How many colors?
lpImage->Colors = lpImage->lpbi->bmiHeader.biPlanes * lpImage->lpbi->bmiHeader.biBitCount;
// XOR bits follow the header and color table
lpImage->lpXOR = FindDIBBits((LPSTR)lpImage->lpbi);
// AND bits follow the XOR bits
lpImage->lpAND = lpImage->lpXOR + (lpImage->Height*BytesPerLine((LPBITMAPINFOHEADER)(lpImage->lpbi)));
return TRUE;
}
/* End AdjustIconImagePointers() *******************************************/
/****************************************************************************
*
* FUNCTION: ReadICOHeader
*
* PURPOSE: Reads the header from an ICO file
*
* PARAMS: HANDLE hFile - handle to the file
*
* RETURNS: UINT - Number of images in file, -1 for failure
*
* History:
* July '95 - Created
*
\****************************************************************************/
UINT ReadICOHeader( HANDLE hFile )
{
WORD Input;
DWORD dwBytesRead;
// Read the 'reserved' WORD
if( ! ReadFile( hFile, &Input, sizeof( WORD ), &dwBytesRead, NULL ) )
return (UINT)-1;
// Did we get a WORD?
if( dwBytesRead != sizeof( WORD ) )
return (UINT)-1;
// Was it 'reserved' ? (ie 0)
if( Input != 0 )
return (UINT)-1;
// Read the type WORD
if( ! ReadFile( hFile, &Input, sizeof( WORD ), &dwBytesRead, NULL ) )
return (UINT)-1;
// Did we get a WORD?
if( dwBytesRead != sizeof( WORD ) )
return (UINT)-1;
// Was it type 1?
if( Input != 1 )
return (UINT)-1;
// Get the count of images
if( ! ReadFile( hFile, &Input, sizeof( WORD ), &dwBytesRead, NULL ) )
return (UINT)-1;
// Did we get a WORD?
if( dwBytesRead != sizeof( WORD ) )
return (UINT)-1;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -