Solved

Changing Status Bar Text  at runtime.

Posted on 1999-01-18
11
205 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
repeatEnd java challenge 42 83
Tab names are off by one 5 49
List out all word 7 224
Smart Camera scanning and reading information 3 86
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

747 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

14 Experts available now in Live!

Get 1:1 Help Now