Solved

Changing Status Bar Text  at runtime.

Posted on 1999-01-18
11
208 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
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.

862 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now