📄 loadimg.c
字号:
// int index,
// BOOL bLoadWithSize )
// 参数:
// IN hFile - 文件句柄
// IN uType - 需要的 类型,为以下值:
// IMAGE_CURSOR - 指光标
// IMAGE_ICON - 指图标
// IN cxDesired - 需要的宽度(当bLoadWithSize为TRUE时,有效)
// IN cyDesired - 需要的高度(当bLoadWithSize为TRUE时,有效)
// IN index - 在文件里的索引号(当bLoadWithSize为TRUE时,无效)
// IN bLoadWithSize - 说明是否需要根据cxDesired和cyDesired来得到资源
// 返回值:
// 假如成功,返回资源的句柄;否则,返回NULL
// 功能描述:
// 从文件得到符合要求的ICON或CURSOR资源
// 引用:
//
// ************************************************
#define DEBUG_HandleIconCursorFile 0
static HANDLE _HandleIconCursorFile( HANDLE hFile, UINT uType, int cxDesired, int cyDesired, int index, BOOL bLoadWithSize )
{
ICOCURSORHDR hdr;
LPICOCURSORDESC lpDesc;
DWORD dwReaded;
DWORD dwFileSize;
HANDLE handle = NULL;
//得到文件大小
DEBUGMSG( DEBUG_HandleIconCursorFile, ( "_HandleIconCursorFile: entry.\r\n" ) );
dwFileSize = SetFilePointer( hFile, 0, NULL, FILE_END );
SetFilePointer( hFile, 0, NULL, FILE_BEGIN );
if( ReadFile( hFile, &hdr, ICOCURSORHDR_REAL_SIZE, &dwReaded, NULL ) )
{ // 是否有效 check valid ?
if( dwReaded == ICOCURSORHDR_REAL_SIZE &&
hdr.wReserved == 0 &&
hdr.wResourceCount < MAXIMAGES &&
(hdr.wResourceType == 1 || hdr.wResourceType == 2)
)
{ //分配并初始化需要的 ICOCURSORDESC 结构
lpDesc = AllocDescAndCheck( hFile, hdr.wResourceCount, dwFileSize );
if( lpDesc )
{ //如果按大小得到资源,则查找一个符合要求的
if( bLoadWithSize )
index = GetNeedImageIndex( hFile, lpDesc, hdr.wResourceCount, cxDesired, cyDesired );
if( index >= 0 )
{ //创建
handle = MakeIconCursor( hFile,
uType == IMAGE_ICON,
lpDesc[index].dwDIBOffset,
lpDesc[index].dwDIBSize,
(WORD)(bLoadWithSize ? -1 : index) );
}
else
{
WARNMSG( DEBUG_HandleIconCursorFile, ( "_HandleIconCursorFile: error index(%d)!.\r\n", index ) );
}
FreeDesc( lpDesc );
}
else
{
WARNMSG( DEBUG_HandleIconCursorFile, ( "_HandleIconCursorFile: AllocDescAndCheck failure.!.\r\n" ) );
}
}
}
else
{
WARNMSG( DEBUG_HandleIconCursorFile, ( "_HandleIconCursorFile: can't read file.!.\r\n" ) );
}
return handle;
}
// **************************************************
// 声明:static HANDLE MakeIconCursor(
// HANDLE hFile,
// BOOL bIcon,
// DWORD dwDIBOffset,
// DWORD dwDIBSize,
// WORD wName )
// 参数:
// IN hFile - 资源文件句柄
// IN bIcon - 是否是创建ICON,如果为FALSE,表示是CURSOR
// IN dwDIBOffset - DIB数据在文件中的便移
// IN dwDIBSize - DIB数据在文件中的大小
// IN wName - 资源在文件中的ID
// 返回值:
// 假如成功,返回有效的非NULL资源句柄;否则,返回NULL
// 功能描述:
// 从文件中读出需要的资源,并创建资源对象,返回句柄
// 引用:
//
// ************************************************
static HANDLE MakeIconCursor(
HANDLE hFile,
BOOL bIcon,
DWORD dwDIBOffset,
DWORD dwDIBSize,
WORD wName )
{
BITMAPINFO *lpbi;
HANDLE handle = NULL;
SetFilePointer( hFile, dwDIBOffset, NULL, FILE_BEGIN );
if( (lpbi = malloc( dwDIBSize )) )
{
DWORD dwReaded;
ReadFile( hFile, lpbi, dwDIBSize, &dwReaded, NULL );
handle = IconCursorCreate( 0, 0, lpbi,
bIcon,
wName );
free( lpbi );
}
return handle;
}
//文件中的定义
//描述各种资源的结构
typedef struct _RESHEADER{
DWORD dwDataSize; // size of data without header
DWORD dwHeaderSize; // length of the header
WORD wType[2]; // type identifier, id or string , if is string, it's various length and null ending.
WORD wName[2]; // type identifier, id or string , if is string, it's various length and null ending.
DWORD dwDataVersion;
WORD wMemFlag;
WORD wLanguageID;
DWORD dwVersion;
DWORD dwCharacteristics;
}RESHEADER, FAR * LPRESHEADER;
//描述ICON资源的头结构
typedef struct _ICONHEADER
{
WORD wReserved; // Currently zero
WORD wType; // 1 for icons
WORD wCount; // Number of components
WORD wDumy; // filler for DWORD alignment
}ICONHEADER, FAR * LPICONHEADER;
//描述每个ICON资源的结构
typedef struct _ICONREC
{
BYTE bWidth;
BYTE bHeight;
BYTE bColorCount;
BYTE bReserved;
WORD wPlanes;
WORD wBitCount;
DWORD lBytesInRes;
WORD wNameOrdinal; // Points to component
WORD wDumy; // filler for DWORD alignment
}ICONREC, FAR * LPICONREC;
//描述每个ICON资源的索引结构
typedef struct _ICONINDEX
{
DWORD dwName; //ICON名
DWORD dwOffsetInFile; //在文件中的便移
}ICONINDEX, FAR * LPICONINDEX;
// **************************************************
// 声明:static HANDLE HandleIconCursorGroupData(
// HANDLE hFile,
// int id,
// LPCTSTR lpszName,
// LPICONINDEX lp,
// int iIndexCount,
// int cxDesired,
// int cyDesired )
// 参数:
// IN hFile - 文件句柄
// IN id - 在文件中的资源类型ID
// IN lpszName- - 资源名
// IN lp - ICONINDEX 结构指针
// IN iIndexCount - ICONINDEX 结构指针所指向的索引数
// IN cxDesired - 需要的宽度
// IN cyDesired - 需要的高度
// 返回值:
// 假如成功,返回有效的非NULL资源句柄;否则,返回NULL
// 功能描述:
// 处理ICON/CURSOR组数据
// 引用:
//
// ************************************************
static HANDLE HandleIconCursorGroupData( HANDLE hFile, int id, LPCTSTR lpszName, LPICONINDEX lp, int iIndexCount, int cxDesired, int cyDesired )
{
ICONHEADER ih;
DWORD dwReaded;
//对头数据
ReadFile( hFile, &ih, sizeof(ih) - sizeof(WORD), &dwReaded, NULL );
if( ih.wCount > 0 && iIndexCount )
{
ICONREC iconRec;
int i;
for( i = 0; i < ih.wCount; i++ )
{ //读每一组的数据
ReadFile( hFile, &iconRec, sizeof(iconRec) - sizeof(WORD), &dwReaded, NULL );
//是否符合要求
if( ( dwReaded == sizeof(iconRec) - sizeof(WORD) ) &&
IS_SIZE_MATCH( iconRec.bWidth, iconRec.bHeight, cxDesired, cyDesired ) )
{
int n;
//在该组中查找名字匹配的资源
for( n = 0; n < iIndexCount; n++ )
{
if( lp->dwName == iconRec.wNameOrdinal )
return MakeIconCursor( hFile,
(BOOL)( (WORD)id == (DWORD)RT_ICON ),
lp->dwOffsetInFile,
iconRec.lBytesInRes,
(WORD)((DWORD)lpszName) );
lp++;
}
}
}
}
return NULL;
}
// **************************************************
// 声明:static HANDLE HandleBitmapData( HANDLE hFile, BOOL bShare )
// 参数:
// IN hFile - 文件句柄
// IN bShare - 是否共享
// 返回值:
// 假如成功,返回有效的非NULL资源句柄;否则,返回NULL
// 功能描述:
// 处理位图文件
// 引用:
//
// ************************************************
#define DEBUG_HANDLE_BITMAP_DATA 0
static HANDLE HandleBitmapData( HANDLE hFile, BOOL bShare )
{
BITMAPINFO *lpbi;
DWORD dwReadSize;
HBITMAP hBitmap = NULL;
DEBUGMSG( DEBUG_HANDLE_BITMAP_DATA, ( "HandleBitmapData: entry.\r\n" ) );
lpbi = (BITMAPINFO *)malloc( sizeof(BITMAPINFO) );
if( lpbi )
{
DEBUGMSG( DEBUG_HANDLE_BITMAP_DATA, ( "HandleBitmapData: read header.\r\n" ) );
ReadFile( hFile, &lpbi->bmiHeader, sizeof(lpbi->bmiHeader), &dwReadSize, NULL );
if( dwReadSize == sizeof(lpbi->bmiHeader) &&
lpbi->bmiHeader.biSize == sizeof(lpbi->bmiHeader) )
{
if( lpbi->bmiHeader.biPlanes == 1 &&
( lpbi->bmiHeader.biCompression == BI_RGB ||
lpbi->bmiHeader.biCompression == BI_BITFIELDS ) )
{ // support 1 planes format
int items = GetColorTableNum( &lpbi->bmiHeader );
if( items > 1 )
{
LPVOID p;
p = realloc( lpbi, (items - 1)* sizeof(RGBQUAD) + sizeof(BITMAPINFO) );
if( p )
{
lpbi = (BITMAPINFO *)p;
ReadFile(hFile, lpbi->bmiColors, items* sizeof(RGBQUAD), &dwReadSize, NULL );
if( items * sizeof(RGBQUAD) == dwReadSize )
{
hBitmap = _HandleImageData( hFile, lpbi, bShare );
}
}
}
else
hBitmap = _HandleImageData( hFile, lpbi, bShare );
}
}
else
{
RETAILMSG( 1, ( "error: invalid bitmap format .\r\n" ) );
}
free( lpbi );
}
return hBitmap;
}
// **************************************************
// 声明:
// 参数:
// 无
// 返回值:
// 假如成功,返回TRUE;否则,返回FALSE
// 功能描述:
//
// 引用:
//
// ************************************************
BOOL IsIconCursorSizeMatch( HANDLE hFile, UINT uiTypeint, int cxDesired, int cyDesired )
{
BITMAPINFO bi;
DWORD dwReaded;
DWORD dwOffset;
dwOffset = SetFilePointer( hFile, 0, NULL, FILE_CURRENT );
ReadFile( hFile, &bi, sizeof(bi), &dwReaded, NULL );
if( dwReaded == sizeof(bi) )
{
if( IS_SIZE_MATCH( bi.bmiHeader.biWidth, bi.bmiHeader.biHeight / 2, cxDesired, cyDesired ) )
{
SetFilePointer( hFile, dwOffset, NULL, FILE_BEGIN );
return TRUE;
}
}
SetFilePointer( hFile, dwOffset, NULL, FILE_BEGIN );
return FALSE;
}
// **************************************************
// 声明:static HANDLE _LoadImageFromResFile(
// HANDLE hFile,
// DWORD dwOffset,
// UINT id,
// LPCTSTR lpszName,
// int cxDesired,
// int cyDesired,
// BOOL bShare )
// 参数:
// IN hFile - 文件句柄
// IN dwOffset - 资源在文件中的偏移
// IN id - 需要的资源类别
// IN lpszName - 资源名字
// IN cxDesired - 需要的宽度
// IN cyDesired - 需要的高度
// IN bShare - 该资源是否会被共享
// 返回值:
// 假如成功,返回 资源句柄;否则,返回 NULL
// 功能描述:
// 从文件得到资源
// 引用:
//
// ************************************************
#define DEBUG_LoadImageFromResFile 0
static HANDLE _LoadImageFromResFile( HANDLE hFile, DWORD dwOffset, UINT id, LPCTSTR lpszName, int cxDesired, int cyDesired, BOOL bShare )
{
int iIndexSize = 8;
int iIndexCount = 0;
HANDLE hImage = NULL;
LPICONINDEX lpIndex, lpIndexHeader;
lpIndexHeader = (LPICONINDEX)malloc( sizeof( ICONINDEX ) * iIndexSize );
if( lpIndexHeader )
{
RESHEADER rh;
DWORD dwReaded;
int i;
int iFilePos = 0;
lpIndex = lpIndexHeader;
iFilePos = SetFilePointer( hFile, dwOffset, NULL, FILE_BEGIN );
while(1)
{
//RETAILMSG( 1, ( "i0.\r\n" ) );
ReadFile( hFile, &rh, sizeof(rh), &dwReaded, NULL );
//RETAILMSG( 1, ( "i1.\r\n" ) );
if( dwReaded != sizeof(rh) )
{
DEBUGMSG( DEBUG_LoadImageFromResFile, ( "_LoadImageFromResFile:file format error.\r\n" ) );
break;
}
//RETAILMSG( 1, ( "_LoadImageFromResFile:rh.wType[0]=%d,rh.wType[1]=%d.\r\n", rh.wType[0],rh.wType[1] ) );
if( rh.wType[0] == 0xffff &&
( rh.wType[1] == id || rh.wType[1] == (id+DIFFERENCE) ) )
{ // find it
if( id == (DWORD)RT_ICON || id == (DWORD)RT_CURSOR )
{ // save the component index data when the id is cursor or icon
//RETAILMSG( 1, ( "Find :rh.wType[0]=%d,rh.wType[1]=%d.\r\n", rh.wType[0],rh.wType[1] ) );
if( rh.wType[1] == id )
{
if( iIndexCount < iIndexSize )
{
lpIndex->dwName = rh.wName[1];
lpIndex->dwOffsetInFile = SetFilePointer( hFile, 0, NULL, FILE_CURRENT );
lpIndex++; iIndexCount++;
}
else
{
ASSERT( 0 );
break;
}
}
if( rh.wName[1] == (DWORD)lpszName )
{
if( rh.wType[1] == (id+DIFFERENCE) )
{
//RETAILMSG( 1, ( "loadimag0:name=%d.\r\n", lpszName ) );
hImage = HandleIconCursorGroupData( hFile, id, lpszName, lpIndexHeader, iIndexCount, cxDesired, cyDesired );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -