From 2a7f4bcd4c04d0e48e580c4b11a94174bc5b09ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Tue, 19 Jul 2022 11:56:59 +0200 Subject: Implement centered message box. --- c/common.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'c/common.cpp') diff --git a/c/common.cpp b/c/common.cpp index d503133..2259b69 100644 --- a/c/common.cpp +++ b/c/common.cpp @@ -57,3 +57,48 @@ 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) +{ + extern HWND g_hWnd; + if (!g_hWnd || nCode < 0 || nCode != HCBT_ACTIVATE) + return CallNextHookEx(0, nCode, wParam, lParam); + + HWND hWnd = (HWND)wParam; + if (long lStyle = GetWindowLong(hWnd, GWL_STYLE)) + if (lStyle & WS_POPUP) { + 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; +} + +int EBMessageBox(const TCHAR* const tszText, const TCHAR* const tszCaption, const unsigned uType) +{ + extern HWND g_hWnd; + HHOOK hHook = SetWindowsHookEx(WH_CBT, CBTProc, 0, GetCurrentThreadId()); + if (!hHook) return 0; + MessageBox(g_hWnd, tszText, tszCaption, uType); + UnhookWindowsHookEx(hHook); + if (!hHook) throw Win32Error(GetLastError()); + return 0; +} + +int EBMessageBoxA(const char* const szText, const char* const szCaption, const unsigned uType) +{ + extern HWND g_hWnd; + HHOOK hHook = SetWindowsHookEx(WH_CBT, CBTProc, 0, GetCurrentThreadId()); + if (!hHook) return 0; + MessageBoxA(g_hWnd, szText, szCaption, uType); + UnhookWindowsHookEx(hHook); + if (!hHook) throw Win32Error(GetLastError()); + return 0; +} -- cgit v1.2.3