void DrawTransparentBitmap(HDC  hdc,  HBITMAP  hBitmap,  short  xStart,  short  yStart,  COLORREF  cTransparentColor);

函数的实现:

void CLoginPanel::DrawTransparentBitmap(HDC  hdc,  HBITMAP  hBitmap,  short  xStart,  short  yStart,  COLORREF  cTransparentColor)  
     {  
     BITMAP          bm;  
     COLORREF      cColor;  
     HBITMAP        bmAndBack,  bmAndObject,  bmAndMem,  bmSave;  
     HBITMAP        bmBackOld,  bmObjectOld,  bmMemOld,  bmSaveOld;  
     HDC                hdcMem,  hdcBack,  hdcObject,  hdcTemp,  hdcSave;  
     POINT            ptSize;  
 
     hdcTemp  =  CreateCompatibleDC(hdc);  
     SelectObject(hdcTemp,  hBitmap);      //  Select  the  bitmap  
 
     GetObject(hBitmap,  sizeof(BITMAP),  (LPSTR)&bm);  
     ptSize.x  =  bm.bmWidth;                        //  Get  width  of  bitmap  
     ptSize.y  =  bm.bmHeight;                      //  Get  height  of  bitmap  
     DPtoLP(hdcTemp,  &ptSize,  1);            //  Convert  from  device  
 
                                                                         //  to  logical  points  
 
     //  Create  some  DCs  to  hold  temporary  data.  
     hdcBack      =  CreateCompatibleDC(hdc);  
     hdcObject  =  CreateCompatibleDC(hdc);  
     hdcMem        =  CreateCompatibleDC(hdc);  
     hdcSave      =  CreateCompatibleDC(hdc);  
 
     //  Create  a  bitmap  for  each  DC.  DCs  are  required  for  a  number  of  
     //  GDI  functions.  
 
     //  Monochrome  DC  
     bmAndBack      =  CreateBitmap(ptSize.x,  ptSize.y,  1,  1,  NULL);  
 
     //  Monochrome  DC  
     bmAndObject  =  CreateBitmap(ptSize.x,  ptSize.y,  1,  1,  NULL);  
 
     bmAndMem        =  CreateCompatibleBitmap(hdc,  ptSize.x,  ptSize.y);  
     bmSave            =  CreateCompatibleBitmap(hdc,  ptSize.x,  ptSize.y);  
 
     //  Each  DC  must  select  a  bitmap  object  to  store  pixel  data.  
     bmBackOld      = (HBITMAP)SelectObject(hdcBack,  bmAndBack);  
     bmObjectOld  =  (HBITMAP)SelectObject(hdcObject,  bmAndObject);  
     bmMemOld        = (HBITMAP)SelectObject(hdcMem,  bmAndMem);  
     bmSaveOld      = (HBITMAP)SelectObject(hdcSave,  bmSave);  
 
     //  Set  proper  mapping  mode.  
     SetMapMode(hdcTemp,  GetMapMode(hdc));  
 
     //  Save  the  bitmap  sent  here,  because  it  will  be  overwritten.  
     BitBlt(hdcSave,  0,  0,  ptSize.x,  ptSize.y,  hdcTemp,  0,  0,  SRCCOPY);  
 
     //  Set  the  background  color  of  the  source  DC  to  the  color.  
     //  contained  in  the  parts  of  the  bitmap  that  should  be  transparent  
     cColor  =  SetBkColor(hdcTemp,  cTransparentColor);  
 
     //  Create  the  object  mask  for  the  bitmap  by  performing  a  BitBlt  
     //  from  the  source  bitmap  to  a  monochrome  bitmap.  
     BitBlt(hdcObject,  0,  0,  ptSize.x,  ptSize.y,  hdcTemp,  0,  0,  
                   SRCCOPY);  
 
     //  Set  the  background  color  of  the  source  DC  back  to  the  original  
     //  color.  
     SetBkColor(hdcTemp,  cColor);  
 
     //  Create  the  inverse  of  the  object  mask.  
     BitBlt(hdcBack,  0,  0,  ptSize.x,  ptSize.y,  hdcObject,  0,  0,  
                   NOTSRCCOPY);  
 
     //  Copy  the  background  of  the  main  DC  to  the  destination.  
     BitBlt(hdcMem,  0,  0,  ptSize.x,  ptSize.y,  hdc,  xStart,  yStart,  
                   SRCCOPY);  
 
     //  Mask  out  the  places  where  the  bitmap  will  be  placed.  
     BitBlt(hdcMem,  0,  0,  ptSize.x,  ptSize.y,  hdcObject,  0,  0,  SRCAND);  
 
     //  Mask  out  the  transparent  colored  pixels  on  the  bitmap.  
     BitBlt(hdcTemp,  0,  0,  ptSize.x,  ptSize.y,  hdcBack,  0,  0,  SRCAND);  
 
     //  XOR  the  bitmap  with  the  background  on  the  destination  DC.  
     BitBlt(hdcMem,  0,  0,  ptSize.x,  ptSize.y,  hdcTemp,  0,  0,  SRCPAINT);  
 
     //  Copy  the  destination  to  the  screen.  
     BitBlt(hdc,  xStart,  yStart,  ptSize.x,  ptSize.y,  hdcMem,  0,  0,  
                   SRCCOPY);  
 
     //  Place  the  original  bitmap  back  into  the  bitmap  sent  here.  
     BitBlt(hdcTemp,  0,  0,  ptSize.x,  ptSize.y,  hdcSave,  0,  0,  SRCCOPY);  
 
     //  Delete  the  memory  bitmaps.  
     DeleteObject(SelectObject(hdcBack,  bmBackOld));  
     DeleteObject(SelectObject(hdcObject,  bmObjectOld));  
     DeleteObject(SelectObject(hdcMem,  bmMemOld));  
     DeleteObject(SelectObject(hdcSave,  bmSaveOld));  
 
     //  Delete  the  memory  DCs.  
     DeleteDC(hdcMem);  
     DeleteDC(hdcBack);  
     DeleteDC(hdcObject);  
     DeleteDC(hdcSave);  
     DeleteDC(hdcTemp);  
     }

