wxwidgets应用手记(4) 看看,一个主界面

未命名

下面是2个主要的类:

MyMenu.h

   1: #pragma once
   2:  
   3:  
   4:  
   5: class MyMenu
   6: {
   7: public:
   8:     MyMenu(wxString text,wxImage* img,int index,wxFrame* frame);
   9:     virtual ~MyMenu(void);
  10:  
  11: private:
  12:     wxString m_text;
  13:     wxBitmap* m_bmp;
  14:     int m_index;
  15:     wxFrame* m_frame; 
  16:     wxArrayString m_subMenuTexts;
  17:  
  18: public:
  19:     int GetX(int menuLen);
  20:     int GetY(int menuLen);
  21:     int GetWidth();
  22:     int GetHeight();
  23:     wxBitmap& GetBMP();
  24:     wxString& GetText();
  25:  
  26:     int GetSubMenuCount();
  27:     wxString& GetSubMenuText(int index);
  28:     void AddSubMenuTexts(wxString* text,int len);
  29:  
  30: public:
  31:     bool IsInRect(int x,int y,int menuLen);
  32: };
  33:  

MyMenuCollection.h

   1: #pragma once
   2:  
   3: #include <wx/dynarray.h>
   4: #include "MyMenu.h"
   5:  
   6:  
   7: class IMyMenuCollection
   8: {
   9: public:
  10:     virtual bool GetSubMenuEnabled(int a,const wxString& A,int b,const wxString& B)=0;
  11:     virtual void SubMenuInvoke(int a,const wxString& A,int b,const wxString& B)=0;
  12: };
  13:  
  14: class MyMenuCollection
  15: {
  16: public:
  17:     MyMenuCollection(wxFrame* frame,IMyMenuCollection* fun);
  18:     virtual ~MyMenuCollection(void);
  19:  
  20: public:
  21:     void AddBackGroundBMP(wxImage* img);
  22:     void AddMenu(wxString text,wxImage* img);
  23:     void AddSubMenuBMP(wxImage* enabled,wxImage* disabled,wxImage* active = NULL);
  24:  
  25: public:
  26:     int GetCount();
  27:     MyMenu* GetItem(int index);
  28:  
  29: public:
  30:     void OnMouseEvents( wxMouseEvent& event );
  31:     void OnPaint(wxDC& dc);
  32:  
  33: protected:
  34:     int GetSubMenu_X(int index);
  35:     int GetSubMenu_Y(int index);
  36:     bool IsInSubMenuRect(int x,int y,int index);
  37:  
  38:  
  39:  
  40: private:
  41:     IMyMenuCollection* m_fun;
  42:     wxFrame* m_frame;
  43:     wxBitmap* m_backgroundBmp;
  44:     wxBitmap* m_subMenuBmp1;//enabled
  45:     wxBitmap* m_subMenuBmp2;//disabled
  46:     wxBitmap* m_subMenuBmp3;//active
  47:     wxArrayPtrVoid m_collection;
  48:  
  49:     int m_mouseEnter;
  50:     int m_mouseDown;
  51:     int m_selected;
  52:  
  53:     int m_subMouseEnter;
  54: };
  55:  

