wxWidgets (2) - Creating A Status Bar (Visual C++)

wxWidgets - (2) Creating A Status Bar (Visual C++)
This tutorial continues on from the previous wxWidgets - (1) Creating A Toolbar (Visual C++).
  1. Open the previously created wxWidgets toolbar creation project.

  2. Prepare an xpm bitmap data with Bin2CU for the 1st status bar field glyph.

  3. Specify #include <wx/statusbr.h> in main header file.

  4. Declare the following enumeration IDs, in the top area of main header file, for the status bar fields to be created :
    //----------------------------------------------------------------------------
    enum enStatusField { sfGlyph, sfHint, sfOxKind, cistatus_fields};
    //----------------------------------------------------------------------------
    

  5. Now declare a status bar variable and a bitmap for a status glyph in the private section of the main header file:
        wxStatusBar m_statusbar;
        wxStaticBitmap *m_bmpStatus;
    

    Then create tool bar creation / deletion function as follows :
    //----------------------------------------------------------------------------
    void MainFrame::AllocFreeStatusBar(bool bAlloc)
    {
        if (bAlloc)
        {
            m_statusbar.Create(this, wxID_ANY
                , wxSTB_SIZEGRIP | wxSTB_SHOW_TIPS
                | wxSTB_ELLIPSIZE_END | wxFULL_REPAINT_ON_RESIZE);
    
            const int ciwidths[] = { 0x10, -0x01, 0x50 };
            // width : -1 -> for stretchable field: appropriate
            // for various long hint display.
            m_statusbar.SetFieldsCount(cistatus_fields, ciwidths);
            this->SetStatusBar(&m_statusbar);
    
            SetStatusText(L"Welcome to wxStatusBar!", 0x01);
            SetStatusText(L"Custom Field", 0x02);
    
            m_bmpStatus = new wxStaticBitmap(&m_statusbar, wxID_ANY
                , wxBitmap::NewFromPNGData(bt_status_bitmap_j
                , sizeof bt_status_bitmap_j));
    
            m_statusbar.Bind(wxEVT_SIZE, &MainFrame::OnStatusBarSize, this);
        }
        else
        {
            this->SetStatusBar(NULL);
        }
    }
    //----------------------------------------------------------------------------
    

  6. And write the following for correct status bitmap display :
    //----------------------------------------------------------------------------
    void MainFrame::OnStatusBarSize(wxSizeEvent& evt)
    {
        // The glyph should be repositioned whenever the statusbar size changes:
        wxRect rt;
        if (!m_statusbar.GetFieldRect(sfGlyph, rt))
        {
            evt.Skip();
            return;
        }
        wxSize size(m_bmpStatus->GetSize());
    
        m_bmpStatus->Move(rt.x + (rt.width - size.x) / 0x02
            , rt.y + (rt.height - size.y) / 0x02);
    
        evt.Skip();
    }
    //---------------------------------------------------------------------------
    
    

  7. Implement the method in the frame constructor to actually create the toolbar at run time:
        AllocFreeToolBar(true);
        
        m_bmpStatus = NULL;
        AllocFreeStatusBar(true);
    

  8. Finally write code for the status bar removal in the frame close event handler as follows :
    //----------------------------------------------------------------------------
    void MainFrame::OnFrameClose(wxCloseEvent &evt)
    {
        AllocFreeStatusBar(false); // add anew
        if (m_bmpStatus)           // add anew
            delete m_bmpStatus;    // add anew
    
        AllocFreeToolBar(false);
        evt.Skip();
    }
    //----------------------------------------------------------------------------
    
    

  9. Run the project and we could see the following application with a toolbar and a status bar on screen:

    Job Done !

Download Source code for the project (Visual C++ 2013) : src_Test_Toolbar(+sb)_2.7z (795 KB)


Previous tutorial (1) Next tutorial (3)

No comments:

Post a Comment