From 7138a921143c801ef42f4190b2242ccce38ff8a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Sun, 31 Jul 2022 00:15:31 +0200 Subject: Move EBMessageBox's CBTProc into lambda. --- c/common.cpp | 50 +++++++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/c/common.cpp b/c/common.cpp index 614bfeb..77d669c 100644 --- a/c/common.cpp +++ b/c/common.cpp @@ -50,7 +50,7 @@ wstring_owner WsoFromSz(const char* const sz, const int iCp) wchar_t* wsz = new wchar_t[cchWideChar]; if (!MultiByteToWideChar(iCp, 0, sz, cbMultiByte, wsz, cchWideChar)) { delete wsz; - throw Win32Error(GetLastError()); + throw Win32Error(); } return wsz; } @@ -108,34 +108,34 @@ Library::~Library() FreeLibrary(m_hModule); } -/* Move message box to center of main window. */ -static LRESULT CALLBACK CBTProc(const int nCode, const WPARAM wParam, const LPARAM lParam) noexcept -{ - extern HWND g_hWnd; - if (!g_hWnd || nCode < 0 || nCode != HCBT_ACTIVATE) - return CallNextHookEx(0, nCode, wParam, lParam); - - HWND hWnd = (HWND)wParam; - long lStyle = GetWindowLong(hWnd, GWL_STYLE); - if (!(lStyle & WS_POPUP)) return 0; - - RECT rcMain, rcMsg; - GetWindowRect(g_hWnd, &rcMain); - GetWindowRect(hWnd, &rcMsg); - SetWindowPos(hWnd, NULL, - rcMain.left+(rcMain.right-rcMain.left)/2-(rcMsg.right-rcMsg.left)/2, - rcMain.top+(rcMain.bottom-rcMain.top)/2-(rcMsg.bottom-rcMsg.top)/2, - -1, -1, - SWP_NOZORDER|SWP_NOSIZE|SWP_NOACTIVATE); - - return 0; -} - /* Show message box owned by and centered in the main window. */ int EBMessageBox(const wchar_t* const wszText, const wchar_t* const wszCaption, const unsigned uType) { extern HWND g_hWnd; - HHOOK hHook = require(SetWindowsHookEx(WH_CBT, CBTProc, (HINSTANCE)NULL, GetCurrentThreadId())); + + auto proc = [](const int nCode, const WPARAM wParam, const LPARAM lParam) noexcept + -> LRESULT CALLBACK + { + if (!g_hWnd || nCode < 0 || nCode != HCBT_ACTIVATE) + return CallNextHookEx(0, nCode, wParam, lParam); + + HWND hWnd = (HWND)wParam; + long lStyle = GetWindowLong(hWnd, GWL_STYLE); + if (!(lStyle & WS_POPUP)) return 0; + + RECT rcMain, rcMsg; + GetWindowRect(g_hWnd, &rcMain); + GetWindowRect(hWnd, &rcMsg); + SetWindowPos(hWnd, NULL, + rcMain.left+(rcMain.right-rcMain.left)/2-(rcMsg.right-rcMsg.left)/2, + rcMain.top+(rcMain.bottom-rcMain.top)/2-(rcMsg.bottom-rcMsg.top)/2, + -1, -1, + SWP_NOZORDER|SWP_NOSIZE|SWP_NOACTIVATE); + + return 0; + }; + + HHOOK hHook = require(SetWindowsHookEx(WH_CBT, proc, (HINSTANCE)NULL, GetCurrentThreadId())); int r = MessageBox(g_hWnd, wszText, wszCaption, uType); require(UnhookWindowsHookEx(hHook)); return r; -- cgit v1.2.3