游戏开发论坛

 找回密码
 立即注册
搜索
查看: 1233|回复: 1

透明的半透明处理

[复制链接]

30

主题

89

帖子

91

积分

注册会员

Rank: 2

积分
91
发表于 2010-6-7 10:55:00 | 显示全部楼层 |阅读模式
//程序为什么不能正确运行?

#include <windows.h>
#include <iostream>
HBITMAP hbk;
HBITMAP hbk_girl_mask;
HBITMAP        hgirl;
HDC                mdc0;
HDC                mdc1;
HDC                mdc_buff;
HDC                mdc_cout;
void paint(HDC);

LRESULT CALLBACK WindowProc        (HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)                                                         
{
        HDC                hdc;
        PAINTSTRUCT ps;
       
        switch(uMsg)
        {
        case WM_PAINT:
                hdc = BeginPaint(hwnd,&ps);
                paint(hdc);
                EndPaint(hwnd,&ps);
                break;
               
        case WM_CLOSE:
                PostQuitMessage(0);
                break;
               
               
    }
        return DefWindowProc (hwnd, uMsg, wParam, lParam) ;
}
int WINAPI WinMain(        HINSTANCE hinstance,HINSTANCE hprevinstance, LPSTR lpcmdline,int ncmdshow)
{
        WNDCLASSEX wc;
        HWND        hwnd;
        MSG                msg;
       
        wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
        wc.hIcon =        LoadIcon(NULL, IDI_INFORMATION);
        wc.hIconSm = LoadIcon(NULL, IDI_INFORMATION);
        wc.hInstance = hinstance;
        wc.lpfnWndProc = WindowProc;
        wc.cbSize = sizeof(WNDCLASSEX );
        wc.lpszClassName = "win";
        wc.lpszMenuName = NULL;
        wc.hCursor = LoadCursor(NULL, IDC_ARROW);
        wc.style  = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
        wc.cbWndExtra =0;
        wc.cbClsExtra =0;
       
        if (!RegisterClassEx(&wc)) return 0;
       
        hwnd=CreateWindowEx(NULL,"win","night",WS_OVERLAPPEDWINDOW | WS_VISIBLE,                50,50,650,450,NULL ,NULL,hinstance,NULL);
       
        //load image and return a handle.
        hbk = (HBITMAP)LoadImage(NULL,"bg.bmp",IMAGE_BITMAP,650,450,LR_LOADFROMFILE);
        hbk_girl_mask = (HBITMAP)LoadImage(NULL,"girlmask.bmp",IMAGE_BITMAP,596,329,LR_LOADFROMFILE);

        //choose a decive context.
        HDC  hdc = GetDC(hwnd);
        mdc0 = CreateCompatibleDC(hdc);
        hgirl = CreateCompatibleBitmap(hdc,298,329);

        SelectObject(mdc0,hgirl);
        SelectObject(mdc_buff,hbk);      //background image.

        BitBlt(mdc0,0,0,298,329,mdc_buff,0,0,SRCCOPY);                //mask and oprator;image or oprator.
        SelectObject(mdc_buff,hbk_girl_mask);
        BitBlt(mdc0,0,0,298,329,mdc_buff,298,0,SRCAND);       //the NO. of 289 in book is wrong.
        BitBlt(mdc0,0,0,298,329,mdc_buff,0,0,SRCPAINT);       //trancelucent in the hgirl.
       
        //half trancelucent.read image to a struct of BITMAP;
        //so hard .
        BITMAP bm1,bm2;
        unsigned char* px1,*px2;
       
        GetObject(hbk,sizeof(BITMAP),&bm1);
        px1 = new unsigned char[bm1.bmWidthBytes*bm1.bmHeight];
        GetBitmapBits(hbk,bm1.bmWidthBytes*bm1.bmHeight,px1);  //get back picture to px1 array.
       
        GetObject(hgirl,sizeof(BITMAP),&bm2);
        px2 = new unsigned char[bm2.bmWidthBytes*bm2.bmHeight];
        GetBitmapBits(hgirl,bm2.bmWidthBytes*bm2.bmHeight,px2);  //get front picturc to px2 array.
       
        int x=0,y=0;
        int i;
        int j;
        int pxbytes =bm1.bmBitsPixel/8;
       
        for(y=0;y<329;y++)
        {
                for(x=0;x<298;x++)
                {
                        j = bm1.bmWidthBytes *y + x*pxbytes;
                        px1[j] = px1[j]*0.7;
                        px1[j+1] = px1[j+1]*0.7;
                        px1[j+2] = px1[j+2]*0.7;
                }
        }
        for(y=0;y<329;y++)
        {
                for(x=0;x<298;x++)
                {
                        j = y*bm2.bmWidthBytes + x*pxbytes;   //px2[j] waste my some time.
                        i = y*bm1.bmWidthBytes + x*pxbytes;   //wrong:px2=px2[j]*0.3+px1

                        px2[j] = px2[j]*0.3 + px1;
                        px2[j+1] = px2[j+1]*0.3 + px1[i+1];
                        px2[j+2] = px2[j+2]*0.3 + px1[i+2];       
                }
        }       

        SetBitmapBits(hgirl,bm2.bmHeight*bm2.bmWidthBytes,px2);      //PX1存放的是背景的像素数组,不要忘了.
                                                                                                                                //PX2是前景的像素数组.
        while(GetMessage(&msg,NULL,0,0))
        {
                TranslateMessage (&msg);
                DispatchMessage (&msg);       
        }
       
        return (msg.wParam);
}

void paint(HDC hdc)
{
        SelectObject(mdc0,hbk);
        BitBlt(hdc,0,0,650,450,mdc0,0,0,SRCCOPY);
        SelectObject(mdc0,hgirl);
        BitBlt(hdc,0,0,329,298,mdc0,0,0,SRCCOPY);
}

30

主题

89

帖子

91

积分

注册会员

Rank: 2

积分
91
 楼主| 发表于 2010-6-11 16:35:00 | 显示全部楼层

Re:透明的半透明处理

HDC mdc_buff;
变量没有赋值就用了。
即没有,mdc_buff = CreateCompatible(hdc);
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-6-8 16:00

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表