windows 延时退出弹出窗口

1)
http://www.codeproject.com/KB/dialog/delaymessagebox.aspx
 
2)
http://www.codeproject.com/KB/dialog/AutoCloseMessageBox.aspx
 
3)
过一段时间消失的MessageBox(源自MSDN)
void CALLBACK MessageBoxTimer(HWND hwnd, UINT uiMsg, UINT idEvent, DWORD dwTime)
{
   PostQuitMessage(0);
}
UINT TimedMessageBox(HWND hwndParent, LPCTSTR ptszMessage, LPCTSTR ptszTitle, UINT flags, DWORD dwTimeout)
{
   MSG msg;
   UINT idTimer = SetTimer(NULL, 0, dwTimeout, (TIMERPROC)MessageBoxTimer);
   UINT uiResult = MessageBox(hwndParent, ptszMessage, ptszTitle, flags);
   KillTimer(NULL, idTimer);
   if (PeekMessage(&msg, NULL, WM_QUIT, WM_QUIT, PM_REMOVE)) {
       uiResult = 0;//默认值
   }
   return uiResult;
}
使用方法同MessageBox,只是多了个DWORD参数 dwTimeout
 
4)微软以前未公开的函数
 
#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 <--Call the function as follows:
 
//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);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值