Page 1 of 1

Microsoft : Working With Status Bars Using status bars in an application Rate Topic: -----

#1 Martyn.Rae   User is offline

  • The programming dinosaur
  • member icon

Reputation: 556
  • View blog
  • Posts: 1,436
  • Joined: 22-August 09

Posted 21 March 2010 - 07:05 AM

Microsoft : Working With Status Bars

Introduction

The status bar is a bar small window that runs along the very bottom of a window that can be used to display status information.

Prerequisites

There are two prerequisites that need to be put in place before we can use the status bar.

Firstly, we need to include the common control header file using #include <CommCtrl.h>. Without that, none of the status bar definitions will be available to us. Secondly, we need to initialize the common controls using InitCommonControlsEx, in order that we can use the status bar. The following two lines of code will do that.

    INITCOMMONCONTROLSEX common_controls;
    common_controls.dwSize = sizeof(INITCOMMONCONTROLSEX);
    common_controls.dwICC = ICC_BAR_CLASSES;
    InitCommonControlsEx(&common_controls);



We are now ready to use the status bar control.

Creating A Status Bar

A status bar is created by specifying the predefined STATUSCLASSNAME as the class name. Using the code snippet from the example found at the end of this tutorial we have:

    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  
    // Create a standard frame window  
    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  
                  
    RegisterClass(&window_class);  
    window_handle = CreateWindowEx(WS_EX_WINDOWEDGE|WS_EX_CLIENTEDGE,   
                                   window_class_name,   
                                   L"Status Bar Example",  
                                   WS_OVERLAPPEDWINDOW|WS_CLIPCHILDREN|WS_CLIPSIBLINGS, 100,  
                                   100, screen_width-200,   
                                   screen_height-200, NULL, NULL,  
                                   instance_handle, NULL); 
    RECT client_rect;
    GetClientRect(window_handle, &client_rect);
    status_handle = CreateWindowEx(0,   
                                   STATUSCLASSNAME,   
                                   L"",  
                                   WS_CHILD|WS_VISIBLE|SBARS_SIZEGRIP, 0,  
                                   client_rect.bottom-client_rect.top-20,
                                   client_rect.right-client_rect.left,   
                                   20, window_handle, NULL,  
                                   instance_handle, NULL); 



This will create us a status bar ready for use.

Defining The Status Bar Parts

A status bar can have up to 256 different parts or areas which can contain text or bitmap images etc. We shall create two parts, one for a status text and the other for the date and time. To set the number of parts in the status bar, we send the status bar window a SB_SETPARTS message as shown in the code below.

     int status_parts[2] = { client_rect.right-client_rect.left-170, 
                             client_rect.right-client_rect.left };
     SendMessage(status_handle, SB_SETPARTS, 2, (LPARAM)&status_parts);



Setting the text in a part

To set the text for an individual part, we send the status bar window an SB_SETTEXT message specifying the part we want to set and the text. For example, say we wanted to set the text in the first part of the status bar to 'Ready'. We would use something like:

    SendMessage(status_handle, SB_SETTEXT, 0, (LPARAM)L"Ready");



Code Example

The code sample below shows how to create a status bar at the bottom of the window, set two parts to that status bar, set the text 'Ready' into the first part, and using a one second windows timer, display the local date and time in the second part.

#include <windows.h>  
#include <CommCtrl.h> 
#include <ctime>  
#include <cstdlib>  
using namespace std;  
  
