2006
07-14

 如果系统支持调色板,创建动态调色板pAnimate与初始调色板pOriginal。从位图中获取初始调色板,若为淡入将背景色赋予动态调色板。利用前面所提到的公式,根据淡入淡出标志,在循环中更改动态调色板,用AnimatePalette函数设置调色板,改变显示结果。
  如果系统不支持调色板,保存位图调色板到初始调色板,若为淡入将背景色赋予位图调色板,利用前面所提到的公式,根据淡入淡出标志,在循环中更改位图调色板,显示位图改变显示结果。返回:位图的淡入淡出。

//参数说明:
//hDIB -位图句柄
//color -指定背景色
//xDest -显示位图的左上角x坐标
//yDest -显示位图的左上角y坐标
//nLoops -循环次数
//nDelay -每次循环中的延时
//nFlag -淡入淡出标志 1–淡入 其它–淡出
void FadeInORFadeOut( CDC *pDC, HANDLE hDIB, COLORREF color, int xDest, int yDest,
int nLoops, int nDelay ,int nFlag)
{
   int i,j;
   HPALETTE hPal;
   PALETTEENTRY pAnimate[256];
   PALETTEENTRY pOriginal[256];
   CPalette pal;
   // 创建236色调色板
   BITMAPINFO &bmInfo = *(LPBITMAPINFO)hDIB ;
   int nColors = bmInfo.bmiHeader.biClrUsed ? bmInfo.bmiHeader.biClrUsed : 1 << bmInfo.bmiHeader.biBitCount;
   int nWidth = bmInfo.bmiHeader.biWidth;
   int nHeight = bmInfo.bmiHeader.biHeight;
   LPVOID lpDIBBits = (LPVOID)(bmInfo.bmiColors + nColors);
   int clrRValue = GetRValue(color);
   int clrGValue = GetGValue(color);
   int clrBValue = GetBValue(color);
   if( pDC->GetDeviceCaps(RASTERCAPS) & RC_PALETTE && nColors <= 256 )
   {
      if (!(hPal = Create236Palette(hDIB)))
      return;
      pal.Attach( hPal );
      // 得到调色板初始值
      GetPaletteEntries(hPal, 0, nColors, (LPPALETTEENTRY)&pOriginal);
      // 设定动态调色板初始值为指定色
      if(nFlag==1)
      {
         for (j = 0; j < nColors; j++)
         {
             pAnimate[j].peRed = clrRValue;
             pAnimate[j].peGreen = clrGValue;
             pAnimate[j].peBlue = clrBValue;
             pAnimate[j].peFlags = PC_RESERVED;
         }
      }
      else
      {
         GetPal etteEntries(hPal, 0, nColors, (LPPALETTEENTRY)&pAnimate);
         CPalet te *pOldPalette = pDC->SelectPalette(&pal, FALSE);
         pDC-&g t;RealizePalette();
         CDC memDC;
         memDC. CreateCompatibleDC( pDC );
         CBitma p bmp;
         bmp.Cr eateCompatibleBitmap( pDC, nWidth, nHeight );
         CBitma p *pOldBitmap = memDC.SelectObject( &bmp );
         CPalet te *pOldMemPalette = memDC.SelectPalette(&pal, FALSE);
         memDC. RealizePalette();
         ::SetD IBitsToDevice(memDC.m_hDC,0,0,nWidth,nHeight,0,0,0,nHeight,l pDIBBits,(LPBITMAPINFO)hDIB,DIB_RGB_COLORS);
         Animat ePalette(hPal, 0, nColors, (LPPALETTEENTRY)&pAnimate);
         pDC-&g t;BitBlt(xDest, yDest, nWidth, nHeight, &memDC,0,0,SRCCOPY );
         // 淡入淡出
         for( i=1; i <= nLoops; i++ )
         {
             for (j = 0; j < nColors; j++)
             {
                if(nFlag==1)
                 {
                    pAnimate[j].peRed = clrRValue – ((clrRValue-pOriginal[j].peRed)*i)/nLoops;
                    pAnimate[j].peGreen = clrGValue – ((clrGValue-pOriginal[j].peGreen)*i)/nLoops;
                    pAnimate[j].peBlue = clrBValue – ((clrBValue-pOriginal[j].peBlue)*i)/nLoops;
                }
                else
                 {
                    pAnimate[j].peRed = pOriginal[j].peRed – ((pOriginal[j].peRed – clrRValue)*i)/nLoops;
                    pAnimate[j].peGreen = pOriginal[j].peGreen – ((pOriginal[j].peGreen – clrGValue)*i)/nLoops;
                    pAnimate[j].peBlue = pOriginal[j].peBlue – ((pOriginal[j].peBlue – clrBValue)*i)/nLoops;
                }
             }
             pal.AnimatePalette(0, nColors, (LPPALETTEENTRY)&pAnimate);
             // Delay…
             Sleep(nDelay);
         }
         memDC. SelectPalette(pOldMemPalette, FALSE);
         memDC. SelectObject( pOldBitmap );
         pDC-&g t;SelectPalette(pOldPalette, FALSE);
      }
      else if( (pDC->GetDeviceCaps(RASTERCAPS) & RC_PALETTE) == 0 && nColors <= 256 )
      {
         for( int i=0; i < nColors; i++)
         {
             pOriginal.peRed = bmInfo.bmiColors.rgbRed ;
             pOriginal.peGreen = bmInfo.bmiColors.rgbGreen;
             pOriginal.peBlue = bmInfo.bmiColors.rgbBlue ;
             if(nFlag==1)
             {
                bmInfo.bmiColors .rgbRed = clrRValue;
                bmInfo.bmiColors .rgbGreen = clrGValue;
                bmInfo.bmiColors .rgbBlue = clrBValue;
             }
         }
         ::SetD IBitsToDevice(pDC->m_hDC,0,0,nWidth,nHeight,0,0,0,nHeight ,lpDIBBits,(LPBITMAPINFO)hDIB,DIB_RGB_COLORS);
         // 淡入淡出
         for( i=1; i <= nLoops; i++ )
         {
             for (j = 0; j < nColors; j++)
             {
                if(nFlag==1)
                 {
                    bmInfo.bmiColors [j].rgbRed = clrRValue – ((clrRValue-pOriginal[j].peRed)*i)/nLoops;
                    bmInfo.bmiColors [j].rgbGreen = clrGValue – ((clrGValue-pOriginal[j].peGreen)*i)/nLoops;
                    bmInfo.bmiColors [j].rgbBlue = clrBValue – ((clrBValue-pOriginal[j].peBlue)*i)/nLoops;
                }
                else
                 {
                    bmInfo.bmiColors [j].rgbRed = pOriginal[j].peRed – ((pOriginal[j].peRed-clrRValue)*i)/nLoops;
                    bmInfo.bmiColors [j].rgbGreen = pOriginal[j].peGreen – ((pOriginal[j].peGreen-clrGValue)*i)/nLoops;
                    bmInfo.bmiColors [j].rgbBlue = pOriginal[j].peBlue – ((pOriginal[j].peBlue-clrBValue)*i)/nLoops;
                }
             }
             ::SetD IBitsToDevice(pDC->m_hDC,0,0,nWidth,nHeight,0,0,0,nHeight ,lpDIBBits,(LPBITMAPINFO)hDIB,DIB_RGB_COLORS);
             // Delay…
             Sleep(nDelay);
         }
      }


留下一个回复