2006年4月22日星期六

TransparentBlt的实现

Windows的TransparentBlt函数在显示器上能够正确画出透明位图,而且MS Office的虚拟打印机也支持此API。遗憾的是Adobe的虚拟打印机,以及市场上的某些打印机都不支持此API。搜索了网上关于此函数的若干实现方法,发现大多都是参照Christian Graus 在WinDEV发表的函数TransparentBltU,此方法在自己的工作中不能完全实现透明效果(可能我自己的代码使用有问题),而且同微软的TransparentBlt一样,也不支持某些打印机,所以只好自己实现了此API,有兴趣的朋友可以参考一下,函数如下:

 

BOOL TransparentBlt( HDC hdcDest,
      int nXOriginDest,
      int nYOriginDest,
      int nWidthDest,
      int nHeightDest,
      HDC hdcSrc,
      int nXOriginSrc,
      int nYOriginSrc,
      int nWidthSrc,
      int nHeightSrc,
      UINT crTransparent )
{
    HDC hdcMem = CreateCompatibleDC( NULL );
    if( hdcMem == NULL )
        return false;
    HBITMAP hBmp, hOldBmp;
    hBmp = CreateCompatibleBitmap( hdcSrc, nWidthDest, nHeightDest );
    if( hBmp == NULL )
        return false;
    hOldBmp = (HBITMAP)SelectObject( hdcMem, hBmp );

    if( hOldBmp == NULL )

        return false;
    if( StretchBlt( hdcMem, 0, 0, nWidthDest, nHeightDest,
           hdcSrc, nXOriginSrc, nYOriginSrc, nWidthSrc, nHeightSrc, SRCCOPY ) )
    {
        return false;
    }

    int i,j;
    COLORREF crColor;
    for( j=0; j<nHeightDest; j++ )
    {
        for( i=0; i<nWidthDest; i++ )
        {
            crColor= GetPixel( hdcMem, i, j );
            if( crColor != crTransparent )
                SetPixel( hdcDest, nXOriginDest+i, nYOriginDest+j, crColor );
        }
    }

    SelectObject( hdcMem, hOldBmp );
    DeleteObject( hBmp );
    DeleteDC(hdcMem );
 
    return true;
}

1 条评论:

雲端(vodoon) 说...

~一堆代码  晕~