https://blog.csdn.net/goodowxy/article/details/2038014

程序中图片透明 函数(使用SetBkColor API函数)的更多相关文章

  1. C#中图片透明【转】

    C#中图片透明 /// <summary> /// 处理图片透明操作 /// </summary> /// <param name="srcImage" ...

  2. Visual C++中最常用的类与API函数

    这篇文章能让初学者快速了解visual C++ MFC中常见的核心的类与函数,虽然全部看下来有点枯燥,但对初学者快速了解MFC的框架结构很有好处. 常用类 CArchive类:用于二进制保存档案 CB ...

  3. C#中可直接调用WIN32的API函数--USER32.DLL

    Win32的API函数可以直接在C#中直接调用,在做WinForm时还是很有帮助的.有时候直接调用Win32的API,可以很高效的实现想要的效果. using System; using System ...

  4. 微信小程序中图片上传阿里云Oss

    本人今年6月份毕业,最近刚在上海一家小公司实习,做微信小程序开发.最近工作遇到一个小问题. 微信小程序图片上传阿里云服务器Oss也折腾了蛮久才解决的,所以特意去记录一下. 第一步:配置阿里云地址: 我 ...

  5. 软件看门狗--别让你地程序无响应(使用未公开API函数IsHungAppWindow,知识点较全)

    正文一.概述一些重要的程序,必须让它一直跑着:而且还要时时关心它的状态——不能让它出现死锁现象.当然,如果一个主程序会出现死锁,肯定是设计或者编程上的失误.我们首要做的事是,把这个Bug揪出来.但如果 ...

  6. 在 Windows 8、Windows 10 桌面模式下的 .NET Framework 程序中,引用 Windows.Runtime 的 API。

    参考:1.https://www.cnblogs.com/webtojs/p/9675956.html 2.http://jennal.com/2016/04/28/using-windows-run ...

  7. C#API函数

    API函数是构筑Windows应用程序的基石,是Windows编程的必备利器.每一种Windows应用程序开发工具都提供了间接或直接调用了Windows API函数的方法,或者是调用Windows A ...

  8. 替换应用程序exe图标,主要使用BeginUpdateResource,UpdateResource API函数

    替换应用程序exe图标,主要使用的API函数是BeginUpdateResource(),UpdateResource(),EndUpdateResource()来使用自定义的ico文件类替换exe程 ...

  9. API函数ShellExecute与ShellExecuteEx用法

    ShellExecute: 1.函数功能:你可以给它任何文件的名字,它都能识别出来并打开它.2.函数原型: HINSTANCE ShellExecute( HWND hwnd, LPCTSTR lpO ...

随机推荐

  1. rman 备份并异机恢复

    1.RMAN 备份脚本 RUN { CONFIGURE RETENTION POLICY DAYS; CONFIGURE CONTROLFILE AUTOBACKUP ON; CONFIGURE CO ...

  2. yum配置中driver-class-name: com.mysql.jdbc.Driver报错

    错误: 原因: 解决方法:把方框中的<scope>runtime</scope>删掉

  3. COGS——T 7. 通信线路

    http://www.cogs.pro/cogs/problem/problem.php?pid=7 ★★   输入文件:mcst.in   输出文件:mcst.out   简单对比时间限制:1.5 ...

  4. 讲的好,php后端模式,php-fpm以及php-cgi, fast-cgi,以及与nginx的关系

    关于cgi是什么,fast-cgi是什么,php-cgi是什么,fast-cgi是什么,下面这篇讲的很清楚: https://segmentfault.com/q/1010000000256516 另 ...

  5. 图解hdu5301Buildings

    这个题就是给出一个大矩形n*m.当中有个1*1的小格子不能被占用,然后要你用非常多小矩形去填满.问小矩形的最小最大面积是多少. 显然小矩形必定是1*x的最好,毕竟i*x,若i>1则还是能够拆成非 ...

  6. 【HDU 4763】Theme Section(KMP)

    这题数据水的一B.直接暴力都能够过. 比赛的时候暴力过的.回头依照正法做了一发. 匹配的时候 失配函数 事实上就是前缀 后缀的匹配长度,之后就是乱搞了. KMP的题可能不会非常直接的出,可是KMP的思 ...

  7. webgoat 7.1 实战指南

    WSASP中文文档参考链接: http://www.owasp.org.cn/owasp-project/2017-owasp-top-10 OWASP Top 10 2017中文版V1.3http: ...

  8. POJ 3040 贪心

    贪心好题 ---. 思路: 从大到小凑C 如果不够 再从小到大补满(超过)C //By SiriusRen #include <cstdio> #include <cstring&g ...

  9. python +uiautomator 安卓UI控件操作

    一.搭建环境 准备:win7.JDK.androidSDK(adt-bundle-windows-x86_64-20140702\sdk).Appium.安卓模拟器(真机也可以),可以到这个地址下载h ...

  10. 洛谷 P1553 数字反转(升级版)

    P1553 数字反转(升级版) 题目描述 给定一个数,请将该数各个位上数字反转得到一个新数. 这次与NOIp2011普及组第一题不同的是:这个数可以是小数,分数,百分数,整数.整数反转是将所有数位对调 ...