OVERVIEW
wxSliders allow the user to control a range of values , which represent the starting and ending point of the slider. Every time the slider value changes it triggers an event. This event can be handled to do whatever processing we need. The sample code below updates a wxStaticText to show the current value of the wxSlider, when it slides.
SAMPLE CODE
slider.h
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
#include <wx/slider.h>
class Slider: public wxFrame {
public:
Slider(const wxString &title);
void onScroll(wxCommandEvent &event);
private:
wxSlider *mSlider;
wxStaticText *mLabel;
};
slider.cpp
#include "slider.h"
Slider::Slider(const wxString &title):
wxFrame(NULL, -1, title, wxDefaultPosition, wxSize(500,200)) {
wxPanel *panel = new wxPanel(this, -1);
wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);
panel->SetSizer(vbox);
mSlider = new wxSlider(panel, -1, 0,0,100,wxDefaultPosition, wxDefaultSize);
mLabel = new wxStaticText(panel, -1, wxT(""));
vbox->Add(mSlider,1,wxEXPAND);
vbox->Add(mLabel, 1, wxEXPAND);
mSlider->Bind(wxEVT_SLIDER, &Slider::onScroll, this);
Center();
}
void Slider::onScroll(wxCommandEvent &event) {
int val = mSlider->GetValue();
mLabel->SetLabel(wxString::Format(wxT("%d"), val));
}
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 "slider.h"
IMPLEMENT_APP(Main)
bool Main::OnInit() {
Slider *app = new Slider(wxT("Slider"));
app->Show(true);
}
The output is shown below
Leave a Reply