Page 1 of 1

WxButton - mouse hover & border size

Posted: Sun Jul 28, 2019 10:06 am
by Standalone
Hello Everyone!,

How do I add the mouse hover effect to the Wxbutton and change the border size? and I went through https://docs.wxwidgets.org/trunk/classw ... 43a8d21563

there is something called "void wxAnyButton::SetBitmapCurrent(const wxBitmap & bitmap)" for mouse hover, but I don't know how to use it and where to add it.

I'm using Windows 7, Wxsmith, Code blocks and I'm new to Wxwidgets please help me.


here is my main.cpp

Code: Select all

#include "_Main.h"
#include <wx/msgdlg.h>


//(*InternalHeaders(_Frame)
#include <wx/font.h>
#include <wx/intl.h>
#include <wx/settings.h>
#include <wx/string.h>
//*)

//helper functions
enum wxbuildinfoformat {
    short_f, long_f };

wxString wxbuildinfo(wxbuildinfoformat format)
{
    wxString wxbuild(wxVERSION_STRING);

    if (format == long_f )
    {
#if defined(__WXMSW__)
        wxbuild << _T("-Windows");
#elif defined(__UNIX__)
        wxbuild << _T("-Linux");
#endif

#if wxUSE_UNICODE
        wxbuild << _T("-Unicode build");
#else
        wxbuild << _T("-ANSI build");
#endif // wxUSE_UNICODE
    }

    return wxbuild;
}

//(*IdInit(_Frame)
const long _Frame::ID_STATICTEXT1 = wxNewId();
const long _Frame::ID_STATICTEXT2 = wxNewId();
const long _Frame::ID_TEXTCTRL1 = wxNewId();
const long _Frame::ID_STATICTEXT3 = wxNewId();
const long _Frame::ID_TEXTCTRL2 = wxNewId();
const long _Frame::ID_BUTTON1 = wxNewId();
//*)

BEGIN_EVENT_TABLE(_Frame,wxFrame)
    //(*EventTable(_Frame)
    //*)
END_EVENT_TABLE()

