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. 修改mysql 的 字符集 解决中文乱码问题

    确定的是自己mysql的字符集是否都是utf8, 通过mysql -u root -p然后输入数据库的密码登陆. 在mysql命令行查询自己的字符集是否都是utf8(除了文件的编码是binary). ...

  2. linux下,yum 安装mysql

    顺手记录一下安装mysqlclient 先安装mysql-devel yum install mysql-devel 再安装mysqlclient pip3 install mysqlclient 开 ...

  3. CODEVS——T1961 躲避大龙

    http://codevs.cn/problem/1961/  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解  查看运行结果     题目描述 De ...

  4. [Python] Working with file

    Python allows you to open a file, do operations on it, and automatically close it afterwards using w ...

  5. Codeforces Round #262 (Div. 2) 题解

    A. Vasya and Socks time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  6. java 链接server上的 mongodb 出现 connect time out 问题

    异常信息 十二月 22, 2014 5:27:58 下午 com.mongodb.DBTCPConnector initDirectConnection 警告: Exception executing ...

  7. vue2留言板

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. 3.常用Bracket插件

    转自:https://blog.csdn.net/iso_wsy/article/details/52608205 1.Emmet 如果你从事Web前端开发的话,对该插件一定不会陌生.它可以加快你的 ...

  9. 给网站设置ICO图标

    方法一:         直接在站点根目录下放入名为:favicon.ico 的图标文件(必须要为 ICO 文件,BMP 及其他格式的图片文件不行).还有将 favicon.ico 中的 favico ...

  10. Hyperic

    https://my.oschina.net/hyperichq/blog/525590