HBITMAP bg,girl;
HDC mdc;
const int xstart=50;
const int ystart=20;
HWND hWnd;
hInst = hInstance; // 将实例句柄存储在全局变量中
HDC hdc,bufdc;
HBITMAP bmp;
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
hdc =GetDC(hWnd);
mdc=CreateCompatibleDC(hdc);
bufdc=CreateCompatibleDC(hdc);
girl=CreateCompatibleBitmap(hdc,298,329);
BITMAP bm1,bm2;
unsigned char *px1,*px2;
//处理背景图
bg=(HBITMAP)LoadImage(NULL,"sky2.bmp",IMAGE_BITMAP,600,450,LR_LOADFROMFILE);
bmp=(HBITMAP)LoadImage(NULL,"dra.bmp",IMAGE_BITMAP,298,329,LR_LOADFROMFILE);
GetObject(bg,sizeof(BITMAP),&bm1);
if (bm1.bmBitsPixel!=32&&bm1.bmBitsPixel!=24)
{
MessageBox(NULL,"此程序只能在32bit或24bit显示模式中运行","警告",0);
return FALSE;
}
SelectObject(mdc,girl);
SelectObject(bufdc,bg);
BitBlt(mdc,0,0,298,329,bufdc,xstart,ystart,SRCCOPY);
SelectObject(bufdc,bmp);
BitBlt(mdc,0,0,298,329,bufdc,298,0,SRCAND);
BitBlt(mdc,0,0,298,329,bufdc,0,0,SRCPAINT);
px1=new unsigned char[bm1.bmHeight*bm1.bmWidthBytes];
GetBitmapBits(bg,bm1.bmHeight*bm1.bmWidthBytes,px1);
//处理前景图
GetObject(girl,sizeof(BITMAP),&bm2);
px2=new unsigned char[bm2.bmHeight*bm2.bmWidthBytes];
GetBitmapBits(girl,bm2.bmHeight*bm2.bmWidthBytes,px2);
int xend,yend;
int x,y,i;
int rgb_b;
int PxBytes=bm1.bmBitsPixel/8;
xend=xstart+298;
yend=ystart+329;
//处理背景图像像素颜色
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+1]=px1[rgb_b+1]*0.7;
px1[rgb_b+2]=px1[rgb_b+2]*0.7;
}
}//836588
//处理前景图像素颜色
for (y=0;y<(bm2.bmHeight);y++)
{
for (x=0;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+1]=px2[rgb_b+1]*0.3+px1[i+1];
px2[rgb_b+2]=px2[rgb_b+2]*0.3+px1[i+2];
}
}
SetBitmapBits(girl,bm2.bmHeight*bm2.bmWidthBytes,px2);
//贴上背景图
SelectObject(mdc,bg);
BitBlt(hdc,0,0,600,450,mdc,0,0,SRCCOPY);
//贴上处理后的半透明图
SelectObject(mdc,girl);
BitBlt(hdc,xstart,ystart,298,329,mdc,0,0,SRCCOPY);
ReleaseDC(hWnd,hdc);
delete [] px1;
delete [] px2;
return TRUE;