如何在C ++ / MFC中使用计时器创建警报框? [英] How to create a alert box with timer in C++/MFC?

查看:86
本文介绍了如何在C ++ / MFC中使用计时器创建警报框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于对话框的应用程序,其中我需要一个警报框,一旦我启动应用程序就会被触发(我有开始按钮)。

点击开始按钮之后它必须触发一个具有几秒计时器的警报器,一旦计时器停止,它必须自动关闭。



如果有人可以提供帮助,我将非常感激,我是MFC的新手。< br $> b $ b

我尝试了什么:



试过Alertbox,但不能得到计时器的东西和按钮不需要关闭警报箱,它必须在计时器停止后自行退出。

I have a dialog based application, in it i need a alert box that will get triggered once i start the application(i have start button).
After i click start button it must trigger an alertbox with timer of some seconds and it must close itself once timer stops.

It will be really grateful if one can help to do it, am new to MFC.

What I have tried:

Tried Alertbox ,but not able to get timer thing and buttons not needed to close the alertbox, it must exit itself once timer stops.

推荐答案

看一下任务对话(Windows) [ ^ ]。


创建对话框模板像往常一样使用资源编辑器,并为它创建一个基于 CDialog 的类。



int 成员添加到计时器和计时器事件处理程序原型的对话框类中:

Create the dialog template using the resource editor as usual and create a CDialog based class for it.

Add an int member to the dialog class for your timer and the timer event handler prototype:
class CMyAlertBox : public CDialog
{
    // Wizard generated code here
protected:
    virtual BOOL OnInitDialog();
    afx_msg void OnClose();
    afx_msg void OnTimer(UINT nIDEvent);
    int m_nTimer;
};

中启动计时器OnInitDialog()

Start the timer in OnInitDialog():

BOOL CMyAlertBox::OnInitDialog() 
{
    BOOL bRet = CDialog::OnInitDialog();
    m_nTimer = SetTimer(1, delayTime, NULL);
    return bRet;
}

实现只关闭对话框的计时器处理程序:

Implement the timer handler where the dialog is simply closed:

void CMyAlertBox::OnTimer(UINT /*nIDEvent*/)
{
    EndDialog(IDOK);
}

还覆盖 OnClose()以终止计时器(按ESC,Alt + F4或对话框仍然可以关闭对话框使用X关闭按钮):

Override also OnClose() to kill the timer (the dialog may be still closed by pressing ESC, Alt+F4, or using the X close button):

void CMyAlertBox::OnClose()
{
    if (m_nTimer)
    {
        KillTimer(m_nTimer);
        m_nTimer = 0;
    }
    CDialog::OnClose();
}



以上是用于阻止你的申请的模态对话框:


The above is for a modal dialog that blocks your application:

// Called from any window (usually your main window)
CMyAlertBox *pDlg = new CMyAlertBox(this);
int nRet = pDlg->DoModal();
delete pDlg;
//nRet is IDOK if the time has elapsed or IDCANCEL if closed by the user



如果你不想阻止应用程序你必须创建对话框无模式使用父窗口中的成员变量,并在对话框关闭时通知您的父窗口(例如,使用用户定义的消息)。然后你负责销毁对话框窗口并将其删除。


If you don't want to block the application you must create the dialog modeless using a member variable in your parent window and inform your parent window when the dialog is closed (e.g. using a user defined message). Then you are responsible for destroying the dialog window and deleting it.


这是另一个选项:一个线程安全的,定时的消息框 [ ^ ]
Here's another option : A thread-safe, timed message box[^]


这篇关于如何在C ++ / MFC中使用计时器创建警报框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