OVERVIEW
Buttons and labels are the most common controls used in GUI. Here we create a sample button and label and display them in a window. Though we can directly create these controls in wxFrame and display them, the end result will not be as expected, since the controls will fill the whole wxFrame. So we use a wxPanel as a container to display the controls. We will explore Panels in the next section.
SAMPLE CODE
button.h
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
class Button: public wxFrame {
public:
Button(const wxString& title);
};
button.cpp
#include "button.h"
Button::Button(const wxString& title):
wxFrame(NULL, -1, title, wxDefaultPosition, wxSize(500,200)) {
wxPanel *panel = new wxPanel(this, -1);
wxButton *btn = new wxButton(panel, -1, wxT("Click me"), wxPoint(220,20));
wxStaticText *label = new wxStaticText(panel, -1, wxT("Click on the button"), wxPoint(1,20));
btn->SetBackgroundColour(wxColour(100,100,20));
Center();
}
main.h
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
class Main: public wxApp {
public:
virtual bool OnInit();
};
main.cpp
#include "main.h"
#include "button.h"
IMPLEMENT_APP(Main)
bool Main::OnInit() {
Button *app = new Button(wxT("Buttons and Labels"));
app->Show(true);
}
The output is shown below
Leave a Reply