Click here to Skip to main content
Licence 
First Posted 19 Feb 2002
Views 189,740
Bookmarked 36 times

Statusbar – it’s just so easy to use in MFC

By | 19 Feb 2002 | Article
How to add your own text to the status bar control using MFC

Introduction

I just had a look at the MSDN articles on CStatusBar, and to my surprise, I found it so easy to use.

First of all, a status bar contains several 'panes'. Each pane is a rectangular area of the status bar that you can use to display information. As you know, many applications display the status of the CAPS, NUM LOCK, and other keys in the rightmost panes. These panes, by default, have a 3-D border around them. The leftmost pane (pane 0), sometimes called the 'message pane', has the BPS_NOBORDERS style, and therefore doesn’t have any border surrounding it. This pane is usually used to display a string explaining the currently selected menu item or toolbar button. Furthermore, by default, the 'message pane' is 'elastic': it takes up the area of the status-bar not used by the other indicator panes, so that the other panes are always right-aligned.

To add a customized pane to display your own message on the status bar, it only takes the following 6 steps. For this example, we are going to display the current time at the rightmost location of the status bar. It will be updated every 60 seconds.

First of all, add a new entry to your string table with an ID of ID_INDICATOR_TIME and a caption of '%5s “. The extra spaces are to give you a little more room in the pane so the text will not be clipped.

Second, append ID_INDICATOR_TIME to the indicators[] array in the MainFrm.cpp file as the last entry (so that it will appear as the rightmost item on status bar).

Third, in the message map in , add the following:-

afx_msg void OnUpdateTimeIndicator(CcmdUI *pCmdUI);

Fourth, in MainFrm.cpp, add the macro call:-

ON_UPDATE_COMMAND_UI ( ID_INDICATOR_TIME,OnUpdateTimeIndicator)

Fifth, in MainFrm.cpp, create the function body:-

void CMainFrame::OnUpdateTimeIndicator(CCmdUI *pCmdUI)
{
    CString strStatus;

    strStatus.Format(ID_INDICATOR_TIME, time);

    m_wndStatusBar.SetPaneText(
		m_wndStatusBar.CommandToIndex(ID_INDICATOR_TIME),
		strStatus );
}

Last of all, update the variable time every 60 seconds:

#define TIME_STATUSBAR 1

class CMainFrame : public CFrameWnd
{
	……
private:
	char time[7];
	…..
}

void CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
   ……
  SetTimer(TIME_STATUSBAR, 60000, NULL);
  Memset(time, ‘\0’, 7);
   ……
}

void CMainFrame::OnTimer(UINT nIDEvent) 
{
    // TODO: Add your message handler code here and/or call default
    if ( nIDEvent == TIME_STATUSBAR )
    {
        char tempchar[20];
        _strtime(tempchar); // get current time
        strncpy(time, tempchar, 5); // extract only hour and minute info
    }

    CFrameWnd::OnTimer(nIDEvent);
}

The secret here is, whenever variable time changes, the display changes during the next idle loop. This is done automatically by MFC. Isn’t it nice?

If you wish, you can download the sample project, which does the same as described above.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

lucy

Software Developer

Canada Canada

Member

I love this place!

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questionhow to change pane text when mouse move over diffrent controls? Pinmemberhvibh17:32 29 Nov '02  
GeneralCannot get time working on MDI :( PinmemberStatic16:23 3 Apr '02  
GeneralRe: Cannot get time working on MDI :( PinmemberStatic16:46 3 Apr '02  
Generalone pane PinmemberDoby8:29 23 Feb '02  
GeneralRe: one pane PinmemberDoby23:40 23 Feb '02  
GeneralRe: one pane PinmemberDoby0:11 24 Feb '02  
GeneralRe: one pane Pinmemberlucy8:02 25 Feb '02  
Generalno topic PinmemberMazdak19:58 20 Feb '02  
GeneralRe: no topic PinmemberNish [BusterBoy]20:27 20 Feb '02  
GeneralRe: no topic PinmemberKannan Kalyanaraman2:27 21 Feb '02  
GeneralRe: no topic Pinmemberlucy2:52 21 Feb '02  
GeneralRe: no topic PinmemberMazdak3:22 21 Feb '02  
GeneralRe: no topic PinmemberHockeyDude10:43 21 Feb '02  
GeneralYay, there is actually a female on this site !! PinmemberAnonymous19:01 20 Feb '02  
GeneralRe: Yay, there is actually a female on this site !! PinmemberChristian Graus19:03 20 Feb '02  
GeneralRe: Yay, there is actually a female on this site !! PinmemberNish [BusterBoy]19:14 20 Feb '02  
GeneralRe: Yay, there is actually a female on this site !! PinmemberMesutGencer_995:59 21 Feb '02  
GeneralRe: try message board Pinmemberlucy8:39 21 Feb '02  
GeneralRe: Yay, there is actually a female on this site !! PinmemberMazdak9:00 21 Feb '02  
GeneralRe: Yay, there is actually a female on this site !! Pinmemberlucy10:40 21 Feb '02  
GeneralRe: Yay, there is actually a female on this site !! PinmemberMazdak19:12 21 Feb '02  
GeneralRe: Yay, there is actually a female on this site !! PinmemberChristian Graus8:45 25 Feb '02  
GeneralRe: Yay, there is actually a female on this site !! PinsussAnonymous11:24 10 Oct '03  
GeneralRe: Yay, there is actually a female on this site !! PinmemberHockeyDude10:37 21 Feb '02  
GeneralRe: Yay, there is actually a female on this site !! PinmemberKathy Nelson8:31 25 Feb '02  

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.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 20 Feb 2002
Article Copyright 2002 by lucy
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid