Click here to Skip to main content
11,643,493 members (76,698 online)
Click here to Skip to main content

Adding a status bar to an MFC dialog

, 16 May 2002 CPOL 329.1K 7.8K 120
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

Nish Nishant
United States United States
Nish Nishant is a Software Architect/Consultant based out of Columbus, Ohio. He has over 15 years of software industry experience in various roles including Lead Software Architect, Principal Software Engineer, and Product Manager. Nish is a recipient of the annual Microsoft Visual C++ MVP Award since 2002 (13 consecutive awards as of 2014).

Nish is an industry acknowledged expert in the Microsoft technology stack. He authored
C++/CLI in Action for Manning Publications in 2005, and had previously 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 vastly experienced in team management, mentoring teams, and directing all stages of software development.

Contact Nish : You can reach Nish on his google email id voidnish.

Website and Blog

You may also be interested in...

Comments and Discussions

 
QuestionButtons OverLapping with status bar Pin
p.uday kishore18-Nov-13 18:38
professionalp.uday kishore18-Nov-13 18:38 
Questionhow to change font color? Pin
luiggivillar9-Jan-12 4:27
memberluiggivillar9-Jan-12 4:27 
QuestionProblem with SetCapture() Pin
Plamen Petrov29-Dec-11 21:27
memberPlamen Petrov29-Dec-11 21:27 
GeneralMy vote of 5 Pin
A.Health6-Oct-11 3:06
memberA.Health6-Oct-11 3:06 
GeneralMy vote of 3 Pin
SharanyaMahi4-Oct-11 20:27
memberSharanyaMahi4-Oct-11 20:27 
Thank u Very much..
Questionhow to revert the Background color? Pin
sally89983-Aug-11 21:54
membersally89983-Aug-11 21:54 
GeneralMy vote of 5 Pin
Bhupendra S. Brahme30-Nov-10 1:44
memberBhupendra S. Brahme30-Nov-10 1:44 
GeneralMy vote of 5 Pin
wbanko21-Oct-10 10:45
memberwbanko21-Oct-10 10:45 
GeneralMy vote of 5 Pin
JongchanAhn7-Jul-10 16:49
memberJongchanAhn7-Jul-10 16:49 
GeneralThat's great and easy to do it Pin
JongchanAhn7-Jul-10 16:49
memberJongchanAhn7-Jul-10 16:49 
GeneralThanks Pin
futurejo19-Jan-10 6:38
memberfuturejo19-Jan-10 6:38 
GeneralMy vote of 2 Pin
j1s2chen116-Dec-09 20:16
memberj1s2chen116-Dec-09 20:16 
GeneralThanks a lot Pin
hnkulkarni31-May-09 22:58
memberhnkulkarni31-May-09 22:58 
GeneralThanks Pin
nobihai26-Oct-08 5:21
membernobihai26-Oct-08 5:21 
GeneralColoring only one pane Pin
CerpinTaxt19-Oct-08 23:46
memberCerpinTaxt19-Oct-08 23:46 
GeneralNice Article!! Pin
loverjersey28-Aug-08 2:24
memberloverjersey28-Aug-08 2:24 
QuestionMFC help needed Pin
abc_nus_student22-Nov-07 23:22
memberabc_nus_student22-Nov-07 23:22 
QuestionAdjusting size of client area of dialog box. Pin
Tushar Jadhav12-Jun-07 23:31
memberTushar Jadhav12-Jun-07 23:31 
AnswerRe: Adjusting size of client area of dialog box. Pin
jeromelan10-Jun-08 3:48
memberjeromelan10-Jun-08 3:48 
Generali m the trainee,thanks for such a grt info Pin
Member #38960347-Mar-07 18:38
memberMember #38960347-Mar-07 18:38 
GeneralThanks for very clear explanations Pin
Raymond_Gimilio20-Feb-07 3:36
memberRaymond_Gimilio20-Feb-07 3:36 
GeneralSizing Grip Pin
BlueKat15-Dec-05 12:07
memberBlueKat15-Dec-05 12:07 
GeneralRe: Sizing Grip Pin
SeanNick17-Aug-06 18:20
memberSeanNick17-Aug-06 18:20 
QuestionRe: Sizing Grip Pin
xu_qing11-Mar-07 4:22
memberxu_qing11-Mar-07 4:22 
AnswerRe: Sizing Grip Pin
SeanNick12-Mar-07 20:26
memberSeanNick12-Mar-07 20:26 
GeneralRe: Sizing Grip Pin
xu_qing13-Mar-07 1:22
memberxu_qing13-Mar-07 1:22 
Generalthank you for easy solution Pin
l00p1n612-Jul-05 6:58
memberl00p1n612-Jul-05 6:58 
GeneralWorks also great for CPropertySheet ! Pin
Axel Magard29-Apr-05 0:58
sussAxel Magard29-Apr-05 0:58 
GeneralXP problem Pin
Yavathol31-Mar-05 23:38
memberYavathol31-Mar-05 23:38 
QuestionHow can i set the button position using Cordinates Pin
kumararaja19-Jul-04 22:00
memberkumararaja19-Jul-04 22:00 
GeneralDialog Design Pin
srinivasa murthy19-May-04 2:27
membersrinivasa murthy19-May-04 2:27 
Generalvc++ how can i insert pic in dialog box Pin
neo_matrix10021-Feb-04 20:10
memberneo_matrix10021-Feb-04 20:10 
GeneralRe: vc++ how can i insert pic in dialog box Pin
vikas amin25-Aug-05 23:54
membervikas amin25-Aug-05 23:54 
Generaldisplaying menu item prompts Pin
toxcct3-Dec-03 1:22
membertoxcct3-Dec-03 1:22 
GeneralGreat Article! Pin
Vetro XL15-Oct-03 16:23
sussVetro XL15-Oct-03 16:23 
GeneralRe: Great Article! Pin
kapildevg9-May-06 3:20
memberkapildevg9-May-06 3:20 
QuestionUrgent Help: How do i add a colour (Back Ground) to a status bar? Pin
kishore Gaddam11-Jul-03 4:04
susskishore Gaddam11-Jul-03 4:04 
AnswerRe: Urgent Help: How do i add a colour (Back Ground) to a status bar? Pin
kishore Babu25-Mar-04 19:49
memberkishore Babu25-Mar-04 19:49 
Generalhttp://wieler-forum.nl/trucking-company--alberta-company-trucking/ Pin
. (DBCS) Read Key20-Jul-05 1:11
suss. (DBCS) Read Key20-Jul-05 1:11 
GeneralChange color of individual Panes Pin
mistroseth7-Apr-03 1:54
membermistroseth7-Apr-03 1:54 
GeneralRe: Change color of individual Panes Pin
SledgeHammer7-Apr-03 2:16
memberSledgeHammer7-Apr-03 2:16 
GeneralRe: Change color of individual Panes Pin
mistroseth7-Apr-03 2:41
membermistroseth7-Apr-03 2:41 
GeneralRe: Change color of individual Panes Pin
SledgeHammer7-Apr-03 7:44
memberSledgeHammer7-Apr-03 7:44 
GeneralRe: Change color of individual Panes Pin
mistroseth8-Apr-03 1:48
membermistroseth8-Apr-03 1:48 
GeneralRe: Change color of individual Panes Pin
SledgeHammer8-Apr-03 8:02
memberSledgeHammer8-Apr-03 8:02 
GeneralRe: Change color of individual Panes Pin
mistroseth8-Apr-03 8:24
membermistroseth8-Apr-03 8:24 
GeneralRe: Change color of individual Panes Pin
NYTSX2-May-06 10:23
memberNYTSX2-May-06 10:23 
GeneralRe: Change color of individual Panes Pin
norm.net2-May-06 20:38
membernorm.net2-May-06 20:38 
Questionhow to change text when mouse move over diffrent controls? Pin
vibhavari26-Nov-02 1:17
sussvibhavari26-Nov-02 1:17 
QuestionHow to refresh the status bar on the dialog when it is expanded or contracted? Pin
tony.wu10-Sep-02 17:33
membertony.wu10-Sep-02 17:33 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150731.1 | Last Updated 17 May 2002
Article Copyright 2002 by Nish Nishant
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid