wxWidgets实现图片和文件按钮
在wxWidgets中,想要通过其本身的控件来实现图片+文件的按钮,貌似不太容易做到。但是可以通过重载wxControl来自绘图片+文件按钮。下面给出的是已经封装好的按钮类:
wxBitmapButtonEx.h
#ifndef_BITMAP_BUTTON_EX_H #define_BITMAP_BUTTON_EX_H #include"wx/wx.h" enumeBitmapButtonStatus { BitmapButtonNormal, BitmapButtonEnter, BitmapButtonDown, BitmapButtonUp, BitmapButtonLeave, BitmapButtonDClick, BitmapButtonDisable }; classwxBitmapButtonEx:publicwxControl { DECLARE_DYNAMIC_CLASS(wxBitmapButtonEx) DECLARE_EVENT_TABLE() public: wxBitmapButtonEx(); virtual~wxBitmapButtonEx(); wxBitmapButtonEx(wxWindow*parent,wxWindowIDid, constwxPoint&pos=wxDefaultPosition, constwxSize&size=wxDefaultSize, longstyle=wxBORDER_NONE, constwxValidator&validator=wxDefaultValidator); boolCreate(wxWindow*parent,wxWindowIDid, constwxPoint&pos=wxDefaultPosition, constwxSize&size=wxDefaultSize, longstyle=wxSUNKEN_BORDER, constwxValidator&validator=wxDefaultValidator); wxSizeDoGetBestSize()const; voidOnPaint(wxPaintEvent&event); virtualvoidOnEnter(wxMouseEvent&event); virtualvoidOnLeave(wxMouseEvent&event); virtualvoidOnDown(wxMouseEvent&event); virtualvoidOnDClick(wxMouseEvent&event); virtualvoidOnUp(wxMouseEvent&event); virtualboolEnable(boolenable=true); virtualboolDisable(); //设置正常图片 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(constwxBitmap&bitmap); //设置背景颜色 boolset_background_colour(constwxColour&colour); //设置按钮文字 voidset_button_text(wxStringstr); //设置文字字体 voidset_text_font(wxFontfont); protected: voidDrawExistBitmap(wxDC*dc,wxBitmap*image1,wxBitmap*exist_image); private: wxBitmapbackground_bitmap; boolis_used_bg; wxBitmap*normal_bitmap; wxBitmap*down_bitmap; wxBitmap*enter_bitmap; wxBitmap*disable_bitmap; intbutton_status; wxStringtext; wxFonttext_font; voidDrawBackground(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,wxWindowIDid, constwxPoint&pos, constwxSize&size, longstyle, constwxValidator&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); } boolwxBitmapButtonEx::Create(wxWindow*parent,wxWindowIDid, constwxPoint&pos, constwxSize&size, longstyle, constwxValidator&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)) { returnfalse; } SetBackgroundStyle(wxBG_STYLE_SYSTEM); is_used_bg=false; text_font.Create(12,wxSWISS,wxFONTSTYLE_NORMAL,wxNORMAL,false,_T("System"),wxFONTENCODING_DEFAULT); returntrue; } wxSizewxBitmapButtonEx::DoGetBestSize()const { returnGetSize(); } voidwxBitmapButtonEx::DrawExistBitmap(wxDC*dc,wxBitmap*image1, wxBitmap*exist_image) { intwidth=DoGetBestSize().GetWidth(); intheight=DoGetBestSize().GetHeight(); //wxClientDCdc(this); dc->SetPen(*wxBLACK_PEN); wxColourclr(2,18,245); dc->SetTextForeground(*wxWHITE); //dc->SetBrush(*wxTRANSPARENT_BRUSH); //dc->SetBrush(*wxBLUE_BRUSH); //wxSystemSettings::GetColour(wxSYS_COLOUR_ACTIVECAPTION) wxBrushbrush(clr);// dc->SetBrush(brush); dc->DrawRectangle(0,0,width,height); /*wxRectrc=wxRect(0,0,width,height); dc->DrawRoundedRectangle(rc,5);*/ intx=normal_bitmap->GetWidth(); inty=(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); } intpointsize=text_font.GetPointSize(); //dc.SetLayoutDirection(wxLayout_Default); intbit=text.Find('\n'); if(bit!=-1) { wxStringstr; str=text.Left(bit); dc->DrawText(str,5,height/2-pointsize-2); intlength=text.Length(); str=text.Mid(bit+1,length-bit-1); dc->DrawText(str,5,height/2); } else { dc->DrawText(text,5,height/2-7); } } voidwxBitmapButtonEx::OnPaint(wxPaintEvent&event) { wxPaintDCdc(this); DrawBackground(&dc); //1.状态控制绘画,好处就是可以调用Refresh连背景一起刷新. switch(button_status) { caseBitmapButtonNormal: caseBitmapButtonUp: caseBitmapButtonLeave: { intwidth=DoGetBestSize().GetWidth(); intheight=DoGetBestSize().GetHeight(); wxClientDCdc(this); dc.SetPen(*wxGREY_PEN); dc.SetBrush(*wxTRANSPARENT_BRUSH); dc.DrawRectangle(0,0,width,height); /*wxRectrc=wxRect(0,0,width,height); dc.DrawRoundedRectangle(rc,5);*/ wxPointpt[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); intx=normal_bitmap->GetWidth(); inty=(height-normal_bitmap->GetHeight())/2.0; dc.DrawBitmap(*normal_bitmap,width-x-5,y,true); if(!text_font.IsNull()) { dc.SetFont(text_font); } intpointsize=text_font.GetPointSize(); //dc.SetLayoutDirection(wxLayout_Default); intbit=text.Find('\n'); if(bit!=-1) { wxStringstr; str=text.Left(bit); dc.DrawText(str,5,height/2-pointsize-2); intlength=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; caseBitmapButtonEnter: //if(!enter_bitmap_) //{ //intwidth=DoGetBestSize().GetWidth(); //intheight=DoGetBestSize().GetHeight(); //wxClientDCdc(this); //dc.SetPen(*wxRED_PEN); //dc.SetBrush(*wxTRANSPARENT_BRUSH); ////dc.DrawRectangle(0,0,width,height); //wxRectrc=wxRect(0,0,width,height); //dc.DrawRoundedRectangle(rc,5); // //break; //} //dc.DrawBitmap(*enter_bitmap_,0,0,true); break; caseBitmapButtonDown: DrawExistBitmap(&dc,down_bitmap,normal_bitmap); break; caseBitmapButtonDClick: //DrawExistBitmap(&dc,down_bitmap,normal_bitmap); break; caseBitmapButtonDisable: //DrawExistBitmap(&dc,disable_bitmap,normal_bitmap); break; default: { intwidth=DoGetBestSize().GetWidth(); intheight=DoGetBestSize().GetHeight(); wxClientDCdc(this); dc.SetPen(*wxGREY_PEN); dc.SetBrush(*wxTRANSPARENT_BRUSH); dc.DrawRectangle(0,0,width,height); /*wxRectrc=wxRect(0,0,width,height); dc.DrawRoundedRectangle(rc,5);*/ wxPointpt[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); intx=normal_bitmap->GetWidth(); inty=(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); } intpointsize=text_font.GetPointSize(); //dc.SetLayoutDirection(wxLayout_Default); intbit=text.Find('\n'); if(bit!=-1) { wxStringstr; str=text.Left(bit); dc.DrawText(str,5,height/2-pointsize-2); intlength=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; } } voidwxBitmapButtonEx::DrawBackground(wxDC*dc) { if(is_used_bg) { dc->DrawBitmap(background_bitmap,0,0,true); }else { wxBrushbrush(GetBackgroundColour()); wxPenpen(GetBackgroundColour()); dc->SetBrush(brush); dc->SetPen(pen); dc->DrawRectangle(0,0,GetSize().x,GetSize().y); } } voidwxBitmapButtonEx::OnEnter(wxMouseEvent&event) { /*button_status=BitmapButtonEnter; Refresh(); Update();*/ } voidwxBitmapButtonEx::OnLeave(wxMouseEvent&event) { if(!IsEnabled()) { return; } button_status=BitmapButtonLeave; Refresh(); Update(); } voidwxBitmapButtonEx::OnDClick(wxMouseEvent&event) { /*button_status=BitmapButtonDown; Refresh(false); Update();*/ } voidwxBitmapButtonEx::OnDown(wxMouseEvent&event) { button_status=BitmapButtonDown; Refresh(); Update(); } voidwxBitmapButtonEx::OnUp(wxMouseEvent&event) { if(BitmapButtonDown!=button_status) { return; } button_status=BitmapButtonUp; Refresh(); Update(); wxCommandEventmyEvent(wxEVT_COMMAND_BUTTON_CLICKED,GetId()); myEvent.SetEventObject(this); GetEventHandler()->ProcessEvent(myEvent); } wxBitmapButtonEx*wxBitmapButtonEx::set_normal_bitmap(wxBitmap*bitmap) { normal_bitmap=bitmap; returnthis; } wxBitmapButtonEx*wxBitmapButtonEx::set_down_bitmap(wxBitmap*bitmap) { down_bitmap=bitmap; returnthis; } wxBitmapButtonEx*wxBitmapButtonEx::set_enter_bitmap(wxBitmap*bitmap) { enter_bitmap=bitmap; returnthis; } boolwxBitmapButtonEx::Enable(boolenable) { if(enable) { button_status=BitmapButtonNormal; }else { button_status=BitmapButtonDisable; } Refresh(false); Update(); returnwxControl::Enable(enable); } boolwxBitmapButtonEx::Disable() { returnEnable(false); } wxBitmapButtonEx*wxBitmapButtonEx::set_disable_bitmap(wxBitmap*bitmap) { disable_bitmap=bitmap; returnthis; } wxBitmapButtonEx*wxBitmapButtonEx::set_background(constwxBitmap&bitmap) { is_used_bg=true; background_bitmap=bitmap; returnthis; } boolwxBitmapButtonEx::set_background_colour(constwxColour&colour) { is_used_bg=false; returnwxControl::SetBackgroundColour(colour); } // voidwxBitmapButtonEx::set_button_text(wxStringstr) { text=str; } // voidwxBitmapButtonEx::set_text_font(wxFontfont) { text_font=font; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。