>_<:Previous part we talk about how to map a transparent picture, and this time we will solve the problem how to change a picture's transparency.

>_<:Here my method is reading the picture's information and change its RGB or add another picture's RGB to it according to certain proportion(here result must between 0~255, like following example you can let background picture's RGB multiply by 0.7 and wanted picture's RGB multiply by 0.3), then map it on window.

>_<:Here is the method introduction:

  • firstly: load picture to handle:
  • bg=(HBITMAP)LoadImage(NULL,"bg.bmp",IMAGE_BITMAP,600,450,LR_LOADFROMFILE);
  • secondly:get the picture information to bm1 (suggestion find the knowledge about bitmap struct and it's better to know how computer save .bmp picture)
  • BITMAP bm1;
  • GetObject(bg,sizeof(BITMAP),&bm1);
  • Thirdly:ensure whether the picture is suit for this model.
  1. if(bm1.bmBitsPixel!= && bm1.bmBitsPixel!=)
  2. {
  3. MessageBox(NULL,"此程序只能在 32 bit 或 24 bit 显示模式中运行","警告",);
  4. return FALSE;
  5. }
  • Fourthly:get the RGB of .bmp picture saving it in unsigned char array.
  1. unsigned char *px1=new unsigned char[bm1.bmHeight * bm1.bmWidthBytes];
  2. GetBitmapBits(bg,bm1.bmHeight*bm1.bmWidthBytes,px1);
  • Fifthly:use the same way to  deal with another picture that you want to change transparency,the first one show as background.
  1. dra=(HBITMAP)LoadImage(NULL,"dra.bmp",IMAGE_BITMAP,,,LR_LOADFROMFILE);
  2. GetObject(dra,sizeof(BITMAP),&bm2);
  3. px2=new unsigned char[bm2.bmHeight * bm2.bmWidthBytes];
  4. GetBitmapBits(dra,bm2.bmHeight*bm2.bmWidthBytes,px2);
  • Sixth:according to wanted transparent picture's lenth and width change corresponding background picture's char array's RGB value.At the same time changing this picture's char array's value,and after this step operation the px2 array will including the already-seted-transparency picture information.
  1. int xend,yend;
  2. int x,y,i;
  3. int rgb_b;
  4. int PxBytes=bm1.bmBitsPixel/;
  5.  
  6. xend=xstart+;
  7. yend=ystart+;
  8.  
  9. for(y=ystart;y<yend;y++)
  10. {
  11. for(x=xstart;x<xend;x++)
  12. {
  13. rgb_b=y*bm1.bmWidthBytes+x*PxBytes;
  14.  
  15. px1[rgb_b]=px1[rgb_b]*0.7;
  16. px1[rgb_b+]=px1[rgb_b+]*0.7;
  17. px1[rgb_b+]=px1[rgb_b+]*0.7;
  18. }
  19. }
  20.  
  21. for(y=;y<(bm2.bmHeight);y++)
  22. {
  23. for(x=;x<bm2.bmWidth;x++)
  24. {
  25. rgb_b=y*bm2.bmWidthBytes+x*PxBytes;
  26. i=(ystart+y)*bm1.bmWidthBytes+(xstart+x)*PxBytes;
  27.  
  28. px2[rgb_b]=px2[rgb_b]*0.3+px1[i];
  29. px2[rgb_b+]=px2[rgb_b+]*0.3+px1[i+];
  30. px2[rgb_b+]=px2[rgb_b+]*0.3+px1[i+];
  31. }
  32. }
  • Finally:use px2[] to reflash dra and make the handle dra include new seted-transparency picture's information.
  1. SetBitmapBits(dra,bm2.bmHeight*bm2.bmWidthBytes,px2);

>_<:now the handle of bg.bmp is in bg;the handle of seted-transparency dra.bmp is in dra.Next,you can directly map them on window's dc.

  1. void MyPaint(HDC hdc)
  2. {
  3. SelectObject(mdc,bg);
  4. BitBlt(hdc,,,,,mdc,,,SRCCOPY);//在窗口位置、大小、原图剪切位
  5.  
  6. SelectObject(mdc,dra);
  7. BitBlt(hdc,xstart,ystart,,,mdc,,,SRCCOPY);
  8. }

>_<:Here is all the code:

  1. //{{NO_DEPENDENCIES}}
  2. // Microsoft Visual C++ generated include file.
  3. // Used by FE.RC
  4. //
  5. #define IDR_MAINFRAME 128
  6. #define IDD_FE_DIALOG 102
  7. #define IDD_ABOUTBOX 103
  8. #define IDS_APP_TITLE 103
  9. #define IDM_ABOUT 104
  10. #define IDM_EXIT 105
  11. #define IDS_HELLO 106
  12. #define IDI_FE 107
  13. #define IDI_SMALL 108
  14. #define IDC_FE 109
  15. #define IDC_MYICON 2
  16. #define IDC_STATIC -1
  17. // Next default values for new objects
  18. //
  19. #ifdef APSTUDIO_INVOKED
  20. #ifndef APSTUDIO_READONLY_SYMBOLS
  21.  
  22. #define _APS_NEXT_RESOURCE_VALUE 129
  23. #define _APS_NEXT_COMMAND_VALUE 32771
  24. #define _APS_NEXT_CONTROL_VALUE 1000
  25. #define _APS_NEXT_SYMED_VALUE 110
  26. #endif
  27. #endif

resourse.h

  1. // stdafx.h : include file for standard system include files,
  2. // or project specific include files that are used frequently, but
  3. // are changed infrequently
  4. //
  5.  
  6. #if !defined(AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_)
  7. #define AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_
  8.  
  9. #if _MSC_VER > 1000
  10. #pragma once
  11. #endif // _MSC_VER > 1000
  12.  
  13. #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
  14.  
  15. // Windows Header Files:
  16. #include <windows.h>
  17.  
  18. // C RunTime Header Files
  19. #include <stdlib.h>
  20. #include <malloc.h>
  21. #include <memory.h>
  22. #include <tchar.h>
  23.  
  24. // Local Header Files
  25.  
  26. // TODO: reference additional headers your program requires here
  27.  
  28. //{{AFX_INSERT_LOCATION}}
  29. // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
  30.  
  31. #endif // !defined(AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_)

StdAfx.h

  1. #include "stdafx.h"
  2. #include "resourse.h"
  3.  
  4. #define MAX_LOADSTRING 100
  5.  
  6. // Global Variables:
  7. HINSTANCE hInst; // current instance
  8. TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
  9. TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text
  10. HBITMAP bg,dra;
  11. HDC mdc;
  12.  
  13. const int xstart=;
  14. const int ystart=;
  15. // Foward declarations of functions included in this code module:
  16. ATOM MyRegisterClass(HINSTANCE hInstance);
  17. BOOL InitInstance(HINSTANCE, int);
  18. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  19. LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
  20. void MyPaint(HDC hdc);
  21. //========================================================================================
  22. int APIENTRY WinMain(HINSTANCE hInstance,
  23. HINSTANCE hPrevInstance,
  24. LPSTR lpCmdLine,
  25. int nCmdShow)
  26. {
  27. // TODO: Place code here.
  28. MSG msg;
  29.  
  30. MyRegisterClass(hInstance);//调用函数向系统注册窗口类别,输入参数hInstance是目前运行程序的对象代码;
  31.  
  32. // 调用InitInstance函数,进行初始化操作;
  33. if (!InitInstance (hInstance, nCmdShow))
  34. {
  35. return FALSE;
  36. }
  37.  
  38. // 消息循环(通过消息循环来获取信息,
  39. //进行必要的键盘信息转换而后将控制权交给操作系统,
  40. //有操作系统决定哪个程序的消息处理函数处理消息
  41. while (GetMessage(&msg, NULL, , )) //获取程序消息
  42. {
  43. TranslateMessage(&msg);//转换伪码及字符
  44. DispatchMessage(&msg);//将控制权交给系统,再有系统决定负责处理消息的程序;
  45. }
  46.  
  47. return msg.wParam;
  48. }
  49. //=====================================================================================
  50.  
  51. //=============================================================================================
  52. //在建立程序窗口实体之前,必须先定义一个窗口类别,其中包含所要建立窗口的信息,
  53. //并向系统注册,这里的MyRegisterClass函数就是进行定义及注册窗口类别的函数。
  54. //==============================================================================================
  55. ATOM MyRegisterClass(HINSTANCE hInstance)
  56. {
  57. WNDCLASSEX wcex; //申请一个窗口类别“WNDCLASSEX”和结构”wcex“
  58. //--------------------------------------------------------------
  59. //定义vcex结构的各项信息,其中设定信息处理函数(lpfnWndProc)
  60. //为WNDPROC,类别名称为(lpszClassName)为”fe";
  61. //--------------------------------------------------------------
  62. wcex.cbSize = sizeof(WNDCLASSEX);
  63.  
  64. wcex.style = CS_HREDRAW | CS_VREDRAW;
  65. wcex.lpfnWndProc = (WNDPROC)WndProc;
  66. wcex.cbClsExtra = ;
  67. wcex.cbWndExtra = ;
  68. wcex.hInstance = hInstance;
  69. wcex.hIcon = NULL;
  70. wcex.hCursor = NULL;
  71. wcex.hCursor = LoadCursor(NULL,IDC_ARROW);
  72. wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+);
  73. wcex.lpszMenuName = NULL;
  74. wcex.lpszClassName = "fe";
  75. wcex.hIconSm = NULL;
  76.  
  77. return RegisterClassEx(&wcex);//调用RegisterClassEx函数注册类别,返回一个“ATOM"形态的字符串
  78. //此字符串即为类别名称”fe";
  79. }
  80. //============================================================================================
  81.  
  82. //============================================================================================
  83. //按照前面所定义的窗口类别来建立并显示实际的程序窗口
  84. //============================================================================================
  85. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  86. {
  87. HWND hWnd;
  88. HDC hdc;
  89. hInst = hInstance; // 把instance handle 储存在全局变量中;
  90.  
  91. hWnd = CreateWindow("fe","绘图窗口",WS_OVERLAPPEDWINDOW,
  92. CW_USEDEFAULT, , CW_USEDEFAULT, , NULL, NULL, hInstance, NULL);
  93. //-----------------------------------------------
  94. //调用CreateWindow函数来建立一个窗口对象
  95. //第一个参数就是窗口建立依据的类别名称
  96. //-----------------------------------------------
  97. if (!hWnd)
  98. {
  99. return FALSE;
  100. }
  101. //------------------------------------------------
  102. //设定窗口的位置及窗口的大小,然后绘制显示在设备上
  103. //-------------------------------------------------
  104. MoveWindow(hWnd,,,,,true);//位置及大小
  105. ShowWindow(hWnd, nCmdShow);//改定窗口显示时的状态
  106. UpdateWindow(hWnd);//将窗口绘制在显示设备上
  107.  
  108. hdc=GetDC(hWnd);
  109. mdc=CreateCompatibleDC(hdc);
  110.  
  111. BITMAP bm1,bm2;
  112. unsigned char *px1,*px2;
  113.  
  114. bg=(HBITMAP)LoadImage(NULL,"bg.bmp",IMAGE_BITMAP,,,LR_LOADFROMFILE);
  115. //名、类型、大小、加载方式;
  116. GetObject(bg,sizeof(BITMAP),&bm1);
  117.  
  118. if(bm1.bmBitsPixel!= && bm1.bmBitsPixel!=)
  119. {
  120. MessageBox(NULL,"此程序只能在 32 bit 或 24 bit 显示模式中运行","警告",);
  121. return FALSE;
  122. }
  123.  
  124. px1=new unsigned char[bm1.bmHeight * bm1.bmWidthBytes];
  125. GetBitmapBits(bg,bm1.bmHeight*bm1.bmWidthBytes,px1);
  126.  
  127. dra=(HBITMAP)LoadImage(NULL,"dra.bmp",IMAGE_BITMAP,,,LR_LOADFROMFILE);
  128. GetObject(dra,sizeof(BITMAP),&bm2);
  129. px2=new unsigned char[bm2.bmHeight * bm2.bmWidthBytes];
  130. GetBitmapBits(dra,bm2.bmHeight*bm2.bmWidthBytes,px2);
  131.  
  132. int xend,yend;
  133. int x,y,i;
  134. int rgb_b;
  135. int PxBytes=bm1.bmBitsPixel/;
  136.  
  137. xend=xstart+;
  138. yend=ystart+;
  139.  
  140. for(y=ystart;y<yend;y++)
  141. {
  142. for(x=xstart;x<xend;x++)
  143. {
  144. rgb_b=y*bm1.bmWidthBytes+x*PxBytes;
  145.  
  146. px1[rgb_b]=px1[rgb_b]*0.7;
  147. px1[rgb_b+]=px1[rgb_b+]*0.7;
  148. px1[rgb_b+]=px1[rgb_b+]*0.7;
  149. }
  150. }
  151.  
  152. for(y=;y<(bm2.bmHeight);y++)
  153. {
  154. for(x=;x<bm2.bmWidth;x++)
  155. {
  156. rgb_b=y*bm2.bmWidthBytes+x*PxBytes;
  157. i=(ystart+y)*bm1.bmWidthBytes+(xstart+x)*PxBytes;
  158.  
  159. px2[rgb_b]=px2[rgb_b]*0.3+px1[i];
  160. px2[rgb_b+]=px2[rgb_b+]*0.3+px1[i+];
  161. px2[rgb_b+]=px2[rgb_b+]*0.3+px1[i+];
  162. }
  163. }
  164.  
  165. SetBitmapBits(dra,bm2.bmHeight*bm2.bmWidthBytes,px2);
  166.  
  167. MyPaint(hdc);
  168.  
  169. ReleaseDC(hWnd,hdc);
  170. delete [] px1;
  171. delete [] px2;
  172.  
  173. return TRUE;
  174. }
  175. //============================================================================================
  176.  
  177. //============================================================================================
  178. //
  179. //============================================================================================
  180. void MyPaint(HDC hdc)
  181. {
  182. SelectObject(mdc,bg);
  183. BitBlt(hdc,,,,,mdc,,,SRCCOPY);//在窗口位置、大小、原图剪切位
  184.  
  185. SelectObject(mdc,dra);
  186. BitBlt(hdc,xstart,ystart,,,mdc,,,SRCCOPY);
  187. }
  188. //============================================================================================
  189.  
  190. //============================================================================================
  191. //在前面定义类别的时候把WndProc定义为消息处理函数(当某些外部消息发生时,会按消息的类型
  192. //来决定该如何进行处理。此外该函数也是一个回叫函数(CALLBACK)(windows系统函数)每一个
  193. //程序都会接收信息,选择性接受、处理;
  194. //============================================================================================
  195. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  196. {
  197. PAINTSTRUCT ps;
  198. HDC hdc;
  199.  
  200. switch (message) //判断消息类型
  201. {
  202. case WM_PAINT: //窗口重绘制
  203. hdc = BeginPaint(hWnd, &ps);
  204. MyPaint(hdc);
  205. EndPaint(hWnd, &ps);
  206. break;
  207. case WM_DESTROY: //处理窗口结束消息
  208. DeleteDC(mdc);
  209. DeleteObject(bg);
  210. PostQuitMessage();
  211. break;
  212. default:
  213. return DefWindowProc(hWnd, message, wParam, lParam);
  214. }
  215. return ;
  216. }
  217. //============================================================================================

