/* GetTickCountとtimeGetTimeによる時間測定 1999/ 6/23 宍戸 輝光 */ #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 hwTickB,hwTimeB,hwIntL; DWORD dwStart,dwLap=0,dwCount=0,dwInt; switch (iMsg) { case WM_CREATE: hwTickB=CreateWindow("Button","Tick", /* Tickボタン作成 */ WS_CHILD | WS_VISIBLE,16,8,96,32,hwnd, (HMENU)0,hInst,NULL); hwTimeB=CreateWindow("Button","Time", /* Timeボタン作成 */ WS_CHILD | WS_VISIBLE,128,8,96,32,hwnd, (HMENU)1,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)" 1ms*1000"); SendMessage(hwIntL,LB_ADDSTRING,0,(LPARAM)" 2ms*1000"); SendMessage(hwIntL,LB_ADDSTRING,0,(LPARAM)" 3ms*1000"); SendMessage(hwIntL,LB_ADDSTRING,0,(LPARAM)" 4ms*1000"); SendMessage(hwIntL,LB_ADDSTRING,0,(LPARAM)" 5ms*1000"); SendMessage(hwIntL,LB_ADDSTRING,0,(LPARAM)" 6ms*1000"); SendMessage(hwIntL,LB_ADDSTRING,0,(LPARAM)" 7ms*1000"); SendMessage(hwIntL,LB_ADDSTRING,0,(LPARAM)" 8ms*1000"); SendMessage(hwIntL,LB_ADDSTRING,0,(LPARAM)" 9ms*1000"); SendMessage(hwIntL,LB_ADDSTRING,0,(LPARAM)"10ms*1000"); SendMessage(hwIntL,LB_ADDSTRING,0,(LPARAM)"11ms*1000"); SendMessage(hwIntL,LB_ADDSTRING,0,(LPARAM)"12ms*1000"); /* リストボックス選択 */ SendMessage(hwIntL,LB_SELECTSTRING,0,(LPARAM)" 1ms"); break; case WM_COMMAND: switch (LOWORD(wParam)) { case 0: /* Tickボタン */ /* 周期設定 */ dwInt=SendMessage(hwIntL,LB_GETCURSEL,0,0); /* 開始時間を保存 */ dwStart=timeGetTime(); dwLap=GetTickCount(); while (dwCount++<1000) { /* 周期で1000回ループ */ while (dwLap+dwInt>=GetTickCount()); /* 周期の時間が経過するまで待つ */ dwLap=GetTickCount(); } /* 結果表示 */ wsprintf(lpszStr,"経過時間%dms",timeGetTime()-dwStart); MessageBox(hwnd,lpszStr,"終了",MB_OK); break; case 1: /* Timeボタン */ dwInt=SendMessage(hwIntL,LB_GETCURSEL,0,0); dwStart=timeGetTime(); dwLap=timeGetTime(); while (dwCount++<1000) { while (dwLap+dwInt>=timeGetTime()); dwLap=timeGetTime(); } wsprintf(lpszStr,"経過時間%dms",timeGetTime()-dwStart); MessageBox(hwnd,lpszStr,"終了",MB_OK); break; } break; case WM_DESTROY : PostQuitMessage(0); break; } return DefWindowProc (hwnd, iMsg, wParam, lParam) ; }