#include #include #define WIDTH 197 #define HEIGHT 100 #define BIT 8 #define COLOR ((DWORD)pow(2,BIT)) LRESULT CALLBACK WindowProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam) { HDC hdc; PAINTSTRUCT ps; static LPBYTE lpPixel; static LPBITMAPINFO lpBmpInfo; int x,y,i; static int length; switch(uMsg) { case WM_CREATE: //4の倍数に補正 if(WIDTH%4) length=WIDTH+(4-WIDTH%4); else length=WIDTH; lpPixel=(LPBYTE)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY, length*HEIGHT+sizeof(BITMAPINFO)+sizeof(RGBQUAD)*(COLOR-1)); lpBmpInfo=(LPBITMAPINFO)(lpPixel+length*HEIGHT); //カラーテーブルを設定する for(i=0;ibmiColors[i].rgbBlue=i%128; lpBmpInfo->bmiColors[i].rgbGreen=128-i%128; lpBmpInfo->bmiColors[i].rgbRed=i%256; } //DIBの情報を設定する lpBmpInfo->bmiHeader.biSize=sizeof(BITMAPINFOHEADER); lpBmpInfo->bmiHeader.biWidth=WIDTH; lpBmpInfo->bmiHeader.biHeight=HEIGHT; lpBmpInfo->bmiHeader.biPlanes=1; lpBmpInfo->bmiHeader.biBitCount=BIT; lpBmpInfo->bmiHeader.biCompression=BI_RGB; //lpBmpInfo->bmiHeader.biClrUsed=COLOR; //描画 for(i=0,y=25;y<=50;y++){ for(x=25;x<=50;x++){ lpPixel[x+y*length]=(++i)%COLOR; } } return 0; case WM_DESTROY: HeapFree(GetProcessHeap(),0,lpPixel); PostQuitMessage(0); return 0; case WM_PAINT: hdc=BeginPaint(hWnd,&ps); //表画面へ転送 StretchDIBits(hdc,0,0,WIDTH,HEIGHT, 0,0,WIDTH,HEIGHT,lpPixel,lpBmpInfo,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__,"8ビットDIBを作る", 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; }