51testing 发表于 2007-12-13 10:26:27

图片镂空算法集合[图]

在开发界面及棋牌游戏过程中,需要很多镂空的图片,而且图片形式一般比较固定.
所以封装了几种常见的镂空方法.
1. 用于没有掩码图,只有指定透明色,不进行伸缩
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifvoid DrawTransBitmap( HDC hdcDest,            // 目标DC
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
int nXOriginDest,       // 目标X偏移
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
int nYOriginDest,       // 目标Y偏移
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
int nWidthDest,          // 目标宽度
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
int nHeightDest,      // 目标高度
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif                                          HDC hdcSrc,            // 源DC
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
int nXOriginSrc,         // 源X起点
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
int nYOriginSrc,         // 源Y起点
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif                                          COLORREF crTransparent // 透明色,COLORREF类型
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif                                        );
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif

适用图片:http://p.blog.csdn.net/images/p_blog_csdn_net/xuweiqun/contact_folder_light.bmp
2. 用于没有掩码图,只有指定透明色,可以进行伸缩

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
void DrawTransBitmap( HDC hdcDest,       // 目标DC
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
int nXOriginDest,   // 目标X偏移
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
int nYOriginDest,   // 目标Y偏移
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
int nWidthDest,      // 目标宽度
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
int nHeightDest,    // 目标高度
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif                                             HDC hdcSrc,          // 源DC
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
int nXOriginSrc,   // 源X起点
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
int nYOriginSrc,   // 源Y起点
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
int nWidthSrc,      // 源宽度
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
int nHeightSrc,      // 源高度
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif                                             COLORREF crTransparent// 透明色,COLORREF类型
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif      );

适用图片:同1,可拉伸
3.指定掩码图,和掩码图属于不同图片
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
void DrawTransBitmap( HDC hdcDest,      // 目标DC
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
int nXOriginDest,    // 目标X偏移
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
int nYOriginDest,    // 目标Y偏移
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
int nWidthDest,       // 目标宽度
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
int nHeightDest,   // 目标高度
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif                                             HDC hdcSrc,          // 源DC
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif                                             HDC hdcMask,       //掩码DC
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
int nXOriginSrc,   // 源X起点
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
int nYOriginSrc,   // 源Y起点
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif                                          COLORREF crTransparent// 透明色,COLORREF类型
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif                                          );

适用图片:http://p.blog.csdn.net/images/p_blog_csdn_net/xuweiqun/200.bmp和 http://p.blog.csdn.net/images/p_blog_csdn_net/xuweiqun/202.bmp
4.指定图片和掩码图同属于一张图片
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifvoid DrawTransBitmap(HDC hDC,
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif                                           int nPosX,
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif                                           int nPosY,
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif                                           int nCX,
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif                                           int nCY,
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif                                           HBITMAP hObj
                                           );

适用图片:http://p.blog.csdn.net/images/p_blog_csdn_net/xuweiqun/20012.bmp
5.得到位图HRGN
   HRGN CreateBitmapRgn(int nWidth,int nHeight,HBITMAP hbmp,COLORREF cTrans);

适用图片:http://p.blog.csdn.net/images/p_blog_csdn_net/xuweiqun/picback.bmp

