[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 227
  • Last Modified:

Changing Status Bar Text at runtime.

Dear friends,

How do I change the Status bar Text in an MFC app at
run-time? I should be able to display any string from
the resource table or a CString created in the stack/heap.

Thanks.



 




0
Diana_pal
Asked:
Diana_pal
  • 6
  • 4
1 Solution
 
Tommy HuiEngineerCommented:
The frame of the window contains a member called m_wndStatusBar. To set the text in the status bar, you can use

  m_wndStatusBar.SetPaneText(0, "Hello");


0
 
Diana_palAuthor Commented:
Hello Thui,

I have a problem.  Since m_wndStatusBar is a protected member,
I cannot access this from other classes. Please clarify.

Also please tell me how to display a string on the Status Bar
directly from the resource table.

Thank you so much.



0
 
MelissaCCommented:
Hi Diana,

in CMainFrame (which contains the statusbar), you can define this:

static UINT indicators[] =
{
   ID_SEPARATOR,          
   ID_INDICATOR_1,
   ID_INDICATOR_2,
   ID_SEPARATOR,          
   ID_INDICATOR_DATE,
   ID_INDICATOR_TIME,
   ID_SEPARATOR,          
   ID_INDICATOR_CAPS,
   ID_INDICATOR_NUM,
}; (one for each thing you like to display in the statusbar.

then you can write an OnUpdateX function, which tells the mainframe what to do when it repaints the window.

In the messagemap you put the line: (one for each indicator, except the Separators)
ON_UPDATE_COMMAND_UI(ID_INDICATOR_1, OnUpdateIndicator_1)

You declare the function in the header file:

protected:
   // Generated message map functions
      //{{AFX_MSG(CMainFrame)
   afx_msg void OnUpdateIndicator_1(CCmdUI* pCmdUI);
      //}}AFX_MSG

Now write the function which updates the statusbar:

void CMainFrame::OnUpdateIndicator_1(CCmdUI* pCmdUI)
{
   //get a pointer to the document
   CDocument*   pDoc = GetActiveDocument();
   ASSERT( pDoc != NULL);

   //ask for a certain string in the document
   CString strMyString= pDoc->GetSomeString();

   //put the string in the statusbar
   pCmdUI->Enable();
   pCmdUI->SetText( strCurrentBatch );
}

Mostly this all is done through the class wizard. It works almost the same way with the menu items, except they only have to be enabled /disabled.
Hope this helps.
0
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

 
MelissaCCommented:
By the way,
for each indicator (except ID_SEPARATOR), you have to enter a string in the stringtable in the resource file, otherwise the names are unknown to the program. The length of this string will be the size of the indicator in the statusbar.
0
 
MelissaCCommented:
By the way, you can use
strMyString.LoadString( IDS_RESOURCE_STRING );
to load a string from the resource table.
0
 
Diana_palAuthor Commented:
Hi MelissaC,

Your last suggestion looks good. I need little more details.
Actually my requirement is bit outside the MFC frame work.

For e.g. If I click on a button in one of the dialogs, the
text in the Status Bar should change. Therefore what should be
the approach to change the status bar text from the
button click event of one of the buttons?
Regards.




0
 
MelissaCCommented:
Hi Diana,
I suppose the dialog is a modal dialog (shown by "DoModal"?)
On the onclick event, you can send a message to the mainframe:

AfxGetMainWnd()->SendMessage( WM_USER_CHANGE_STATUS);

(don't forget to define the user:
#define WM-USER_CHANGE_STATUS     WM_USER + 1;
)

Then define a message handler in the mainframe like
CMainFrame::OnChangeStatus()
{
   CString strMyString;
   strMyString.LoadString( IDS_RESOURCE_STRING );
   m_wndStatusBar.SetPaneText(0, strMyString);
}

There is a chance that you have to convert strMyString to a constant string using (LPCTSTR)(LPTSTR).

Good luck!

PS: SendMessage makes the program handle the message before returning. PostMessage just drops the message in the MainFrame's postbox, so it can be handled whenever MainFrame wants to.


0
 
Diana_palAuthor Commented:
That sounds great MelissaC,

I will try it little later. Any how I feel, I  should transfer the points to your account. I think thui is bit busy today. Pl post a response once again as answer.

regards.



 
0
 
MelissaCCommented:
Thanx. Good luck!
0
 
MelissaCCommented:
PS: the message handler will be a user defined message handler, which has the following syntax:

Map Entry Function Prototype:
ON_MESSAGE( WM_USER_CHANGE_STATUS, OnChangeStatus )

//Header file
afx_msg LRESULT OnChangeStatus(WPARAM x, LPARAM y);

//implementation
LRESULT OnChangeStatus(WPARAM x, LPARAM y)
{
  return 0L; (zero L)
}
, just to make life easier

0
 
Diana_palAuthor Commented:
Thanks a lot MelissaC, for your effort.

Also sorry for being late due to some link problem.
 
bye



 

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now