MyMenu.cpp

   1: #include "MyMenu.h"
   2: #include "math.h"
   3:  
   4: #define  RadiusA 300 //椭圆半径1
   5: #define  RadiusB 200 //椭圆半径2
   6:  
   7:  
   8: MyMenu::MyMenu(wxString text,wxImage* img,int index,wxFrame* frame)
   9: {
  10:     this->m_text = text;
  11:     this->m_bmp = new wxBitmap(img->Rescale(GetWidth(),GetHeight()));
  12:     this->m_index = index;
  13:     this->m_frame = frame;
  14: }
  15:  
  16: MyMenu::~MyMenu(void)
  17: {
  18:     delete m_bmp;
  19: }
  20:  
  21: int MyMenu::GetX(int menuLen)
  22: {
  23:     int x1 = m_frame->GetClientSize().GetWidth()/2;
  24:     return (int)(x1 + RadiusA*cos(3*M_PI/2 - 2.0* M_PI*m_index/menuLen - M_PI/3) -GetWidth()/2) ;
  25:  
  26: }
  27: int MyMenu::GetY(int menuLen)
  28: {
  29:     int y1 = m_frame->GetClientSize().GetHeight()/2;
  30:     return (int)(y1 + RadiusB*sin(3*M_PI/2 -2.0* M_PI*m_index/menuLen - M_PI/3) -GetHeight()/2) ;
  31: }
  32:  
  33: int MyMenu::GetWidth()
  34: {
  35:     return 100;
  36: }
  37:  
  38: int MyMenu::GetHeight()
  39: {
  40:     return 100;
  41: }
  42:  
  43: wxBitmap& MyMenu::GetBMP() 
  44: {
  45:     return *m_bmp;
  46: }
  47:  
  48: wxString& MyMenu::GetText() 
  49: {
  50:     return m_text;
  51: }
  52:  
  53: bool MyMenu::IsInRect(int x,int y,int menuLen)
  54: {
  55:     int x1 = GetX(menuLen);
  56:     int y1 = GetY(menuLen);
  57:  
  58:     return x> x1 && x < (x1 + GetWidth()) && y> y1 && y < (y1 + GetHeight());
  59: }
  60:  
  61: int MyMenu::GetSubMenuCount()
  62: {
  63:     return m_subMenuTexts.GetCount();
  64: }
  65:  
  66: wxString& MyMenu::GetSubMenuText(int index)
  67: {
  68:     return m_subMenuTexts[index];
  69: }
  70:  
  71: void MyMenu::AddSubMenuTexts(wxString* text,int len)
  72: {
  73:     for (int i = 0;i< len;i++)
  74:     {
  75:         m_subMenuTexts.Add(text[i]);
  76:     }
  77: }

