关于弹出消息窗口的自动关闭
2016-07-14 11:50
567 查看
1.方法1
1.1. 直接在代码中添加
1.2. 调用
MessageBoxTimeout(this->GetSafeHwnd(), _T("弹出5秒后会自动关闭!这是一个模态对话框。"), _T("会自动关闭的MessageBox"), MB_ICONINFORMATION, GetSystemDefaultLangID(), 5000);
//参数说明:父窗口的句柄为NULL的情况下,将弹出非模态对话框;延时关闭的时间为0的情况下,弹出的MessageBox需要手动关闭
MessageBoxTimeout(NULL, _T("弹出5秒后会自动关闭!这是一个非模态对话框。"), _T("会自动关闭的MessageBox"), MB_ICONINFORMATION, GetSystemDefaultLangID(), 5000);
参考网址:http://blog.sina.com.cn/s/blog_4b0f3b420100mglb.html
win7 x86,vs2013,测试通过
2.方法2
2.1.此api是微软的一个未公开api,在user32.dll中,功能就是弹出一个对话框MessageBox,并定时自动退出。
下面为头文件,随便取个名字,我取的是MsgBoxTimeout.h 。
2.2. 调用方式
参考网址:http://blog.csdn.net/a379039233/article/details/49445207
1.1. 直接在代码中添加
// 弹出消息窗口自动关闭,需要指出的是,Windows 2000的user32.dll没有导出这个函数。 extern "C" { int WINAPI MessageBoxTimeoutA(IN HWND hWnd, IN LPCSTR lpText, IN LPCSTR lpCaption, IN UINT uType, IN WORD wLanguageId, IN DWORD dwMilliseconds); int WINAPI MessageBoxTimeoutW(IN HWND hWnd, IN LPCWSTR lpText, IN LPCWSTR lpCaption, IN UINT uType, IN WORD wLanguageId, IN DWORD dwMilliseconds); }; #ifdef UNICODE #define MessageBoxTimeout MessageBoxTimeoutW #else #define MessageBoxTimeout MessageBoxTimeoutA #endif
1.2. 调用
MessageBoxTimeout(this->GetSafeHwnd(), _T("弹出5秒后会自动关闭!这是一个模态对话框。"), _T("会自动关闭的MessageBox"), MB_ICONINFORMATION, GetSystemDefaultLangID(), 5000);
//参数说明:父窗口的句柄为NULL的情况下,将弹出非模态对话框;延时关闭的时间为0的情况下,弹出的MessageBox需要手动关闭
MessageBoxTimeout(NULL, _T("弹出5秒后会自动关闭!这是一个非模态对话框。"), _T("会自动关闭的MessageBox"), MB_ICONINFORMATION, GetSystemDefaultLangID(), 5000);
参考网址:http://blog.sina.com.cn/s/blog_4b0f3b420100mglb.html
win7 x86,vs2013,测试通过
2.方法2
2.1.此api是微软的一个未公开api,在user32.dll中,功能就是弹出一个对话框MessageBox,并定时自动退出。
下面为头文件,随便取个名字,我取的是MsgBoxTimeout.h 。
#include <windows.h> #include <tchar.h> //Functions & other definitions required--> typedef int (__stdcall *MSGBOXAAPI)(IN HWND hWnd, IN LPCSTR lpText, IN LPCSTR lpCaption, IN UINT uType, IN WORD wLanguageId, IN DWORD dwMilliseconds); typedef int (__stdcall *MSGBOXWAPI)(IN HWND hWnd, IN LPCWSTR lpText, IN LPCWSTR lpCaption, IN UINT uType, IN WORD wLanguageId, IN DWORD dwMilliseconds); int MessageBoxTimeoutA(IN HWND hWnd, IN LPCSTR lpText, IN LPCSTR lpCaption, IN UINT uType, IN WORD wLanguageId, IN DWORD dwMilliseconds); int MessageBoxTimeoutW(IN HWND hWnd, IN LPCWSTR lpText, IN LPCWSTR lpCaption, IN UINT uType, IN WORD wLanguageId, IN DWORD dwMilliseconds); #ifdef UNICODE #define MessageBoxTimeout MessageBoxTimeoutW #else #define MessageBoxTimeout MessageBoxTimeoutA #endif #define MB_TIMEDOUT 32000 int MessageBoxTimeoutA(HWND hWnd, LPCSTR lpText, LPCSTR lpCaption, UINT uType, WORD wLanguageId, DWORD dwMilliseconds) { static MSGBOXAAPI MsgBoxTOA = NULL; if (!MsgBoxTOA) { HMODULE hUser32 = GetModuleHandle(_T("user32.dll")); if (hUser32) { MsgBoxTOA = (MSGBOXAAPI)GetProcAddress(hUser32, "MessageBoxTimeoutA"); //fall through to 'if (MsgBoxTOA)...' } else { //stuff happened, add code to handle it here //(possibly just call MessageBox()) return 0; } } if (MsgBoxTOA) { return MsgBoxTOA(hWnd, lpText, lpCaption, uType, wLanguageId, dwMilliseconds); } return 0; } int MessageBoxTimeoutW(HWND hWnd, LPCWSTR lpText, LPCWSTR lpCaption, UINT uType, WORD wLanguageId, DWORD dwMilliseconds) { static MSGBOXWAPI MsgBoxTOW = NULL; if (!MsgBoxTOW) { HMODULE hUser32 = GetModuleHandle(_T("user32.dll")); if (hUser32) { MsgBoxTOW = (MSGBOXWAPI)GetProcAddress(hUser32, "MessageBoxTimeoutW"); //fall through to 'if (MsgBoxTOW)...' } else { //stuff happened, add code to handle it here //(possibly just call MessageBox()) return 0; } } if (MsgBoxTOW) { return MsgBoxTOW(hWnd, lpText, lpCaption, uType, wLanguageId, dwMilliseconds); } return 0; } //End required definitions <--
2.2. 调用方式
#include "MsgBoxTimeout.h" void CTestDlg::OnBnClickedButton2() { //you must load user32.dll before calling the function HMODULE hUser32 = LoadLibrary(_T("user32.dll")); if (hUser32) { int iRet = 0; UINT uiFlags = MB_OK | MB_SETFOREGROUND | MB_SYSTEMMODAL | MB_ICONINFORMATION; iRet = MessageBoxTimeout(NULL, _T("Test a timeout of 2 seconds."), _T("MessageBoxTimeout Test"), uiFlags, 0, 2000); //iRet will = 1 uiFlags = MB_YESNO | MB_SETFOREGROUND | MB_SYSTEMMODAL | MB_ICONINFORMATION; iRet = MessageBoxTimeout(NULL, _T("Test a timeout of 5 seconds."), _T("MessageBoxTimeout Test"), uiFlags, 0, 5000); //iRet will = MB_TIMEDOUT if no buttons pressed, button values otherwise //only unload user32.dll when you have no further need //for the MessageBoxTimeout function FreeLibrary(hUser32); } }
参考网址:http://blog.csdn.net/a379039233/article/details/49445207
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 如何重装TCP/IP协议
- 关于指针的一些事情
- Windows 8 官方高清壁纸欣赏与下载
- 微软无线镭射简报鲨8000激光笔记本鼠标 - (2)
- 谁是桌面王者?Win PK Linux三大镇山之宝
- 对《大家都在点赞 Windows Terminal,我决定给你泼一盆冷水》一文的商榷
- Windows Clang开发环境备忘
- 从Windows系统下访问Linux分区相关软件
- 对《大家都在点赞 Windows Terminal,我决定给你泼一盆冷水》一文的商榷
- Windows下搭建本地SVN服务器
- 使用Windows原生命令一键清空剪贴板
- c++ primer 第五版 笔记前言
- windows用windeployqt发布qt quick application程序
- 利用开源软件打造自己的全功能远程工具
- Windows 8虚拟机不能全屏的解决方法