/*   タイマー(WM_TIMER)の精度  1999/ 6/25 宍戸 輝光 */ #include #include LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM); HINSTANCE hInst; HWND hwnd; int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow){ MSG msg; WNDCLASSEX wndclass ; hInst=hInstance; /* プロセスのハンドルを保存 */ 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 = "CWindow"; wndclass.hIconSm = LoadIcon (NULL, IDI_APPLICATION); RegisterClassEx (&wndclass); hwnd = CreateWindow ("CWindow","タイマーの周期", WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,320,240, 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) { static char lpszStr[64]; static HWND hwTimerB,hwIntL; static DWORD dwStart,dwLap,dwCount,dwInt; MSG msg; switch (iMsg) { case WM_CREATE: hwTimerB=CreateWindow("Button","Timer", /* Timeボタン作成 */ WS_CHILD | WS_VISIBLE,128,8,96,32,hwnd, (HMENU)0,hInst,NULL); hwIntL=CreateWindow("LISTBOX","LIST", /* リストボックス */ WS_CHILD | WS_VISIBLE | WS_BORDER |WS_VSCROLL, 16,64,128,96,hwnd,(HMENU)2,hInst,NULL); /* リストボックスに項目追加 */ SendMessage(hwIntL,LB_ADDSTRING,0,(LPARAM)" 10ms*100"); SendMessage(hwIntL,LB_ADDSTRING,0,(LPARAM)" 20ms*100"); SendMessage(hwIntL,LB_ADDSTRING,0,(LPARAM)" 30ms*100"); SendMessage(hwIntL,LB_ADDSTRING,0,(LPARAM)" 40ms*100"); SendMessage(hwIntL,LB_ADDSTRING,0,(LPARAM)" 50ms*100"); SendMessage(hwIntL,LB_ADDSTRING,0,(LPARAM)" 60ms*100"); SendMessage(hwIntL,LB_ADDSTRING,0,(LPARAM)" 70ms*100"); SendMessage(hwIntL,LB_ADDSTRING,0,(LPARAM)" 80ms*100"); SendMessage(hwIntL,LB_ADDSTRING,0,(LPARAM)" 90ms*100"); SendMessage(hwIntL,LB_ADDSTRING,0,(LPARAM)"100ms*100"); SendMessage(hwIntL,LB_ADDSTRING,0,(LPARAM)"110ms*100"); SendMessage(hwIntL,LB_ADDSTRING,0,(LPARAM)"120ms*100"); /* リストボックス選択 */ SendMessage(hwIntL,LB_SELECTSTRING,0,(LPARAM)" 10ms*100"); break; case WM_COMMAND: switch (LOWORD(wParam)) { case 0: /* Timerボタン */ EnableWindow(hwTimerB,FALSE); /* 周期設定 */ dwInt=SendMessage(hwIntL,LB_GETCURSEL,0,0); /* 開始時間を保存 */ dwStart=timeGetTime(); dwCount=0; /* タイマーセット */ SetTimer(hwnd,1,(dwInt+1)*10,NULL); /* タイマ−メッセージが100回処理されるまで待つ */ while (dwCount<100) { if(GetMessage (&msg,NULL,0,0)==0) break; TranslateMessage(&msg); DispatchMessage(&msg); /* メッセージ処理 */ } KillTimer(hwnd,1); /* タイマーストップ */ /* 結果表示 */ wsprintf(lpszStr,"経過時間%dms",timeGetTime()-dwStart); MessageBox(hwnd,lpszStr,"終了",MB_OK); EnableWindow(hwTimerB,TRUE); break; } break; case WM_TIMER: // タイマーメッセージの処理 dwCount++; break; case WM_DESTROY : PostQuitMessage(0); break; } return DefWindowProc (hwnd, iMsg, wParam, lParam) ; }