Search code examples
c++mfc

How to show a different text in MFCStatusBar Progress bar


I would like to have a progress bar in the MFC status bar, which shows the progress in the amount of elapsed counts (e.g., 15/235) instead of the progress in percentage, while still showing a colored progress bar.

It seems, however, that I can either show the amounts and not the colored progress, or vice versa. By doing EnablePaneProgressBar(1, 150, TRUE), only the percentage is shown:

How do I achieve this?


Solution

  • Searched the MFC sources to see how this works, and found its implementation. It's not in the CMFCStatusBar code actually, instead it's in file afxvisualmanager.cpp line 1897, function CMFCVisualManager::OnDrawStatusBarProgress(). Namely, this is an MFC feature (the MFC library draws the progress-bar), and it seems there is no some Win32 Progerss Bar control there - these do not display text anyway.

    Don't know if this can be changed, but a possible implementation could be:

    • Define a new CMFCVisualManager-derived class, overriding the OnDrawStatusBarProgress() member - it's virtual fortunately.
    • In the override change the Format() call, to something like strText.Format(_T("%d/%d"), nProgressCurr, nProgressTotal);
    • Finally you have to replace the CMFCVisualManager instance with your overridden visual manager. It's not quite easy to do, as the visual manager is a single-instance object, created and maintained by the MFC framework, which is a bit tricky. The CMFCVisualManager::SetDefaultManager() method seems to do this properly though. See here and here.

    A downside is that this will change the behavior of all progress-bars in your status-bar (not a problem if there is only one). It can still be changed though (ie draw each progress-bar differently, based on its pane number).

    I can't tell if this is worth the effort, depends on your project requirements, schedule, budget etc.


    EDIT:
    Well, it seems it is quite easier actually. There is a call of CMFCVisualManager::SetDefaultManager() in CMainFrame::OnCreate(). It's in the wizard-generated code of your frame-window class (MainFrm.cpp). Change it to use your overridden visual manager class instead.