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 +++++++++++++++++++++++++++++++++++++++++++++ c/common.h | 3 +++ c/main.cpp | 14 ++++++++------ 3 files changed, 56 insertions(+), 6 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; +} diff --git a/c/common.h b/c/common.h index a4f1fd4..9d14b1d 100644 --- a/c/common.h +++ b/c/common.h @@ -11,6 +11,9 @@ #define WA "A" #endif +int EBMessageBox(const TCHAR* tszText, const TCHAR* tszCaption, unsigned uType); +int EBMessageBoxA(const char* szText, const char* szCaption, unsigned uType); + struct Win32Error : public std::exception { Win32Error(DWORD dwErr); diff --git a/c/main.cpp b/c/main.cpp index 08e6b96..f7b3472 100644 --- a/c/main.cpp +++ b/c/main.cpp @@ -143,10 +143,10 @@ int WINAPI WinMain(const HINSTANCE hInstance, const HINSTANCE, char* const, cons return 0; } -static LRESULT CALLBACK CBTProc(const int nCode, const WPARAM wParam, const LPARAM) +static LRESULT CALLBACK CBTProc(const int nCode, const WPARAM wParam, const LPARAM lParam) { - if (nCode != HCBT_CREATEWND) return 0; - if (g_hWnd) return 0; + if (nCode < 0 || nCode != HCBT_CREATEWND || g_hWnd) + return CallNextHookEx(0, nCode, wParam, lParam); /* This code is run once at the creation of the top-level * window -- before WndProc! This is important, as it @@ -198,7 +198,7 @@ LRESULT CALLBACK WndProc(const HWND hWnd, const UINT uMsg, const WPARAM wParam, case WM_CREATE: { UpdateTheme(); - SetWindowPos(hWnd, NULL, -1, -1, Dpi(510), Dpi(400), SWP_NOMOVE); + SetWindowPos(hWnd, NULL, -1, -1, Dpi(510), Dpi(400), SWP_NOZORDER|SWP_NOMOVE|SWP_NOACTIVATE); SetFocus(g_pElv->hWnd); /* Set menu item checkmarks according to saved settings. */ @@ -239,7 +239,7 @@ LRESULT CALLBACK WndProc(const HWND hWnd, const UINT uMsg, const WPARAM wParam, lpr->left, lpr->top, lpr->right-lpr->left, lpr->bottom-lpr->top, - SWP_NOZORDER | SWP_NOACTIVATE); + SWP_NOZORDER|SWP_NOACTIVATE); UpdateLayout(); break; } @@ -387,7 +387,9 @@ LRESULT CALLBACK WndProc(const HWND hWnd, const UINT uMsg, const WPARAM wParam, switch (LOWORD(wParam)) { case IDM_WATCH_LOCALLY: - Pl("local_episode","open_episode_locally",lvi.lParam); + if (!Pl("local_episode","open_episode_locally",lvi.lParam)) + EBMessageBox(TEXT("Local episode could not be opened."), + TEXT("Error"), MB_ICONWARNING); break; case IDM_WATCH_ONLINE: Pl("local_episode","open_episode_online",lvi.lParam); -- cgit v1.2.3