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

本文介绍了如何使用C++和wxWidgets库创建一个自定义的wxListViewComboPopup类,该类结合了wxListView和wxComboPopup的功能。这个控件允许用户在下拉列表中选择项,并提供了添加列、设置和获取选中值等功能。通过设置列宽和响应鼠标事件,实现了列表的交互性。示例代码展示了如何创建和使用这个自定义组件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


#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"));

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值