定时关闭MessageBox
MessageBoxTimeout是一个微软未公开的Windows API函数。实现定时消息,功能类似于MessageBox。如果用户不回应,能定时关闭消息框。函数由user32.dll导出,windows2000及以下没有此函数。
直接上代码啦~
#includeint DU_MessageBoxTimeout(HWND hWnd, const WCHAR* sText, const WCHAR* sCaption, UINT uType, DWORD dwMilliseconds) { // Displays a message box, and dismisses it after the specified timeout. typedef int(__stdcall* MSGBOXWAPI)(IN HWND hWnd, IN LPCWSTR lpText, IN LPCWSTR lpCaption, IN UINT uType, IN WORD wLanguageId, IN DWORD dwMilliseconds); int iResult; HMODULE hUser32 = LoadLibraryW(L"user32.dll"); if (hUser32) { auto MessageBoxTimeoutW = (MSGBOXWAPI)GetProcAddress(hUser32, "MessageBoxTimeoutW"); iResult = MessageBoxTimeoutW(hWnd, sText, sCaption, uType, 0, dwMilliseconds); FreeLibrary(hUser32); } else { iResult = MessageBoxW(hWnd, sText, sCaption, uType); // oups, fallback to the standard function! } return iResult; } int main() { // Timeout MessageBox DU_MessageBoxTimeout(nullptr, L"PAUSE", L"Pause", MB_OK | MB_IConERROR | MB_SETFOREGROUND, 10000); // Normal MessageBox HMODULE user32 = LoadLibraryW(L"user32.dll"); if (user32) { decltype(MessageBoxW)* messageBoxW = (decltype(MessageBoxW)*)GetProcAddress(user32, "MessageBoxW"); if (messageBoxW) { messageBoxW(nullptr, L"PAUSE AGAIN", L"Pause", MB_OK | MB_IConERROR | MB_SETFOREGROUND); } FreeLibrary(user32); } return 0; }



