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. 03015_DBUtils

    1.概述 (1)如果只使用JDBC进行开发,我们会发现冗余代码过多,为了简化JDBC开发,本案例我们讲采用apache commons组件一个成员:DBUtils : (2)DBUtils就是JDBC ...

  2. 03014_properties配置文件

    1.使用properties配置文件 (1)开发中获得连接的4个参数(驱动.URL.用户名.密码)通常都存在配置文件中,方便后期维护,程序如果需要更换数据库,只需要修改配置文件即可: (2)通常情况下 ...

  3. POJ——T 3067 Japan

    http://poj.org/problem?id=3067 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29474   ...

  4. pipPython运维日记

    一 Python 工作环境管理 1.1 使用 pyenv 管理不同的Python 版本 克隆项目安装 git clone https://github.com/yyuu/pyenv.git ~/.py ...

  5. 洛谷P1720 月落乌啼算钱

    目背景 (本道题目木有以藏歌曲……不用猜了……) <爱与愁的故事第一弹·heartache>最终章. 吃完pizza,月落乌啼知道超出自己的预算了.为了不在爱与愁大神面前献丑,只好还是硬着 ...

  6. mount---挂载文件系统

    挂载概念 Linux中的根目录以外的文件要想被访问,需要将其“关联”到根目录下的某个目录来实现,这种关联操作就是“挂载”,这个目录就是“挂载点”,解除次关联关系的过程称之为“卸载”. 注意:“挂载点” ...

  7. python 补0的方法

    # 方法一 z = 'bb' z.zfill(6) ----'0000bb' n = ' n.zfill(5) ----' # 方法二 ' " ---- '报错' # 方法的区别 方法二只能 ...

  8. Python 3 与"Hello World!"

    Python 3 版本 Python的3.0版本,常被称为Python 3000,或简称Py3k.相对于Python的早期版本,这是一个较大的升级.为了不带入过多的累赘,Python 3.0在设计的时 ...

  9. 洛谷 P1226 取余运算||快速幂

    P1226 取余运算||快速幂 题目描述 输入b,p,k的值,求b^p mod k的值.其中b,p,k*k为长整型数. 输入输出格式 输入格式: 三个整数b,p,k. 输出格式: 输出“b^p mod ...

  10. 注意string的insert函数的几种形式

    string (1) string& insert (size_t pos, const string& str); substring (2) string& insert ...