天天看点

52、wxWidgets之自定义wxCombox的弹出窗口-wxListView

52、wxWidgets之自定义wxCombox的弹出窗口-wxListView

#include <wx/wx.h>

#include <wx/combo.h>

#include <wx/listctrl.h>

class wxListViewComboPopup : public wxListView, public wxComboPopup

{

public:

    virtual wxListView* GetListView();

    // Initialize member variables

    virtual void Init();

    // Create popup control

    virtual bool Create(wxWindow* parent);

    // Return pointer to the created control

    virtual wxWindow* GetControl();

    // Translate string into a list selection

    virtual void SetStringValue(const wxString& s);

    // Get list selection as a string

    virtual wxString GetStringValue() const;

    // Do mouse hot-tracking (which is typical in list popups)

    long AppendColumn(wxString& columnName,int nWidth);

    void OnMouseMove(wxMouseEvent& event);

    // On mouse left up, set the value and close the popup

//    void OnMouseClick(wxListEvent& WXUNUSED(event));

    void OnSelected(wxListEvent& event);

protected:

    wxWindow* m_parent;

    long m_value; // current item index

    wxListView* lv;

private:

//    wxDECLARE_EVENT_TABLE();

};

wxListView* wxListViewComboPopup::GetListView()

{

    return lv;

}

// Initialize member variables

void wxListViewComboPopup::Init()

{

    m_value = -1;

}

// Create popup control

bool wxListViewComboPopup::Create(wxWindow* parent)

{

//        return wxListView::Create(parent,1,wxPoint(0,0),wxDefaultSize,wxLC_LIST);

    lv = new wxListView();

    bool b = lv->Create(parent, 1, wxPoint(0, 0), wxDefaultSize, wxLC_HRULES | wxLC_REPORT | wxLC_VRULES);

    lv->Bind(wxEVT_LIST_ITEM_SELECTED,wxListEventHandler( wxListViewComboPopup::OnSelected), this);

    return b;

}

// Return pointer to the created control

wxWindow* wxListViewComboPopup::GetControl()

{

    return lv;

}

// Translate string into a list selection

void wxListViewComboPopup::SetStringValue(const wxString& s)

{

    int n = lv->FindItem(-1, s);

    if ( n >= 0 && n < lv->GetItemCount()) lv->Select(n);

}

// Get list selection as a string

wxString wxListViewComboPopup::GetStringValue() const

{

    long index = lv->GetFirstSelected();

    if(index !=-1){

//       return lv->GetItemText(index,0);//第一列的值

       return lv->GetItemText(index,1);//第二列的值

    }

    return wxEmptyString;

}

// Do mouse hot-tracking (which is typical in list popups)

void wxListViewComboPopup::OnMouseMove(wxMouseEvent& event)

{

    // TODO: Move selection to cursor

}

// On mouse left up, set the value and close the popup

void wxListViewComboPopup::OnSelected(wxListEvent& event)

{

    m_value = lv->GetFirstSelected();

    // TODO: Send event as well

    Dismiss();//hide the window

}

long  wxListViewComboPopup::AppendColumn(wxString& columnName,int nWidth = wxLIST_AUTOSIZE)

{

    long index = lv->AppendColumn(columnName);

    lv->SetColumnWidth(index,nWidth);

    return index;

}

使用方法

    wxComboCtrl* comboCtrl = new wxComboCtrl(this, wxID_ANY, wxEmptyString);

    wxListViewComboPopup* popupCtrl = new wxListViewComboPopup();

// It is important to call SetPopupControl() as soon as possible

    comboCtrl->SetPopupControl(popupCtrl);

    auto* lv= popupCtrl->GetListView();

    lv->AppendColumn(wxT("任务"),wxLIST_FORMAT_LEFT,wxLIST_AUTOSIZE);

    lv->AppendColumn(wxT("名称"),wxLIST_FORMAT_LEFT,wxLIST_AUTOSIZE_USEHEADER);

    lv->AppendColumn(wxT("名"),wxLIST_FORMAT_LEFT,wxLIST_AUTOSIZE);

    lv->AppendColumn(wxT("称"),wxLIST_FORMAT_LEFT,wxLIST_AUTOSIZE);

    wxListItem colID;

    colID.SetId(0);//必须设置

    colID.SetText(wxT("ID"));

    colID.SetAlign(wxLIST_FORMAT_CENTER);

    wxListItem colName;

    colName.SetId(1);

    colName.SetText(wxT("名称"));

    colName.SetAlign(wxLIST_FORMAT_CENTER);

    colName.SetData(1);

    long index;

    index = lv->InsertItem(colID);

    lv->SetItem(index,1,wxT("名称1"));//此处设置的名称的图标

//    wxMessageBox(wxString::Format("%d",index));

    index = lv->InsertItem(colName);

    lv->SetItem(index,1,wxT("名称2"));