一、自定义控件MyPanel
class MyPanel:public wxPanel
{
public:
MyPanel(wxPanel* parent);
int GetCurWidth(){return cur_width;};
void SetCurWidth(int n){cur_width = n;};
protected:
void OnSize(wxSizeEvent& event);
void OnPaint(wxPaintEvent& event);
wxPanel* m_parent;
int cur_width;
};
int num[] = {0,75,150,225,300,375,450,525,600,675};
int asize = sizeof(num) / sizeof(num[0]);
MyPanel::MyPanel(wxPanel* parent)
:wxPanel(parent,wxID_ANY)
{
// m_parent = parent;
int cur_width = 1;
this->SetMinSize(wxSize(300,30));
Bind(wxEVT_PAINT,wxPaintEventHandler(MyPanel::OnPaint),this);
Bind(wxEVT_SIZE,wxSizeEventHandler(MyPanel::OnSize),this);
this->SetAutoLayout(true);
}
void MyPanel::OnSize(wxSizeEvent& event)
{
Refresh();
}
void MyPanel::OnPaint(wxPaintEvent& event)
{
wxFont font(9,wxFONTFAMILY_DEFAULT
,wxFONTSTYLE_NORMAL
,wxFONTWEIGHT_NORMAL
,false
,"Arial"
);
font.SetSymbolicSize(wxFONTSIZE_SMALL );
wxPaintDC dc(this);
dc.SetFont(font);
wxSize sz = this->GetSize();
int width = sz.GetWidth();
int step = (int) round(width / 10.0);
int till = (int) ((width / 750.0) * cur_width);
int full = (int) ((width / 750.0) * 700);
dc.DrawLine(0,0,full,0);
if (cur_width >= 675)
{
dc.SetPen(wxPen(wxColour(255,255,184)));
dc.SetBrush(wxBrush(wxColour(255,255,184)));
dc.DrawRectangle(0,0,full,30);
dc.SetPen(wxPen(wxColour(255,175,175)));
dc.SetBrush(wxBrush(wxColour(255,175,175)));
dc.DrawRectangle(full,0,till - full,30);
}
else
{
dc.SetPen(*wxYELLOW);
dc.SetBrush(*wxYELLOW);
dc.DrawRectangle(0,0,till,30);
}
dc.SetPen(*wxBLACK);
for(int i =0; i< asize ;i++)
{
wxSize msz = dc.GetTextExtent(wxString::Format("%d",num[i]));
dc.DrawText(wxString::Format("%d",num[i])
,i*step - msz.GetWidth()/2
,8
);
dc.DrawLine(i*step,0,i*step,6);
}
}
二、主程序
class Simple:public wxFrame
{
public:
Simple(const wxString& title);
int GetCurWidth(){return cur_width;};
protected:
void OnScroll(wxScrollEvent& event);
wxPanel* panel;
wxSlider* m_slider;
MyPanel* m_wid;
int cur_width;
};
Simple::Simple(const wxString& title)
:wxFrame(NULL,-1,title)
{
cur_width = 75;
panel = new wxPanel(this,wxID_ANY);
wxPanel* centerPanel = new wxPanel(panel,wxID_ANY);
m_slider = new wxSlider(centerPanel,wxID_ANY,75,0,750,wxPoint(-1,-1),wxSize(150,-1),wxSL_LABELS);
wxBoxSizer* vbox = new wxBoxSizer(wxVERTICAL);
wxBoxSizer* hbox = new wxBoxSizer(wxHORIZONTAL);
wxBoxSizer* hbox2 = new wxBoxSizer(wxHORIZONTAL);
wxBoxSizer* hbox3 = new wxBoxSizer(wxHORIZONTAL);
m_wid = new MyPanel(panel);
m_wid->SetCurWidth(cur_width);
hbox->Add(m_wid,1,wxEXPAND);
hbox2->Add(centerPanel,1,wxEXPAND);
hbox3->Add(m_slider,0,wxTOP|wxLEFT,35);
centerPanel->SetSizer(hbox3);
vbox->Add(hbox2,1,wxEXPAND);
vbox->Add(hbox,0,wxEXPAND);
panel->SetSizer(vbox);
m_slider->SetFocus();
Bind(wxEVT_COMMAND_SLIDER_UPDATED
,wxScrollEventHandler(Simple::OnScroll)
,this
,m_slider->GetId());
Centre();
}
void Simple::OnScroll(wxScrollEvent& event)
{
cur_width = m_slider->GetValue();
m_wid->SetCurWidth(cur_width);
m_wid->Refresh();
}