- Code:
- // 创建逻辑调色板
- void MakePalette(int nColorTableLength, LPRGBQUAD lpColorTable, HPALETTE hPalette)
- {
- // 如果是真彩色就不用创建
- if (nColorTableLength == 0)
- return;
- // 删除旧的逻辑调色板
- if (hPalette != NULL)
- DeleteObject(hPalette);
- //
- LPLOGPALETTE pLogPal = (LPLOGPALETTE) new char[2*sizeof(WORD)+
- nColorTableLength*sizeof(PALETTEENTRY)];
- pLogPal->palVersion = 0x300;
- pLogPal->palNumEntries = nColorTableLength;
- LPRGBQUAD lpDibQuad = (LPRGBQUAD)lpColorTable;
- for (int i = 0; i<nColorTableLength; i++)
- {
- pLogPal->palPalEntry[i].peRed = lpDibQuad->rgbRed;
- pLogPal->palPalEntry[i].peGreen = lpDibQuad->rgbGreen;
- pLogPal->palPalEntry[i].peBlue = lpDibQuad->rgbBlue;
- pLogPal->palPalEntry[i].peFlags = 0;
- lpDibQuad++;
- }
- // 创建逻辑调色板
- hPalette = CreatePalette(pLogPal);
- delete pLogPal;
- }
- // 计算颜色表长度
- int ComputeColorTableLength(int nBCount)
- {
- printf("/nbit:%d/n", nBCount);
- int nColorLength;
- switch(nBCount)
- {
- case 1:
- nColorLength = 2;
- break;
- case 4:
- nColorLength = 16;
- break;
- case 8:
- nColorLength = 256;
- break;
- case 16:
- case 24:
- case 32:
- nColorLength = 0;
- break;
- default:
- assert(FALSE);//断言它的条件返回错误,则终止程序执行
- }
- assert((nColorLength>=0) && (nColorLength <=256));
- return nColorLength;
- }