_Frame::_Frame(wxWindow* parent,wxWindowID id)
{
    //(*Initialize(_Frame)
    wxBoxSizer* BoxSizer2;
    wxBoxSizer* BoxSizer3;

    Create(parent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE, _T("wxID_ANY"));
    SetClientSize(wxSize(385,392));
    SetMinSize(wxSize(-1,-1));
    SetMaxSize(wxSize(-1,-1));
    SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_MENUTEXT));
    BoxSizer1 = new wxBoxSizer(wxVERTICAL);
    StaticText1 = new wxStaticText(this, ID_STATICTEXT1, _("LOGIN"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT1"));
    StaticText1->SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
    wxFont StaticText1Font(36,wxFONTFAMILY_SWISS,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_BOLD,false,_T("Calibri"),wxFONTENCODING_DEFAULT);
    StaticText1->SetFont(StaticText1Font);
    BoxSizer1->Add(StaticText1, 0, wxALIGN_CENTER_HORIZONTAL, 5);
    BoxSizer1->Add(10,60,0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    BoxSizer2 = new wxBoxSizer(wxHORIZONTAL);
    StaticText2 = new wxStaticText(this, ID_STATICTEXT2, _("Username"), wxDefaultPosition, wxSize(70,20), 0, _T("ID_STATICTEXT2"));
    StaticText2->SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
    wxFont StaticText2Font(12,wxFONTFAMILY_SWISS,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_BOLD,false,_T("Calibri"),wxFONTENCODING_DEFAULT);
    StaticText2->SetFont(StaticText2Font);
    BoxSizer2->Add(StaticText2, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    BoxSizer2->Add(25,10,0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    TextCtrl1 = new wxTextCtrl(this, ID_TEXTCTRL1, wxEmptyString, wxPoint(-1,-1), wxSize(250,20), wxSIMPLE_BORDER, wxDefaultValidator, _T("ID_TEXTCTRL1"));
    TextCtrl1->SetForegroundColour(wxColour(255,255,255));
    TextCtrl1->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
    wxFont TextCtrl1Font(12,wxFONTFAMILY_SWISS,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_NORMAL,false,_T("Calibri"),wxFONTENCODING_DEFAULT);
    TextCtrl1->SetFont(TextCtrl1Font);
    BoxSizer2->Add(TextCtrl1, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    BoxSizer1->Add(BoxSizer2, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    BoxSizer1->Add(20,1,0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    BoxSizer3 = new wxBoxSizer(wxHORIZONTAL);
    StaticText3 = new wxStaticText(this, ID_STATICTEXT3, _("Password"), wxDefaultPosition, wxSize(70,20), 0, _T("ID_STATICTEXT3"));
    StaticText3->SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
    wxFont StaticText3Font(12,wxFONTFAMILY_SWISS,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_BOLD,false,_T("Calibri"),wxFONTENCODING_DEFAULT);
    StaticText3->SetFont(StaticText3Font);
    BoxSizer3->Add(StaticText3, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    BoxSizer3->Add(25,10,0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    TextCtrl2 = new wxTextCtrl(this, ID_TEXTCTRL2, wxEmptyString, wxDefaultPosition, wxSize(250,20), wxTE_PASSWORD|wxSIMPLE_BORDER, wxDefaultValidator, _T("ID_TEXTCTRL2"));
    TextCtrl2->SetForegroundColour(wxColour(255,255,255));
    TextCtrl2->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
    wxFont TextCtrl2Font(12,wxFONTFAMILY_SWISS,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_NORMAL,false,_T("Calibri"),wxFONTENCODING_DEFAULT);
    TextCtrl2->SetFont(TextCtrl2Font);
    BoxSizer3->Add(TextCtrl2, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    BoxSizer1->Add(BoxSizer3, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    BoxSizer1->Add(10,60,0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    Button1 = new wxButton(this, ID_BUTTON1, _("Sign in"), wxDefaultPosition, wxSize(260,25), wxNO_BORDER, wxDefaultValidator, _T("ID_BUTTON1"));
    Button1->SetBackgroundColour(wxColour(0,128,255));
    wxFont Button1Font(12,wxFONTFAMILY_SWISS,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_BOLD,false,_T("Calibri"),wxFONTENCODING_DEFAULT);
    Button1->SetFont(Button1Font);
    BoxSizer1->Add(Button1, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    BoxSizer1->Add(20,30,0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    SetSizer(BoxSizer1);
    SetSizer(BoxSizer1);
    Layout();
    Center();
    //*)
}

_Frame::~_Frame()
{
    //(*Destroy(_Frame)
    //*)
}

void _Frame::OnQuit(wxCommandEvent& event)
{
    Close();
}

void _Frame::OnAbout(wxCommandEvent& event)
{
    wxString msg = wxbuildinfo(long_f);
    wxMessageBox(msg, _("Welcome to..."));
}

Re: WxButton - mouse hover & border size

Posted: Sun Jul 28, 2019 11:46 am
by doublemax
wxAnyButton is a base class of wxButton, so you can use its methods on a wxButton. AFAIK you can't change the border size.

Button background color change when the mouse is over the button

Posted: Mon Jul 29, 2019 11:28 am
by Standalone
I used a Wxbutton in my Test frame, will I able to change the background color when the mouse is over the wxbutton.

I did it according to https://docs.wxwidgets.org/trunk/classw ... 43a8d21563 this please correct me if I'm wrong. :|

1. I include this in top of my mian.cpp

Code: Select all

#include <wx/anybutton.h>
2. then I added these two functions in mian.cpp

Code: Select all

wxAnyButton::GetBitmapCurrent()const{}
wxAnyButton::SetBitmapCurrent(const wxBitmap& bitmap){}
Here is my Button Details:
var name -> button1
class name -> wxbutton
Identifier -> ID_BUTTON1

but it gives me errors like this: :cry:

TestMain.cpp|62|error: no 'int wxAnyButton::GetBitmapCurrent() const' member function declared in class 'wxAnyButton'|
TestMain.cpp|67|error: no 'int wxAnyButton::SetBitmapCurrent(const wxBitmap&)' member function declared in class 'wxAnyButton'|

please if anyone can give a brief explanation of how to do it.....thank you. [-o<

Re: WxButton - mouse hover & border size

Posted: Mon Jul 29, 2019 11:48 am
by doublemax
Based on the code from your original post:

Code: Select all

Button1 = new wxButton(this, ID_BUTTON1, _("Sign in"), wxDefaultPosition, wxSize(260,25), wxNO_BORDER, wxDefaultValidator, _T("ID_BUTTON1"));
Button1->SetBackgroundColour(wxColour(0,128,255));
Button1->SetBitmapCurrent( .. );
That's all you need to do.

Re: WxButton - mouse hover & border size

Posted: Mon Jul 29, 2019 1:27 pm
by Standalone
doublemax wrote:
Mon Jul 29, 2019 11:48 am
Based on the code from your original post:

Code: Select all

Button1 = new wxButton(this, ID_BUTTON1, _("Sign in"), wxDefaultPosition, wxSize(260,25), wxNO_BORDER, wxDefaultValidator, _T("ID_BUTTON1"));
Button1->SetBackgroundColour(wxColour(0,128,255));
Button1->SetBitmapCurrent( .. );
That's all you need to do.

Thank you for your help it works fine, but I like to change the background color of my button when the mouse is over the button. is there a way I can do that?

Re: WxButton - mouse hover & border size

Posted: Mon Jul 29, 2019 1:42 pm
by doublemax
Not in the same way.

My idea was that the button consists of the bitmap only (no border and has the exact size as the bitmap). Then you can provide custom bitmaps and make the button look however you like.