wxStaticText
Introduction
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.
#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); panel->SetSizer(panelSizer); // 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); SetSizerAndFit(topSizer); }
The application is shown in Figure 1 below.
The rest of the files don't have any significant changes but are shown here for completeness.
#ifndef _TUTORIALS_WXWIDGETS_WXSTATICTEXT1APP_H_ #define _TUTORIALS_WXWIDGETS_WXSTATICTEXT1APP_H_ #include <wx/app.h> class WxStaticText1App : public wxApp { public: virtual bool OnInit(); }; #endif
#include "WxStaticText1App.h" #include "WxStaticText1Frame.h" wxIMPLEMENT_APP(WxStaticText1App); bool WxStaticText1App::OnInit() { WxStaticText1Frame* frame = new WxStaticText1Frame("WxStaticText1"); frame->Show(true); return true; }
#ifndef _TUTORIALS_WXWIDGETS_WXSTATICTEXT1FRAME_H_ #define _TUTORIALS_WXWIDGETS_WXSTATICTEXT1FRAME_H_ #include <wx/frame.h> class WxStaticText1Frame : public wxFrame { public: WxStaticText1Frame(const wxString& title); }; #endif