MFC淡出效果核心代码

来源:互联网 发布:迪恩网络模板 编辑:程序博客网 时间:2024/05/22 17:56
//透明混合
void  AlphaBlendU(PBYTE pDest, PBYTE pSrcBack, int cx, int cy, PBYTE pSrc, BYTE byAlpha)
{
    
const BYTE byDiff = (BYTE)(255 - byAlpha);

    
for (int i = 0; i < cx * cy; i++)
    
{
        pDest[
0]=(BYTE)((pSrcBack[0* byDiff + pSrc[0* byAlpha)>>8);  //blue
        pDest[1]=(BYTE)((pSrcBack[1* byDiff + pSrc[1* byAlpha)>>8);  //green
        pDest[2]=(BYTE)((pSrcBack[2* byDiff + pSrc[2* byAlpha)>>8);  //red

        pDest 
+= 4;   // the 4th byte is preserved.
        pSrcBack += 4;
        pSrc 
+= 4;
    }

}



//淡出绘制核心代码
void AnimateFade(CDC* pDestDC, CDC* pSrcDC, CRect rc, int nSteps, int nAnimationTime)
{
        
const int cx = rc.Width();
        
const int cy = rc.Height();

        BITMAPINFOHEADER BMI;
        
// Fill in the header info.
        ZeroMemory (&BMI, sizeof (BMI));
        BMI.biSize 
= sizeof(BITMAPINFOHEADER);
        BMI.biWidth 
= cx;
        BMI.biHeight 
= cy;
        BMI.biPlanes 
= 1;
        BMI.biBitCount 
= 32;
        BMI.biCompression 
= BI_RGB;   // No compression

        BYTE 
* pSrcBits = NULL;
        HBITMAP hbmSrc 
= CreateDIBSection (NULL, (BITMAPINFO *)&BMI, DIB_RGB_COLORS, (void **)&pSrcBits, 00l);

        BYTE 
* pSrcBackBits = NULL;
        HBITMAP hbmSrcBack 
= CreateDIBSection (NULL, (BITMAPINFO *)&BMI, DIB_RGB_COLORS, (void **)&pSrcBackBits, 00l);

        BYTE 
* pDestBits = NULL;
        HBITMAP hbmDest 
= CreateDIBSection (NULL, (BITMAPINFO *)&BMI, DIB_RGB_COLORS, (void **)&pDestBits, 00l);

        
// Copy our source and destination bitmaps onto our DIBSections,
        
// so we can get access to their bits using the BYTE *'s we passed into CreateDIBSection

        CDC dc;
        dc.CreateCompatibleDC(NULL);
        HBITMAP hbmpOld 
= (HBITMAP) ::SelectObject(dc, hbmSrc);
        ::BitBlt(dc, 
00, cx, cy, pSrcDC->GetSafeHdc(), 00, SRCCOPY);

        ::SelectObject(dc, hbmSrcBack);
        ::BitBlt(dc, 
00, cx, cy, pDestDC->GetSafeHdc (), 00, SRCCOPY);

        DWORD dwTimePer 
= nAnimationTime / nSteps;

        ::SelectObject(dc, hbmDest);
        
for (int i = 1; i < nSteps; ++i)
        
{
            DWORD dwTime 
= GetTickCount ();
            AlphaBlendU(pDestBits, pSrcBackBits, cx, cy, pSrcBits, (BYTE)(
255 * i  / nSteps));
                pDestDC
->BitBlt(rc.left, rc.top, rc.Width(), rc.Height(), &dc, 00, SRCCOPY);
                    dwTime 
= GetTickCount () - dwTime;
            
if (dwTime < dwTimePer)
            
{
                Sleep(dwTimePer 
- dwTime);
            }

        }

        ::SelectObject(dc, hbmpOld);
        DeleteObject(hbmSrc);
        DeleteObject(hbmSrcBack);
        DeleteObject(hbmDest);
}