wxSlider custom Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
mxoliveira73
Experienced Solver
Experienced Solver
Posts: 54
Joined: Sun May 05, 2019 7:12 am

wxSlider custom

Post by mxoliveira73 » Fri Jun 21, 2019 4:01 am

I'm planning to create a custom wxSlider. I'm thinking load a icon, drag it, and create a event custom to get x position and then link this value for my necessities....

Ok, I'm a little bit lost... but, could someone give tips about thi process, the steps that i have follow to this?

First image is a original wxSlider
second image is that i pretend develop....

Sem título3.jpg
Sem título3.jpg (4.69 KiB) Viewed 112 times

New Pagodi
Super wx Problem Solver
Super wx Problem Solver
Posts: 307
Joined: Tue Jun 20, 2006 6:47 pm
Contact:

Re: wxSlider custom

Post by New Pagodi » Fri Jun 21, 2019 4:46 am

This should get you started:

Code: Select all

// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

// for all others, include the necessary headers (this file is usually all you
// need because it includes almost all "standard" wxWidgets headers)
#ifndef WX_PRECOMP
    #include "wx/wx.h"
#endif

#include <wx/dcbuffer.h>

class CustomSlider:public wxWindow
{
    public:
        CustomSlider(wxWindow *parent, wxWindowID id = wxID_ANY,
                     const wxPoint &pos=wxDefaultPosition,
                     const wxSize &size=wxSize(200,10), long style=0);
    private:
        void OnPaint(wxPaintEvent&);
        void OnLeftDown(wxMouseEvent&);
        void OnLeftUp(wxMouseEvent&);
        void OnMouseMotion(wxMouseEvent&);
        int m_sliderPos;
};

CustomSlider::CustomSlider(wxWindow *parent, wxWindowID id, const wxPoint& pos,
                           const wxSize& size, long style)
             :wxWindow(parent, id,  pos,  size,  style)
{
    SetBackgroundStyle(wxBG_STYLE_PAINT);
    m_sliderPos = 10;

    Bind(wxEVT_PAINT,&CustomSlider::OnPaint,this);
    Bind(wxEVT_LEFT_DOWN,&CustomSlider::OnLeftDown,this);
}

void CustomSlider::OnPaint(wxPaintEvent&)
{
    wxAutoBufferedPaintDC dc(this);
    wxSize sz = GetClientSize();

    dc.Clear();
    dc.SetPen(*wxBLACK_PEN);
    dc.DrawLine(0, sz.GetHeight()/2, sz.GetWidth(), sz.GetHeight()/2);
    dc.DrawLine(m_sliderPos, 0, m_sliderPos, sz.GetHeight());
}

void CustomSlider::OnLeftDown(wxMouseEvent& event)
{
    m_sliderPos = event.GetX();
    CaptureMouse();
    Bind(wxEVT_LEFT_UP,&CustomSlider::OnLeftUp,this);
    Bind(wxEVT_MOTION,&CustomSlider::OnMouseMotion,this);
}

void CustomSlider::OnLeftUp(wxMouseEvent&)
{
    ReleaseMouse();
    Unbind(wxEVT_LEFT_UP,&CustomSlider::OnLeftUp,this);
    Unbind(wxEVT_MOTION,&CustomSlider::OnMouseMotion,this);
}

void CustomSlider::OnMouseMotion(wxMouseEvent& event)
{
    if ( GetClientRect().Contains(event.GetPosition()) )
    {
        if ( event.GetX() != m_sliderPos )
        {
            m_sliderPos = event.GetX();
            Refresh();
        }
    }
}

class MyFrame : public wxFrame
{
    public:
        MyFrame( wxWindow* parent, int id = wxID_ANY, wxString title = "Demo",
                 wxPoint pos = wxDefaultPosition, wxSize size = wxDefaultSize,
                 int style = wxDEFAULT_FRAME_STYLE|wxTAB_TRAVERSAL );
};

MyFrame::MyFrame(wxWindow* parent, int id, wxString title, wxPoint pos,
                wxSize size, int style )
        :wxFrame( parent, id, title, pos, size, style )
{
    CustomSlider* cs = new CustomSlider(this);

    wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
    sizer->Add(cs);
    this->SetSizer(sizer);
    Layout();
}

class MyApp : public wxApp
{
    public:
        virtual bool OnInit()
        {
            wxInitAllImageHandlers();
            MyFrame* frame = new MyFrame(NULL);
            frame->Show();
            return true;
        }
};

wxIMPLEMENT_APP(MyApp);
To create what you want, at a minimum you would need to adjust the OnPaint method to draw the fancy graphics you want instead of 2 lines. You might want to do other things like
  • Add Get/SetPosition methods.
  • Handle the size event.
  • Add some kind of scaling so that the slider can take values other than its pixel position.
  • Throw an event when the slider position changes.

mxoliveira73
Experienced Solver
Experienced Solver
Posts: 54
Joined: Sun May 05, 2019 7:12 am

Re: wxSlider custom

Post by mxoliveira73 » Fri Jun 21, 2019 8:05 am

New Pagodi... You were amazing.... I have no words to thank you man....

Post Reply