main.cpp

[游戏模版9] Win32 半透明 图像处理的更多相关文章

  1. [游戏模版2] Win32最小框架

    >_<:Just the minimum Win32  frame don't have any other special function. //{{NO_DEPENDENCIES}} ...

  2. [游戏模版18] Win32 五子棋

    >_<:Learning its AI logic. >_<:resource >_<:code: #include <windows.h> // C ...

  3. [游戏模版3] Win32 画笔 画刷 图形

    >_<:introduce the functions of define\create\use pen and brush to draw all kinds of line and s ...

  4. [游戏模版4] Win32 显示鼠标位置

    >_<:use MOUSE_MOVE message refresh the position information. >_<:use LOWORD(lParam) get ...

  5. [游戏模版5] Win32 折线 弧线

    >_<:first build some points put in poly1[],poly2[] and poly3[] in the function of InitInstance ...

  6. [游戏模版6] Win32 graph

    >_<:there in the MyPaint(...) function respectively use Ellipse(...) draw ellipse, use RoundRe ...

  7. [游戏模版7] Win32 最简单贴图

    >_<:this is the first using mapping. >_<:There will be introducing how to do: First load ...

  8. [游戏模版8] Win32 透明贴图

    >_<:The same with previous introduction. In the InitInstance fanction make a little change: &g ...

  9. [游戏模版10] Win32 平面地图贴图 正

    >_<:picture resource >_<:If you master the ways of mapping picture,then this problem is ...

