/////////////////////////////////////////////////////// /// /// このソースコードを引用・改変した結果如何なる /// 損害が発生しても、著者はは責任を負いません。 /// 広島大学 脳外科 橋詰顕 /// //////////////////////////////////////////////////////// #include //ミリ秒単位の時間制御用 #include //timeGetTime 本来windows.hに含まれているはずだが明示しないとだめみたい #pragma comment(lib,"winmm.lib") //wav file用でもある //GIVEIO.sys用 #include //_outpに必要 BOOL CanGIVEIO=TRUE; //GIVEIO.sysを使ってパラレルポートの制御をするかどうか HBITMAP hnsCreateRandomDotBitmap(HDC hDeviceContext,int width,int height,DWORD seed); void hnsDeleteWindowMessage(); void hnsDrawText(HWND hWindow,COLORREF FontColor); HANDLE hnsEnableGIVEIO(); void hnsTaskLoop(HWND hWindow); void hnsTriggerGIVEIO(int num); 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_POPUP | WS_VISIBLE, //Window Style //WS_OVERLAPPEDWINDOW | 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); //GIVEIO.sysを有効にする HANDLE h=hnsEnableGIVEIO(); MSG message; //無限ループです。 while(GetMessage(&message,NULL,0,0)) { TranslateMessage(&message); DispatchMessage(&message); } //GIVEIOを終了する。 if(h!=NULL){CloseHandle(h);} return 0; } HBITMAP hnsCreateRandomDotBitmap(HDC hDeviceContext,int width,int height,DWORD seed) { //random dotの作成 BYTE* VGA=new BYTE[width*height*4];//32bit color srand(seed); int x,y,n,nn; BYTE k; for(y=0;y7){k=0;}else{k=255;} nn=n+x*4; VGA[nn]=k; VGA[nn+1]=k; VGA[nn+2]=k; VGA[nn+3]=0; } } //bitmapの作成 HBITMAP hBitmap=CreateBitmap( width,height, 1,32,VGA);//color planeの数、32bit delete VGA; return hBitmap; } 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;} } } void hnsDrawText(HWND hWindow,COLORREF FontColor) { int WindowWidth=640; int WindowHeight=480; //DeviceContextの作成 HDC hDeviceContext=GetDC(hWindow); HBITMAP hBitmap=hnsCreateRandomDotBitmap(hDeviceContext,WindowWidth,WindowHeight,timeGetTime()); //MemoryDeviceContextの作成 HDC hMemoryDeviceContext=CreateCompatibleDC(hDeviceContext); SelectObject(hMemoryDeviceContext,hBitmap); DeleteObject(hBitmap); //文字の設定 SetTextColor(hMemoryDeviceContext,FontColor); //文字色は黒 SetBkMode(hMemoryDeviceContext,TRANSPARENT); //背景は透明 SetBkColor(hMemoryDeviceContext,RGB(0,0,0)); //とりあえず背景は黒 int CharacterSize=100; //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[]="脳磁図"; //描画矩形領域の設定 RECT rect; rect.left=0; rect.right=WindowWidth; rect.top=0; rect.bottom=WindowHeight; //Graphic pathに文字を描画 BeginPath(hMemoryDeviceContext); DrawText( hMemoryDeviceContext, textbuffer, -1,//文字列の長さは自動的に計算 &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);//横中央・縦中央 1列表示 //hFontの解放 DeleteObject(hFont); EndPath(hMemoryDeviceContext); //regionに変換 HRGN hRegion=PathToRegion(hMemoryDeviceContext); //文字のランダムドット HBITMAP hBitmap2=hnsCreateRandomDotBitmap(hDeviceContext,40,40,1); //MemoryDeviceContextに描画 HBRUSH hBrush=CreatePatternBrush(hBitmap2); SelectObject(hMemoryDeviceContext,hBrush); FillRgn(hMemoryDeviceContext,hRegion,hBrush); DeleteObject(hBrush); DeleteObject(hRegion); //MemoryDeviceContextからDeviceContextへブロック転送 BitBlt(hDeviceContext,0,0,640,480, hMemoryDeviceContext,0,0,SRCCOPY); //bitmapの画像の解放 DeleteObject(hBitmap); DeleteObject(hBitmap2); //MemoryDeviceContextの解放 DeleteDC(hMemoryDeviceContext); //DeviceContextの解放 ReleaseDC(hWindow,hDeviceContext); } HANDLE hnsEnableGIVEIO() { if(!CanGIVEIO){return NULL;}//GIVEIOを最初から使わないとき //Windows2000/XPの時のGIVEIO.sysの設定 HANDLE h=CreateFile( (LPCTSTR)"\\\\.\\giveio", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if(h==INVALID_HANDLE_VALUE) { MessageBox(NULL,TEXT("GIVEIOは使えません"),TEXT("GIVEIO.sys check"),MB_ICONERROR); CanGIVEIO=TRUE; } return h; } void hnsTaskLoop(HWND hWindow) { bool EscapeFlag=FALSE; int WaitTime=100; //500msec int t,t0=timeGetTime(); //Window Messageを取り除く hnsDeleteWindowMessage(); timeBeginPeriod(1); srand(timeGetTime()); while(1) //御法度の無限ループ { //文字表示 hnsDrawText(hWindow,RGB(rand()%256,rand()%256,rand()%256)); //パラレルポートにトリガー信号1を出力 hnsTriggerGIVEIO(1); //ESCAPEキー入力があればループ脱出 while(1) { t=timeGetTime(); if(t>=t0+WaitTime){t0=t;EscapeFlag=FALSE;break;} else if((GetAsyncKeyState(VK_ESCAPE)&0x8000)!=0){EscapeFlag=TRUE;break;} } if(EscapeFlag){break;} //パラレルポートにトリガー信号0を出力(これがないと次から認識してもらえない) hnsTriggerGIVEIO(0); } timeEndPeriod(1); //Window Messageを取り除く hnsDeleteWindowMessage(); } void hnsTriggerGIVEIO(int num) { //パラレルポートの設定 const unsigned short data_port =0x00378;//D7,D6,D5,D4,D3,D2,D1,D0 const unsigned short status_port =0x00379;//^BSY,^ACK,PE,SLCT,^ERR,・・・,・・・,・・・ //^BSY=0 printer is busy //^ACK=0 ACK受信 //PE=1 ペーパーエンド //^ERR=0 printer error const unsigned short control_port=0x0037A;//・・・,・・・,DIRC,IRQE,SLIN,IPRT,ATFD,STRB //DIRC:ポート方向の切り替え 1:入力 0:出力 //IRQE:ACK割り込み許可    1:許可 0:禁止 //SLIN:プリンタセレクト制御 1:ON 0:OFF //IPRT:プリンタリセット 1:ON 0:OFF //ATFD:オートフィード 1:ON 0:OFF if(!CanGIVEIO){return;} _outp(control_port,0);// control port 出力モードに _outp(data_port,num);// data port 0~255 } LRESULT CALLBACK WindowProcedure(HWND hWindow,UINT message,WPARAM wParameter,LPARAM lParameter) { static int WindowWidth=640; static int WindowHeight=480; LONG lResult; //画面を640x480に変更できたかどうか switch(message) { case WM_CREATE: //画面を640x480にする DEVMODE DeviceMode; DeviceMode.dmSize =sizeof(DEVMODE); DeviceMode.dmFields =DM_PELSWIDTH | DM_PELSHEIGHT; DeviceMode.dmPelsWidth =WindowWidth; DeviceMode.dmPelsHeight =WindowHeight; lResult=ChangeDisplaySettings(&DeviceMode,CDS_FULLSCREEN); if(lResult!=DISP_CHANGE_SUCCESSFUL) { MessageBox(hWindow,"full screenの変換は失敗しました","",MB_ICONERROR); break; } SetWindowPos(hWindow,HWND_TOPMOST,0,0,WindowWidth,WindowHeight,0); 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); break; } break; case WM_DESTROY: //画面モードを元に戻す ChangeDisplaySettings(&DeviceMode,NULL); PostQuitMessage(0); return 0; } return DefWindowProc(hWindow,message,wParameter,lParameter); }