/* DIBクラス 1999/ 4/27  宍戸 輝光 */ #include class CDIB { // DIBクラス private: DWORD dwWidth,dwHeight,dwLength; LPBYTE lpBMP; BITMAPINFO biDIB; BITMAPINFOHEADER bhDIB; public: CDIB(DWORD wid,DWORD hei) { // コンストラクタ dwWidth=wid; dwHeight=hei; if ((dwWidth*3) % 4==0) // バッファの1ラインの長さを計算 dwLength=dwWidth*3; else dwLength=dwWidth*3+(4-(dwWidth*3) % 4); lpBMP= // ビットマップ用バッファ確保 (LPBYTE)GlobalAlloc(GPTR,dwLength*dwHeight); bhDIB.biSize=sizeof(BITMAPINFOHEADER); // 構造体の大きさ bhDIB.biWidth=dwWidth; // 横幅 bhDIB.biHeight=dwHeight; // 高さ bhDIB.biPlanes=1; // プレーンの数 bhDIB.biBitCount=24; // プレーンの色数 bhDIB.biCompression=BI_RGB; bhDIB.biSizeImage=0; bhDIB.biXPelsPerMeter=0; bhDIB.biYPelsPerMeter=0; bhDIB.biClrUsed=0; bhDIB.biClrImportant=0; biDIB.bmiHeader=bhDIB; } ~CDIB() { // デストラクタ GlobalFree(lpBMP); // ビットマップバッファ解放 } LPBYTE getBMP() { // ビットマップバッファの先頭アドレスを返す return lpBMP; } void draw(HDC hdc,DWORD x,DWORD y) { // ビットマップ描画 StretchDIBits(hdc,x,y,dwWidth,dwHeight,0,0,dwWidth,dwHeight, lpBMP,&biDIB,DIB_RGB_COLORS,SRCCOPY); } }; CDIB* dib; LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM); 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 = LoadIcon (NULL, IDI_APPLICATION); wndclass.hCursor = LoadCursor (NULL, IDC_ARROW); wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wndclass.lpszMenuName = NULL; wndclass.lpszClassName = "CWindow"; wndclass.hIconSm = LoadIcon (NULL, IDI_APPLICATION); RegisterClassEx (&wndclass); dib=new CDIB(256,256); // DIBオブジェクト作成 for (int i=0;i<256;i++) // ビットマップにグレースケール描画 FillMemory(dib->getBMP()+i*256*3,256*3,i); HWND hwnd = CreateWindow ("CWindow","DIBクラステスト",WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,CW_USEDEFAULT,288,300, NULL,NULL,hInstance,NULL); ShowWindow (hwnd,iCmdShow); // ウインドウを表示 UpdateWindow (hwnd); while (GetMessage (&msg,NULL,0,0)) { // メッセージループ TranslateMessage(&msg); DispatchMessage(&msg); } delete dib; // DIBオブジェクト削除 return msg.wParam ; } LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRUCT ps; switch (iMsg) { case WM_PAINT: hdc=BeginPaint(hwnd,&ps); // ウインドウのDC取得 dib->draw(hdc,4,4); // HDCの(4, 4)にビットマップ描画 EndPaint(hwnd,&ps); break; case WM_DESTROY : PostQuitMessage(0); break; } return DefWindowProc (hwnd, iMsg, wParam, lParam) ; }