#include #define SCRW 512 #define SCRH 512 LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) { HDC hdc, hBmpDC; PAINTSTRUCT ps; static HBITMAP hBitmap, hBitmap_mask, hBitmap_bg; static BITMAP bp; static int imgw, imgh; static int bgw, bgh; static int x, y; switch (msg) { case WM_CREATE: // load bitmaps hBitmap = LoadBitmap(((LPCREATESTRUCT)lp)->hInstance, TEXT("BALL_IMG")); hBitmap_mask = LoadBitmap(((LPCREATESTRUCT)lp)->hInstance, TEXT("BALL_MSK")); hBitmap_bg = LoadBitmap(((LPCREATESTRUCT)lp)->hInstance, TEXT("BG_IMG")); // get source image information GetObject(hBitmap, sizeof(BITMAP), &bp); imgw = bp.bmWidth; imgh = bp.bmHeight; // get bg image information GetObject(hBitmap_bg, sizeof(BITMAP), &bp); bgw = bp.bmWidth; bgh = bp.bmHeight; return 0; case WM_PAINT: x = 32; y = 32; hdc = BeginPaint(hwnd, &ps); // draw bg image hBmpDC = CreateCompatibleDC(hdc); SelectObject(hBmpDC, hBitmap_bg); BitBlt(hdc, 0, 0, bgw, bgh, hBmpDC, 0, 0, SRCCOPY); DeleteDC(hBmpDC); // draw mask hBmpDC = CreateCompatibleDC(hdc); SelectObject(hBmpDC, hBitmap_mask); BitBlt(hdc, x, y, imgw, imgh, hBmpDC, 0, 0, SRCAND); DeleteDC(hBmpDC); // draw source image hBmpDC = CreateCompatibleDC(hdc); SelectObject(hBmpDC, hBitmap); BitBlt(hdc, x, y, imgw, imgh, hBmpDC, 0, 0, SRCPAINT); DeleteDC(hBmpDC); EndPaint(hwnd, &ps); return 0; case WM_DESTROY: DeleteObject(hBitmap); DeleteObject(hBitmap_mask); DeleteObject(hBitmap_bg); PostQuitMessage(0); return 0; } return DefWindowProc(hwnd, msg, wp, lp); } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR lpCmdLine, int nCmdShow) { HWND hwnd; MSG msg; WNDCLASS winc; winc.style = CS_HREDRAW | CS_VREDRAW; winc.lpfnWndProc = WndProc; winc.cbClsExtra = winc.cbWndExtra = 0; winc.hInstance = hInstance; winc.hIcon = LoadIcon(NULL, IDI_APPLICATION); winc.hCursor = LoadCursor(NULL, IDC_ARROW); winc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); winc.lpszMenuName = NULL; winc.lpszClassName = TEXT("DrawBitmapSample"); if (!RegisterClass(&winc)) return -1; hwnd = CreateWindow( TEXT("DrawBitmapSample"), TEXT("Draw Bitmap Sample"), WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, SCRW, SCRH, NULL, NULL, hInstance, NULL ); if (hwnd == NULL) return -1; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; }