Basic timing
This commit is contained in:
		
							parent
							
								
									bf96e3bc9c
								
							
						
					
					
						commit
						fd6a14675b
					
				| @ -20,12 +20,12 @@ BEGIN | |||||||
|     END |     END | ||||||
| END | END | ||||||
| 
 | 
 | ||||||
| IDD_DIALOGMAIN DIALOGEX 0, 0, 175, 75 | IDD_DIALOGMAIN DIALOGEX 0, 0, 175, 70 | ||||||
| STYLE WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | DS_SETFONT | STYLE WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | DS_SETFONT | ||||||
| FONT 8, "MS Shell Dlg", 400, 0, 0x1 | FONT 8, "MS Shell Dlg", 400, 0, 0x1 | ||||||
| MENU IDR_MENUMAIN | MENU IDR_MENUMAIN | ||||||
| BEGIN | BEGIN | ||||||
|     CTEXT "APPNAME", IDC_STATICTITLE, 30, 5, 115, 20, SS_CENTERIMAGE |     CTEXT "APPNAME", IDC_STATICTITLE, 30, 0, 115, 20, SS_CENTERIMAGE | ||||||
|     CTEXT "TIME", IDC_STATICTIME, 10, 25, 155, 25, SS_CENTERIMAGE |     CTEXT "TIME", IDC_STATICTIME, 0, 20, 175, 25, SS_CENTERIMAGE | ||||||
|     DEFPUSHBUTTON "STARTPAUSE" IDC_BUTTONSTARTPAUSE, 63, 55, 50, 15 |     DEFPUSHBUTTON "STARTPAUSE" IDC_BUTTONSTARTPAUSE, 63, 50, 50, 15 | ||||||
| END | END | ||||||
| @ -3,6 +3,9 @@ | |||||||
| #include "Application.h" | #include "Application.h" | ||||||
| #include "resources.h" | #include "resources.h" | ||||||
| 
 | 
 | ||||||
|  | // Define window message to update the timer display
 | ||||||
|  | #define WM_UPDATETIME WM_USER | ||||||
|  | 
 | ||||||
| // Enable visual styles
 | // Enable visual styles
 | ||||||
