// // DIB(デバイス独立ビットマップ)操作関数を作る第2回 // //     1998/ 4/28 宍戸 輝光 // #include typedef struct dibstruct { // DIB構造体 int width,height; // 大きさ BYTE* lpBMP; // ビットマップバッファへのポインタ BITMAPINFO* lpInfo; // BITMAPINFO 構造体へのポインタ BITMAPINFOHEADER* lpInfoh; //BITMAPINFOHEADER 構造体へのポインタ } HIBMP; LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM); HIBMP CreateIBMP(int width,int height); void drawIBMPtoDC(HIBMP hb,HDC hdc,int x,int y); void drawIBMPtoIBMP(HIBMP,int,int,HIBMP); int psetIBMP(HIBMP,int,int,int); void FreeIBMP(HIBMP hb); int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow){ HWND hwnd; 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 = "Test Window"; wndclass.hIconSm = LoadIcon (NULL, IDI_APPLICATION); RegisterClassEx (&wndclass); hwnd = CreateWindow ("Test Window", "DIB Test", WS_OVERLAPPEDWINDOW, 32,32, 128,128, NULL, NULL, hInstance, NULL); ShowWindow (hwnd,iCmdShow); // ウインドウを表示 UpdateWindow (hwnd); // 再描画 while (GetMessage (&msg,NULL,0,0)) { // メッセージ処理 TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam ; } LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRUCT ps; static HIBMP hb,sb; int i,j; switch (iMsg) { case WM_CREATE: hb=CreateIBMP(64,64); // 64*64のDIBを作成 for (i=0;i<64;i++) // 全ピクセルを青にする for (j=0;j<64;j++) psetIBMP(hb,j,i,0x00ff0000); sb=CreateIBMP(32,32); // 32*32のDIBを作成 for (i=0;i<32;i++) // 全ピクセルを緑にする for (j=0;j<32;j++) psetIBMP(sb,j,i,0x0000ff00); drawIBMPtoIBMP(sb,8,8,hb); // ビットマップを上書き return 0; case WM_PAINT: hdc=BeginPaint(hwnd,&ps); drawIBMPtoDC(hb,hdc,8,8); // 表示 EndPaint(hwnd,&ps); return 0; case WM_DESTROY : FreeIBMP(hb); // 後始末 PostQuitMessage(0); return 0; } return DefWindowProc (hwnd, iMsg, wParam, lParam) ; } HIBMP CreateIBMP(int width,int height) { // DIB作成 HIBMP hb; hb.width=width; // 大きさ設定 hb.height=height; hb.lpBMP=GlobalAlloc(GMEM_FIXED,width*height*3); // メモリ割り当て hb.lpInfoh=GlobalAlloc(GMEM_FIXED,sizeof(BITMAPINFOHEADER)); hb.lpInfo=GlobalAlloc(GMEM_FIXED,sizeof(BITMAPINFO)); ZeroMemory(hb.lpBMP,width*height*3); // ビットマップをクリア hb.lpInfoh->biSize=sizeof(BITMAPINFOHEADER); // BITMAPINFOHEADER構造体 hb.lpInfoh->biWidth=width; hb.lpInfoh->biHeight=height; hb.lpInfoh->biPlanes=1; hb.lpInfoh->biBitCount=24; hb.lpInfoh->biCompression=BI_RGB; hb.lpInfoh->biSizeImage=0; hb.lpInfoh->biXPelsPerMeter=0; hb.lpInfoh->biYPelsPerMeter=0; hb.lpInfoh->biClrUsed=0; hb.lpInfoh->biClrImportant=0; hb.lpInfo->bmiHeader=*hb.lpInfoh; return hb; // 作成したDIBを返す } void drawIBMPtoDC(HIBMP hb,HDC hdc,int x,int y) { // DIB表示 StretchDIBits(hdc,x,y,hb.width,hb.height,0,0,hb.width,hb.height, hb.lpBMP,hb.lpInfo,DIB_RGB_COLORS,SRCCOPY); } void drawIBMPtoIBMP(HIBMP srcI,int x,int y,HIBMP desI){ //DIBにDIB描画 int i; for (i=0;ihb.width-1 || y>hb.height-1) // 範囲外ならエラー return 0; b=(c & 0x00ff0000)>>16; // B(青)成分を分離 g=(c & 0x0000ff00)>>8; // G(緑)成分を分離 r=c & 0x000000ff; // R(赤)成分を分離 hb.lpBMP[(y*hb.width+x)*3]=b; hb.lpBMP[(y*hb.width+x)*3+1]=g; hb.lpBMP[(y*hb.width+x)*3+2]=r; return 1; } void FreeIBMP(HIBMP hb) { // 後始末 GlobalFree(hb.lpBMP); // メモリを解放 GlobalFree(hb.lpInfo); GlobalFree(hb.lpInfoh); }