#include #define WIDTH 197 #define HEIGHT 100 LRESULT CALLBACK WindowProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam) { HDC hdc; PAINTSTRUCT ps; static LPBYTE lpPixel; static BITMAPINFO bmpInfo; static HDC hMemDC; HBITMAP hBitmap; int x,y; static int length; switch(uMsg) { case WM_CREATE: //4の倍数に補正 if(WIDTH*3%4) length=WIDTH*3+(4-WIDTH*3%4); else length=WIDTH*3; //DIB作成 lpPixel=(LPBYTE)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,length*HEIGHT); bmpInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER); bmpInfo.bmiHeader.biWidth=WIDTH; bmpInfo.bmiHeader.biHeight=HEIGHT; bmpInfo.bmiHeader.biPlanes=1; bmpInfo.bmiHeader.biBitCount=24; bmpInfo.bmiHeader.biCompression=BI_RGB; //灰色で塗りつぶす FillMemory(lpPixel,length*HEIGHT,0x80); //DIB→DDB変換 hdc=GetDC(hWnd); hBitmap=CreateDIBitmap(hdc,&bmpInfo.bmiHeader,CBM_INIT,lpPixel,&bmpInfo,DIB_RGB_COLORS); hMemDC=CreateCompatibleDC(hdc); SelectObject(hMemDC,hBitmap); DeleteObject(hBitmap); ReleaseDC(hWnd,hdc); for(y=25;y<50;y++){ //DIBに描画 for(x=25;x<50;x++){ lpPixel[(x*3 )+y*length]=0; //B lpPixel[(x*3+1)+y*length]=0; //G lpPixel[(x*3+2)+y*length]=255; //R } } Rectangle(hMemDC,25,25,50,50); //DDBに描画 return 0; case WM_DESTROY: DeleteDC(hMemDC); //DDB削除 HeapFree(GetProcessHeap(),0,lpPixel); //DIB削除 PostQuitMessage(0); return 0; case WM_PAINT: hdc=BeginPaint(hWnd,&ps); BitBlt(hdc,0,0,WIDTH,HEIGHT,hMemDC,0,0,SRCCOPY); //DDBを表画面へ転送 StretchDIBits(hdc,250,0,WIDTH,HEIGHT, //DIBを表画面へ転送 0,0,WIDTH,HEIGHT,lpPixel,&bmpInfo,DIB_RGB_COLORS,SRCCOPY); EndPaint(hWnd,&ps); return 0; } return DefWindowProc(hWnd,uMsg,wParam,lParam); } int WINAPI WinMain( HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR lpCmdLine,int nCmdShow) { WNDCLASS wc; MSG msg; wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = WindowProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon(NULL,IDI_APPLICATION); wc.hCursor = LoadCursor(NULL,IDC_ARROW); wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wc.lpszMenuName = NULL; wc.lpszClassName = __FILE__; if(!RegisterClass(&wc)) return 0; HWND hWnd=CreateWindow( __FILE__,"24ビットDIB→DDB変換", WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT,CW_USEDEFAULT, CW_USEDEFAULT,CW_USEDEFAULT, NULL,NULL,hInstance,NULL); if(hWnd==NULL) return 0; BOOL bRet; while((bRet=GetMessage(&msg,NULL,0,0))!=0){ if(bRet==-1) break; DispatchMessage(&msg); } return (int)msg.wParam; }