BitBlt関数を使った方法です。

下の画像を背景に、カップの画像を重ねる場合に、

カップの画像をコピーしただけでは上手く表示されません。

背景にいったん次のマスク画像を書き込みます。

書込みは、論理和で行います。

次に論理積でカップを描画します。

すると、背景とカップが上手く重なった表示ができます。

 

〇プログラム例

 

  1. #include <windows.h>
  2.  
  3. static LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  4. {
  5.     HDC hdc;
  6.     PAINTSTRUCT ps;
  7.     POINT pt;
  8.     LPSTR str = TEXT("こんにちは、日本");
  9.     static HDC hMemDC;
  10.     static HBITMAP hBitmap,hMaskBmp,hBackBmp;
  11.     static HFONT hFont[2];
  12.     LPSTR BitMapFile = TEXT("カップ.bmp");
  13.     LPSTR Back_BitMapFile = TEXT("背景.bmp");
  14.     LPSTR Mask_BitMapFile = TEXT("カップマスク.bmp");
  15.  
  16.     static BITMAP bitmap,bitmapBack;
  17.  
  18.     switch (uMsg)
  19.     {
  20.     case WM_DESTROY:
  21.         DeleteDC(hMemDC);
  22.         DeleteObject(hBitmap);
  23.         DeleteObject(hMaskBmp);
  24.         DeleteObject(hBackBmp);
  25.         DeleteObject(hFont[0]);
  26.  
  27.         PostQuitMessage(0);
  28.         return 0;
  29.  
  30.     case WM_CREATE:
  31.         hFont[0] = CreateFont(
  32.             40, 0, 0, 0,
  33.             FW_NORMAL, FALSE, FALSE, FALSE,
  34.             SHIFTJIS_CHARSET, OUT_DEFAULT_PRECIS,
  35.             CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
  36.             VARIABLE_PITCH | FF_DONTCARE, L"メイリオ"
  37.         );
  38.         ShowWindow(hWnd, SW_SHOW);
  39.         hMemDC = CreateCompatibleDC(NULL);
  40.         hBitmap = LoadImage(NULL, BitMapFile, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
  41.         hMaskBmp = LoadImage(NULL, Mask_BitMapFile, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
  42.         hBackBmp = LoadImage(NULL, Back_BitMapFile, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
  43.  
  44.         GetObject(hBitmap, sizeof(BITMAP), &bitmap);
  45.         GetObject(hBackBmp, sizeof(BITMAP), &bitmapBack);
  46.  
  47.  
  48.  
  49.         return 0;
  50.  
  51.     case WM_PAINT:
  52.         hdc = BeginPaint(hWnd, &ps);
  53.         //背景の描画
  54.         SelectObject(hMemDC, hBackBmp);
  55.         BitBlt(hdc, 0, 0, bitmapBack.bmWidth, bitmapBack.bmHeight, hMemDC, 0, 0, SRCCOPY);
  56.  
  57.         //マスクの描画
  58.         SelectObject(hMemDC, hMaskBmp);
  59.         pt.x = bitmapBack.bmWidth / 2 - bitmap.bmWidth / 2;
  60.         pt.y = bitmapBack.bmHeight - bitmap.bmHeight;
  61.         BitBlt(hdc, pt.x, pt.y, bitmap.bmWidth, bitmap.bmHeight,hMemDC, 0, 0, SRCPAINT);
  62.  
  63.         //カップの描画
  64.         SelectObject(hMemDC, hBitmap);
  65.         BitBlt(hdc, pt.x, pt.y, bitmap.bmWidth, bitmap.bmHeight, hMemDC, 0, 0, SRCAND);
  66.  
  67.         SelectObject(hdc, hFont[0]);
  68.         SetTextColor(hdc, RGB(0, 0xFF, 0));
  69.         TextOut(hdc, 0, 0, str, lstrlen(str));
  70.         EndPaint(hWnd, &ps);
  71.  
  72.         return 0;
  73.     }
  74.  
  75.     return DefWindowProc(hWnd, uMsg, wParam, lParam);
  76.  
  77. }
  78.  
  79. int APIENTRY wWinMain(
  80.     _In_ HINSTANCE hInstance,
  81.     _In_opt_ HINSTANCE hPrevInstance,
  82.     _In_ LPWSTR ipCmdLine,
  83.     _In_ int nCmdShow
  84. ) {
  85.  
  86.     WNDCLASSEXW wcex;
  87.  
  88.     wcex.cbSize = sizeof(WNDCLASSEX);
  89.  
  90.     wcex.style = CS_HREDRAW | CS_VREDRAW;
  91.     wcex.lpfnWndProc = WndProc;
  92.     wcex.cbClsExtra = 0;
  93.     wcex.cbWndExtra = 0;
  94.     wcex.hInstance = hInstance;
  95.     wcex.hIcon = NULL;
  96.     wcex.hCursor = NULL;
  97.     wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  98.     wcex.lpszMenuName = NULL;
  99.     wcex.lpszClassName = TEXT("TestWindow");
  100.     wcex.hIconSm = NULL;
  101.  
  102.     RegisterClassExW(&wcex);
  103.  
  104.     HWND hWnd = CreateWindowEx(0UL, TEXT("TestWindow"), TEXT("日本"), WS_OVERLAPPEDWINDOW,
  105.         CW_USEDEFAULT, 0, 400, 400, NULL, NULL, hInstance, NULL);
  106.  
  107.     if (!hWnd)
  108.     {
  109.         return FALSE;
  110.     }
  111.  
  112.     MSG msg;
  113.  
  114.     while (GetMessage(&msg, NULL, 0, 0))
  115.     {
  116.         DispatchMessage(&msg);
  117.     }
  118.  
  119.     return (int)msg.wParam;
  120. }