BOOL CClockST::GetBitmapAndPalette(UINT nIDResource, CBitmap& bitmap, CPalette& pal) { HINSTANCE hInstResource = NULL; // Find correct resource handle hInstResource = AfxFindResourceHandle(MAKEINTRESOURCE(nIDResource), RT_BITMAP/*RT_GROUP_BITMAP*/); HBITMAP hBmp = (HBITMAP)::LoadImage( hInstResource, MAKEINTRESOURCE(nIDResource), IMAGE_BITMAP, 0,0, LR_CREATEDIBSECTION); if (hBmp == NULL) return FALSE; bitmap.Attach(hBmp); // Create a logical palette for the bitmap DIBSECTION ds; BITMAPINFOHEADER &bmInfo = ds.dsBmih; bitmap.GetObject(sizeof(ds), &ds); int nColors = bmInfo.biClrUsed ? bmInfo.biClrUsed : 1 << bmInfo.biBitCount; // Create a halftone palette if colors > 256. CClientDC dc(NULL); // Desktop DC if(nColors > 256) pal.CreateHalftonePalette(&dc); else { // Create the palette RGBQUAD *pRGB = new RGBQUAD[nColors]; CDC memDC; memDC.CreateCompatibleDC(&dc); memDC.SelectObject( &bitmap ); ::GetDIBColorTable( memDC, 0, nColors, pRGB ); UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * nColors); LOGPALETTE *pLP = (LOGPALETTE *) new BYTE[nSize]; pLP->palVersion = 0x300; pLP->palNumEntries = nColors; for (int i=0; i < nColors; i++) { pLP->palPalEntry[i].peRed = pRGB[i].rgbRed; pLP->palPalEntry[i].peGreen = pRGB[i].rgbGreen; pLP->palPalEntry[i].peBlue = pRGB[i].rgbBlue; pLP->palPalEntry[i].peFlags = 0; } pal.CreatePalette( pLP ); delete[] pLP; delete[] pRGB; } return TRUE; } // End of GetBitmapAndPalette
///////////////////////////////////////////////////////////////////////////// // // Create the palette. Use halftone palette for hi-color bitmaps. // ///////////////////////////////////////////////////////////////////////////// zBOOL ZDib::CreatePalette( CPalette& pal ) { // Should not already have palette ASSERT( pal.m_hObject == 0 ); zBOOL bRC = FALSE; RGBQUAD *colors = new RGBQUAD[ MAXPALCOLORS ]; UINT nColors = GetColorTable( colors, MAXPALCOLORS ); if ( nColors > 0 ) { // Allocate memory for logical palette. int nLth = sizeof( LOGPALETTE ) + sizeof( PALETTEENTRY ) * nColors; LOGPALETTE *pLogPal = (LOGPALETTE *) new char[ nLth ]; if ( pLogPal == 0 ) return( 0 ); // Set version and number of palette entries. pLogPal->palVersion = PALVERSION; pLogPal->palNumEntries = nColors; // Copy color entries. for ( UINT k = 0; k < nColors; k++ ) { pLogPal->palPalEntry[ k ].peRed = colors[ k ].rgbRed; pLogPal->palPalEntry[ k ].peGreen = colors[ k ].rgbGreen; pLogPal->palPalEntry[ k ].peBlue = colors[ k ].rgbBlue; pLogPal->palPalEntry[ k ].peFlags = 0; } // Create the palette and destroy LOGPAL. bRC = pal.CreatePalette( pLogPal ); delete [] (zPCHAR) pLogPal; } else { CWindowDC dcScreen( 0 ); bRC = pal.CreateHalftonePalette( &dcScreen ); } delete [] colors; return( bRC ); }
////////////////// // Create the palette. Use halftone palette for hi-color bitmaps. // BOOL CDib::CreatePalette(CPalette& pal) { // should not already have palette ASSERT(pal.m_hObject==NULL); BOOL bRet = FALSE; RGBQUAD* colors = new RGBQUAD[MAXPALCOLORS]; UINT nColors = GetColorTable(colors, MAXPALCOLORS); if (nColors > 0) { // Allocate memory for logical palette int len = sizeof(LOGPALETTE) + sizeof(PALETTEENTRY) * nColors; LOGPALETTE* pLogPal = (LOGPALETTE*)new char[len]; if (!pLogPal) return NULL; // set version and number of palette entries pLogPal->palVersion = PALVERSION; pLogPal->palNumEntries = nColors; // copy color entries for (UINT i = 0; i < nColors; i++) { pLogPal->palPalEntry[i].peRed = colors[i].rgbRed; pLogPal->palPalEntry[i].peGreen = colors[i].rgbGreen; pLogPal->palPalEntry[i].peBlue = colors[i].rgbBlue; pLogPal->palPalEntry[i].peFlags = 0; } // create the palette and destroy LOGPAL bRet = pal.CreatePalette(pLogPal); delete [] (char*)pLogPal; } else { CWindowDC dcScreen(NULL); bRet = pal.CreateHalftonePalette(&dcScreen); } delete colors; return bRet; }