要在几秒钟后自动关闭消息框,或者如何将计时器添加到消息框 [英] To close a Messagebox after few seconds automatically OR How to add timer to messagebox
问题描述
大家好..
在WIN32中,我创建了这样的消息框:
MessageBox(NULL,初始化软件",初始化",MB_OK);
我的问题是:如何在几秒钟后自动关闭此消息框,而无需按确定"按钮..
请给我建议.
在此先谢谢您..
Hello Everyone..
In WIN32, I have created the Messagebox like this:
MessageBox(NULL,"Initilizing the Software","Initilization",MB_OK);
And my question is: how do i close this Messagebox after few seconds automatically, without pressing OK button..
Please suggest me..
Thanks in advance..
推荐答案
,您可以按预期工作.但这将是显示进度的非常奇怪的方法.您必须创建一个separte线程来处理关闭消息框的消息.因为消息框是模式对话框.一旦调用,不关闭就无法执行下一行代码.因此,首先您必须编写一个线程函数.在该代码中,如下所示获取消息框窗口句柄,然后将其关闭
you can get this thing work as you expect. But it is going to be very weird method to show progress. You have to create a separte thread to handle the message box closing stuffs. Because a message box is of a modal dialog. Once invoked, without closing you can not execute next line of code. So first you have to write a thread function. In that Write the code to get the message box window handle as below and then close it
UINT ThreadFun(LPVOID lpvParam)
{
Sleep( 4000 );// Waiting untill the message box gets invoked(you can use an effective synch method also)
HWND hWnd = FindWindow( 0, "Initilization"/*Window Title*/ );
Sleep( 4000 );// Wait for some second
CloseWidnow( hWnd );
}
void ShowMsg()
{
:
AfxBeginThread( ThreadFun, 0 );
MessageBox(NULL,"Initilizing the Software","Initilization",MB_OK);
:
}
但是,除非您有效地处理同步问题,否则这段代码肯定会崩溃.
现在可以真正解决您的问题,最好使用无模态对话框显示进度消息
http://www.winprog.org/tutorial/modeless_dialogs.html [
But this code will for sure crash unless you handle the synchronizantion effeciently.
Now coming to a real solution to your issue, better you use a modless dialog which shows the progress message
http://www.winprog.org/tutorial/modeless_dialogs.html[^]
and send the message WM_CLOSE to this dialog after some time or once your s/w initialization is done
不要为此使用MessageBox
,而是使用 ^ ]控件,可采用更专业的方法.您可以将其嵌入到模式对话框或无模式对话框中,也可以单独使用.
Don''t use aMessageBox
for this but utilise theProgressBar
[^] control for a more professional approach. You can embed it into a modal or modeless dialog or use it stand alone.
这篇关于要在几秒钟后自动关闭消息框,或者如何将计时器添加到消息框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!