Solved

Changing Status Bar Text  at runtime.

Posted on 1999-01-18
11
215 Views
Last Modified: 2013-11-20
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
Comment
Question by:Diana_pal
  • 6
  • 4
11 Comments
 
LVL 15

Expert Comment

by:Tommy Hui
ID: 1327910
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
 

Author Comment

by:Diana_pal
ID: 1327911
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
 
LVL 2

Expert Comment

by:MelissaC
ID: 1327912
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 2

Expert Comment

by:MelissaC
ID: 1327913
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
 
LVL 2

Expert Comment

by:MelissaC
ID: 1327914
By the way, you can use
strMyString.LoadString( IDS_RESOURCE_STRING );
to load a string from the resource table.
0
 

Author Comment

by:Diana_pal
ID: 1327915
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
 
LVL 2

Expert Comment

by:MelissaC
ID: 1327916
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
 

Author Comment

by:Diana_pal
ID: 1327917
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
 
LVL 2

Accepted Solution

by:
MelissaC earned 100 total points
ID: 1327918
Thanx. Good luck!
0
 
LVL 2

Expert Comment

by:MelissaC
ID: 1327919
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
 

Author Comment

by:Diana_pal
ID: 1327920
Thanks a lot MelissaC, for your effort.

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



 

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question