栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++ MessageBox 定时关闭

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++ MessageBox 定时关闭

定时关闭MessageBox
MessageBoxTimeout是一个微软未公开的Windows API函数。实现定时消息,功能类似于MessageBox。如果用户不回应,能定时关闭消息框。函数由user32.dll导出,windows2000及以下没有此函数。
直接上代码啦~

#include 

int 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;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/430008.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号