The wxStaticText (wxWidgets: wxStaticText Class Reference) control allows read-only text to be displayed. It can be single line or multiline.

Single Line Example

The wxStaticText control is easy to use. Below we show a simple example of a single line wxStaticText. The example is a simple modification of the MinimalApp2 example we presented in the minimal application tutorial. We simply added a wxStaticText to the frame contents. The full source for this example is available from our GitHub repository: wxWidgetsTutorials/StandardControls/WxStaticText1.

 File: StandardControls/WxStaticText1/src/WxStaticText1Frame.cpp
#include "WxStaticText1Frame.h"
#include <wx/panel.h>
#include <wx/stattext.h>
#include <wx/sizer.h>

WxStaticText1Frame::WxStaticText1Frame(const wxString& title)
    : wxFrame(NULL, wxID_ANY, title)
    // Create a top-level panel to hold all the contents of the frame
    wxPanel* panel = new< wxPanel(this, wxID_ANY);

    // Create the wxStaticText control
    wxStaticText* staticText = new wxStaticText(panel, wxID_ANY, "Static Text");

    // Set up the sizer for the panel
    wxBoxSizer* panelSizer = new wxBoxSizer(wxHORIZONTAL);
    panelSizer->Add(staticText, 1, wxEXPAND);

    // Set up the sizer for the frame and resize the frame
    // according to its contents
    wxBoxSizer* topSizer = new wxBoxSizer(wxHORIZONTAL);
    topSizer->SetMinSize(215, 50);
    topSizer->Add(panel, 1, wxEXPAND);

The application is shown in Figure 1 below.

Figure 1: The WxStaticText1 Application

The rest of the files don't have any significant changes but are shown here for completeness.

 File: StandardControls/WxStaticText1/src/WxStaticText1App.h

#include <wx/app.h>

class WxStaticText1App : public wxApp
    virtual bool OnInit();

 File: StandardControls/WxStaticText1/src/WxStaticText1App.cpp
#include "WxStaticText1App.h"
#include "WxStaticText1Frame.h"


bool WxStaticText1App::OnInit()
    WxStaticText1Frame* frame = new WxStaticText1Frame("WxStaticText1");
    return true;
 File: StandardControls/WxStaticText1/src/WxStaticText1Frame.h

#include <wx/frame.h>

class WxStaticText1Frame : public wxFrame
    WxStaticText1Frame(const wxString& title);