#include #include #include "stopWatch.h" #include "stopWatchClock.h" stopWatch clockObject; const int threadTimer = 1; //The time object thing bool timerActive = false; void displayTime (HWND hwnd); BOOL CALLBACK digTimerEventListener (HWND hwnd, UINT event, WPARAM object, LPARAM lParam); void displayTime (HWND hwnd) //Displays the time in H:i:s format { char answer[15]; sprintf(answer, "%02u:%02u:%02u", clockObject.getHour(), clockObject.getMin(), clockObject.getSec()); SetDlgItemText(hwnd, lblTime, answer); } BOOL CALLBACK digTimerEventListener (HWND hwnd, UINT event, WPARAM object, LPARAM lParam) { if (event == WM_INITDIALOG) { displayTime(hwnd); } else if (event == WM_TIMER) //Timer pulsed (ever second) { //Increment by a second and then display the new time clockObject.incSec(); displayTime(hwnd); } else if (event == WM_COMMAND) { int objectID = LOWORD(object); if (objectID == cmdTimer) { if (!timerActive) //Start timer { int status = SetTimer(hwnd, threadTimer, 1000, NULL); if (status == 0) { MessageBox(hwnd, "There was an error when trying to start the timer", "Thread error", MB_OK | MB_ICONEXCLAMATION); } else { //Change the caption text of the button SendMessage(GetDlgItem(hwnd, cmdTimer), WM_SETTEXT, 0, (LPARAM)(LPCTSTR)"&Stop"); timerActive = true; } } else //Stop timer { KillTimer(hwnd, threadTimer); //Stops the timer SendMessage(GetDlgItem(hwnd, cmdTimer), WM_SETTEXT, 0, (LPARAM)(LPCTSTR)"&Start"); timerActive = false; if (clockObject.isOverflowSet()) //Warns the user if the stop watch hour went over 23 { MessageBox(hwnd, "The stopWatchClock was active for more then a day, but the amount of days is not shown", "Overflow", MB_OK | MB_ICONEXCLAMATION); } } } else if (objectID == cmdReset) //Reset the clock { clockObject.reset(); displayTime(hwnd); } else if (objectID == cmdClose) { EndDialog(hwnd, objectID); } } else if (event == WM_CLOSE) { EndDialog(hwnd, object); } else if (event == WM_DESTROY) { if (timerActive) //Make sure the timer object is destroyed { KillTimer(hwnd, threadTimer); } } else { return false; } return true; } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR args, int nCmdShow) { stopWatch clockObject(); return DialogBox(hInstance, MAKEINTRESOURCE(digTimer), NULL, digTimerEventListener); }