MessageBox倒计时问题
我在Time1里面用了倒计时MessageBox
HHOOK hook=NULL;
int MSGRET=0;
int timek=0;
HWND MSGHWND,TEXTHWND;
UINT TD;
String MessageBoxString="";
void CALLBACK TimerProc(HWND hwnd,UINT uMsg,UINT idEvent, DWORD dwTime )
{
if (timek==0)
{
timek=30;
SendMessageA(MSGHWND,WM_COMMAND,7,0); //计时结束后传回的值
}
else
{
timek--;
char title[100]={0};
sprintf(title,(MessageBoxString+"\n退出时间还剩: %d 秒").c_str(),timek);
SetWindowText(TEXTHWND,title);
}
}
LRESULT CALLBACK CBTProc(
int nCode, // hook code
WPARAM wParam, // depends on hook code
LPARAM lParam // depends on hook code
)
{
if (nCode==WH_CBT)
{
UnhookWindowsHookEx(hook);
timek=30;
MSGHWND=(HWND)wParam;
TEXTHWND=GetDlgItem(MSGHWND,65535);
TD=SetTimer(0,1,1000,(TIMERPROC)TimerProc);
}
return 0;
}
//----------------------
上面是倒计时实现回调函数
下面是time1的代码
//-----------------------
void __fastcall TGDFast::Timer1Timer(TObject *Sender)
{
Timer1->Enabled=false;
MessageBox(this->Handle,(MessageBoxString+"\n退出时间还剩: 30 秒").c_str(),"新增提示",MB_YESNO+MB_ICONWARNING+MB_DEFBUTTON2);
Timer1->Interval=10000;
Timer1->Enabled=true;
}
问题如下:第一次显示倒计时是正确的,但是第二次开始倒计时开始变得步长变成2,第三次步长是4,越来越快。请问如何解决,是不是没有killtime或者是没有初始化。请给出详细代码和解释,谢谢
[解决办法]
先试试API:MessageBoxTimeout能否满足你需求?
[解决办法]
转发一下,只是MessageBoxTimeout无法显示还剩几秒钟,如果直接新建一个Dialog加Timer也很简单快捷
#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 <--