MyMenuCollection.cpp

   1: #include "MyMenuCollection.h"
   2:  
   3: #define  SubMeunWidth 150        //子菜单背景图宽度
   4: #define  SubMeunHeight 40        //子菜单背景图高度
   5: #define  SubMeunFromTop 100        //子菜单背景图距离窗口顶部的距离
   6: #define  SubMeunFromRight 50    //子菜单背景图距离窗口右边的距离
   7: #define  SubMeunBetween  10        //子菜单背景图间距
   8:  
   9: MyMenuCollection::MyMenuCollection(wxFrame* frame,IMyMenuCollection* fun)
  10: {
  11:     this->m_fun = fun;
  12:     this->m_frame = frame;
  13:     wxASSERT(this->m_frame != NULL);
  14:  
  15:     m_backgroundBmp = NULL;
  16:     m_subMenuBmp1 = NULL;
  17:     m_subMenuBmp2 = NULL;
  18:     m_subMenuBmp3 = NULL;
  19:  
  20:     m_selected = -1;
  21:     m_mouseEnter = -1;
  22:     m_mouseDown=-1;
  23:  
  24:     m_subMouseEnter = -1;
  25: }
  26:  
  27: MyMenuCollection::~MyMenuCollection(void)
  28: {
  29:     if (m_backgroundBmp != NULL)
  30:     {
  31:         delete m_backgroundBmp;
  32:     }
  33:     if (m_subMenuBmp1 != NULL)
  34:     {
  35:         delete m_subMenuBmp1;
  36:     }
  37:     if ( m_subMenuBmp2 != NULL)
  38:     {
  39:         delete m_subMenuBmp2;
  40:     }
  41:     if ( m_subMenuBmp3 != NULL)
  42:     {
  43:         delete m_subMenuBmp3;
  44:     }
  45:     for (int i = 0;i< GetCount();i++)
  46:     {
  47:         delete m_collection[i];
  48:     }
  49:     m_collection.Clear();
  50: }
  51:  
  52:  
  53:  
  54: void MyMenuCollection::AddBackGroundBMP(wxImage* img)
  55: {
  56:     tagRECT rect;
  57:     ::GetClientRect(::GetDesktopWindow(),&rect);
  58:  
  59:     m_backgroundBmp = new wxBitmap(img->Rescale(rect.right- rect.left,rect.bottom-rect.top-(m_frame-> GetSize().GetHeight()-m_frame->GetClientSize().GetHeight() )));
  60: }
  61:  
  62: void MyMenuCollection::AddMenu(wxString text,wxImage* img)
  63: {
  64:     MyMenu* meun = new MyMenu(text,img,GetCount(),m_frame);
  65:     m_collection.Add(meun);
  66: }
  67:  
  68: int MyMenuCollection::GetCount()
  69: {
  70:     return m_collection.GetCount();
  71: }
  72:  
  73: MyMenu* MyMenuCollection::GetItem(int index)
  74: {
  75:     return (MyMenu*)m_collection[index];
  76: }
  77:  
  78: void MyMenuCollection::OnMouseEvents( wxMouseEvent& event )
  79: {
  80:     //主菜单
  81:     if (event.Moving())
  82:     {
  83:         int mouseEnter = -1;
  84:         for (int i = 0;i< GetCount();i++)
  85:         {
  86:             if (GetItem(i)->IsInRect(event.GetX(),event.GetY(),GetCount()))
  87:             {
  88:                 mouseEnter = i;
  89:                 break;
  90:             }
  91:         }
  92:         if (m_mouseEnter != mouseEnter)
  93:         {
  94:             m_mouseEnter = mouseEnter;
  95:  
  96:             wxCursor cursor(m_mouseEnter > -1 ? wxCURSOR_HAND:wxCURSOR_ARROW);
  97:             m_frame->SetCursor(cursor);
  98:             m_frame->Refresh();
  99:         }
 100:     }
 101:     else if (event.LeftDown())
 102:     {
 103:         if (m_mouseEnter > -1 )
 104:         {
 105:             m_mouseDown = m_mouseEnter;
 106:             m_selected = m_mouseEnter;
 107:             m_frame->Refresh();
 108:         }        
 109:     }
 110:     else if(event.LeftUp())
 111:     {
 112:         if (m_mouseDown > -1)
 113:         {
 114:             m_mouseDown =-1;
 115:             m_frame->Refresh();
 116:         }
 117:     }
 118:  
 119:     //子菜单
 120:     if (m_selected > -1)
 121:     {
 122:         MyMenu* menu = GetItem(m_selected);
 123:         if (event.Moving())
 124:         {
 125:             int mouseEnter = -1;
 126:             for(int i = 0;i< menu->GetSubMenuCount();i++)
 127:             {
 128:                 if (IsInSubMenuRect(event.GetX(),event.GetY(),i))
 129:                 {
 130:                     mouseEnter = i;
 131:                     break;
 132:                 }
 133:             }
 134:             if (m_subMouseEnter != mouseEnter)
 135:             {
 136:                 m_subMouseEnter = mouseEnter;
 137:  
 138:                 wxCursor cursor(m_subMouseEnter > -1 ? wxCURSOR_HAND:wxCURSOR_ARROW);
 139:                 m_frame->SetCursor(cursor);
 140:                 //m_frame->Refresh();
 141:             }
 142:         }
 143:         else if (event.LeftDown())
 144:         {
 145:             if (m_subMouseEnter > -1)
 146:             {
 147:                 if (m_fun != NULL && m_fun->GetSubMenuEnabled(m_selected,menu->GetText() , m_subMouseEnter,menu->GetSubMenuText(m_subMouseEnter)))
 148:                 {
 149:                     m_fun->SubMenuInvoke(m_selected,menu->GetText() , m_subMouseEnter,menu->GetSubMenuText(m_subMouseEnter));
 150:                 }
 151:             }
 152:         }
 153:     }
 154:  
 155:  
 156: }
 157:  
 158:  
 159: void MyMenuCollection::OnPaint(wxDC& dc)
 160: {
 161:     //背景图
 162:     if(m_backgroundBmp != NULL)
 163:     {
 164:         dc.DrawBitmap(*m_backgroundBmp,0,0,true);
 165:     }
 166:  
 167:     wxFont font1(25, wxFONTFAMILY_DEFAULT, wxNORMAL, wxNORMAL,false,_T("宋体"));
 168:     wxFont font2(15, wxFONTFAMILY_DEFAULT, wxNORMAL, wxNORMAL,false,_T("宋体"));
 169:  
 170:     //主菜单
 171:     dc.SetFont(font1);
 172:     for (int i = 0;i< GetCount();i++)
 173:     {
 174:         MyMenu* menu = GetItem(i);
 175:  
 176:         if ( i==m_mouseDown)
 177:         {
 178:             dc.DrawBitmap(menu->GetBMP(),menu->GetX(GetCount())+2 ,menu->GetY(GetCount())+2,true);
 179:         }
 180:         else
 181:         {
 182:             dc.DrawBitmap(menu->GetBMP(),menu->GetX(GetCount()) ,menu->GetY(GetCount()),true);
 183:         }
 184:         if (i == m_mouseEnter || i==m_selected)
 185:         {
 186:             dc.DrawText( menu->GetText() ,menu->GetX(GetCount()),menu->GetY(GetCount()) + menu->GetHeight() +5);
 187:         }
 188:     }
 189:  
 190:     //子菜单
 191:     if (m_selected > -1)
 192:     {
 193:         dc.SetFont(font2);
 194:         MyMenu* menu = GetItem(m_selected);
 195:         for (int i = 0;i< menu->GetSubMenuCount();i++)
 196:         {
 197:             bool enabled =true;
 198:             if (m_fun != NULL)
 199:             {
 200:                 enabled=m_fun->GetSubMenuEnabled(m_selected,menu->GetText() , i,menu->GetSubMenuText(i));
 201:             }
 202:             if (enabled)
 203:             {
 204:                 dc.DrawBitmap(*m_subMenuBmp1,GetSubMenu_X(i),GetSubMenu_Y(i));
 205:             }
 206:             else
 207:             {
 208:                 dc.DrawBitmap(*m_subMenuBmp2,GetSubMenu_X(i),GetSubMenu_Y(i));
 209:             }
 210:  
 211:             dc.DrawText(menu->GetSubMenuText(i) , GetSubMenu_X(i)+20,GetSubMenu_Y(i)+10);
 212:         }
 213:  
 214:     }
 215: }
 216:  
 217:  
 218: void MyMenuCollection::AddSubMenuBMP(wxImage* enabled,wxImage* disabled,wxImage* active)
 219: {
 220:     wxASSERT(enabled != NULL);
 221:     wxASSERT(disabled != NULL);
 222:  
 223:     m_subMenuBmp1 = new wxBitmap(enabled->Rescale(SubMeunWidth,SubMeunHeight));
 224:     m_subMenuBmp2 = new wxBitmap(disabled->Rescale(SubMeunWidth,SubMeunHeight));
 225:  
 226:     if (active != NULL)
 227:     {
 228:         m_subMenuBmp3 = new wxBitmap(active->Rescale(SubMeunWidth,SubMeunHeight));
 229:     }
 230: }
 231:  
 232: int MyMenuCollection::GetSubMenu_X(int index)
 233: {
 234:     return m_frame->GetClientRect().GetRight() - SubMeunFromRight - SubMeunWidth;
 235: }
 236:  
 237: int MyMenuCollection::GetSubMenu_Y(int index)
 238: {
 239:     return SubMeunFromTop+ (SubMeunHeight+ SubMeunBetween)*index;
 240: }
 241:  
 242: bool MyMenuCollection::IsInSubMenuRect(int x,int y,int index)
 243: {
 244:     int x1 = GetSubMenu_X(index);
 245:     int y1 = GetSubMenu_Y(index);
 246:     return x>x1 && y>y1 && x<(x1 + SubMeunWidth) && y<(y1 + SubMeunHeight);
 247: }

