>_<: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.
  && bm1.bmBitsPixel!=)
 {
     MessageBox(NULL,);
     return FALSE;
 }
  • Fourthly:get the RGB of .bmp picture saving it in unsigned char array.
 unsigned char *px1=new unsigned char[bm1.bmHeight * bm1.bmWidthBytes];
 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.
 dra=(HBITMAP)LoadImage(NULL,,,LR_LOADFROMFILE);
 GetObject(dra,sizeof(BITMAP),&bm2);
 px2=new unsigned char[bm2.bmHeight * bm2.bmWidthBytes];
 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.
 int xend,yend;
 int x,y,i;
 int rgb_b;
 ;

 xend=xstart+;
 yend=ystart+;

 for(y=ystart;y<yend;y++)
 {
    for(x=xstart;x<xend;x++)
    {
        rgb_b=y*bm1.bmWidthBytes+x*PxBytes;

        px1[rgb_b]=px1[rgb_b]*0.7;
        px1[rgb_b+]=px1[rgb_b+]*0.7;
        px1[rgb_b+]=px1[rgb_b+]*0.7;
    }
 }

 ;y<(bm2.bmHeight);y++)
 {
    ;x<bm2.bmWidth;x++)
    {
        rgb_b=y*bm2.bmWidthBytes+x*PxBytes;
        i=(ystart+y)*bm1.bmWidthBytes+(xstart+x)*PxBytes;

        px2[rgb_b]=px2[rgb_b]*0.3+px1[i];
        px2[rgb_b+]=px2[rgb_b+]*];
        px2[rgb_b+]=px2[rgb_b+]*];
    }
 }
  • Finally:use px2[] to reflash dra and make the handle dra include new seted-transparency picture's information.
 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.

 void MyPaint(HDC hdc)
 {
     SelectObject(mdc,bg);
     BitBlt(hdc,,,,,mdc,,,SRCCOPY);//在窗口位置、大小、原图剪切位

     SelectObject(mdc,dra);
     BitBlt(hdc,xstart,ystart,,,mdc,,,SRCCOPY);
 }

>_<:Here is all the code:

 //{{NO_DEPENDENCIES}}
 // Microsoft Visual C++ generated include file.
 // Used by FE.RC
 //
 #define IDR_MAINFRAME                    128
 #define IDD_FE_DIALOG        102
 #define IDD_ABOUTBOX                    103
 #define IDS_APP_TITLE                    103
 #define IDM_ABOUT                        104
 #define IDM_EXIT                        105
 #define IDS_HELLO                        106
 #define IDI_FE                107
 #define IDI_SMALL                        108
 #define IDC_FE                109
 #define IDC_MYICON                        2
 #define IDC_STATIC                        -1
 // Next default values for new objects
 //
 #ifdef APSTUDIO_INVOKED
 #ifndef APSTUDIO_READONLY_SYMBOLS

 #define _APS_NEXT_RESOURCE_VALUE        129
 #define _APS_NEXT_COMMAND_VALUE         32771
 #define _APS_NEXT_CONTROL_VALUE         1000
 #define _APS_NEXT_SYMED_VALUE           110
 #endif
 #endif

resourse.h

 // stdafx.h : include file for standard system include files,
 //  or project specific include files that are used frequently, but
 //      are changed infrequently
 //

 #if !defined(AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_)
 #define AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_

 #if _MSC_VER > 1000
 #pragma once
 #endif // _MSC_VER > 1000

 #define WIN32_LEAN_AND_MEAN        // Exclude rarely-used stuff from Windows headers

 // Windows Header Files:
 #include <windows.h>

 // C RunTime Header Files
 #include <stdlib.h>
 #include <malloc.h>
 #include <memory.h>
 #include <tchar.h>

 // Local Header Files

 // TODO: reference additional headers your program requires here

 //{{AFX_INSERT_LOCATION}}
 // Microsoft Visual C++ will insert additional declarations immediately before the previous line.

 #endif // !defined(AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_)

