|
//程序为什么不能正确运行?
#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);
} |
|