OVERVIEW
wxWidgets provides a class called wxScrolled<T> which is a template for specialized scrolling classes. It has two implementations of this class:
- wxScrolledCanvas
- wxScrolledWindow
In this section, we look at wxScrolledWindow. wxScrolledWindow is actually wxScrolled(wxPanel) in that, it wraps a wxPanel into wxScroll.
In the sample code below, we add 50 labels to a wxScrolledWindow, which is only vertically scrollable.
SAMPLE CODE
scrolledwindow.h
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
class ScrolledWindow: public wxFrame {
public:
ScrolledWindow(const wxString &title);
private:
wxStaticText *mText[50];
};
scrolledwindow.cpp
#include "scrolledwindow.h"
ScrolledWindow::ScrolledWindow(const wxString &title):
wxFrame(NULL, -1, title, wxDefaultPosition, wxSize(700,550)) {
wxScrolledWindow *sw = new wxScrolledWindow(this, -1, wxDefaultPosition, wxDefaultSize, wxVSCROLL);
wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);
sw->SetSizer(vbox);
wxString text = wxT("");
for(int i =0; i < 50; i++) {
text = wxT("This is row ");
mText[i] = new wxStaticText(sw, -1, text.Append(wxString::Format(wxT("%d"), (i+1))));
vbox->Add(mText[i],1, wxEXPAND);
}
Center();
int w=0, h=0;
GetSize(&w, &h);
sw->SetScrollbars(0,10,0, h/10);
}
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 "scrolledwindow.h"
IMPLEMENT_APP(Main)
bool Main::OnInit() {
ScrolledWindow *app = new ScrolledWindow(wxT("Scrolled Window"));
app->Show(true);
}
The output is shown below
Leave a Reply