AfxMessageBox with Auto-close – Part 2

In a previous article I showed how to make AfxMessageBox to display a message box with auto-close, by using a CBT hook.
One reader suggested that whould be nice if the message box displays also the time left until auto-closing. I thought that’s a very good idea and promissed an update. Here it is!

AfxMessageBox with Auto-close

AfxMessageBox with Auto-close

Of course, may be more than one solution. I chose one which appends “Time left: xxx sec.” in the bottom of the built-in static text control. For this purpose, I added two private methods to CAutoMessageBox class:

  • CAutoMessageBox::_InitStaticTextCtrl – initializes the static contol: finds the static control, keeps in mind the initial text, makes space for an extra-line of text, and assigns an ID to be used later.
  • CAutoMessageBox::_SetStaticTextCtrl – sets the text which contains time left.

For more implementation details of CAutoMessageBox class, download and have a look in the attached demo project.

See also

Downloads

4 thoughts on “AfxMessageBox with Auto-close – Part 2

  1. Flaviu

    Is there a way to handle the messagebox buttons size ? I mean, what if I want to modify “Ok” button text, with “Go to next file” button text …

    Reply
  2. Flaviu

    Here is code that stretch ABORT button, for ex.

    LRESULT CAutoMessageBox::OnInitDialog(WPARAM wParam, LPARAM lParam)
    {

    if(m_nTimeOut > 0)
    {
    _InitStaticTextCtrl();
    SetTimer(0, m_nTimerStep, NULL);
    }
    if(m_bShowProgress)
    {
    VERIFY(_AddProgressCtrl());
    // m_progress.SetRange(0, m_nTimeOut);
    m_progress.SetRange32(0, m_nTimeOut); // use 32-bit limit for a larger range
    m_progress.SetPos(m_nElapsed);
    }

    CButton* pAbort = (CButton*)GetDlgItem(IDABORT);
    if(pAbort)
    {
    CRect rect;
    pAbort->GetWindowRect(&rect);
    ScreenToClient(&rect);
    rect.OffsetRect(CPoint(-20, 0));
    rect.right += 20;
    pAbort->MoveWindow(rect);
    }

    return TRUE;
    }

    Reply
  3. Flaviu

    I still have a question: I don’t know why doesn’t work MB_DEFBUTTON2 or MB_DEFBUTTON3 style flags on CAutoMessageBox … as long I didn’t use CAutoMessageBox on demo project, everything is all right.

    Reply

Leave a Reply