对位图中的掩码图,或图片合成还需要做进一步的了解,继续熟悉了内存中图片处理的相应过程。
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam) { static HBITMAP hBitmapImag,hBitmapMask; static HINSTANCE hInstance; static int cxClient,cyClient,cxBitmap,cyBitmap; BITMAP bitmap; HDC hdc,hdcMemImag,hdcMemMask; int x,y; PAINTSTRUCT ps; switch(message) { case WM_CREATE: //实例句柄 hInstance=((LPCREATESTRUCT)lParam)->hInstance; //图片句柄 hBitmapImag=LoadBitmap(hInstance,TEXT("Matthew")); //图片相关信息 GetObject(hBitmapImag,sizeof(BITMAP),&bitmap); cxBitmap=bitmap.bmWidth; cyBitmap=bitmap.bmHeight; //创建内存设备上下文 hdcMemImag=CreateCompatibleDC(NULL); //图片选入内存设备 SelectObject(hdcMemImag,hBitmapImag); //创建一个图片 hBitmapMask=CreateBitmap(cxBitmap,cyBitmap,1,1,NULL); //创建内存设备上下文 hdcMemMask=CreateCompatibleDC(NULL); //选入设备上下文 SelectObject(hdcMemMask,hBitmapMask); //对内存设备选入黑色画刷 SelectObject(hdcMemMask,GetStockObject(BLACK_BRUSH)); //画矩形 Rectangle(hdcMemMask,0,0,cxBitmap,cyBitmap); //选入白色画刷 SelectObject(hdcMemMask,GetStockObject(WHITE_BRUSH)); //画椭圆 Ellipse(hdcMemMask,0,0,cxBitmap,cyBitmap); //由hdcMemImag中的位图移动到hdcMemMask中,与原位图做AND运算 BitBlt(hdcMemImag,0,0,cxBitmap,cyBitmap,hdcMemMask,0,0,SRCAND); DeleteDC(hdcMemImag); DeleteDC(hdcMemMask); return 0; case WM_SIZE: cxClient=LOWORD(lParam); cyClient=HIWORD(lParam); return 0; case WM_PAINT: hdc=BeginPaint(hwnd,&ps); //要显示了,创建与显示相关的内存设备上下文 hdcMemImag=CreateCompatibleDC(hdc); //hBitmapImag这个句柄是不会变的,创建时的相当操作改变了hBitmapImag的内容,但没有改变句柄 SelectObject(hdcMemImag,hBitmapImag); hdcMemMask=CreateCompatibleDC(hdc); SelectObject(hdcMemMask,hBitmapMask); x=(cxClient-cxBitmap)/2; y=(cyClient-cyBitmap)/2; //下面这一行,使得椭圆内部为黑色,其它地方为浅色 BitBlt(hdc,x,y,cxBitmap,cyBitmap,hdcMemMask,0,0,0x220326); //做或运算,合成图像 BitBlt(hdc,x,y,cxBitmap,cyBitmap,hdcMemImag,0,0,SRCPAINT); DeleteDC(hdcMemImag); DeleteDC(hdcMemMask); EndPaint(hwnd,&ps); return 0; case WM_DESTROY: DeleteObject(hBitmapImag); DeleteObject(hBitmapMask); PostQuitMessage(0); return 0; } return DefWindowProc(hwnd,message,wParam,lParam); }