/////////////////////////////////////////////////////// /// /// このソースコードを引用・改変した結果如何なる /// 損害が発生しても、著者はは責任を負いません。 /// 広島大学 脳外科 橋詰顕 /// //////////////////////////////////////////////////////// #define STRICT //型チェックを厳密に行う #define WIN32_LEAN_AND_MEAN //ヘッダーからあまり使われていない関数を省く #include //DirectDraw7用 #include #pragma comment(lib,"ddraw.lib") #pragma comment(lib,"dxguid.lib") void hnsDeleteWindowMessage(); LPDIRECTDRAWSURFACE7 hnsMakeDirectDraw7BackSurface(LPDIRECTDRAWSURFACE7 pSurface); LPDIRECTDRAW7 hnsMakeDirectDraw7Object(HWND hWindow,int WindowWidth,int WindowHeight); void hnsMakeDirectDraw7PrimarySurface(LPDIRECTDRAW7 pDirectDraw,int BackBuffer,LPDIRECTDRAWSURFACE7 pSurface); void hnsSetFullScreenVGA(HWND hWindow); void hnsTaskLoop(HWND hWindow); LRESULT CALLBACK WindowProcedure(HWND hWindow,UINT message,WPARAM wParameter,LPARAM lParameter); int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow) { //WNDCLASSの設計 WNDCLASS WindowClass; WindowClass.style=CS_HREDRAW | CS_VREDRAW; WindowClass.lpfnWndProc=WindowProcedure; WindowClass.cbClsExtra=0; WindowClass.cbWndExtra=0; WindowClass.hInstance=hInstance; WindowClass.hIcon=LoadIcon(NULL,IDI_APPLICATION); WindowClass.hCursor=LoadCursor(NULL,IDC_ARROW); WindowClass.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);//背景は黒 WindowClass.lpszMenuName=NULL; WindowClass.lpszClassName="WindowClassName"; //設計したWNDCLASSの登録 if(!RegisterClass(&WindowClass)) { MessageBox(NULL,"設計したWNDCLASSの設計に失敗しました","Error!",MB_ICONERROR); return 0; } //Windowの作成 HWND hWindow=CreateWindow( "WindowClassName", //window class name "Let's make MEG task", //title //WS_OVERLAPPEDWINDOW | WS_VISIBLE, //Window Style WS_POPUP | WS_VISIBLE, //枠なし表示 CW_USEDEFAULT,CW_USEDEFAULT, //top left CW_USEDEFAULT,CW_USEDEFAULT, //width,height NULL, //parent window handle (HMENU)NULL, //window menu handle hInstance, //application handle NULL); //creation parameter ShowWindow(hWindow,SW_SHOW); MSG message; //無限ループです。 while(GetMessage(&message,NULL,0,0)) { TranslateMessage(&message); DispatchMessage(&message); } return 0; } void hnsDeleteWindowMessage() { //Window Messageを取り除く MSG message; int flag=1; while (1) { flag=::PeekMessage(&message,NULL,WM_KEYFIRST,WM_KEYLAST,PM_REMOVE ); if(flag==0){break;} } } LPDIRECTDRAWSURFACE7 hnsMakeDirectDraw7BackSurface(LPDIRECTDRAWSURFACE7 pSurface) { //pSurfaceはprimary surface,back surface,back surface,...の順で取得すること DDSCAPS2 DirectDrawSurfaceCaps; LPDIRECTDRAWSURFACE7 pBackSurface; ZeroMemory(&DirectDrawSurfaceCaps,sizeof(DirectDrawSurfaceCaps)); DirectDrawSurfaceCaps.dwCaps=DDSCAPS_BACKBUFFER; HRESULT hResult=pSurface->GetAttachedSurface(&DirectDrawSurfaceCaps,&pBackSurface); if(FAILED(hResult)){return NULL;} return pBackSurface; } LPDIRECTDRAW7 hnsMakeDirectDraw7Object(HWND hWindow,int WindowWidth,int WindowHeight) { HRESULT hResult; LPDIRECTDRAW7 pDirectDraw=NULL; hResult=DirectDrawCreateEx(NULL,(void**)&pDirectDraw,IID_IDirectDraw7,NULL); if(FAILED(hResult)){ MessageBox(NULL,"DirectDraw objectは作成できませんでした","",MB_ICONERROR); return NULL; } pDirectDraw->SetCooperativeLevel( hWindow, DDSCL_EXCLUSIVE | //Device占有 DDSCL_ALLOWREBOOT | //CTRL + ALT + DEL の機能を許可する DDSCL_FULLSCREEN); //fullscreen hResult=pDirectDraw->SetDisplayMode( WindowWidth,WindowHeight,//dwWidth,dwHeight 32, //dwBPP Bit/Pixel checkすること! 0, //dwRefeshRate 0:default refresh rate 0); //dwFlags if(FAILED(hResult)){ MessageBox(NULL,"Display Modeの設定はできませんでした","",MB_ICONERROR); return NULL; } pDirectDraw->WaitForVerticalBlank(DDWAITVB_BLOCKBEGIN,0);//垂直描画開始にあわせる return pDirectDraw; } LPDIRECTDRAWSURFACE7 hnsMakeDirectDraw7PrimarySurface(LPDIRECTDRAW7 pDirectDraw,int BackBuffer) { LPDIRECTDRAWSURFACE7 pPrimarySurface; DDSURFACEDESC2 DirectDrawSurfaceDesc; ZeroMemory(&DirectDrawSurfaceDesc,sizeof(DirectDrawSurfaceDesc)); DirectDrawSurfaceDesc.dwSize=sizeof(DirectDrawSurfaceDesc); if(BackBuffer==0) { DirectDrawSurfaceDesc.dwFlags=DDSD_CAPS; DirectDrawSurfaceDesc.dwRefreshRate=0;//default? DirectDrawSurfaceDesc.ddsCaps.dwCaps=DDSCAPS_PRIMARYSURFACE; } else { DirectDrawSurfaceDesc.dwFlags= DDSD_CAPS | //ddsCapsを有効に DDSD_BACKBUFFERCOUNT;//back bufferを有効に DirectDrawSurfaceDesc.ddsCaps.dwCaps= DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP | DDSCAPS_COMPLEX; DirectDrawSurfaceDesc.dwBackBufferCount=BackBuffer; } HRESULT hResult=pDirectDraw->CreateSurface(&DirectDrawSurfaceDesc,&pPrimarySurface,NULL); if(FAILED(hResult)){return NULL;} return pPrimarySurface; } void hnsSetFullScreenVGA(HWND hWindow) { static int WindowWidth=640; static int WindowHeight=480; LONG lResult; //画面を640x480に変更できたかどうか //画面を640x480にする DEVMODE DeviceMode; DeviceMode.dmSize =sizeof(DEVMODE); DeviceMode.dmFields =DM_PELSWIDTH | DM_PELSHEIGHT; DeviceMode.dmPelsWidth =WindowWidth; DeviceMode.dmPelsHeight =WindowHeight; //画面を元に戻す if(hWindow==NULL){ ChangeDisplaySettings(&DeviceMode,NULL); return; } lResult=ChangeDisplaySettings(&DeviceMode,CDS_FULLSCREEN); if(lResult!=DISP_CHANGE_SUCCESSFUL) { MessageBox(hWindow,"full screenの変換は失敗しました","",MB_ICONERROR); return; } SetWindowPos(hWindow,HWND_TOPMOST,0,0,WindowWidth,WindowHeight,0); } void hnsTaskLoop(HWND hWindow) { static int WindowWidth=640; static int WindowHeight=480; //DirectDraw objectの作成 LPDIRECTDRAW7 pDirectDraw=hnsMakeDirectDraw7Object(hWindow,WindowWidth,WindowHeight); if(pDirectDraw==NULL) { return;//失敗したらやめる } //DirectDraw objectからprimary surfaceの作成 LPDIRECTDRAWSURFACE7 pPrimarySurface=hnsMakeDirectDraw7PrimarySurface(pDirectDraw,1); if(pPrimarySurface==NULL) { pDirectDraw->Release(); return; } //primary surfaceからback surfaceの作成 LPDIRECTDRAWSURFACE7 pBackSurface=hnsMakeDirectDraw7BackSurface(pPrimarySurface); if(pPrimarySurface==NULL) { pPrimarySurface->Release(); pDirectDraw->Release(); return; } //device contextに直接描画するのではなくmemory device contextを使う HDC hDeviceContext=GetDC(hWindow); HDC hMemoryDeviceContext=CreateCompatibleDC(hDeviceContext);//この時点では1x1 1bit HBITMAP hBitmap=CreateCompatibleBitmap(hDeviceContext,WindowWidth,WindowHeight); SelectObject(hMemoryDeviceContext,hBitmap); DeleteObject(hBitmap); ReleaseDC(hWindow,hDeviceContext); //文字描画の設定 SetTextColor(hMemoryDeviceContext,RGB(255,255,255)); SetBkMode(hMemoryDeviceContext,TRANSPARENT); SetBkColor(hMemoryDeviceContext,RGB(0,0,0)); int CharacterSize=50; //fontの設定 HFONT hFont=CreateFont( CharacterSize,CharacterSize/2, //高さ・幅 2byte文字は*2が必要 0,0, //角度・ベースラインの角度 FW_BOLD, //太さ FALSE, //斜体 FALSE, //下線 FALSE, //打消し線 SHIFTJIS_CHARSET, //文字セット OUT_DEFAULT_PRECIS, //出力精度 CLIP_DEFAULT_PRECIS,//クリッピング精度 DEFAULT_QUALITY, //出力品質 VARIABLE_PITCH | FF_ROMAN,//ピッチとフォントファミリ "MS ゴシック"); //書体 //fontの選択 SelectObject(hMemoryDeviceContext,hFont); //文字列の作成 char textbuffer[]="Hello World"; //描画矩形領域の設定 RECT rect; rect.left=0; rect.right=WindowWidth; rect.top=0; rect.bottom=WindowHeight; DrawText( hMemoryDeviceContext, textbuffer, -1,//文字列の長さは自動的に計算 &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);//横中央・縦中央 1列表示 //hFontの解放 DeleteObject(hFont); //memory device contextからback surfaceへブロック転送 HDC hSurfaceDeviceContext; pBackSurface->GetDC(&hSurfaceDeviceContext); BitBlt(hSurfaceDeviceContext,0,0,WindowWidth,WindowHeight, hMemoryDeviceContext,0,0,SRCCOPY); //back surfaceの内容をprimary surfaceに転送 pBackSurface->ReleaseDC(hSurfaceDeviceContext);//flipする前にdevice contextを消しておくこと! pPrimarySurface->Flip(NULL,DDFLIP_NOVSYNC);//次の走査線にできる限り近い物理的なフリップを実行する //memory device contextの解放 DeleteDC(hMemoryDeviceContext); bool EscapeFlag=FALSE; //Window Messageを取り除く hnsDeleteWindowMessage(); while(1) //御法度の無限ループ { while(1) { if((GetAsyncKeyState(VK_ESCAPE)&0x8000)!=0){EscapeFlag=TRUE;break;} } if(EscapeFlag){break;} } //Window Messageを取り除く hnsDeleteWindowMessage(); //Direct Drawで作った部品を作成順の逆で解放 pBackSurface->Release(); pPrimarySurface->Release(); pDirectDraw->Release(); } LRESULT CALLBACK WindowProcedure(HWND hWindow,UINT message,WPARAM wParameter,LPARAM lParameter) { switch(message) { case WM_CREATE: hnsSetFullScreenVGA(hWindow); break; case WM_KEYDOWN: switch(wParameter) { //ESCAPEキーをおすとWindowを破壊するWM_DESTROYメッセージを送る case VK_ESCAPE: PostMessage(hWindow,WM_DESTROY,0,0); break; //RETURNキーをおすとタスク開始 case VK_RETURN: hnsTaskLoop(hWindow); hnsSetFullScreenVGA(hWindow); break; } break; case WM_DESTROY: //hnsSetFullScreenVGA(NULL); ない方がいいみたい PostQuitMessage(0); return 0; } return DefWindowProc(hWindow,message,wParameter,lParameter); }