aboutsummaryrefslogtreecommitdiff
path: root/c/common.cpp
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2022-07-19 11:56:59 +0200
committerJohn Ankarström <john@ankarstrom.se>2022-07-19 11:56:59 +0200
commit2a7f4bcd4c04d0e48e580c4b11a94174bc5b09ae (patch)
treea0286659c4dbf55c068ed6f3d172a56d53c9c27c /c/common.cpp
parent53abfac6acd04dff80ec6c8cfb0dd53f2f74dadb (diff)
downloadEpisodeBrowser-2a7f4bcd4c04d0e48e580c4b11a94174bc5b09ae.tar.gz
Implement centered message box.
Diffstat (limited to 'c/common.cpp')
-rw-r--r--c/common.cpp45
1 files changed, 45 insertions, 0 deletions
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;
+}