以下是完整代码

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif//用于没有掩码图,只有指定透明色,不进行伸缩
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifvoid CCommon::DrawTransBitmap( HDC hdcDest,      // 目标DC
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
int nXOriginDest,   // 目标X偏移
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
int nYOriginDest,   // 目标Y偏移
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
int nWidthDest,   // 目标宽度
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
int nHeightDest,    // 目标高度
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif                  HDC hdcSrc,         // 源DC
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
int nXOriginSrc,    // 源X起点
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
int nYOriginSrc,    // 源Y起点
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif                  COLORREF crTransparent// 透明色,COLORREF类型
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif                  )
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif......{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    HBITMAP hOldImageBMP, hImageBMP = CreateCompatibleBitmap(hdcDest, nWidthDest, nHeightDest);    // 创建兼容位图
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    HBITMAP hOldMaskBMP, hMaskBMP = CreateBitmap(nWidthDest, nHeightDest, 1, 1, NULL);            // 创建单色掩码位图
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    HDC      hImageDC = CreateCompatibleDC(hdcDest);//临时DC
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    HDC      hMaskDC = CreateCompatibleDC(hdcDest);//临时掩码DC
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    hOldImageBMP = (HBITMAP)SelectObject(hImageDC, hImageBMP);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    hOldMaskBMP = (HBITMAP)SelectObject(hMaskDC, hMaskBMP);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    // 将源DC中的位图拷贝到临时DC中,源DC已经载入位图
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif   BitBlt(hImageDC, 0, 0, nWidthDest, nHeightDest, hdcSrc, nXOriginSrc, nYOriginSrc, SRCCOPY);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    // 设置临时DC的透明色
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    SetBkColor(hImageDC, crTransparent);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    // 生成透明区域为白色,其它区域为黑色的临时掩码DC的掩码位图
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    // 位图来自临时DC
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    BitBlt(hMaskDC, 0, 0, nWidthDest, nHeightDest, hImageDC, 0, 0, SRCCOPY);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    // 生成透明区域为黑色,其它区域保持不变的位图
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    SetBkColor(hImageDC, RGB(0,0,0));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    SetTextColor(hImageDC, RGB(255,255,255));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    BitBlt(hImageDC, 0, 0, nWidthDest, nHeightDest, hMaskDC, 0, 0, SRCAND);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    // 透明部分保持屏幕不变,其它部分变成黑色
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    SetBkColor(hdcDest,RGB(255,255,255));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    SetTextColor(hdcDest,RGB(0,0,0));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif   BitBlt(hdcDest, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest, hMaskDC, 0, 0, SRCAND);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    // "或"运算,生成最终效果
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    BitBlt(hdcDest, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest, hImageDC, 0, 0, SRCPAINT);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    // 清理、恢复   
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    SelectObject(hImageDC, hOldImageBMP);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    DeleteDC(hImageDC);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    SelectObject(hMaskDC, hOldMaskBMP);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    DeleteDC(hMaskDC);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    DeleteObject(hImageBMP);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    DeleteObject(hMaskBMP);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif//用于没有掩码图,只有指定透明色,可以进行伸缩
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifvoid CCommon::DrawTransBitmap( HDC hdcDest,      // 目标DC
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
int nXOriginDest,   // 目标X偏移
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
int nYOriginDest,   // 目标Y偏移
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
int nWidthDest,   // 目标宽度
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
int nHeightDest,    // 目标高度
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif                     HDC hdcSrc,         // 源DC
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
int nXOriginSrc,    // 源X起点
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
int nYOriginSrc,    // 源Y起点
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
int nWidthSrc,      // 源宽度
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
int nHeightSrc,   // 源高度
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif                     COLORREF crTransparent// 透明色,COLORREF类型
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif                     )
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif......{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    HBITMAP hOldImageBMP, hImageBMP = CreateCompatibleBitmap(hdcDest, nWidthDest, nHeightDest);    // 创建兼容位图
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    HBITMAP hOldMaskBMP, hMaskBMP = CreateBitmap(nWidthDest, nHeightDest, 1, 1, NULL);            // 创建单色掩码位图
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    HDC      hImageDC = CreateCompatibleDC(hdcDest);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    HDC      hMaskDC = CreateCompatibleDC(hdcDest);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    hOldImageBMP = (HBITMAP)SelectObject(hImageDC, hImageBMP);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    hOldMaskBMP = (HBITMAP)SelectObject(hMaskDC, hMaskBMP);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif   
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    // 将源DC中的位图拷贝到临时DC中
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
if (nWidthDest == nWidthSrc && nHeightDest == nHeightSrc)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif    ......{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif      BitBlt(hImageDC, 0, 0, nWidthDest, nHeightDest, hdcSrc, nXOriginSrc, nYOriginSrc, SRCCOPY);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    else
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif    ......{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif      StretchBlt(hImageDC, 0, 0, nWidthDest, nHeightDest,
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif      hdcSrc, nXOriginSrc, nYOriginSrc, nWidthSrc, nHeightSrc, SRCCOPY);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif   
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    // 设置透明色
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    SetBkColor(hImageDC, crTransparent);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    // 生成透明区域为白色,其它区域为黑色的掩码位图
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    BitBlt(hMaskDC, 0, 0, nWidthDest, nHeightDest, hImageDC, 0, 0, SRCCOPY);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif   
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    // 生成透明区域为黑色,其它区域保持不变的位图
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    SetBkColor(hImageDC, RGB(0,0,0));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    SetTextColor(hImageDC, RGB(255,255,255));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    BitBlt(hImageDC, 0, 0, nWidthDest, nHeightDest, hMaskDC, 0, 0, SRCAND);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    // 透明部分保持屏幕不变,其它部分变成黑色
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    SetBkColor(hdcDest,RGB(0xff,0xff,0xff));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    SetTextColor(hdcDest,RGB(0,0,0));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    BitBlt(hdcDest, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest, hMaskDC, 0, 0, SRCAND);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif   
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    // "或"运算,生成最终效果
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    BitBlt(hdcDest, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest, hImageDC, 0, 0, SRCPAINT);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif   
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    SelectObject(hImageDC, hOldImageBMP);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    DeleteDC(hImageDC);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    SelectObject(hMaskDC, hOldMaskBMP);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    DeleteDC(hMaskDC);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    DeleteObject(hImageBMP);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    DeleteObject(hMaskBMP);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif   
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif指定掩码图,和掩码图属于不同图片
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifvoid CCommon::DrawTransBitmap( HDC hdcDest,      // 目标DC
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
int nXOriginDest,   // 目标X偏移
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
int nYOriginDest,   // 目标Y偏移
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
int nWidthDest,   // 目标宽度
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
int nHeightDest,    // 目标高度
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif                  HDC hdcSrc,         // 源DC
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif                  HDC hdcMask,
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif                  int nXOriginSrc,    // 源X起点
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
int nYOriginSrc,    // 源Y起点
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif                  COLORREF crTransparent// 透明色,COLORREF类型
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif                  )
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif......{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    HBITMAP hOldImageBMP, hImageBMP = CreateCompatibleBitmap(hdcDest, nWidthDest, nHeightDest);    // 创建兼容位图
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif   HDC      hImageDC = CreateCompatibleDC(hdcDest);//临时DC
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif   hOldImageBMP = (HBITMAP)SelectObject(hImageDC, hImageBMP);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    // 将源DC中的位图拷贝到临时DC中,源DC已经载入位图
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif   BitBlt(hImageDC, 0, 0, nWidthDest, nHeightDest, hdcSrc, nXOriginSrc, nYOriginSrc, SRCCOPY);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif   // 设置临时DC的透明色
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    SetBkColor(hImageDC, crTransparent);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    // 生成透明区域为黑色,其它区域保持不变的位图
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    SetBkColor(hImageDC, RGB(0,0,0));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    SetTextColor(hImageDC, RGB(255,255,255));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    BitBlt(hImageDC, 0, 0, nWidthDest, nHeightDest, hdcMask, 0, 0, SRCAND);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    // 透明部分保持屏幕不变,其它部分变成黑色
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    SetBkColor(hdcDest,RGB(255,255,255));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    SetTextColor(hdcDest,RGB(0,0,0));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif   BitBlt(hdcDest, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest, hdcMask, 0, 0, SRCAND);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif   // "或"运算,生成最终效果
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    BitBlt(hdcDest, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest, hImageDC, 0, 0, SRCPAINT);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    // 清理、恢复   
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    SelectObject(hImageDC, hOldImageBMP);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    DeleteDC(hImageDC);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    DeleteObject(hImageBMP);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif指定图片和掩码图同属于一张图片
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifvoid CCommon::DrawTransBitmap(HDC hDC, int nPosX, int nPosY, int nCX, int nCY, HBITMAP hObj)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif......{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    HDC hMemDC= CreateCompatibleDC(hDC);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    HBITMAP hOldBMP=(HBITMAP)::SelectObject(hMemDC,hObj);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif      BitBlt(hDC,nPosX,nPosY,nCX,nCY,    hMemDC,nCX,0,SRCAND);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    BitBlt(hDC,nPosX,nPosY,nCX,nCY,    hMemDC,0,0,SRCPAINT);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    SelectObject(hMemDC,hOldBMP);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    DeleteDC(hMemDC);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifHRGN CCommon::CreateBitmapRgn(int nWidth,int nHeight,HBITMAP hbmp, COLORREF TransColor)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif......{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif      HDChmemDC;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif      //创建与传入DC兼容的临时DC
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif      hmemDC = ::CreateCompatibleDC(NULL);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif      
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif      HBITMAP hOldBmp = (HBITMAP)::SelectObject(hmemDC,hbmp);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif      
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif      
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif      //创建总的窗体区域,初始region为0
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif      HRGN hrgn;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif      hrgn = ::CreateRectRgn(0,0,0,0);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif         
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif      
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif      int y;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif      for(y=0;y<nHeight ;y++)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif      ......{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            HRGN rgnTemp; //保存临时region
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            int iX =
0;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            do
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif            ......{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif                //跳过透明色找到下一个非透明色的点.
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
while (iX < nWidth&& ::GetPixel(hmemDC,iX, y) == TransColor)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif                  iX++;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif               
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif                //记住这个起始点
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
int iLeftX = iX;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif               
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif                //寻找下个透明色的点
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
while (iX < nWidth&& ::GetPixel(hmemDC,iX, y) != TransColor)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif                  ++iX;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif               
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif                //创建一个包含起点与重点间高为1像素的临时“region”
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif                rgnTemp=::CreateRectRgn(iLeftX, y, iX, y+1);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif               
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif                //合并到主"region".
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif                CombineRgn( hrgn,hrgn,rgnTemp, RGN_OR);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif               
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif                //删除临时"region",否则下次创建时和出错
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif                ::DeleteObject(rgnTemp);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif            }while(iX <nWidth );
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            iX =
0;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif      }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif      
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif      ::SelectObject(hmemDC,hOldBmp);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif      ::DeleteDC(hmemDC);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif      return hrgn;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif   
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif

yuandjing 发表于 2007-12-13 13:05:12

强劲的技术贴阿
页: [1]
查看完整版本: 图片镂空算法集合[图]