class frame_window {  
    private:    LPWSTR window_class_name;  
                HINSTANCE instance_handle;  
                HCURSOR cursor_arrow;  
                HWND window_handle;  
                HWND status_handle; 
                RECT client_rectangle;   
    public:     frame_window(LPWSTR window_class_identity) : window_class_name(window_class_identity),  
                                                             instance_handle(NULL) {  
                    INITCOMMONCONTROLSEX common_controls; 
                    common_controls.dwSize = sizeof(INITCOMMONCONTROLSEX); 
                    common_controls.dwICC = ICC_BAR_CLASSES; 
                    InitCommonControlsEx(&common_controls); 
                    int screen_width = GetSystemMetrics(SM_CXFULLSCREEN);  
                    int screen_height = GetSystemMetrics(SM_CYFULLSCREEN);  
                    instance_handle = GetModuleHandle(NULL);  
                       
                    WNDCLASS window_class = { CS_OWNDC, main_window_proc, 0, 0,    
                                              instance_handle, NULL,    
                                              NULL, NULL, NULL,    
                                              window_class_name };   
                   
                    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++   
                    // Create a standard frame window   
                    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++   
                   
                    RegisterClass(&window_class);   
                    window_handle = CreateWindowEx(WS_EX_WINDOWEDGE|WS_EX_CLIENTEDGE,    
                                                   window_class_name,    
                                                   L"Status Bar Example",   
                                                   WS_OVERLAPPEDWINDOW|WS_CLIPCHILDREN|WS_CLIPSIBLINGS, 100,   
                                                   100, screen_width-200,    
                                                   screen_height-200, NULL, NULL,   
                                                   instance_handle, NULL);  
                    RECT client_rect; 
                    GetClientRect(window_handle, &client_rect); 
                    status_handle = CreateWindowEx(0,    
                                                   STATUSCLASSNAME,    
                                                   L"",   
                                                   WS_CHILD|WS_VISIBLE|SBARS_SIZEGRIP, 0,   
                                                   client_rect.bottom-client_rect.top-20, 
                                                   client_rect.right-client_rect.left,    
                                                   20, window_handle, NULL,   
                                                   instance_handle, NULL);  
                    SetCursor(LoadCursor(NULL, IDC_ARROW)); 
                    int status_parts[2] = { client_rect.right-client_rect.left-170,  
                                            client_rect.right-client_rect.left }; 
                    SendMessage(status_handle, SB_SETPARTS, 2, (LPARAM)&status_parts); 
                    SendMessage(status_handle, SB_SETTEXT, 0, (LPARAM)L"Ready"); 
                    SetTimer(window_handle, 1, 1000, NULL); 
                    SetWindowLongPtr(window_handle, GWL_USERDATA, (LONG)this);  
                    ShowWindow(window_handle, SW_SHOW);   
                    UpdateWindow(window_handle); 
                }  
                ~frame_window() {  
                    UnregisterClass(window_class_name, instance_handle);   
                }  
                //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++   
                // Windows message processing function   
                //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++   
                static LRESULT WINAPI main_window_proc(HWND window_handle, UINT message,    
                                                       WPARAM wparam, LPARAM lparam) {  
                    frame_window *This = (frame_window *)GetWindowLongPtr(window_handle, GWL_USERDATA);  
                    switch ( message ) {  
                        case WM_PAINT:   
                        {   
                            PAINTSTRUCT paint_structure;   
                            RECT client_rect;   
                            HDC paint_device_context, paint_dc;   
                            HBITMAP bitmap;   
  
                            paint_device_context = BeginPaint(window_handle, &paint_structure);   
                            paint_dc = CreateCompatibleDC(paint_device_context);   
                            GetClientRect(window_handle, &client_rect);   
                            int window_width = client_rect.right - client_rect.left;  
                            int window_height = client_rect.bottom - client_rect.top;  
                            bitmap = CreateBitmap(window_width, window_height, 1, 32, NULL);  
                            HGDIOBJ old_bitmap = SelectObject(paint_dc, bitmap);  
  
                            // Fill the client aread with the user selected face colour  
                            HBRUSH light_brush = CreateSolidBrush(RGB(255, 255, 238));  
                            FillRect(paint_dc, &paint_structure.rcPaint, light_brush);  
                            DeleteObject(light_brush);  
                                                                                    
                            BitBlt(paint_device_context, 0, 0,    
                                   client_rect.right-client_rect.left,   
                                   client_rect.bottom-client_rect.top,   
                                   paint_dc, 0, 0, SRCCOPY);   
                            SelectObject(paint_dc, old_bitmap);   
                            DeleteObject(bitmap);   
                            DeleteDC(paint_dc);   
                            EndPaint(window_handle, &paint_structure);   
                            return 0;   
                        }   
                        case WM_ERASEBKGND:   
                        {   
                            return TRUE;   
                        }   
                        case WM_SIZE:   
                        {
                            SetWindowPos(This->status_handle, NULL, 0, HIWORD(lparam)-20, LOWORD(lparam), 20, SWP_NOZORDER);
                            int status_parts[2] = { LOWORD(lparam)-170, LOWORD(lparam) }; 
                            SendMessage(This->status_handle, SB_SETPARTS, 2, (LPARAM)&status_parts); 
                            InvalidateRect(window_handle, NULL, TRUE);   
                            return 0;   
                        } 
                        case WM_TIMER: 
                        { 
                            wchar_t buffer[30]; 
                            time_t time_now =time(NULL); 
                            wcsftime(buffer, sizeof(buffer), L"%a %d %b %Y %H:%M:%S", localtime(&time_now)); 
                            SendMessage(This->status_handle, SB_SETTEXT, 1, (LPARAM)buffer); 
                            return 0;   
                        } 
                        case WM_CLOSE:   
                        {   
                            //++++++++++++++++++++++++++++++++++++++++++++++++++++   
                            // The user wants to close the window   
                            //++++++++++++++++++++++++++++++++++++++++++++++++++++   
                   
                            PostQuitMessage(0);   
                            return 0;   
                        }   
                    }   
                    return DefWindowProc(window_handle, message, wparam, lparam);   
                }   
                  
                //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++   
                // Windows message loop   
                //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++   
                void run() {  
                    MSG window_message;   
                    while ( GetMessage(&window_message, NULL, 0, 0) ) {   
                        TranslateMessage(&window_message);   
                        DispatchMessage(&window_message);   
                    }   
                }  
};  
   
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++   
// Windows main entry point   
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++   
   
int WINAPI wWinMain(HINSTANCE instance_handle, HINSTANCE, LPWSTR, INT) {  
    frame_window main_window(L"my base window");  
    main_window.run();  
    return 0;   
}


This post has been edited by Martyn.Rae: 21 March 2010 - 10:31 PM


Is This A Good Question/Topic? 0
  • +

Page 1 of 1