diff --git a/StayAwake/StayAwake.cpp b/StayAwake/StayAwake.cpp index 031619c..c4768c5 100644 --- a/StayAwake/StayAwake.cpp +++ b/StayAwake/StayAwake.cpp @@ -1,7 +1,3 @@ - -// StayAwake.cpp : Defines the class behaviors for the application. -// - #include "pch.h" #include "framework.h" #include "StayAwake.h" @@ -12,96 +8,37 @@ #endif -// CStayAwakeApp - BEGIN_MESSAGE_MAP(CStayAwakeApp, CWinApp) ON_COMMAND(ID_HELP, &CWinApp::OnHelp) END_MESSAGE_MAP() -// CStayAwakeApp construction - CStayAwakeApp::CStayAwakeApp() { - // support Restart Manager m_dwRestartManagerSupportFlags = AFX_RESTART_MANAGER_SUPPORT_RESTART; - - // TODO: add construction code here, - // Place all significant initialization in InitInstance } -// The one and only CStayAwakeApp object - CStayAwakeApp theApp; -// CStayAwakeApp initialization - BOOL CStayAwakeApp::InitInstance() { - // InitCommonControlsEx() is required on Windows XP if an application - // manifest specifies use of ComCtl32.dll version 6 or later to enable - // visual styles. Otherwise, any window creation will fail. - INITCOMMONCONTROLSEX InitCtrls{}; - InitCtrls.dwSize = sizeof(InitCtrls); - // Set this to include all the common control classes you want to use - // in your application. - InitCtrls.dwICC = ICC_WIN95_CLASSES; - InitCommonControlsEx(&InitCtrls); - CWinApp::InitInstance(); - AfxEnableControlContainer(); - // Create the shell manager, in case the dialog contains - // any shell tree view or shell list view controls. - CShellManager *pShellManager = new CShellManager; - // Activate "Windows Native" visual manager for enabling themes in MFC controls CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerWindows)); - // Standard initialization - // If you are not using these features and wish to reduce the size - // of your final executable, you should remove from the following - // the specific initialization routines you do not need - // Change the registry key under which our settings are stored - // TODO: You should modify this string to be something appropriate - // such as the name of your company or organization - SetRegistryKey(_T("Local AppWizard-Generated Applications")); - CStayAwakeDlg dlg; m_pMainWnd = &dlg; INT_PTR nResponse = dlg.DoModal(); - if (nResponse == IDOK) - { - // TODO: Place code here to handle when the dialog is - // dismissed with OK - } - else if (nResponse == IDCANCEL) - { - // TODO: Place code here to handle when the dialog is - // dismissed with Cancel - } - else if (nResponse == -1) - { - TRACE(traceAppMsg, 0, "Warning: dialog creation failed, so application is terminating unexpectedly.\n"); - TRACE(traceAppMsg, 0, "Warning: if you are using MFC controls on the dialog, you cannot #define _AFX_NO_MFC_CONTROLS_IN_DIALOGS.\n"); - } - - // Delete the shell manager created above. - if (pShellManager != nullptr) - { - delete pShellManager; - } #if !defined(_AFXDLL) && !defined(_AFX_NO_MFC_CONTROLS_IN_DIALOGS) ControlBarCleanUp(); #endif - // Since the dialog has been closed, return FALSE so that we exit the - // application, rather than start the application's message pump. return FALSE; } diff --git a/StayAwake/StayAwake.h b/StayAwake/StayAwake.h index 34f8461..49466d4 100644 --- a/StayAwake/StayAwake.h +++ b/StayAwake/StayAwake.h @@ -1,31 +1,22 @@ - -// StayAwake.h : main header file for the PROJECT_NAME application -// - #pragma once #ifndef __AFXWIN_H__ #error "include 'pch.h' before including this file for PCH" #endif -#include "resource.h" // main symbols +#include "resource.h" -// CStayAwakeApp: -// See StayAwake.cpp for the implementation of this class -// - class CStayAwakeApp : public CWinApp { public: + const UINT WM_SHOWFIRSTINSTANCE{RegisterWindowMessage(L"WM_SHOWFIRSTINSTANCE|StayAwake_Restore_Tray_Icon")}; CStayAwakeApp(); // Overrides public: virtual BOOL InitInstance(); -// Implementation - DECLARE_MESSAGE_MAP() }; diff --git a/StayAwake/StayAwake.vcxproj b/StayAwake/StayAwake.vcxproj index ef5e497..dedb3e4 100644 --- a/StayAwake/StayAwake.vcxproj +++ b/StayAwake/StayAwake.vcxproj @@ -202,6 +202,7 @@ + @@ -210,6 +211,7 @@ + Create diff --git a/StayAwake/StayAwake.vcxproj.filters b/StayAwake/StayAwake.vcxproj.filters index 8974961..f1f6329 100644 --- a/StayAwake/StayAwake.vcxproj.filters +++ b/StayAwake/StayAwake.vcxproj.filters @@ -36,6 +36,9 @@ Header Files + + Header Files + @@ -50,6 +53,9 @@ Source Files + + Source Files + diff --git a/StayAwake/StayAwakeAboutDlg.cpp b/StayAwake/StayAwakeAboutDlg.cpp new file mode 100644 index 0000000..086033e --- /dev/null +++ b/StayAwake/StayAwakeAboutDlg.cpp @@ -0,0 +1,56 @@ +#include "pch.h" +#include "framework.h" +#include "StayAwake.h" +#include "afxdialogex.h" +#include "Utils.h" +#include "StayAwakeAboutDlg.h" + +#ifdef _DEBUG +#define new DEBUG_NEW +#endif + + +BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx) + ON_NOTIFY(NM_CLICK, IDC_ABOUT_PROD_URL, OnProdUrlClick) + ON_NOTIFY(NM_RETURN, IDC_ABOUT_PROD_URL, OnProdUrlReturn) +END_MESSAGE_MAP() + +CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX) +{ +} + +void CAboutDlg::DoDataExchange(CDataExchange* pDX) +{ + CDialogEx::DoDataExchange(pDX); +} + + +BOOL CAboutDlg::OnInitDialog() +{ + CDialogEx::OnInitDialog(); + +#ifdef _WIN64 + wstring buildBit{ L" (64-bit)" }; +#else + wstring buildBit{ L" (32-bit)" }; +#endif // _WIN64 + + SetDlgItemText(IDC_ABOUT_NAME, Utils::getVersionInfo(theApp.m_hInstance, L"FileDescription").c_str()); + SetDlgItemText(IDC_ABOUT_VERSION, (L"Version: " + Utils::getVersionInfo(theApp.m_hInstance, L"FileVersion") + buildBit).c_str()); + SetDlgItemTextA(m_hWnd, IDC_ABOUT_BUILD_TIME, ("Build time: " + string(__DATE__) + " - " + string(__TIME__)).c_str()); + SetDlgItemText(IDC_ABOUT_ATTRIBUTION, Utils::getVersionInfo(theApp.m_hInstance, L"LegalCopyright").c_str()); + + return TRUE; +} + + +afx_msg void CAboutDlg::OnProdUrlClick(NMHDR* pNotifyStruct, LRESULT* result) +{ + ShellExecute(NULL, L"open", Utils::getVersionInfo(theApp.m_hInstance, L"CompanyName").c_str(), NULL, NULL, SW_SHOW); +} + + +afx_msg void CAboutDlg::OnProdUrlReturn(NMHDR* pNotifyStruct, LRESULT* result) +{ + ShellExecute(NULL, L"open", Utils::getVersionInfo(theApp.m_hInstance, L"CompanyName").c_str(), NULL, NULL, SW_SHOW); +} diff --git a/StayAwake/StayAwakeAboutDlg.h b/StayAwake/StayAwakeAboutDlg.h new file mode 100644 index 0000000..0be751b --- /dev/null +++ b/StayAwake/StayAwakeAboutDlg.h @@ -0,0 +1,18 @@ +#pragma once + +class CAboutDlg : public CDialogEx +{ +public: + CAboutDlg(); + +#ifdef AFX_DESIGN_TIME + enum { IDD = IDD_ABOUTBOX }; +#endif + +protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + virtual BOOL OnInitDialog(); + afx_msg void OnProdUrlClick(NMHDR* pNotifyStruct, LRESULT* result); + afx_msg void OnProdUrlReturn(NMHDR* pNotifyStruct, LRESULT* result); + DECLARE_MESSAGE_MAP() +}; diff --git a/StayAwake/StayAwakeDlg.cpp b/StayAwake/StayAwakeDlg.cpp index a0dd7d1..1baa748 100644 --- a/StayAwake/StayAwakeDlg.cpp +++ b/StayAwake/StayAwakeDlg.cpp @@ -1,11 +1,8 @@ - -// StayAwakeDlg.cpp : implementation file -// - #include "pch.h" #include "framework.h" #include "StayAwake.h" #include "StayAwakeDlg.h" +#include "StayAwakeAboutDlg.h" #include "afxdialogex.h" #include "Utils.h" @@ -14,75 +11,6 @@ #endif -// CAboutDlg dialog used for App About - -class CAboutDlg : public CDialogEx -{ -public: - CAboutDlg(); - -// Dialog Data -#ifdef AFX_DESIGN_TIME - enum { IDD = IDD_ABOUTBOX }; -#endif - -protected: - virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support - virtual BOOL OnInitDialog(); - afx_msg void OnProdUrlClick(NMHDR* pNotifyStruct, LRESULT* result); - afx_msg void OnProdUrlReturn(NMHDR* pNotifyStruct, LRESULT* result); - DECLARE_MESSAGE_MAP() -}; - -BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx) - ON_NOTIFY(NM_CLICK, IDC_ABOUT_PROD_URL, OnProdUrlClick) - ON_NOTIFY(NM_RETURN, IDC_ABOUT_PROD_URL, OnProdUrlReturn) -END_MESSAGE_MAP() - -CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX) -{ -} - -void CAboutDlg::DoDataExchange(CDataExchange* pDX) -{ - CDialogEx::DoDataExchange(pDX); -} - - -BOOL CAboutDlg::OnInitDialog() -{ - CDialogEx::OnInitDialog(); - -#ifdef _WIN64 - wstring buildBit{ L" (64-bit)" }; -#else - wstring buildBit{ L" (32-bit)" }; -#endif // _WIN64 - - SetDlgItemText(IDC_ABOUT_NAME, Utils::getVersionInfo(theApp.m_hInstance, L"FileDescription").c_str()); - SetDlgItemText(IDC_ABOUT_VERSION, (L"Version: " + Utils::getVersionInfo(theApp.m_hInstance, L"FileVersion") + buildBit).c_str()); - SetDlgItemTextA(m_hWnd, IDC_ABOUT_BUILD_TIME, ("Build time: " + string(__DATE__) + " - " + string(__TIME__)).c_str()); - SetDlgItemText(IDC_ABOUT_ATTRIBUTION, Utils::getVersionInfo(theApp.m_hInstance, L"LegalCopyright").c_str()); - - return TRUE; -} - - -afx_msg void CAboutDlg::OnProdUrlClick(NMHDR* pNotifyStruct, LRESULT* result) -{ - ShellExecute(NULL, L"open", Utils::getVersionInfo(theApp.m_hInstance, L"CompanyName").c_str(), NULL, NULL, SW_SHOW); -} - - -afx_msg void CAboutDlg::OnProdUrlReturn(NMHDR* pNotifyStruct, LRESULT* result) -{ - ShellExecute(NULL, L"open", Utils::getVersionInfo(theApp.m_hInstance, L"CompanyName").c_str(), NULL, NULL, SW_SHOW); -} - - -// CStayAwakeDlg dialog - - CStayAwakeDlg::CStayAwakeDlg(CWnd* pParent /*=nullptr*/) : CDialogEx(IDD_STAYAWAKE_DIALOG, pParent) { @@ -96,10 +24,9 @@ void CStayAwakeDlg::DoDataExchange(CDataExchange* pDX) BEGIN_MESSAGE_MAP(CStayAwakeDlg, CDialogEx) ON_WM_SYSCOMMAND() - ON_WM_PAINT() - ON_WM_QUERYDRAGICON() ON_MESSAGE(WM_POST_OPEN, &CStayAwakeDlg::OnPostOpen) ON_MESSAGE(WM_TRAY_NOTIFY, &CStayAwakeDlg::OnTrayNotify) + ON_REGISTERED_MESSAGE(WM_RESTORE_DIALOG, &CStayAwakeDlg::OnRestoreDialog) ON_COMMAND(IDCANCEL, &CStayAwakeDlg::OnCancel) ON_COMMAND(IDM_RESTORE, &CStayAwakeDlg::OnRestore) ON_COMMAND(IDM_EXIT, &CStayAwakeDlg::OnExit) @@ -166,42 +93,6 @@ void CStayAwakeDlg::OnSysCommand(UINT nID, LPARAM lParam) } } -// If you add a minimize button to your dialog, you will need the code below -// to draw the icon. For MFC applications using the document/view model, -// this is automatically done for you by the framework. - -void CStayAwakeDlg::OnPaint() -{ - if (IsIconic()) - { - CPaintDC dc(this); // device context for painting - - SendMessage(WM_ICONERASEBKGND, reinterpret_cast(dc.GetSafeHdc()), 0); - - // Center icon in client rectangle - int cxIcon = GetSystemMetrics(SM_CXICON); - int cyIcon = GetSystemMetrics(SM_CYICON); - CRect rect; - GetClientRect(&rect); - int x = (rect.Width() - cxIcon + 1) / 2; - int y = (rect.Height() - cyIcon + 1) / 2; - - // Draw the icon - dc.DrawIcon(x, y, m_hIcon); - } - else - { - CDialogEx::OnPaint(); - } -} - -// The system calls this function to obtain the cursor to display while the user drags -// the minimized window. -HCURSOR CStayAwakeDlg::OnQueryDragIcon() -{ - return static_cast(m_hIcon); -} - afx_msg LRESULT CStayAwakeDlg::OnPostOpen(WPARAM wParam, LPARAM lParam) { @@ -213,7 +104,8 @@ afx_msg LRESULT CStayAwakeDlg::OnPostOpen(WPARAM wParam, LPARAM lParam) if (wstring{ sMulti } != L"Y" && Utils::getProcessRunCount(L"StayAwake.exe") > 1) { - MessageBox(L"Another instance of StayAwake is already running.\n\nHence this instance will exit."); + ::PostMessage(HWND_BROADCAST, theApp.WM_SHOWFIRSTINSTANCE, 0, 0); + DestroyWindow(); return 0; } @@ -242,6 +134,13 @@ afx_msg LRESULT CStayAwakeDlg::OnTrayNotify(WPARAM wParam, LPARAM lParam) return 0; } +afx_msg LRESULT CStayAwakeDlg::OnRestoreDialog(WPARAM wParam, LPARAM lParam) +{ + RestoreFromTray(); + SetForegroundWindow(); + return 0; +} + void CStayAwakeDlg::OnKillfocusInterval() { diff --git a/StayAwake/StayAwakeDlg.h b/StayAwake/StayAwakeDlg.h index 8c9f51b..fb5041d 100644 --- a/StayAwake/StayAwakeDlg.h +++ b/StayAwake/StayAwakeDlg.h @@ -1,11 +1,9 @@ -// StayAwakeDlg.h : header file -// - #pragma once -#define WM_POST_OPEN (WM_APP + 1) -#define WM_TRAY_NOTIFY (WM_APP + 2) +#define WM_POST_OPEN (WM_APP + 1) +#define WM_TRAY_NOTIFY (WM_APP + 2) +#define WM_RESTORE_DIALOG theApp.WM_SHOWFIRSTINSTANCE constexpr auto PREF_INI_FILE = L".\\StayAwake.ini"; constexpr auto PREF_DEFAULTS = L"Defaults"; @@ -13,14 +11,11 @@ constexpr auto PREF_MULTI_INSTANCE = L"MultipleInstancesAllowed"; constexpr auto PREF_TIMER_INTERVAL = L"TimerIntervalInSeconds"; -// CStayAwakeDlg dialog class CStayAwakeDlg : public CDialogEx { -// Construction public: CStayAwakeDlg(CWnd* pParent = nullptr); // standard constructor -// Dialog Data #ifdef AFX_DESIGN_TIME enum { IDD = IDD_STAYAWAKE_DIALOG }; #endif @@ -29,15 +24,11 @@ class CStayAwakeDlg : public CDialogEx virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support -// Implementation protected: HICON m_hIcon; - // Generated message map functions virtual BOOL OnInitDialog(); afx_msg void OnSysCommand(UINT nID, LPARAM lParam); - afx_msg void OnPaint(); - afx_msg HCURSOR OnQueryDragIcon(); DECLARE_MESSAGE_MAP() private: @@ -47,12 +38,14 @@ class CStayAwakeDlg : public CDialogEx NOTIFYICONDATA m_TrayData{}; CMenu m_menu; - afx_msg LRESULT OnTrayNotify(WPARAM wParam, LPARAM lParam); afx_msg LRESULT OnPostOpen(WPARAM wParam, LPARAM lParam); + afx_msg LRESULT OnTrayNotify(WPARAM wParam, LPARAM lParam); + afx_msg LRESULT OnRestoreDialog(WPARAM wParam, LPARAM lParam); afx_msg void OnTimer(UINT_PTR nIDEvent); afx_msg void OnKillfocusInterval(); afx_msg void OnSetInterval(); afx_msg void OnMinimize(); + afx_msg void OnClickedAboutButton(); afx_msg void OnCancel(); afx_msg void OnRestore(); afx_msg void OnExit(); @@ -64,6 +57,4 @@ class CStayAwakeDlg : public CDialogEx void OnTrayButtonDown(CPoint pt); void RestoreFromTray(); void ToggleScrollLock(); -public: - afx_msg void OnClickedAboutButton(); };