wxRibbonBar 常用三种控件Button,DropdownButton,HybridButton

这三种控件的效果如下所示:

wxRibbonBar 常用三种控件Button,DropdownButton,HybridButton_第1张图片

点击下拉的效果:

wxRibbonBar 常用三种控件Button,DropdownButton,HybridButton_第2张图片

 这一部分可以设置wxITEM_CHECK,wxITEM_RADIO等效果

 但我们可能更关注实现实例:

(1)MyFrame.h

#pragma once

#include
#include "wx/wxprec.h"
#include "wx/app.h"
#include "wx/frame.h"
#include "wx/textctrl.h"
#include "wx/ribbon/bar.h"
#include "wx/ribbon/buttonbar.h"
#include "wx/ribbon/gallery.h"
#include "wx/ribbon/toolbar.h"
#include "wx/sizer.h"
#include "wx/menu.h"
#include "wx/msgdlg.h"
#include "wx/dcbuffer.h"
#include "wx/colordlg.h"
#include "wx/artprov.h"
#include "wx/combobox.h"
#include "wx/tglbtn.h"
#include "wx/wrapsizer.h"

class MyFrame : public wxFrame
{
public:
    MyFrame(const wxString& title);
    ~MyFrame(){};

    void OnEnableUpdated(wxRibbonButtonBarEvent& evt);
    void OnEnableUpdateUI(wxUpdateUIEvent& evt);
    void OnBarButtonDropDown(wxRibbonButtonBarEvent& event);
    void OnPolygonDropdown(wxRibbonButtonBarEvent& evt);

    wxDECLARE_EVENT_TABLE();
};

(2)MyFrame.cpp

#include "stdafx.h"
#include "MyFrame.h"

#define ID_SMALL_BUTTON_1 1000
#define ID_REMOVE_PAGE 1001
#define ID_MAIN_TOOLBAR 1002
#define ID_SELECTION_EXPAND_V 1003
#define ID_UI_CHECK_UPDATED 1004
#define ID_DROPBUTTON 1005

#define ID_HYBIRDBTN 1020

wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_RIBBONBUTTONBAR_CLICKED(ID_REMOVE_PAGE, MyFrame::OnEnableUpdated)
EVT_UPDATE_UI(ID_REMOVE_PAGE, MyFrame::OnEnableUpdateUI)
//EVT_RIBBONBUTTONBAR_DROPDOWN_CLICKED(ID_DROPBUTTON, MyFrame::OnPolygonDropdown) //点击弹出下来框

wxEND_EVENT_TABLE()

void MyFrame::OnBarButtonDropDown(wxRibbonButtonBarEvent& event)
{
}

void MyFrame::OnPolygonDropdown(wxRibbonButtonBarEvent& evt)
{
    //普通下拉文本
    //wxMenu menu;
    /*
    menu.Append(wxID_ANY, wxT("Pentagon (1 sided)"));
    menu.Append(wxID_ANY, wxT("Hexagon (2 sided)"));
    menu.Append(wxID_ANY, wxT("Heptagon (3 sided)"));
    menu.Append(wxID_ANY, wxT("Octogon (4 sided)"));
    menu.Append(wxID_ANY, wxT("Nonagon (5 sided)"));
    menu.Append(wxID_ANY, wxT("Decagon (6 sided)"));
    evt.PopupMenu(&menu);
    */

    //check
    /*wxMenu menu;
    menu.Append(ID_DROPBUTTON + 1, wxT("Pentagon (1 sided)"), wxEmptyString, wxITEM_CHECK);
    menu.Append(ID_DROPBUTTON + 2, wxT("Hexagon (2 sided)"), wxEmptyString, wxITEM_CHECK);
    menu.Append(ID_DROPBUTTON + 3, wxT("Heptagon (3 sided)"), wxEmptyString, wxITEM_CHECK);
    menu.Append(ID_DROPBUTTON + 4, wxT("Octogon (4 sided)"), wxEmptyString, wxITEM_CHECK);
    menu.Check(ID_DROPBUTTON + 4, true);
    evt.PopupMenu(&menu);*/

    //radio
    wxMenu menu;
    menu.Append(ID_DROPBUTTON + 1, wxT("Pentagon (1 sided)"), wxEmptyString, wxITEM_RADIO);
    menu.Append(ID_DROPBUTTON + 2, wxT("Hexagon (2 sided)"), wxEmptyString, wxITEM_RADIO);
    menu.Append(ID_DROPBUTTON + 3, wxT("Heptagon (3 sided)"), wxEmptyString, wxITEM_RADIO);
    menu.Append(ID_DROPBUTTON + 4, wxT("Octogon (4 sided)"), wxEmptyString, wxITEM_RADIO);
    menu.Check(ID_DROPBUTTON + 4, true);
    evt.PopupMenu(&menu);
}