StdAfx.h

 #include "stdafx.h"
 #include "resourse.h"

 #define MAX_LOADSTRING 100

 // Global Variables:
 HINSTANCE hInst;                                // current instance
 TCHAR szTitle[MAX_LOADSTRING];                                // The title bar text
 TCHAR szWindowClass[MAX_LOADSTRING];                                // The title bar text
 HBITMAP bg,dra;
 HDC mdc;

 ;
 ;
 // Foward declarations of functions included in this code module:
 ATOM                MyRegisterClass(HINSTANCE hInstance);
 BOOL                InitInstance(HINSTANCE, int);
 LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
 LRESULT CALLBACK    About(HWND, UINT, WPARAM, LPARAM);
 void                MyPaint(HDC hdc);
 //========================================================================================
 int APIENTRY WinMain(HINSTANCE hInstance,
                      HINSTANCE hPrevInstance,
                      LPSTR     lpCmdLine,
                      int       nCmdShow)
 {
      // TODO: Place code here.
     MSG msg;

     MyRegisterClass(hInstance);//调用函数向系统注册窗口类别,输入参数hInstance是目前运行程序的对象代码;

     // 调用InitInstance函数,进行初始化操作;
     if (!InitInstance (hInstance, nCmdShow))
     {
         return FALSE;
     }

     // 消息循环(通过消息循环来获取信息,
     //进行必要的键盘信息转换而后将控制权交给操作系统,
     //有操作系统决定哪个程序的消息处理函数处理消息
     , )) //获取程序消息
     {
             TranslateMessage(&msg);//转换伪码及字符
             DispatchMessage(&msg);//将控制权交给系统,再有系统决定负责处理消息的程序;
     }

     return msg.wParam;
 }
 //=====================================================================================

 //=============================================================================================
 //在建立程序窗口实体之前,必须先定义一个窗口类别,其中包含所要建立窗口的信息,
 //并向系统注册,这里的MyRegisterClass函数就是进行定义及注册窗口类别的函数。
 //==============================================================================================
 ATOM MyRegisterClass(HINSTANCE hInstance)
 {
     WNDCLASSEX wcex;            //申请一个窗口类别“WNDCLASSEX”和结构”wcex“
                                 //--------------------------------------------------------------
                                 //定义vcex结构的各项信息,其中设定信息处理函数(lpfnWndProc)
                                 //为WNDPROC,类别名称为(lpszClassName)为”fe";
                                 //--------------------------------------------------------------
     wcex.cbSize = sizeof(WNDCLASSEX);

     wcex.style            = CS_HREDRAW | CS_VREDRAW;
     wcex.lpfnWndProc    = (WNDPROC)WndProc;
     wcex.cbClsExtra        = ;
     wcex.cbWndExtra        = ;
     wcex.hInstance        = hInstance;
     wcex.hIcon            = NULL;
     wcex.hCursor        = NULL;
     wcex.hCursor        = LoadCursor(NULL,IDC_ARROW);
     wcex.hbrBackground    = (HBRUSH)(COLOR_WINDOW+);
     wcex.lpszMenuName    = NULL;
     wcex.lpszClassName    = "fe";
     wcex.hIconSm        = NULL;

     return RegisterClassEx(&wcex);//调用RegisterClassEx函数注册类别,返回一个“ATOM"形态的字符串
                                   //此字符串即为类别名称”fe";
 }
 //============================================================================================

 //============================================================================================
 //按照前面所定义的窗口类别来建立并显示实际的程序窗口
 //============================================================================================
 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
 {
    HWND hWnd;
    HDC hdc;
    hInst = hInstance; // 把instance handle 储存在全局变量中;

    hWnd = CreateWindow("fe","绘图窗口",WS_OVERLAPPEDWINDOW,
       CW_USEDEFAULT, , CW_USEDEFAULT, , NULL, NULL, hInstance, NULL);
                       //-----------------------------------------------
                       //调用CreateWindow函数来建立一个窗口对象
                       //第一个参数就是窗口建立依据的类别名称
                       //-----------------------------------------------
    if (!hWnd)
    {
       return FALSE;
    }
    //------------------------------------------------
    //设定窗口的位置及窗口的大小,然后绘制显示在设备上
    //-------------------------------------------------
    MoveWindow(hWnd,,,,,true);//位置及大小
    ShowWindow(hWnd, nCmdShow);//改定窗口显示时的状态
    UpdateWindow(hWnd);//将窗口绘制在显示设备上

    hdc=GetDC(hWnd);
    mdc=CreateCompatibleDC(hdc);

    BITMAP bm1,bm2;
    unsigned char *px1,*px2;

    bg=(HBITMAP)LoadImage(NULL,,,LR_LOADFROMFILE);
    //名、类型、大小、加载方式;
    GetObject(bg,sizeof(BITMAP),&bm1);

     && bm1.bmBitsPixel!=)
    {
        MessageBox(NULL,);
        return FALSE;
    }

    px1=new unsigned char[bm1.bmHeight * bm1.bmWidthBytes];
    GetBitmapBits(bg,bm1.bmHeight*bm1.bmWidthBytes,px1);

    dra=(HBITMAP)LoadImage(NULL,,,LR_LOADFROMFILE);
    GetObject(dra,sizeof(BITMAP),&bm2);
    px2=new unsigned char[bm2.bmHeight * bm2.bmWidthBytes];
    GetBitmapBits(dra,bm2.bmHeight*bm2.bmWidthBytes,px2);

    int xend,yend;
    int x,y,i;
    int rgb_b;
    ;

    xend=xstart+;
    yend=ystart+;

    for(y=ystart;y<yend;y++)
    {
        for(x=xstart;x<xend;x++)
        {
            rgb_b=y*bm1.bmWidthBytes+x*PxBytes;

            px1[rgb_b]=px1[rgb_b]*0.7;
            px1[rgb_b+]=px1[rgb_b+]*0.7;
            px1[rgb_b+]=px1[rgb_b+]*0.7;
        }
    }

    ;y<(bm2.bmHeight);y++)
    {
        ;x<bm2.bmWidth;x++)
        {
            rgb_b=y*bm2.bmWidthBytes+x*PxBytes;
            i=(ystart+y)*bm1.bmWidthBytes+(xstart+x)*PxBytes;

            px2[rgb_b]=px2[rgb_b]*0.3+px1[i];
            px2[rgb_b+]=px2[rgb_b+]*];
            px2[rgb_b+]=px2[rgb_b+]*];
        }
    }

    SetBitmapBits(dra,bm2.bmHeight*bm2.bmWidthBytes,px2);

    MyPaint(hdc);

    ReleaseDC(hWnd,hdc);
    delete [] px1;
    delete [] px2;

    return TRUE;
 }
 //============================================================================================

 //============================================================================================
 //
 //============================================================================================
 void MyPaint(HDC hdc)
 {
     SelectObject(mdc,bg);
     BitBlt(hdc,,,,,mdc,,,SRCCOPY);//在窗口位置、大小、原图剪切位

     SelectObject(mdc,dra);
     BitBlt(hdc,xstart,ystart,,,mdc,,,SRCCOPY);
 }
 //============================================================================================

 //============================================================================================
 //在前面定义类别的时候把WndProc定义为消息处理函数(当某些外部消息发生时,会按消息的类型
 //来决定该如何进行处理。此外该函数也是一个回叫函数(CALLBACK)(windows系统函数)每一个
 //程序都会接收信息,选择性接受、处理;
 //============================================================================================
 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
 {
     PAINTSTRUCT ps;
     HDC hdc;

     switch (message)                   //判断消息类型
     {
         case WM_PAINT:                //窗口重绘制
             hdc = BeginPaint(hWnd, &ps);
             MyPaint(hdc);
             EndPaint(hWnd, &ps);
             break;
         case WM_DESTROY:              //处理窗口结束消息
             DeleteDC(mdc);
             DeleteObject(bg);
             PostQuitMessage();
             break;
         default:
             return DefWindowProc(hWnd, message, wParam, lParam);
    }
    ;
 }
 //============================================================================================

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. marked.js简易手册

    marked.js简易手册 本文介绍的是marked.js.秉持"来之即用"的原则,对它进行简要的翻译和归纳, 安装 在网上引用或者是引用本地文件即可.要么就用命令行: npm i ...

  2. z-stack协议uart分析(DMA)

    1.从ZMain里面的main函数开始分析 2.进入int main( void ); HalDriverInit();   //硬件相关初始化,有DMA初始化和UART初始化 3.进入HalDriv ...

  3. nginx做nodejs(express等通用)反向代理

    首先配置环境nginx+nodejs...(没有请看我的其他文章,此处不重复) cd 到nginx的site-available目录 ubuntu的在 cd /etc/nginx/site-avail ...

  4. 20145229&amp;20145316 《信息安全系统设计基础》实验三 实时系统的移植

    实验封面 实验内容 1.安装ADS(安装文件在00-ads1.2目录下,破解方法00-ads1.2\Crack目录下) 2.安装GIVEIO驱动(安装文件在01-GIVEIO目录下) 3.把整个GIV ...

  5. html基础总结版

    一.html版本 HTML    1991年 HTML+    1993年 HTML2.0    1995年 HTML3.2    1997年 HTML4.0.1    1999年 XHTML1.0  ...

  6. IBatis 构建 In语句

    语句 <select id="qryProjectsByIds" parameterClass="Hashtable" resultMap="p ...

  7. 部署lamp服务器

    系统:CentOS 6.5 64位 1.卸载旧版本软件 rpm -qa | grep mysql #查询是否已经安装MySQL,如有执行下面的操作将其全部删除 rpm -e mysql --nodep ...

  8. ecshop登陆后价格可见,会员注册登陆才能显示价格

    打开模版文件夹里面的goods.dwt 查找{$lang.shop_price}<font class="price" id="ECS_SHOPPRICE" ...

  9. Java Web中将oracle的数据库内容以表格形式展现到页面中(分页展示)

    分页SQL语句: ----分页显示 select * from (select rownum as r,t.* from () ; 查询的结果如下: 这个SQL,使用了三层嵌套的查询方式: 1)最内层 ...

  10. Git 提交大文件提示 fatal: The remote end hung up unexpectedly

    使用gitlab搭建的git server,如果直接使用http的方式去提交的话,提交小文件不会有问题,但是提交大文件时,会出错: fatal: The remote end hung up unex ...