在wxWidgets中,想要通过其本身的控件来实现图片+文件的按钮,貌似不太容易做到。但是可以通过重载wxControl来自绘图片+文件按钮。下面给出的是已经封装好的按钮类:
wxBitmapButtonEx.h
#ifndef _BITMAP_BUTTON_EX_H #define _BITMAP_BUTTON_EX_H #include "wx/wx.h" enum eBitmapButtonStatus { BitmapButtonNormal, BitmapButtonEnter, BitmapButtonDown, BitmapButtonUp, BitmapButtonLeave, BitmapButtonDClick, BitmapButtonDisable }; class wxBitmapButtonEx : public wxControl { DECLARE_DYNAMIC_CLASS(wxBitmapButtonEx) DECLARE_EVENT_TABLE() public: wxBitmapButtonEx(); virtual ~wxBitmapButtonEx(); wxBitmapButtonEx(wxWindow* parent, wxWindowID id, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = wxBORDER_NONE, const wxValidator& validator = wxDefaultValidator); bool Create(wxWindow* parent, wxWindowID id, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = wxSUNKEN_BORDER, const wxValidator& validator = wxDefaultValidator); wxSize DoGetBestSize() const; void OnPaint(wxPaintEvent& event); virtual void OnEnter(wxMouseEvent& event); virtual void OnLeave(wxMouseEvent& event); virtual void OnDown(wxMouseEvent& event); virtual void OnDClick(wxMouseEvent& event); virtual void OnUp(wxMouseEvent& event); virtual bool Enable(bool enable = true); virtual bool Disable(); //设置正常图片 wxBitmapButtonEx* set_normal_bitmap(wxBitmap* bitmap); //设置按钮按下时的切换图片 wxBitmapButtonEx* set_down_bitmap(wxBitmap* bitmap); //设置按钮按经过时的切换图片 wxBitmapButtonEx* set_enter_bitmap(wxBitmap* bitmap); //设置Disable图片. wxBitmapButtonEx* set_disable_bitmap(wxBitmap* bitmap); //设置背景图片 wxBitmapButtonEx* set_background(const wxBitmap& bitmap); //设置背景颜色 bool set_background_colour(const wxColour& colour); //设置按钮文字 void set_button_text(wxString str); //设置文字字体 void set_text_font(wxFont font); protected: void DrawExistBitmap(wxDC* dc,wxBitmap* image1,wxBitmap* exist_image); private: wxBitmap background_bitmap; bool is_used_bg; wxBitmap* normal_bitmap; wxBitmap* down_bitmap; wxBitmap* enter_bitmap; wxBitmap* disable_bitmap; int button_status; wxString text; wxFont text_font; void DrawBackground(wxDC* dc); }; #endif /* _BITMAP_BUTTON_EX_H */
wxBitmapButtonEx.cpp
#include "wxBitmapButtonEx.h" #includeBEGIN_EVENT_TABLE(wxBitmapButtonEx, wxControl) EVT_PAINT(wxBitmapButtonEx::OnPaint) EVT_ENTER_WINDOW(wxBitmapButtonEx::OnEnter) EVT_LEAVE_WINDOW(wxBitmapButtonEx::OnLeave) EVT_LEFT_DOWN(wxBitmapButtonEx::OnDown) EVT_LEFT_DCLICK(wxBitmapButtonEx::OnDClick) EVT_LEFT_UP(wxBitmapButtonEx::OnUp) END_EVENT_TABLE() IMPLEMENT_DYNAMIC_CLASS(wxBitmapButtonEx, wxControl) wxBitmapButtonEx::wxBitmapButtonEx() { } wxBitmapButtonEx::wxBitmapButtonEx(wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style, const wxValidator& validator) : normal_bitmap(NULL), down_bitmap(NULL), enter_bitmap(NULL) { Create(parent, id, pos, size, style, validator); } wxBitmapButtonEx::~wxBitmapButtonEx() { wxDELETE(normal_bitmap); wxDELETE(enter_bitmap); wxDELETE(down_bitmap); } bool wxBitmapButtonEx::Create(wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style, const wxValidator& validator) { normal_bitmap = NULL; down_bitmap = NULL; enter_bitmap = NULL; disable_bitmap = NULL; button_status = BitmapButtonNormal; if (!wxControl::Create(parent, id, pos, size, style, validator)) { return false; } SetBackgroundStyle(wxBG_STYLE_SYSTEM); is_used_bg = false; text_font.Create(12,wxSWISS,wxFONTSTYLE_NORMAL,wxNORMAL,false,_T("System"),wxFONTENCODING_DEFAULT); return true; } wxSize wxBitmapButtonEx::DoGetBestSize() const { return GetSize(); } void wxBitmapButtonEx::DrawExistBitmap(wxDC* dc, wxBitmap* image1, wxBitmap* exist_image) { int width = DoGetBestSize().GetWidth(); int height = DoGetBestSize().GetHeight(); //wxClientDC dc(this); dc->SetPen(*wxBLACK_PEN); wxColour clr(2,18,245); dc->SetTextForeground(*wxWHITE); //dc->SetBrush(*wxTRANSPARENT_BRUSH); //dc->SetBrush(*wxBLUE_BRUSH); //wxSystemSettings::GetColour(wxSYS_COLOUR_ACTIVECAPTION) wxBrush brush(clr);// dc->SetBrush(brush); dc->DrawRectangle(0, 0, width, height); /*wxRect rc = wxRect(0, 0, width, height); dc->DrawRoundedRectangle(rc,5);*/ int x = normal_bitmap->GetWidth(); int y = (height-normal_bitmap->GetHeight())/2.0; if (image1) { dc->DrawBitmap(*image1, width-x-5, y, true); //dc->DrawBitmap(*image1, 0, 0, true); } else { dc->DrawBitmap(*exist_image, width-x-5, y, true); //dc->DrawBitmap(*exist_image, 0, 0, true); } if (text.IsEmpty()) { return; } if (!text_font.IsNull()) { dc->SetFont(text_font); } int pointsize = text_font.GetPointSize(); //dc.SetLayoutDirection(wxLayout_Default); int bit = text.Find('\n'); if (bit!=-1) { wxString str; str = text.Left(bit); dc->DrawText(str,5,height/2-pointsize-2); int length = text.Length(); str = text.Mid(bit+1,length-bit-1); dc->DrawText(str,5,height/2); } else { dc->DrawText(text,5,height/2-7); } } void wxBitmapButtonEx::OnPaint(wxPaintEvent& event) { wxPaintDC dc(this); DrawBackground(&dc); //1.状态控制绘画,好处就是可以调用Refresh连背景一起刷新. switch (button_status) { case BitmapButtonNormal: case BitmapButtonUp: case BitmapButtonLeave: { int width = DoGetBestSize().GetWidth(); int height = DoGetBestSize().GetHeight(); wxClientDC dc(this); dc.SetPen(*wxGREY_PEN); dc.SetBrush(*wxTRANSPARENT_BRUSH); dc.DrawRectangle(0, 0, width, height); /*wxRect rc = wxRect(0, 0, width, height); dc.DrawRoundedRectangle(rc,5);*/ wxPoint pt[3]; pt[0].x = 1; pt[0].y = height-1; pt[1].x = pt[1].y = 1; pt[2].x = width-1; pt[2].y = 1; dc.SetPen(*wxWHITE_PEN); dc.DrawLines(3,pt); pt[1].x = width-1; pt[1].y = height-1; dc.SetPen(*wxBLACK_PEN); dc.DrawLines(3,pt); int x = normal_bitmap->GetWidth(); int y = (height-normal_bitmap->GetHeight())/2.0; dc.DrawBitmap(*normal_bitmap, width-x-5, y, true); if (!text_font.IsNull()) { dc.SetFont(text_font); } int pointsize = text_font.GetPointSize(); //dc.SetLayoutDirection(wxLayout_Default); int bit = text.Find('\n'); if (bit!=-1) { wxString str; str = text.Left(bit); dc.DrawText(str,5,height/2-pointsize-2); int length = text.Length(); str = text.Mid(bit+1,length-bit-1); dc.DrawText(str,5,height/2); } else { dc.DrawText(text,5,height/2-7); } } //dc.DrawBitmap(*normal_bitmap, 0, 0, true); break; case BitmapButtonEnter: // if (!enter_bitmap_) // { // int width = DoGetBestSize().GetWidth(); // int height = DoGetBestSize().GetHeight(); // wxClientDC dc(this); // dc.SetPen(*wxRED_PEN); // dc.SetBrush(*wxTRANSPARENT_BRUSH); // //dc.DrawRectangle(0, 0, width, height); //wxRect rc = wxRect(0, 0, width, height); //dc.DrawRoundedRectangle(rc,5); // // break; // } // dc.DrawBitmap(*enter_bitmap_, 0, 0, true); break; case BitmapButtonDown: DrawExistBitmap(&dc,down_bitmap,normal_bitmap); break; case BitmapButtonDClick: //DrawExistBitmap(&dc,down_bitmap,normal_bitmap); break; case BitmapButtonDisable: //DrawExistBitmap(&dc,disable_bitmap,normal_bitmap); break; default: { int width = DoGetBestSize().GetWidth(); int height = DoGetBestSize().GetHeight(); wxClientDC dc(this); dc.SetPen(*wxGREY_PEN); dc.SetBrush(*wxTRANSPARENT_BRUSH); dc.DrawRectangle(0, 0, width, height); /*wxRect rc = wxRect(0, 0, width, height); dc.DrawRoundedRectangle(rc,5);*/ wxPoint pt[3]; pt[0].x = 1; pt[0].y = height-1; pt[1].x = pt[1].y = 1; pt[2].x = width-1; pt[2].y = 1; dc.SetPen(*wxWHITE_PEN); dc.DrawLines(3,pt); pt[1].x = width-1; pt[1].y = height-1; dc.SetPen(*wxBLACK_PEN); dc.DrawLines(3,pt); int x = normal_bitmap->GetWidth(); int y = (height-normal_bitmap->GetHeight())/2.0; dc.DrawBitmap(*normal_bitmap, width-x-5, y, true); if (text.IsEmpty()) { break; } if (!text_font.IsNull()) { dc.SetFont(text_font); } int pointsize = text_font.GetPointSize(); //dc.SetLayoutDirection(wxLayout_Default); int bit = text.Find('\n'); if (bit!=-1) { wxString str; str = text.Left(bit); dc.DrawText(str,5,height/2-pointsize-2); int length = text.Length(); str = text.Mid(bit+1,length-bit-1); dc.DrawText(str,5,height/2); } else { dc.DrawText(text,5,height/2-7); } } break; } } void wxBitmapButtonEx::DrawBackground(wxDC* dc) { if (is_used_bg) { dc->DrawBitmap(background_bitmap, 0, 0, true); } else { wxBrush brush(GetBackgroundColour()); wxPen pen(GetBackgroundColour()); dc->SetBrush(brush); dc->SetPen(pen); dc->DrawRectangle(0, 0, GetSize().x, GetSize().y); } } void wxBitmapButtonEx::OnEnter(wxMouseEvent& event) { /* button_status = BitmapButtonEnter; Refresh(); Update();*/ } void wxBitmapButtonEx::OnLeave(wxMouseEvent& event) { if (!IsEnabled()) { return; } button_status = BitmapButtonLeave; Refresh(); Update(); } void wxBitmapButtonEx::OnDClick(wxMouseEvent& event) { /* button_status = BitmapButtonDown; Refresh(false); Update();*/ } void wxBitmapButtonEx::OnDown(wxMouseEvent& event) { button_status = BitmapButtonDown; Refresh(); Update(); } void wxBitmapButtonEx::OnUp(wxMouseEvent& event) { if (BitmapButtonDown != button_status) { return; } button_status = BitmapButtonUp; Refresh(); Update(); wxCommandEvent myEvent(wxEVT_COMMAND_BUTTON_CLICKED, GetId()); myEvent.SetEventObject(this); GetEventHandler()->ProcessEvent(myEvent); } wxBitmapButtonEx* wxBitmapButtonEx::set_normal_bitmap(wxBitmap* bitmap) { normal_bitmap = bitmap; return this; } wxBitmapButtonEx* wxBitmapButtonEx::set_down_bitmap(wxBitmap* bitmap) { down_bitmap = bitmap; return this; } wxBitmapButtonEx* wxBitmapButtonEx::set_enter_bitmap(wxBitmap* bitmap) { enter_bitmap = bitmap; return this; } bool wxBitmapButtonEx::Enable(bool enable) { if (enable) { button_status = BitmapButtonNormal; } else { button_status = BitmapButtonDisable; } Refresh(false); Update(); return wxControl::Enable(enable); } bool wxBitmapButtonEx::Disable() { return Enable(false); } wxBitmapButtonEx* wxBitmapButtonEx::set_disable_bitmap(wxBitmap* bitmap) { disable_bitmap = bitmap; return this; } wxBitmapButtonEx* wxBitmapButtonEx::set_background(const wxBitmap& bitmap) { is_used_bg = true; background_bitmap = bitmap; return this; } bool wxBitmapButtonEx::set_background_colour(const wxColour& colour) { is_used_bg = false; return wxControl::SetBackgroundColour(colour); } // void wxBitmapButtonEx::set_button_text(wxString str) { text = str; } // void wxBitmapButtonEx::set_text_font(wxFont font) { text_font = font; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。