MyFrame::MyFrame(const wxString& title)
        : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(800, 600))
    {
        wxRibbonBar* mainMenu = new wxRibbonBar(this, -1, wxDefaultPosition, wxDefaultSize, wxRIBBON_BAR_FLOW_HORIZONTAL
            | wxRIBBON_BAR_SHOW_PAGE_LABELS
            | wxRIBBON_BAR_SHOW_PANEL_EXT_BUTTONS
            | wxRIBBON_BAR_SHOW_TOGGLE_BUTTON
            | wxRIBBON_BAR_SHOW_HELP_BUTTON
            );

        wxRibbonPage* home = new wxRibbonPage(mainMenu, wxID_ANY, "Examples");
        {
            wxRibbonPanel* panel = new wxRibbonPanel(home, wxID_ANY, "Button bar manipulation");
            wxRibbonButtonBar* bar = new wxRibbonButtonBar(panel, wxID_ANY);
            bar->AddButton(ID_REMOVE_PAGE, "Remove", wxArtProvider::GetBitmap(wxART_DELETE, wxART_OTHER, wxSize(32, 32)));

            //点击弹出下来框 事件DropdownButton
            bar->AddDropdownButton(ID_DROPBUTTON, "Remove", wxArtProvider::GetBitmap(wxART_DELETE, wxART_OTHER, wxSize(32, 32)));
            Connect(ID_DROPBUTTON, wxEVT_COMMAND_RIBBONBUTTON_DROPDOWN_CLICKED,
                wxRibbonButtonBarEventHandler(MyFrame::OnPolygonDropdown));

            //点击弹出下拉框 并且可以点击当前按钮
            wxRibbonButtonBarButtonBase*  p1 = bar->AddHybridButton(ID_DROPBUTTON, "hybrid", 
                wxArtProvider::GetBitmap(wxART_DELETE, wxART_OTHER, wxSize(32, 32)));

        }
        mainMenu->Realize();
        wxSizer *s = new wxBoxSizer(wxVERTICAL);
        s->Add(mainMenu, 0, wxEXPAND);

        SetSizer(s);

        Centre();
    }

void MyFrame::OnEnableUpdated(wxRibbonButtonBarEvent& evt)
{
    wxFileDialog dialog(this, _("Open file"), wxEmptyString, wxEmptyString);
    if (dialog.ShowModal() == wxID_OK)
    {
        wxString strPath = dialog.GetPath();
    }
    //wxMessageBox("点击了按钮", "提示框");
}

void MyFrame::OnEnableUpdateUI(wxUpdateUIEvent& evt)
{
    evt.Enable(true);
}

(3)调用MyFrame类

// RibbonSample.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "MyFrame.h"

class MyApp : public wxApp
{
public:
    bool OnInit() wxOVERRIDE;
};

wxDECLARE_APP(MyApp);
wxIMPLEMENT_APP(MyApp);

bool MyApp::OnInit()
{
    if (!wxApp::OnInit())
        return false;

    wxFrame* frame = new MyFrame(wxT("Ribbon Sample"));
    frame->Show();

    return true;
}
 

你可能感兴趣的:(wxwidgets,c++,wxwidgets,wxRibbonBar,DropdownButton,HybridButton)