/* GDIビットマップ(DDB)作成 2005/ 2/24 宍戸 輝光 */ #include LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); HWND g_hwMain; int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { MSG msg; WNDCLASSEX wndclass; wndclass.cbSize = sizeof(wndclass); wndclass.style = CS_HREDRAW | CS_VREDRAW; wndclass.lpfnWndProc = WndProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = hInstance; wndclass.hIcon = NULL; wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); wndclass.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH); wndclass.lpszMenuName = NULL; wndclass.lpszClassName = "DDBTest"; wndclass.hIconSm = NULL; RegisterClassEx(&wndclass); g_hwMain = CreateWindow ("DDBTest", "DDB作成", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 300, 320, NULL, NULL, hInstance, NULL); ShowWindow (g_hwMain, iCmdShow); while (GetMessage(&msg, NULL, 0, 0)) { /* メッセージループ */ TranslateMessage(&msg); DispatchMessage(&msg); } return (int)msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRUCT ps; static HBITMAP hbmpTest, hbmpTestOld; static HDC hdcTest; int i, j; switch (iMsg) { case WM_CREATE: /* ウインドウのHDC取得 */ hdc = GetDC(hwnd); /* ウインドウのHDCと互換性のあるDDB作成 */ hbmpTest = CreateCompatibleBitmap(hdc, 256, 256); /* ウインドウのHDCと互換性のあるデバイスコンテキスト作成 */ hdcTest = CreateCompatibleDC(hdc); /* ウインドウのHDC解放 */ ReleaseDC(hwnd, hdc); /* DDBをデバイスコンテキストに選択 */ hbmpTestOld = (HBITMAP)SelectObject(hdcTest, hbmpTest); /* DDBにグラデーション描画 */ for (i = 0;i < 256;i++) { for (j = 0;j < 256;j++) { SetPixel(hdcTest, j, i, RGB(128, j, i)); } } return 0; case WM_PAINT: hdc = BeginPaint(hwnd,&ps); /* DDBをウインドウ上に描画 */ BitBlt(hdc, 0, 0, 256, 256, hdcTest, 0, 0, SRCCOPY); EndPaint(hwnd, &ps); return 0; case WM_DESTROY : /* 終了処理 */ /* DDBの選択状態を解除 */ SelectObject(hdcTest, hbmpTestOld); /* メモリデバイスコンテキスト削除 */ DeleteDC(hdcTest); /* DDB削除 */ DeleteObject(hbmpTest); PostQuitMessage(0); return 0; } return DefWindowProc(hwnd, iMsg, wParam, lParam) ; }