📄 fadein.shtml
字号:
1 << bmInfo.bmiHeader.biBitCount;
int nWidth = bmInfo.bmiHeader.biWidth;
int nHeight = bmInfo.bmiHeader.biHeight;
// Obtain the original entries in the palette
GetPaletteEntries(hPal, 0, nColors, (LPPALETTEENTRY)&peOriginal);
// Change all the entries in the palette to the start color
int clrRValue = GetRValue(clrStart);
int clrGValue = GetGValue(clrStart);
int clrBValue = GetBValue(clrStart);
for (int j = 0; j < nColors; j++)
{
peAnimate[j].peRed = clrRValue;
peAnimate[j].peGreen = clrGValue;
peAnimate[j].peBlue = clrBValue;
peAnimate[j].peFlags = PC_RESERVED;
}
// Select the palette
CPalette *pOldPalette = pDC->SelectPalette(&pal, FALSE);
pDC->RealizePalette();
// We need to draw the image so that it appears as a rectangle
// with the start color. At the same time we don't want the
// colors to be remapped, otherwise all the pixels would remain
// the same color. We use a memory DC to achieve this.
CDC memDC;
memDC.CreateCompatibleDC( pDC );
CBitmap bmp;
bmp.CreateCompatibleBitmap( pDC, nWidth, nHeight );
CBitmap *pOldBitmap = memDC.SelectObject( &bmp );
CPalette *pOldMemPalette = memDC.SelectPalette(&pal, FALSE);
memDC.RealizePalette();
// Draw the image to memDC
LPVOID lpDIBBits = (LPVOID)(bmInfo.bmiColors + nColors);
::SetDIBitsToDevice(memDC.m_hDC, // hDC
0, // XDest
0, // YDest
nWidth, // nDestWidth
nHeight, // nDestHeight
0, // XSrc
0, // YSrc
0, // nStartScan
nHeight, // nNumScans
lpDIBBits, // lpBits
(LPBITMAPINFO)hDIB, // lpBitsInfo
DIB_RGB_COLORS); // wUsage
// Set the color to start color
AnimatePalette(hPal, 0, nColors, (LPPALETTEENTRY)&peAnimate);
// Now copy the image from the memory DC
// Since the palettes in memDC and pDC are the same, no color mapping
// takes place. The image will appear in the start color
pDC->BitBlt(xDest, yDest, nWidth, nHeight, &memDC,0,0,SRCCOPY );
// Fade in
for( int i=1; i <= nLoops; i++ )
{
for (j = 0; j < nColors; j++)
{
peAnimate[j].peRed = clrRValue -
((clrRValue-peOriginal[j].peRed)*i)/nLoops;
peAnimate[j].peGreen = clrGValue -
((clrGValue-peOriginal[j].peGreen)*i)/nLoops;
peAnimate[j].peBlue = clrBValue -
((clrBValue-peOriginal[j].peBlue)*i)/nLoops;
}
pal.AnimatePalette(0, nColors, (LPPALETTEENTRY)&peAnimate);
// Delay...
Sleep(nDelay);
}
// Reselect old objects into DC
memDC.SelectPalette(pOldMemPalette, FALSE);
memDC.SelectObject( pOldBitmap );
pDC->SelectPalette(pOldPalette, FALSE);
}
</FONT></TT></PRE>
<h4>Function 3: Fade out an image to a given color</h4>
Fading out an image is much simpler than fading it in. We don't have to bother with an identity palette. After creating the reserved palette, we can select it into the device context, realize and then draw the image onto the device context. Now we have the image on the target device and we need to animate the palette so that at the end the colors in the palette are all the same.
<PRE><TT><FONT COLOR="#990000">
// FadeOut - Draws a bitmap and fades it out to the desired end color
// pDC - Pointer to the device context to draw in
// hDIB - Handle to a device-independent bitmap
// clrEnd - The final color of the bitmap
// xDest - x-coordinate of upper-left corner of dest. rect.
// yDest - y-coordinate of upper-left corner of dest. rect.
// nLoops - How many loops to fade the image out.
// nDelay - Delay in milli-seconds between each loop
//
void FadeOut( CDC *pDC, HANDLE hDIB, COLORREF clrEnd, int xDest, int yDest,
int nLoops, int nDelay )
{
HPALETTE hPal;
PALETTEENTRY peAnimate[256];
PALETTEENTRY peOriginal[256];
CPalette pal;
// Create a 236 colors or less logical palette with PC_RESERVED set
if (!(hPal = CreateReservedPalette(hDIB)))
return;
// The palette will be deleted when the CPalette object is destroyed
pal.Attach( hPal );
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;
// Obtain the original entries in the palette
GetPaletteEntries(hPal, 0, nColors, (LPPALETTEENTRY)&peAnimate);
GetPaletteEntries(hPal, 0, nColors, (LPPALETTEENTRY)&peOriginal);
// Get end color values
int clrRValue = GetRValue(clrEnd);
int clrGValue = GetGValue(clrEnd);
int clrBValue = GetBValue(clrEnd);
// Select the palette
CPalette *pOldPalette = pDC->SelectPalette(&pal, FALSE);
pDC->RealizePalette();
// Draw the image
LPVOID lpDIBBits = (LPVOID)(bmInfo.bmiColors + nColors);
::SetDIBitsToDevice(pDC->m_hDC, // hDC
xDest, // XDest
yDest, // YDest
nWidth, // nDestWidth
nHeight, // nDestHeight
0, // XSrc
0, // YSrc
0, // nStartScan
nHeight, // nNumScans
lpDIBBits, // lpBits
(LPBITMAPINFO)hDIB, // lpBitsInfo
DIB_RGB_COLORS); // wUsage
// Fade out
for( int i=1; i <= nLoops; i++ )
{
for (int j = 0; j < nColors; j++)
{
peAnimate[j].peRed = peOriginal[j].peRed -
((peOriginal[j].peRed - clrRValue)*i)/nLoops;
peAnimate[j].peGreen = peOriginal[j].peGreen -
((peOriginal[j].peGreen - clrGValue)*i)/nLoops;
peAnimate[j].peBlue = peOriginal[j].peBlue -
((peOriginal[j].peBlue - clrBValue)*i)/nLoops;
}
pal.AnimatePalette(0, nColors, (LPPALETTEENTRY)&peAnimate);
// Delay...
Sleep(nDelay);
}
// Reselect old objects into DC
pDC->SelectPalette(pOldPalette, FALSE);
}
</FONT></TT></PRE>
<P>
<HR>
<TABLE BORDER=0 WIDTH="100%" >
<TR>
<TD WIDTH="33%"><FONT SIZE=-1><A HREF="http://www.codeguru.com">Goto HomePage</A></FONT></TD>
<TD WIDTH="33%">
<CENTER><FONT SIZE=-2>© 1998 Zafir Anjum</FONT> </CENTER>
</TD>
<TD WIDTH="34%">
<DIV ALIGN=right><FONT SIZE=-1>Contact me: <A HREF="mailto:zafir@home.com">zafir@home.com</A> </FONT></DIV>
</TD>
</TR>
</TABLE>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -