Click here to Skip to main content
15,892,298 members
Articles / Desktop Programming / MFC
Article

Adding a status bar to an MFC dialog

Rate me:
Please Sign up or sign in to vote.
4.97/5 (89 votes)
16 May 2002CPOL2 min read 452.3K   10.2K   128   96
Shows how you can have status bars in your dialog based apps

Introduction

Someone asked in the VC++ forum how they can add a status bar to a dialog and I foolishly replied saying that all they had to do was to have a CStatusBar member in their dialog class and that they should call Create() from the OnInitDialog() handler. Then someone else replied saying that it didn't work and then I tried it out myself and to my horror found that nothing happened. Anyhow I just realized, it's not too complicated a task. I thought I'd write a small article on adding a status bar to a dialog. There is an MSDN sample that does this too, but they derive a class from CStatusBar and do some complicated stuff which is not required for the usual simple things we do with status bars.

Eight simple steps

Step 1

I assume that you have a dialog based MFC application ready for use. Take Resource Symbols from the View menu and add two new symbols, ID_INDICATOR_NISH and ID_INDICATOR_TIME. You can use the default values that VS 6 suggests, but sometimes it might suggest an already used value, in which case you might have to manually change it. I had to anyway. I wonder if this is a known bug.

Step 2

Open your String Table and add the two entries there as well - ID_INDICATOR_NISH and ID_INDICATOR_TIME. And set some default values, whatever you want to use. It doesn't really matter.

Step 3

Add a CStatusBar member to your main dialog class.

CStatusBar m_bar;

Step 4

Open the corresponding cpp file and add the following on top of the file :-

static UINT BASED_CODE indicators[] =
{
    ID_INDICATOR_NISH,
    ID_INDICATOR_TIME
};

Step 5

Now we have to create our status bar. A nice place to do this would be in the OnInitDialog function of our CDialog derived class.

m_bar.Create(this); //We create the status bar

m_bar.SetIndicators(indicators,2); //Set the number of panes 

CRect rect;
GetClientRect(&rect);
//Size the two panes
m_bar.SetPaneInfo(0,ID_INDICATOR_NISH, 
    SBPS_NORMAL,rect.Width()-100);      
m_bar.SetPaneInfo(1,ID_INDICATOR_TIME,SBPS_STRETCH ,0);

//This is where we actually draw it on the screen
RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,
    ID_INDICATOR_TIME);

Step 6 - background color

By the end of Step 5, we actually have a status bar on screen. The two panes show the default values we set in our string table. But now say, you want to change the background color. This is wholly optional of course. You can add this line to the OnInitDialog(...)

m_bar.GetStatusBarCtrl().SetBkColor(RGB(180,180,180));

Note that we had to get the underlying status bar control to call the SetBkColor function.

Step 7 - adding the clock

Say, you want the right pane to show the current time. First set a timer. Just add this line to the OnInitDialog(...)

SetTimer(100,1000,NULL);

Now add the following code to the WM_TIMER handler

void CDlgStatusBarDlg::OnTimer(UINT nIDEvent) 
{
    if(nIDEvent==100) 
    {
        CTime t1;
        t1=CTime::GetCurrentTime();
        m_bar.SetPaneText(1,t1.Format("%H:%M:%S"));
    }
    CDialog::OnTimer(nIDEvent);
}

Step 8 - showing X and Y co-ordinates

Say, you want to show the X,Y co-ordinates of the mouse as it moves along your dialog. What you need to do is to override OnMouseMove.

