wxWidgets教程(15)——wxListCtrl用法

来源:互联网 发布:石家庄直销软件 编辑:程序博客网 时间:2024/06/03 05:16

wxListCtrl

1、构造函数

// 构造函数声明wxListCtrl(wxWindow *parent, // 父窗口           wxWindowID id = wxID_ANY,// 控件ID           const wxPoint& pos = wxDefaultPosition, // 控件左上角坐标           const wxSize& size = wxDefaultSize,// 控件宽高尺寸           long style = wxLC_ICON, // 控件类型           const wxValidator& validator = wxDefaultValidator,// 验证           const wxString& name = wxListCtrlNameStr // 控件窗口名称           );// report类型的控件示例wxListCtrl * m_list = new wxListCtrl( m_panel1,wxID_LIST,wxDefaultPosition,            wxDefaultSize,wxLC_HRULES|wxLC_REPORT|wxLC_VRULES );

2、控件类型

必须设置的类型:wxLC_LIST和wxLC_REPORT,前者如文件夹中的文件列表,后者如html的table控件。
通常还需要设置:wxLC_HRULES和wxLC_VRULES,分别是水平与垂直分割线,一般report需要设置。
可编辑:wxLC_EDIT_LABELS
无表头:wxLC_NO_HEADER
单行选择:wxLC_SINGLE_SEL,默认是可选多行的
图标相关:wxLC_ICON、wxLC_SMALL_ICON、wxLC_ALIGN_TOP和wxLC_ALIGN_LEFT

3、REPORT类型控件使用

添加表头
    // 添加第一列的表头    wxListItem col0;    col0.SetId(0); // id必须设置,代表第0列    col0.SetText(wxT("#"));    col0.SetWidth(50); // 设置列宽    col0.SetAlign(wxLIST_FORMAT_CENTER); // 此列的文本居中显示    m_listname->InsertColumn(0, col0); // 插入列    // 添加第二列的表头    wxListItem col1;    col1.SetId(1);    col1.SetText(wxT("Name"));    m_listname->InsertColumn(1, col1);    // 添加第三列的表头         wxListItem col2;    col2.SetId(2);    col2.SetText(wxT("Age"));    m_listname->InsertColumn(2, col2);
多列控件中添加元素
    int total = m_listname->GetItemCount(); // 获取当前元素数量    long indexItem = m_listname->InsertItem(total,wxString::FromDouble(total + 1));    m_listname->SetItem(indexItem, 1, wxT("name1"));    m_listname->SetItem(indexItem, 2, wxT("age1"));    // 设置一些其他的属性,如颜色,字体...等等    m_listname->SetItemBackgroundColour(indexItem,wxColour(255, 255, 0));    m_listname->SetItemTextColour(indexItem, wxColour(0,255,255));    // 你还可以通过id获取指定行的item对象,单独设置,然后重新设置回去,如下:    wxListItem item;    item.SetId(1); // 获取id为1的那一行的元素    m_listname->GetItem(item);    item.SetBackgroundColour(wxColour(100, 100, 255));    item.SetState(wxLIST_STATE_SELECTED); // 此行item被选中    m_listname->SetItem(item); // 重新设置回去    // 获取指定行indexItem列为1的文本数据    m_listname->GetItemText(indexItem, 1);
获取所有选中的行
    long indexItem = -1;    while ((indexItem = m_listname->GetNextItem(indexItem,wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED)) != wxNOT_FOUND) {        wxLogDebug(m_listname->GetItemText(indexItem));    }
设置item的图标
    wxImageList *m_pImageList = new wxImageList(16, 16);    wxIcon icon0,icon1;    icon0.LoadFile(wxT("res/main.ico"), wxBITMAP_TYPE_ICO);    icon1.LoadFile(wxT("res/MG.ico"), wxBITMAP_TYPE_ICO);    m_pImageList->Add(icon0);    m_pImageList->Add(icon1);    m_listname->SetImageList(m_pImageList, wxIMAGE_LIST_SMALL);    m_listname->SetItemImage(indexItem, 0); // 给indexItem设置icon0
删除选中的item
    long indexItem = -1;    wxArrayInt indexItems;    while ((indexItem = m_listname->GetNextItem(indexItem, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED)) != wxNOT_FOUND) {        indexItems.Add(indexItem);    }    // 记住,要从下向上删除    for (int i = indexItems.GetCount() - 1; i >= 0;--i)    {        m_listname->DeleteItem(i);    }    indexItems.Clear();
阅读全文
0 0