CreateCompatibleDC関数を使用して、

デバイスコンテキストを作成し、

CreateCompatibleBitmap関数でメモリ上に

ビットマップを作成することができます。

 

メモリ上のビットマップについても、

通常の画面への描画関数が使用できます。

 

〇プログラム例

メモリデバイスに文字列を描画して、

それを、画面にコピーします。

 

  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.     LPSTR str = TEXT("こんにちは、日本");
  8.     static HDC hMemDC;
  9.     static HBITMAP hBitmap;
  10.     static HFONT hFont[2];
  11.  
  12.     switch (uMsg)
  13.     {
  14.     case WM_DESTROY:
  15.         DeleteDC(hMemDC);
  16.         DeleteObject(hBitmap);
  17.         DeleteObject(hFont[0]);
  18.  
  19.         PostQuitMessage(0);
  20.         return 0;
  21.  
  22.     case WM_CREATE:
  23.         hFont[0] = CreateFont(
  24.             40, 0, 0, 0,
  25.             FW_NORMAL, FALSE, FALSE, FALSE,
  26.             SHIFTJIS_CHARSET, OUT_DEFAULT_PRECIS,
  27.             CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
  28.             VARIABLE_PITCH | FF_DONTCARE, L"メイリオ"
  29.         );
  30.         ShowWindow(hWnd, SW_SHOW);
  31.         hdc = GetDC(hWnd);
  32.         hMemDC = CreateCompatibleDC(hdc);
  33.         hBitmap = CreateCompatibleBitmap(hdc, 400, 200);
  34.         ReleaseDC(hWnd, hdc);
  35.  
  36.         SelectObject(hMemDC, hBitmap);
  37.         SelectObject(hMemDC, GetStockObject(WHITE_BRUSH));
  38.         SelectObject(hMemDC, hFont[0]);
  39.         SetTextColor(hMemDC, RGB(0, 0xFF, 0));
  40.         TextOut(hMemDC, 10, 10, str, lstrlen(str));
  41.  
  42.         return 0;
  43.  
  44.     case WM_PAINT:
  45.         hdc = BeginPaint(hWnd, &ps);
  46.         BitBlt(hdc, 0, 0, 400, 200, hMemDC, 0, 0, SRCCOPY);
  47.         EndPaint(hWnd, &ps);
  48.         return 0;
  49.     }
  50.  
  51.     return DefWindowProc(hWnd, uMsg, wParam, lParam);
  52.  
  53. }
  54.  
  55. int APIENTRY wWinMain(
  56.     _In_ HINSTANCE hInstance,
  57.     _In_opt_ HINSTANCE hPrevInstance,
  58.     _In_ LPWSTR ipCmdLine,
  59.     _In_ int nCmdShow
  60. ) {
  61.  
  62.     WNDCLASSEXW wcex;
  63.  
  64.     wcex.cbSize = sizeof(WNDCLASSEX);
  65.  
  66.     wcex.style = CS_HREDRAW | CS_VREDRAW;
  67.     wcex.lpfnWndProc = WndProc;
  68.     wcex.cbClsExtra = 0;
  69.     wcex.cbWndExtra = 0;
  70.     wcex.hInstance = hInstance;
  71.     wcex.hIcon = NULL;
  72.     wcex.hCursor = NULL;
  73.     wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  74.     wcex.lpszMenuName = NULL;
  75.     wcex.lpszClassName = TEXT("TestWindow");
  76.     wcex.hIconSm = NULL;
  77.  
  78.     RegisterClassExW(&wcex);
  79.  
  80.     HWND hWnd = CreateWindowEx(0UL, TEXT("TestWindow"), TEXT("日本"), WS_OVERLAPPEDWINDOW,
  81.         CW_USEDEFAULT, 0, 400, 200, NULL, NULL, hInstance, NULL);
  82.  
  83.     if (!hWnd)
  84.     {
  85.         return FALSE;
  86.     }
  87.  
  88.     MSG msg;
  89.  
  90.     while (GetMessage(&msg, NULL, 0, 0))
  91.     {
  92.         DispatchMessage(&msg);
  93.     }
  94.  
  95.     return (int)msg.wParam;
  96. }

 

〇実行結果