Someone asked in the VC++ forum how they can add a status bar to a dialog and
I foolishly replied saying that all they had to do was to have a
CStatusBar member in their dialog class and that they should call
Create() from the
OnInitDialog() handler. Then someone
else replied saying that it didn't work and then I tried it out myself and to my
horror found that nothing happened. Anyhow I just realized, it's not too
complicated a task. I thought I'd write a small article on adding a status bar
to a dialog. There is an MSDN sample that does this too, but they derive a class
CStatusBar and do some complicated stuff which is not required
for the usual simple things we do with status bars.
Eight simple steps
I assume that you have a dialog based MFC application ready for use. Take
Resource Symbols from the View menu and add two new symbols,
ID_INDICATOR_TIME. You can use the default values that VS 6 suggests, but
sometimes it might suggest an already used value, in which case you might have
to manually change it. I had to anyway. I wonder if this is a known bug.
Open your String Table and add the two entries there as well -
ID_INDICATOR_TIME. And set some default values, whatever
you want to use. It doesn't really matter.
CStatusBar member to your main dialog class.
Open the corresponding cpp file and add the following on top of the file
static UINT BASED_CODE indicators =
Now we have to create our status bar. A nice place to do this would be in the
OnInitDialog function of our
CDialog derived class.
Step 6 - background color
By the end of Step 5, we actually have a status bar on screen. The two panes
show the default values we set in our string table. But now say, you want to
change the background color. This is wholly optional of course. You can add this
line to the
Note that we had to get the underlying status bar control to call the
Step 7 - adding the clock
Say, you want the right pane to show the current time. First set a timer.
Just add this line to the
Now add the following code to the
void CDlgStatusBarDlg::OnTimer(UINT nIDEvent)
Step 8 - showing X and Y co-ordinates
Say, you want to show the X,Y co-ordinates of the mouse as it moves along
your dialog. What you need to do is to override
void CDlgStatusBarDlg::OnMouseMove(UINT nFlags, CPoint point)
Nish is a real nice guy who has been writing code since 1990 when he first got his hands on an 8088 with 640 KB RAM. Originally from sunny Trivandrum in India, he has been living in various places over the past few years and often thinks it’s time he settled down somewhere.
Nish has been a Microsoft Visual C++ MVP since October, 2002 - awfully nice of Microsoft, he thinks. He maintains an MVP tips and tricks web site - www.voidnish.com
where you can find a consolidated list of his articles, writings and ideas on VC++, MFC, .NET and C++/CLI. Oh, and you might want to check out his blog on C++/CLI, MFC, .NET and a lot of other stuff - blog.voidnish.com
Nish loves reading Science Fiction, P G Wodehouse and Agatha Christie, and also fancies himself to be a decent writer of sorts. He has authored a romantic comedy Summer Love and Some more Cricket
as well as a programming book – Extending MFC applications with the .NET Framework
Nish's latest book C++/CLI in Action
published by Manning Publications is now available for purchase. You can read more about the book on his blog.
Despite his wife's attempts to get him into cooking, his best effort so far has been a badly done omelette. Some day, he hopes to be a good cook, and to cook a tasty dinner for his wife.