主窗口要实现接口 IMyMenuCollection

   1: class IMyMenuCollection
   2: {
   3: public:
   4:     virtual bool GetSubMenuEnabled(int a,const wxString& A,int b,const wxString& B)=0;
   5:     virtual void SubMenuInvoke(int a,const wxString& A,int b,const wxString& B)=0;
   6: };

主窗口代码:

   1: #include "GUIDemoMainFrame.h"
   2:  
   3: GUIDemoMainFrame::GUIDemoMainFrame( wxWindow* parent )
   4: :
   5: MainFrame( parent )
   6: {
   7:     m_mainMenu = new MyMenuCollection(this,this);
   8:  
   9:     wxImage img,img1,img2;
  10:     img.LoadFile(_T("images/背景图.jpg"),wxBITMAP_TYPE_JPEG);
  11:     m_mainMenu->AddBackGroundBMP(&img);
  12:  
  13:     img1.LoadFile(_T("images/submenu2.png"),wxBITMAP_TYPE_PNG);
  14:     img2.LoadFile(_T("images/submenu1.png"),wxBITMAP_TYPE_PNG);
  15:     m_mainMenu->AddSubMenuBMP(&img1,&img2);
  16:  
  17:     wxString menuNames[] = {_T("登录系统"), _T("维护系统"), _T("档案管理"), _T("卡片管理"), _T("出入监控"), _T("记录查询"), _T("报表统计"), _T("系统帮助")};
  18:  
  19:     wxString subMenuName0[]={_T("交接班"),_T("退出系统")};
  20:     wxString subMenuName1[]={_T("修改密码"),_T("设置参数"),_T("数据维护"),_T("设置收费"),_T("注册道闸")};
  21:     wxString subMenuName2[]={_T("权限组档案"),_T("操作员档案"),_T("停车场档案")};
  22:     wxString subMenuName3[]={_T("卡片检测"),_T("卡片发行"),_T("卡片延期"),_T("卡片充值"),_T("卡片挂失"),_T("卡片恢复"),_T("卡片更换"),_T("卡片回收"),_T("卡片档案"),_T("卡片出场"),_T("卡片下载"),_T("卡片上载")};
  23:     wxString subMenuName4[]={_T("出入监控")};
  24:     wxString subMenuName5[]={_T("入场记录"),_T("出场记录"),_T("卡片记录"),_T("在场停车"),_T("交接班记录"),_T("值班流水帐"),_T("非法抬闸")};
  25:     wxString subMenuName6[]={_T("操作员收费"),_T("收费日报表"),_T("收费月报表"),_T("收费年报表"),_T("入场日报表"),_T("入场月报表"),_T("入场年报表"),_T("出场日报表"),_T("出场月报表"),_T("出场年报表")};
  26:     wxString subMenuName7[]={_T("帮助"),_T("关于系统")};
  27:  
  28:     for (int i = 0;i< 8;i++)
  29:     {
  30:         img.LoadFile( wxString::Format(_T("images/菜单1/%s.png"),menuNames[i]),wxBITMAP_TYPE_PNG);
  31:         m_mainMenu->AddMenu(menuNames[i],&img);
  32:     }
  33:     m_mainMenu->GetItem(0)->AddSubMenuTexts(subMenuName0,2);
  34:     m_mainMenu->GetItem(1)->AddSubMenuTexts(subMenuName1,5);
  35:     m_mainMenu->GetItem(2)->AddSubMenuTexts(subMenuName2,3);
  36:     m_mainMenu->GetItem(3)->AddSubMenuTexts(subMenuName3,12);
  37:     m_mainMenu->GetItem(4)->AddSubMenuTexts(subMenuName4,1);
  38:     m_mainMenu->GetItem(5)->AddSubMenuTexts(subMenuName5,7);
  39:     m_mainMenu->GetItem(6)->AddSubMenuTexts(subMenuName6,10);
  40:     m_mainMenu->GetItem(7)->AddSubMenuTexts(subMenuName7,2);
  41:  
  42:     this->Maximize(true);
  43: }
  44:  
  45: void GUIDemoMainFrame::MainFrameOnClose( wxCloseEvent& event )
  46: {
  47:     delete m_mainMenu;
  48:  
  49:     Destroy();
  50: }
  51:  
  52: void GUIDemoMainFrame::MainFrameOnEraseBackground( wxEraseEvent& event )
  53: {
  54:     // TODO: Implement MainFrameOnEraseBackground
  55: }
  56:  
  57: void GUIDemoMainFrame::MainFrameOnMouseEvents( wxMouseEvent& event )
  58: {
  59:     m_mainMenu->OnMouseEvents(event);
  60: }
  61:  
  62: void GUIDemoMainFrame::MainFrameOnPaint( wxPaintEvent& event )
  63: {
  64:     // TODO: Implement MainFrameOnPaint
  65:     wxPaintDC dc(this);
  66:  
  67:     dc.BeginDrawing();
  68:     m_mainMenu->OnPaint(dc);
  69:     dc.EndDrawing();
  70: }
  71:  
  72:  
  73: void GUIDemoMainFrame::MainFrameOnSize( wxSizeEvent& event )
  74: {
  75:     this->Refresh();
  76: }
  77:  
  78:  
  79: bool GUIDemoMainFrame::GetSubMenuEnabled(int a,const wxString& A,int b,const wxString& B)
  80: {
  81:     return a!=b;
  82: }
  83:  
  84: void GUIDemoMainFrame::SubMenuInvoke(int a,const wxString& A,int b,const wxString& B)
  85: {
  86:     if (a== 0 && b==1)
  87:     {
  88:         this->Close();
  89:         return;
  90:     }
  91:  
  92:     wxString str;
  93:     str<<A;
  94:     str<<_T(",");
  95:     str<<B;
  96:     wxMessageBox(str);
  97: }

代码下载

c#版下载

posted @ 2012-01-23 16:51  快乐驿站  阅读(1388)  评论(0编辑  收藏  举报