From c4f3936d981cc6267203c6e5bf90f3f9920ffe9c Mon Sep 17 00:00:00 2001 From: Quantum Date: Sun, 14 Sep 2025 15:19:14 -0400 Subject: [PATCH] [idd] helper: split CWindow into base class and CNotifyWindow --- idd/LGIddHelper/CNotifyWindow.cpp | 97 +++++++++++++++++++++ idd/LGIddHelper/CNotifyWindow.h | 24 +++++ idd/LGIddHelper/CWindow.cpp | 73 +--------------- idd/LGIddHelper/CWindow.h | 23 ++--- idd/LGIddHelper/LGIddHelper.vcxproj | 2 + idd/LGIddHelper/LGIddHelper.vcxproj.filters | 15 ++++ idd/LGIddHelper/main.cpp | 6 +- 7 files changed, 152 insertions(+), 88 deletions(-) create mode 100644 idd/LGIddHelper/CNotifyWindow.cpp create mode 100644 idd/LGIddHelper/CNotifyWindow.h diff --git a/idd/LGIddHelper/CNotifyWindow.cpp b/idd/LGIddHelper/CNotifyWindow.cpp new file mode 100644 index 00000000..a03a4989 --- /dev/null +++ b/idd/LGIddHelper/CNotifyWindow.cpp @@ -0,0 +1,97 @@ +#include "CNotifyWindow.h" +#include +#include +#include + +#define ID_MENU_SHOW_LOG 3000 + +ATOM CNotifyWindow::s_atom = 0; +UINT CNotifyWindow::s_taskbarCreated = 0; + +bool CNotifyWindow::registerClass() +{ + s_taskbarCreated = RegisterWindowMessage(L"TaskbarCreated"); + if (!s_taskbarCreated) + DEBUG_WARN_HR(GetLastError(), "RegisterWindowMessage(TaskbarCreated)"); + + WNDCLASSEX wx = {}; + populateWindowClass(wx); + wx.lpszClassName = L"LookingGlassIddHelper"; + + s_atom = RegisterClassEx(&wx); + return s_atom; +} + +CNotifyWindow::CNotifyWindow() : m_iconData({ 0 }), m_menu(CreatePopupMenu()) +{ + CreateWindowEx(0, MAKEINTATOM(s_atom), NULL, + 0, 0, 0, 0, 0, NULL, NULL, hInstance, this); + + if (m_menu) + { + AppendMenu(m_menu, MF_STRING, ID_MENU_SHOW_LOG, L"Open log directory"); + } +} + +CNotifyWindow::~CNotifyWindow() +{ + DestroyMenu(m_menu); +} + +LRESULT CNotifyWindow::handleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam) +{ + switch (uMsg) + { + case WM_NCDESTROY: + PostQuitMessage(0); + return 0; + case WM_NOTIFY_ICON: + return onNotifyIcon(LOWORD(lParam), HIWORD(lParam), GET_X_LPARAM(wParam), GET_Y_LPARAM(wParam)); + default: + if (s_taskbarCreated && uMsg == s_taskbarCreated) + { + registerIcon(); + return 0; + } + return CWindow::handleMessage(uMsg, wParam, lParam); + } +} + +LRESULT CNotifyWindow::onCreate() +{ + registerIcon(); + return 0; +} + +LRESULT CNotifyWindow::onNotifyIcon(UINT uEvent, WORD wIconId, int x, int y) +{ + switch (uEvent) + { + case WM_CONTEXTMENU: + switch (TrackPopupMenu(m_menu, TPM_RETURNCMD | TPM_NONOTIFY, x, y, 0, m_hwnd, NULL)) + { + case ID_MENU_SHOW_LOG: + ShellExecute(m_hwnd, L"open", g_debug.logDir(), NULL, NULL, SW_NORMAL); + break; + } + break; + } + return 0; +} + +void CNotifyWindow::registerIcon() +{ + m_iconData.cbSize = sizeof m_iconData; + m_iconData.hWnd = m_hwnd; + m_iconData.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP; + m_iconData.uCallbackMessage = WM_NOTIFY_ICON; + m_iconData.hIcon = LoadIcon(hInstance, IDI_APPLICATION); + m_iconData.uVersion = NOTIFYICON_VERSION_4; + StringCbCopy(m_iconData.szTip, sizeof m_iconData.szTip, L"Looking Glass (IDD)"); + + if (!Shell_NotifyIcon(NIM_ADD, &m_iconData)) + DEBUG_ERROR_HR(GetLastError(), "Shell_NotifyIcon(NIM_ADD)"); + + if (!Shell_NotifyIcon(NIM_SETVERSION, &m_iconData)) + DEBUG_ERROR_HR(GetLastError(), "Shell_NotifyIcon(NIM_SETVERSION)"); +} diff --git a/idd/LGIddHelper/CNotifyWindow.h b/idd/LGIddHelper/CNotifyWindow.h new file mode 100644 index 00000000..e7186c80 --- /dev/null +++ b/idd/LGIddHelper/CNotifyWindow.h @@ -0,0 +1,24 @@ +#pragma once +#include "CWindow.h" + +#define WM_NOTIFY_ICON (WM_USER) + +class CNotifyWindow : public CWindow +{ + static UINT s_taskbarCreated; + static ATOM s_atom; + + NOTIFYICONDATA m_iconData; + HMENU m_menu; + + LRESULT onNotifyIcon(UINT uEvent, WORD wIconId, int x, int y); + void registerIcon(); + + virtual LRESULT handleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam) override; + virtual LRESULT onCreate() override; + +public: + CNotifyWindow(); + ~CNotifyWindow() override; + static bool registerClass(); +}; diff --git a/idd/LGIddHelper/CWindow.cpp b/idd/LGIddHelper/CWindow.cpp index c37f29df..af5f668e 100644 --- a/idd/LGIddHelper/CWindow.cpp +++ b/idd/LGIddHelper/CWindow.cpp @@ -3,41 +3,17 @@ #include #include -#define ID_MENU_SHOW_LOG 3000 +HINSTANCE CWindow::hInstance = (HINSTANCE)GetModuleHandle(NULL); -ATOM CWindow::s_atom = 0; -UINT CWindow::s_taskbarCreated = 0; -static HINSTANCE hInstance = (HINSTANCE)GetModuleHandle(NULL); - -bool CWindow::registerClass() +void CWindow::populateWindowClass(WNDCLASSEX &wx) { - s_taskbarCreated = RegisterWindowMessage(L"TaskbarCreated"); - if (!s_taskbarCreated) - DEBUG_WARN_HR(GetLastError(), "RegisterWindowMessage(TaskbarCreated)"); - - WNDCLASSEX wx = {}; wx.cbSize = sizeof(WNDCLASSEX); wx.lpfnWndProc = wndProc; wx.hInstance = hInstance; - wx.lpszClassName = L"LookingGlassIddHelper"; wx.hIcon = LoadIcon(NULL, IDI_APPLICATION); wx.hIconSm = LoadIcon(NULL, IDI_APPLICATION); wx.hCursor = LoadCursor(NULL, IDC_ARROW); wx.hbrBackground = (HBRUSH)COLOR_APPWORKSPACE; - - s_atom = RegisterClassEx(&wx); - return s_atom; -} - -CWindow::CWindow() : m_iconData({ 0 }), m_menu(CreatePopupMenu()) -{ - CreateWindowEx(0, MAKEINTATOM(s_atom), NULL, - 0, 0, 0, 0, 0, NULL, NULL, hInstance, this); - - if (m_menu) - { - AppendMenu(m_menu, MF_STRING, ID_MENU_SHOW_LOG, L"Open log directory"); - } } LRESULT CWindow::wndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) @@ -67,60 +43,16 @@ LRESULT CWindow::handleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam) { case WM_CREATE: return onCreate(); - case WM_NCDESTROY: - PostQuitMessage(0); - return 0; - case WM_NOTIFY_ICON: - return onNotifyIcon(LOWORD(lParam), HIWORD(lParam), GET_X_LPARAM(wParam), GET_Y_LPARAM(wParam)); default: - if (s_taskbarCreated && uMsg == s_taskbarCreated) - { - registerIcon(); - return 0; - } return DefWindowProc(m_hwnd, uMsg, wParam, lParam); } } LRESULT CWindow::onCreate() { - registerIcon(); return 0; } -LRESULT CWindow::onNotifyIcon(UINT uEvent, WORD wIconId, int x, int y) -{ - switch (uEvent) - { - case WM_CONTEXTMENU: - switch (TrackPopupMenu(m_menu, TPM_RETURNCMD | TPM_NONOTIFY, x, y, 0, m_hwnd, NULL)) - { - case ID_MENU_SHOW_LOG: - ShellExecute(m_hwnd, L"open", g_debug.logDir(), NULL, NULL, SW_NORMAL); - break; - } - break; - } - return 0; -} - -void CWindow::registerIcon() -{ - m_iconData.cbSize = sizeof m_iconData; - m_iconData.hWnd = m_hwnd; - m_iconData.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP; - m_iconData.uCallbackMessage = WM_NOTIFY_ICON; - m_iconData.hIcon = LoadIcon(hInstance, IDI_APPLICATION); - m_iconData.uVersion = NOTIFYICON_VERSION_4; - StringCbCopy(m_iconData.szTip, sizeof m_iconData.szTip, L"Looking Glass (IDD)"); - - if (!Shell_NotifyIcon(NIM_ADD, &m_iconData)) - DEBUG_ERROR_HR(GetLastError(), "Shell_NotifyIcon(NIM_ADD)"); - - if (!Shell_NotifyIcon(NIM_SETVERSION, &m_iconData)) - DEBUG_ERROR_HR(GetLastError(), "Shell_NotifyIcon(NIM_SETVERSION)"); -} - void CWindow::destroy() { if (m_hwnd) @@ -133,5 +65,4 @@ void CWindow::destroy() CWindow::~CWindow() { destroy(); - DestroyMenu(m_menu); } diff --git a/idd/LGIddHelper/CWindow.h b/idd/LGIddHelper/CWindow.h index 924ccc98..594ebd8e 100644 --- a/idd/LGIddHelper/CWindow.h +++ b/idd/LGIddHelper/CWindow.h @@ -4,26 +4,21 @@ #include #include -#define WM_NOTIFY_ICON (WM_USER) - class CWindow { - static ATOM s_atom; - static UINT s_taskbarCreated; static LRESULT CALLBACK wndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam); - HWND m_hwnd = NULL; - NOTIFYICONDATA m_iconData; - HMENU m_menu; + virtual LRESULT onCreate(); - LRESULT handleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam); - LRESULT onCreate(); - LRESULT onNotifyIcon(UINT uEvent, WORD wIconId, int x, int y); - void registerIcon(); +protected: + static HINSTANCE hInstance; + static void populateWindowClass(WNDCLASSEX &wx); + + virtual LRESULT handleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam); + + HWND m_hwnd = NULL; public: - static bool registerClass(); - CWindow(); - ~CWindow(); + virtual ~CWindow(); void destroy(); HWND hwnd() { return m_hwnd; } diff --git a/idd/LGIddHelper/LGIddHelper.vcxproj b/idd/LGIddHelper/LGIddHelper.vcxproj index 247a73e7..78c52ffa 100644 --- a/idd/LGIddHelper/LGIddHelper.vcxproj +++ b/idd/LGIddHelper/LGIddHelper.vcxproj @@ -177,12 +177,14 @@ copy /Y "$(ProjectDir)VERSION" "$(SolutionDir)$(Platform)\$(Configuration)\LGIdd + + diff --git a/idd/LGIddHelper/LGIddHelper.vcxproj.filters b/idd/LGIddHelper/LGIddHelper.vcxproj.filters index cba02d1d..bebc45bc 100644 --- a/idd/LGIddHelper/LGIddHelper.vcxproj.filters +++ b/idd/LGIddHelper/LGIddHelper.vcxproj.filters @@ -22,12 +22,27 @@ Source Files + + + Source Files + + + Source Files + Header Files + + + + Header Files + + + Header Files + diff --git a/idd/LGIddHelper/main.cpp b/idd/LGIddHelper/main.cpp index 77a9567f..a1fa63f8 100644 --- a/idd/LGIddHelper/main.cpp +++ b/idd/LGIddHelper/main.cpp @@ -12,7 +12,7 @@ using namespace Microsoft::WRL::Wrappers::HandleTraits; #include "CDebug.h" #include "VersionInfo.h" #include "CPipeClient.h" -#include "CWindow.h" +#include "CNotifyWindow.h" #define ARRAY_SIZE(x) (sizeof(x) / sizeof*(x)) #define SVCNAME L"Looking Glass (IDD Helper)" @@ -73,13 +73,13 @@ int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _ return EXIT_FAILURE; } - if (!CWindow::registerClass()) + if (!CNotifyWindow::registerClass()) { DEBUG_ERROR("Failed to register message window class"); return EXIT_FAILURE; } - CWindow window; + CNotifyWindow window; if (!g_pipe.Init()) {