void CDlgStatusBarDlg::OnMouseMove(UINT nFlags, CPoint point) 
{
    CString s;
    s.Format("X=%d Y=%d",point.x,point.y);
    m_bar.SetPaneText(0,s);
    CDialog::OnMouseMove(nFlags, point);
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
Nish Nishant is a technology enthusiast from Columbus, Ohio. He has over 20 years of software industry experience in various roles including Chief Technology Officer, Senior Solution Architect, Lead Software Architect, Principal Software Engineer, and Engineering/Architecture Team Leader. Nish is a 14-time recipient of the Microsoft Visual C++ MVP Award.

Nish authored C++/CLI in Action for Manning Publications in 2005, and co-authored Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on CodeProject.com and another 250+ blog articles on his WordPress blog. Nish is experienced in technology leadership, solution architecture, software architecture, cloud development (AWS and Azure), REST services, software engineering best practices, CI/CD, mentoring, and directing all stages of software development.

Nish's Technology Blog : voidnish.wordpress.com

Comments and Discussions

 
GeneralRe: Resizing Dialog Pin
Trollslayer15-Oct-05 8:37
mentorTrollslayer15-Oct-05 8:37 
GeneralRe: Resizing Dialog Pin
Balkrishna Talele17-Nov-03 2:00
Balkrishna Talele17-Nov-03 2:00 
GeneralRe: Resizing Dialog Pin
aquawicket30-Oct-06 12:02
aquawicket30-Oct-06 12:02 
GeneralLinks to code and binary swapped Pin
Dean Michaud7-Aug-02 3:15
Dean Michaud7-Aug-02 3:15 
GeneralRe: Links to code and binary swapped Pin
Nish Nishant7-Aug-02 6:10
sitebuilderNish Nishant7-Aug-02 6:10 
GeneralStill not fixed. Pin
WREY18-Sep-03 18:37
WREY18-Sep-03 18:37 
GeneralPlease read MSDN Documentation Pin
Zac Howland4-Jun-02 5:45
Zac Howland4-Jun-02 5:45 
GeneralRe: Please read MSDN Documentation Pin
Nish Nishant4-Jun-02 6:05
sitebuilderNish Nishant4-Jun-02 6:05 
Zac Howland wrote:
Nish
I know you are pretty smart, but the last few articles you have written have been flat out lazy. This problem could be solved by reading the article in MSDN on Controlbars (since the code is basic MFC, there isn't much to explain). You should have simply pointed the person who asked you to MSDN.

Zac


The technique used in the Controlbars sample is different from what I have done. I have demonstrated the simplest manner in which we can add a status bar. This is useful when we don't want to update the status bar when menu items are highlighted or say toolbar buttons. Situations where we make only an elementary use of the status bar. Here we don;t even derive our own class. the complexity is left out.

Anyway I am sorry if this article displeased you. I do try my best. Maybe sometimes in my enthusiasm toweards writing I might speed things up. But I honestly believe there is something in each of my articles for somebody! Of course that's just a matter of personal opinion.


Regards,
Nish
Native CPian.
Born and brought up on CP.
With the CP blood in him.

GeneralRe: Please read MSDN Documentation Pin
Zac Howland4-Jun-02 6:42
Zac Howland4-Jun-02 6:42 
GeneralRe: Please read MSDN Documentation Pin
Nish Nishant4-Jun-02 6:51
sitebuilderNish Nishant4-Jun-02 6:51 
GeneralRe: Please read MSDN Documentation Pin
Zac Howland4-Jun-02 7:03
Zac Howland4-Jun-02 7:03 
GeneralRe: Please read MSDN Documentation Pin
Nish Nishant4-Jun-02 7:18
sitebuilderNish Nishant4-Jun-02 7:18 
GeneralRe: Please read MSDN Documentation Pin
Zac Howland4-Jun-02 7:55
Zac Howland4-Jun-02 7:55 
GeneralRe: Please read MSDN Documentation Pin
ihaml11-Oct-02 0:03
ihaml11-Oct-02 0:03 
GeneralRe: Please read MSDN Documentation Pin
Nish Nishant11-Oct-02 0:07
sitebuilderNish Nishant11-Oct-02 0:07 
GeneralRe: Please read MSDN Documentation Pin
thowra30-Sep-03 0:02
thowra30-Sep-03 0:02 
GeneralDisregard Bozo Pin
Fool Finder24-Oct-02 8:00
sussFool Finder24-Oct-02 8:00 
GeneralRe: Disregard Bozo Pin
Zac Howland24-Oct-02 9:14
Zac Howland24-Oct-02 9:14 
GeneralRe: Re: Disregard Bozo Pin
Fool Finder24-Oct-02 9:38
sussFool Finder24-Oct-02 9:38 
QuestionNo Sizegrip ? Pin
Stephan Pilz20-May-02 20:59
Stephan Pilz20-May-02 20:59 
GeneralNice! Pin
Ravi Bhavnani17-May-02 3:45
professionalRavi Bhavnani17-May-02 3:45 
GeneralRe: Nice! Pin
Nish Nishant17-May-02 15:57
sitebuilderNish Nishant17-May-02 15:57 
GeneralFinally! No scrolling :-) Pin
Nish Nishant16-May-02 23:46
sitebuilderNish Nishant16-May-02 23:46 
GeneralRe: Finally! No scrolling :-) Pin
Shog917-May-02 4:31
sitebuilderShog917-May-02 4:31 
GeneralRe: Finally! No scrolling :-) Pin
Nish Nishant17-May-02 15:56
sitebuilderNish Nishant17-May-02 15:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.