使用wxWidgets怎么實(shí)現(xiàn)圖片和文件按鈕?針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。
創(chuàng)新互聯(lián)是專業(yè)的隆子網(wǎng)站建設(shè)公司,隆子接單;提供網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站,網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行隆子網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!
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(); //設(shè)置正常圖片 wxBitmapButtonEx* set_normal_bitmap(wxBitmap* bitmap); //設(shè)置按鈕按下時(shí)的切換圖片 wxBitmapButtonEx* set_down_bitmap(wxBitmap* bitmap); //設(shè)置按鈕按經(jīng)過時(shí)的切換圖片 wxBitmapButtonEx* set_enter_bitmap(wxBitmap* bitmap); //設(shè)置Disable圖片. wxBitmapButtonEx* set_disable_bitmap(wxBitmap* bitmap); //設(shè)置背景圖片 wxBitmapButtonEx* set_background(const wxBitmap& bitmap); //設(shè)置背景顏色 bool set_background_colour(const wxColour& colour); //設(shè)置按鈕文字 void set_button_text(wxString str); //設(shè)置文字字體 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.狀態(tài)控制繪畫,好處就是可以調(diào)用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; }
關(guān)于使用wxWidgets怎么實(shí)現(xiàn)圖片和文件按鈕問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。