Working with the Statusbar in MFC - Changing the text
(Page 3 of 4 )
We have three ways to change the text. I will use the extra space we have to display the current clock and the name of the current day of the week. For this first, I will set up a timed event. Let us trigger it every second to update the clock as the seconds pass by. I have used the WM_TIMER event message. Just add it into the main frame via the class view and properties method. After this, we will set up the timed event as soon as we are ready with the window. The end of the OnCreate() method is perfect. Add the following line:
this->SetTimer(1,1000,NULL);
The first part is the ID of the timed event. Given that we will have only one timed event, any number will do. The second argument states after how many milliseconds the timer should be triggered. To find out the time format, I have used the CTime class and the following lines:
CTime today = CTime::GetCurrentTime();
time = today.Format(_T("%A - %H:%M:%S"));
First, we ask for the local time from the system with the static GetCurrentTime function. The time variable is just a string to store the value. The formatting characters used in the extraction of the current time are the same as for the strftime function. Now that we have the text, we can show it on the Statusbar.
The easiest and most straightforward method is to set the new text in the corresponding text. In my case, the m_wndStatusBar is the CMFCStatusBar declared in the Mainframe class.
m_wndStatusBar.SetPaneText(
m_wndStatusBar.CommandToIndex(ID_STATUS_TEXT),
time );
The panes after the addition are numbered in an increasing order, starting from zero. However, sometimes we might not exactly know in what position that specific text is. For these situations, you can use the CommandToIndex function that will return the current index number of the allocated resource ID. The first argument is the index of the pane, while the second is the text to set. Once you've added this line after finding out the time, when you run the application, success is at hand:

The second option is to assign to it an update controller. This function will be called every time an update is required for the assigned pane. Look at it as an OnDraw function of the pane. To start, assign the corresponding pane to a function as follows:
ON_UPDATE_COMMAND_UI(ID_STATUS_TEXT, &CMainFrame::OnUpdateStatusText)
Finally, add to the mainframe class function:
afx_msg void OnUpdateStatusText(CCmdUI *pCmdUI);
void CMainFrame::OnUpdateStatusText( CCmdUI *pCmdUI )
{
pCmdUI->Enable();
pCmdUI->SetText(time);
}
To make sure that the refresh will be made no matter what, we could force an update by calling the InvalidatePaneContent function with the corresponding index as a parameter. Here you also have the option to enable or disable the text in the message text. I am talking about using the gray or the black font (as you can observe in the case of the caps pane). Of course, you can use this function to perform various other tasks as well:

Next: Extra information >>
More .NET Articles
More By Gabor Bernat