在Win2K下面Load一个32bit的图标会出现黑边,下面给出的这个函数可以取代LoadImage加载32位ICON但是在2K下面不会出现黑边:)
#include
"
StdAfx.h
"
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
//
Copied from amvideo.h
#define
LINE_BYTES( Width, BitCt ) (long)( ((long)Width*BitCt+31)/32 * 4 )
#define
WIDTHBYTES(bits) ((DWORD)(((bits)+31) & (~31)) / 8)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
HICON LoadIcon(HMODULE hModule, LPCTSTR lpszResource, UINT cx, UINT cy, UINT fuLoad
=
LR_DEFAULTCOLOR)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
if(hModule == NULL || lpszResource == NULL)
return NULL;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
//Load the Icon Group firstly.
HRSRC hResource = ::FindResource(hModule, lpszResource, RT_GROUP_ICON);
HGLOBAL hMemGroup = ::LoadResource(hModule, hResource);
LPVOID lpResourceGroup = ::LockResource(hMemGroup);
UINT nID = ::LookupIconIdFromDirectoryEx((PBYTE)lpResourceGroup, TRUE, cx, cy, fuLoad);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
HRSRC hRes = ::FindResource(hModule, MAKEINTRESOURCE(nID), RT_ICON);
DWORD dwError = GetLastError();
if(hRes == NULL)
return 0;
HGLOBAL hGlobal = ::LoadResource(hModule, hRes);
if(hGlobal == NULL)
return 0;
BYTE* lpData = (BYTE*)::LockResource(hGlobal);
ULONG ulSize = ::SizeofResource(hModule, hRes);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
BYTE* pBmpInfoStart = lpData;
BYTE* pNewDIB = NULL;
BITMAPINFOHEADER* pHeader = (BITMAPINFOHEADER*)pBmpInfoStart;
if(_winmajor == 5 && _winminor == 0 && pHeader->biBitCount == 32) // For Win2k 32bit only
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
int nWidth = pHeader->biWidth;
int nHeight = pHeader->biHeight / 2;
int nLineByte32 = LINE_BYTES(nWidth, 32);
int nLineByte24 = LINE_BYTES(nWidth, 24);
pHeader->biBitCount = 24;
if(nWidth <= 0 || nHeight <= 0 )
return NULL;
int nRemainMask = ulSize - sizeof(BITMAPINFOHEADER) - nWidth * nLineByte32;
pNewDIB = new BYTE[sizeof(BITMAPINFOHEADER) + nWidth * nLineByte24 + nRemainMask];
memcpy(pNewDIB, pHeader, sizeof(BITMAPINFOHEADER));
memcpy(pNewDIB + sizeof(BITMAPINFOHEADER) + nWidth * nLineByte24,
pBmpInfoStart + sizeof(BITMAPINFOHEADER) + nWidth * nLineByte32, nRemainMask);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
BYTE *pData24 = pNewDIB + sizeof(BITMAPINFOHEADER);
BYTE *pData32 = (BYTE*)(pHeader + 1);
for(int i =0; i <nHeight; ++i)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
BYTE *pLine24 = pData24;
BYTE *pLine32 = pData32;
BYTE *pLine32Last = pLine32 + nLineByte32;
while (pLine32 < pLine32Last)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
unsigned char ba = *(pLine32 + 3);
double da = ((double)(ba)) / 255;
if(da > 0)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
double fda = 1 - da;
*(pLine24++) = (*(pLine32++)) * da + 255 * fda;
*(pLine24++) = (*(pLine32++)) * da + 255 * fda;
*(pLine24++) =(*(pLine32++)) * da + 255 * fda;
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
*(pLine24++) = (*(pLine32++));
*(pLine24++) = (*(pLine32++));
*(pLine24++) =(*(pLine32++));
}
pLine32++;
}
pData24 += nLineByte24;
pData32 += nLineByte32;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
pBmpInfoStart = pNewDIB;
ulSize = sizeof(BITMAPINFOHEADER) + nWidth * nLineByte24 + nRemainMask;
}
HICON hIcon = CreateIconFromResourceEx(pBmpInfoStart, ulSize, TRUE,
0x00030000, (*(LPBITMAPINFOHEADER)pBmpInfoStart).biWidth,
(*(LPBITMAPINFOHEADER)pBmpInfoStart).biHeight / 2, 0);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
if(pNewDIB != NULL)
delete [] pNewDIB;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
return hIcon;
}