Skip to content

Commit

Permalink
Restore dialog from tray when blocking multiple app instances
Browse files Browse the repository at this point in the history
Also, clean up unnecessary code added by MFC App Wizard
  • Loading branch information
shriprem committed Sep 25, 2024
1 parent 51ad349 commit 3313591
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 201 deletions.
63 changes: 0 additions & 63 deletions StayAwake/StayAwake.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@

// StayAwake.cpp : Defines the class behaviors for the application.
//

#include "pch.h"
#include "framework.h"
#include "StayAwake.h"
Expand All @@ -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;
}

13 changes: 2 additions & 11 deletions StayAwake/StayAwake.h
Original file line number Diff line number Diff line change
@@ -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()
};

Expand Down
2 changes: 2 additions & 0 deletions StayAwake/StayAwake.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@
<ItemGroup>
<ClInclude Include="framework.h" />
<ClInclude Include="StayAwake.h" />
<ClInclude Include="StayAwakeAboutDlg.h" />
<ClInclude Include="StayAwakeDlg.h" />
<ClInclude Include="pch.h" />
<ClInclude Include="Resource.h" />
Expand All @@ -210,6 +211,7 @@
</ItemGroup>
<ItemGroup>
<ClCompile Include="StayAwake.cpp" />
<ClCompile Include="StayAwakeAboutDlg.cpp" />
<ClCompile Include="StayAwakeDlg.cpp" />
<ClCompile Include="pch.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
Expand Down
6 changes: 6 additions & 0 deletions StayAwake/StayAwake.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
<ClInclude Include="Utils.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="StayAwakeAboutDlg.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="StayAwake.cpp">
Expand All @@ -50,6 +53,9 @@
<ClCompile Include="Utils.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="StayAwakeAboutDlg.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="StayAwake.rc">
Expand Down
56 changes: 56 additions & 0 deletions StayAwake/StayAwakeAboutDlg.cpp
Original file line number Diff line number Diff line change
@@ -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);
}
18 changes: 18 additions & 0 deletions StayAwake/StayAwakeAboutDlg.h
Original file line number Diff line number Diff line change
@@ -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()
};
Loading

0 comments on commit 3313591

Please sign in to comment.