From 489824b1f935b1f8130fba5d84be75e248f40cc6 Mon Sep 17 00:00:00 2001 From: Grayson Riffe Date: Tue, 21 Jan 2025 01:48:01 -0600 Subject: [PATCH] Add accelerators --- Mainspring/resource.rc | 8 +++++++ Mainspring/src/Application.cpp | 43 ++++++++++++++++++++++++++++++---- Mainspring/src/resources.h | 3 +++ 3 files changed, 50 insertions(+), 4 deletions(-) diff --git a/Mainspring/resource.rc b/Mainspring/resource.rc index be11515..a400cb8 100644 --- a/Mainspring/resource.rc +++ b/Mainspring/resource.rc @@ -30,4 +30,12 @@ BEGIN CTEXT "APPNAME", IDC_STATICTITLE, 30, 0, 115, 20, SS_CENTERIMAGE CTEXT "TIME", IDC_STATICTIME, 0, 20, 175, 25, SS_CENTERIMAGE DEFPUSHBUTTON "STARTPAUSE" IDC_BUTTONSTARTPAUSE, 63, 50, 50, 15 +END + +IDA_ACCELMAIN ACCELERATORS +BEGIN + "N", ID_FILE_NEW, CONTROL, VIRTKEY + "S", ID_FILE_SAVE, CONTROL, VIRTKEY + "S", ID_FILE_SAVEAS, CONTROL, SHIFT, VIRTKEY + "O", ID_FILE_OPEN, CONTROL, VIRTKEY END \ No newline at end of file diff --git a/Mainspring/src/Application.cpp b/Mainspring/src/Application.cpp index 930c73a..9ec88da 100644 --- a/Mainspring/src/Application.cpp +++ b/Mainspring/src/Application.cpp @@ -4,7 +4,7 @@ #include "resources.h" // Define window message to update the timer display -#define WM_UPDATEAPP WM_USER +#define WM_UPDATEAPP WM_USER + 1 // Enable visual styles #pragma comment(linker, "\"/manifestdependency:type='win32' \ @@ -25,7 +25,14 @@ namespace mainspring { void Application::run() { // Read last time here - DialogBoxParam(nullptr, MAKEINTRESOURCE(IDD_DIALOGMAIN), nullptr, reinterpret_cast(mainDlgProc), reinterpret_cast(this)); + HWND dlg = CreateDialogParam(nullptr, MAKEINTRESOURCE(IDD_DIALOGMAIN), nullptr, reinterpret_cast(mainDlgProc), reinterpret_cast(this)); + + HACCEL hAccel = LoadAccelerators(GetModuleHandle(NULL), MAKEINTRESOURCE(IDA_ACCELMAIN)); + MSG msg = {}; + ShowWindow(dlg, SW_SHOW); + while (GetMessage(&msg, nullptr, 0, 0)) + if (!TranslateAccelerator(dlg, hAccel, &msg)) + IsDialogMessage(dlg, &msg); } BOOL CALLBACK Application::mainDlgProc(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam) { @@ -57,7 +64,6 @@ namespace mainspring { SendMessage(GetDlgItem(dlg, IDC_STATICTIME), WM_SETFONT, reinterpret_cast(timeFont), NULL); SendMessage(dlg, WM_UPDATEAPP, NULL, NULL); - SetTimer(dlg, 1, 50, nullptr); return TRUE; @@ -69,6 +75,31 @@ namespace mainspring { app->m_startpauseButtonPressed = true; return TRUE; + case ID_FILE_NEW: { + std::cout << std::format("New!\n"); + return TRUE; + } + + case ID_FILE_SAVE: { + std::cout << std::format("Save!\n"); + return TRUE; + } + + case ID_FILE_SAVEAS: { + std::cout << std::format("Save As!\n"); + return TRUE; + } + + case ID_FILE_OPEN: { + std::cout << std::format("Open!\n"); + return TRUE; + } + + case ID_FILE_EXIT: { + SendMessage(dlg, WM_CLOSE, NULL, NULL); + 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; @@ -109,7 +140,11 @@ namespace mainspring { case WM_CLOSE: // TODO: Ask to / save time here KillTimer(dlg, 1); - EndDialog(dlg, 0); + DestroyWindow(dlg); + return TRUE; + + case WM_DESTROY: + PostQuitMessage(0); return TRUE; } diff --git a/Mainspring/src/resources.h b/Mainspring/src/resources.h index a5b7ae2..194f98f 100644 --- a/Mainspring/src/resources.h +++ b/Mainspring/src/resources.h @@ -16,6 +16,9 @@ #define ID_HELP_ABOUT 311 +// Accelerators +#define IDA_ACCELMAIN 401 + // Controls (text and buttons) #define IDC_BUTTONSTARTPAUSE 1001 #define IDC_STATICTITLE 1002