| #pragma comment(linker, "\"/manifestdependency:type='win32' \ | #pragma comment(linker, "\"/manifestdependency:type='win32' \ | ||||||
| name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \ | name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \ | ||||||
| @ -12,11 +15,15 @@ namespace mainspring { | |||||||
|     Application::Application(const char* appName, const char* appVersion) |     Application::Application(const char* appName, const char* appVersion) | ||||||
|         : m_appName(appName) |         : m_appName(appName) | ||||||
|         , m_appVersion(appVersion) |         , m_appVersion(appVersion) | ||||||
|  |         , m_timing(false) | ||||||
|  |         , m_elapsed() | ||||||
|     { |     { | ||||||
|         std::cout << std::format("{} {}\n", m_appName, m_appVersion); |         std::cout << std::format("{} {}\n", m_appName, m_appVersion); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     void Application::run() { |     void Application::run() { | ||||||
|  |         // Read last time here
 | ||||||
|  | 
 | ||||||
|         DialogBoxParam(nullptr, MAKEINTRESOURCE(IDD_DIALOGMAIN), nullptr, reinterpret_cast<DLGPROC>(mainDlgProc), reinterpret_cast<LPARAM>(this)); |         DialogBoxParam(nullptr, MAKEINTRESOURCE(IDD_DIALOGMAIN), nullptr, reinterpret_cast<DLGPROC>(mainDlgProc), reinterpret_cast<LPARAM>(this)); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| @ -42,24 +49,68 @@ namespace mainspring { | |||||||
|                 HFONT titleFont = CreateFontIndirect(&lFont); |                 HFONT titleFont = CreateFontIndirect(&lFont); | ||||||
|                 SendMessage(GetDlgItem(dlg, IDC_STATICTITLE), WM_SETFONT, reinterpret_cast<WPARAM>(titleFont), NULL); |                 SendMessage(GetDlgItem(dlg, IDC_STATICTITLE), WM_SETFONT, reinterpret_cast<WPARAM>(titleFont), NULL); | ||||||
| 
 | 
 | ||||||
|                 lFont.lfHeight = 40; |                 std::memcpy(lFont.lfFaceName, "Consolas", 9); | ||||||
|  |                 lFont.lfHeight = 60; | ||||||
|                 HFONT timeFont = CreateFontIndirect(&lFont); |                 HFONT timeFont = CreateFontIndirect(&lFont); | ||||||
|                 SendMessage(GetDlgItem(dlg, IDC_STATICTIME), WM_SETFONT, reinterpret_cast<WPARAM>(timeFont), NULL); |                 SendMessage(GetDlgItem(dlg, IDC_STATICTIME), WM_SETFONT, reinterpret_cast<WPARAM>(timeFont), NULL); | ||||||
| 
 | 
 | ||||||
|                 SetDlgItemText(dlg, IDC_BUTTONSTARTPAUSE, "Start"); |                 SetDlgItemText(dlg, IDC_BUTTONSTARTPAUSE, "Start"); | ||||||
| 
 | 
 | ||||||
|  |                 SendMessage(dlg, WM_UPDATETIME, NULL, NULL); | ||||||
|  | 
 | ||||||
|  |                 return TRUE; | ||||||
|  |             } | ||||||
|  | 
 | ||||||
|  |             case WM_COMMAND: | ||||||
|  |                 switch (LOWORD(wParam)) { | ||||||
|  |                     case IDC_BUTTONSTARTPAUSE: { | ||||||
|  |                         app->m_timing = !app->m_timing; | ||||||
|  | 
 | ||||||
|  |                         if (app->m_timing) { | ||||||
|  |                             SetDlgItemText(dlg, IDC_BUTTONSTARTPAUSE, "Pause"); | ||||||
|  |                             app->m_startTime = Clock::now(); | ||||||
|  |                             // Timer to update time
 | ||||||
|  |                             SetTimer(dlg, 1, 50, nullptr); | ||||||
|  |                         } | ||||||
|  |                         else { | ||||||
|  |                             SetDlgItemText(dlg, IDC_BUTTONSTARTPAUSE, "Start"); | ||||||
|  |                             KillTimer(dlg, 1); | ||||||
|  |                         } | ||||||
|  | 
 | ||||||
|  |                         return TRUE; | ||||||
|  |                     } | ||||||
|  | 
 | ||||||
|  |                     case ID_HELP_ABOUT: | ||||||
|  |                         MessageBox(dlg, std::format("{} {}\nCopyright Grayson Riffe 2025\ngraysonriffe.com", app->m_appName, app->m_appVersion).c_str(), std::format("About {}", app->m_appName).c_str(), MB_OK); | ||||||
|  |                         return TRUE; | ||||||
|  |                 } | ||||||
|  | 
 | ||||||
|  |                 return FALSE; | ||||||
|  | 
 | ||||||
|  |             case WM_TIMER: { | ||||||
|  |                 SendMessage(dlg, WM_UPDATETIME, NULL, NULL); | ||||||
|  |                 return TRUE; | ||||||
|  |             } | ||||||
|  | 
 | ||||||
|  |             case WM_UPDATETIME: { | ||||||
|  |                 Seconds sinceLastStart = 0; | ||||||
|  |                 if(app->m_timing) | ||||||
|  |                     sinceLastStart = Duration(Clock::now() - app->m_startTime).count(); | ||||||
|  | 
 | ||||||
|  |                 Seconds totalTime = app->m_elapsed + sinceLastStart; | ||||||
|  | 
 | ||||||
|  |                 uint32_t hr = totalTime / (3600), min = totalTime % (3600) / 60, sec = totalTime % 60; | ||||||
|  |                 SetDlgItemText(dlg, IDC_STATICTIME, std::format("{:02}:{:02}:{:02}", hr, min, sec).c_str()); | ||||||
|  | 
 | ||||||
|                 return TRUE; |                 return TRUE; | ||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
|             case WM_CLOSE: |             case WM_CLOSE: | ||||||
|  |                 // TODO: Ask to / save time here
 | ||||||
|                 EndDialog(dlg, 0); |                 EndDialog(dlg, 0); | ||||||
|                 return TRUE; |                 return TRUE; | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         return FALSE; |         return FALSE; | ||||||
|     } |     } | ||||||
| 
 |  | ||||||
|     Application::~Application() { |  | ||||||
| 
 |  | ||||||
|     } |  | ||||||
| } | } | ||||||
| @ -1,17 +1,26 @@ | |||||||
| // Application class header
 | // Application class header
 | ||||||
| 
 | 
 | ||||||
| namespace mainspring { | namespace mainspring { | ||||||
|  |     using Clock = std::chrono::high_resolution_clock; | ||||||
|  |     using TimePoint = std::chrono::time_point<Clock>; | ||||||
|  |     using Duration = std::chrono::duration<double, std::ratio<1, 1>>; | ||||||
|  | 
 | ||||||
|  |     using Seconds = uint64_t; | ||||||
|  | 
 | ||||||
|     class Application { |     class Application { | ||||||
|     public: |     public: | ||||||
|         Application(const char* appName, const char* appVersion); |         Application(const char* appName, const char* appVersion); | ||||||
| 
 | 
 | ||||||
|         void run(); |         void run(); | ||||||
| 
 |  | ||||||
|         ~Application(); |  | ||||||
|     private: |     private: | ||||||
|         static BOOL CALLBACK mainDlgProc(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam); |         static BOOL CALLBACK mainDlgProc(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam); | ||||||
| 
 | 
 | ||||||
|         const char* m_appName; |         const char* m_appName; | ||||||
|         const char* m_appVersion; |         const char* m_appVersion; | ||||||
|  | 
 | ||||||
|  |         // Current "Time" (state of the app)
 | ||||||
|  |         bool m_timing; // Currently counting time?
 | ||||||
|  |         Seconds m_elapsed; // in seconds
 | ||||||
|  |         TimePoint m_startTime; // When the timer was started
 | ||||||
|     }; |     }; | ||||||
| } | } | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user