随机推荐

  1. easyui1.32 各种问题汇总

    问题一 场景:tab切换,每个tab里用div放一个dataGrid,默认display:none隐藏,当display:'block'的时候,dataGrid会显示不全,仅显示一条线. 解决方法:切 ...

  2. 2016.02.17 JS DOM编程艺术 第四五六章

    看完这三章内容,集中精力,加快速度.

  3. scala学习心得3

    在scala中可以定义函数字面量参数,定义方式如下:

  4. SVM1 线性SVM

    一.Linear Support Vector Machine 接下来的讨论假设数据都是线性可分的. 1.1 SVM的引入:增大对测量误差的容忍度 假设有训练数据和分类曲线如下图所示: 很明显,三个分 ...

  5. decimalFormat("#","##0.00") java

    importjava.text.DecimalFormat; publicclassTestNumberFormat{ publicstaticvoidmain(String[]args){ doub ...

  6. SecureCRT自动备份脚本-华为

    # $language = "VBScript" # $interface = "1.0" Sub Main Const ForReading = 1, For ...

  7. calc()问题

    什么是calc()? 学习calc()之前,我们有必要先知道calc()是什么?只有知道了他是个什么东东?在实际运用中更好的使用他. calc() 从字面我们可以把他理解为一个函数function.其 ...

  8. PHP开发环境配置

    wamp:windows apache MySQL php 下载php版本问题在windows 下apache+php用vc6 thread safe版本 1.首先安装apache到e盘myenv/a ...

  9. SILVERLIGHT 多维表头、复杂表头 MULTIPLE HEADER

    先上图, 众所周知,利用silverlight datagrid展示数据相当方便,但是想要弄出一个漂亮的表头却要费尽周折.此文的目的就是简要介绍一下利用第三方控件 C1.Silverlight.Fle ...

  10. 关于checkbox与文字混排无法对齐的解决方法

    先前代码如下 <span style="vertical-align:middle"><input  type="checkbox" name ...