HomeC&C++WainTutorialsSamplesTip&TrickTools


The next step is to add a status bar
First we must create the window:
   StatusBarWindow = CreateStatusWindow(WS_CHILD|WS_VISIBLE|
                                        WS_BORDER|SBARS_SIZEGRIP,
                                        "Ready",
                                        MainWindow,
                                        StatusBarId);
"Ready" is the default text for the first part of the status bar.
MainWindow is the handle for the main window.
StatusBarId is a ID for the window, it could be defined as:
enum WindowId
{
   StatusBarId = 128
};
Which can seem to be overkill, but we will add more ID's later on.
Next we must specify how many parts we want to have on the status bar, and the widths of these parts. We create a function with this purpose:
void SetStatusBarWidths(HWND ParentWindow)
{
   int BoxRight[3];
   RECT ParentRect;
   GetClientRect(ParentWindow, &ParentRect);
   int Width = ParentRect.right - ParentRect.left;
   BoxRight[0] = 100;
   BoxRight[1] = Width - 100;
   BoxRight[2] = -1;
   SendMessage(StatusBarWindow,
               SB_SETPARTS,
               3,
               (LPARAM )BoxRight);
}
This will create 3 parts, the first (leftmost) has a width of 100, the third is also 100 pixels, the middle part fills the rest. BoxRight specifies the right position of each part, -1 is to specify that it should fill the rest.
The next step is to make sure that the status bar moves when the main window moves. So we must catch the WM_SIZE event and pass it on to the status bar window. These lines has to be added to the MainWndProc:
      case WM_SIZE:
         SendMessage(StatusBarWindow, msg, wParam, lParam);
         SetStatusBarWidths(hwnd);
         break;
First we tell the Statusbar to move, and then we adjust the position and sizes of the parts on the status bar.
Now we will create a handy function to set the text on the status bar:
void SetStatusBarText(const char *Text, WORD PartNumber)
{
   SendMessage(StatusBarWindow,
               SB_SETTEXT,
               PartNumber,
               (LPARAM )Text);
}
Then some code to show that it works:
   SetStatusBarText("First",  0);
   SetStatusBarText("Middle", 1);
   SetStatusBarText("Right",  2);
To be able to compile we must include commctrl.h and tell the compiler/linker that it should use comctl32.lib (or libcomctl32.a with gcc compilers).
We also have to initialize common controls:
InitCommonControls();
The complete code for the application can be found here