windows 如何创建多列列表框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2166473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:50:55  来源:igfitidea点击:

How to create a multicolumn Listbox?

c++windowslistbox

提问by kampi

I'm working on a program, which should list all files and it's size(for now...). I created a simple application, which should write the data to a listbox. I'm trying to write the data in two columns(the first should be the name, and next to it, in an other column, it's size), but i can't figure out, how should i do this. Can someone help me?

我正在开发一个程序,它应该列出所有文件和它的大小(现在......)。我创建了一个简单的应用程序,它应该将数据写入列表框。我试图在两列中写入数据(第一列应该是名称,在它旁边的另一列中,它是大小),但我不知道,我应该怎么做。有人能帮我吗?

Thanks in advance!

提前致谢!

kampi

坎皮

Update:

更新:

I try to use ListControl., but unfortunately i can't. I can succesfully compile my App, but i can only see, the empty rectangle. Does someone know what i am doing wrong?

我尝试使用 ListControl。,但不幸的是我不能。我可以成功编译我的应用程序,但我只能看到空矩形。有人知道我做错了什么吗?

BOOL CGetFileListDlg::OnInitDialog()
{  
CDialog::OnInitDialog();

// Set the icon for this dialog.  The framework does this automatically
//  when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE);         // Set big icon
SetIcon(m_hIcon, FALSE);        // Set small icon

// TODO: Add extra initialization here

LVITEM lvItem;
LVCOLUMN lvColumn;
int nCol;

lvColumn.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
lvColumn.fmt = LVCFMT_CENTER;
lvColumn.cx = 10;
lvColumn.pszText = _T("Filename");
ListView_InsertColumn( m_List, 0, &lvColumn ); 
ListView_SetItemText( m_List, 0, 0, _T("TEST") );

return TRUE;  // return TRUE  unless you set the focus to a control    
}

采纳答案by Sam

The list box control does support multiple columns, but it only supports a single series of entries; the multiple column support just makes the items continue onto the next columns so that vertical scrolling is not necessary.

列表框控件确实支持多列,但只支持单列条目;多列支持只是使项目继续到下一列,因此不需要垂直滚动。

As Kornel has suggested, a list view controlmay be more appropriate. After creating a list view control, use ListView_InsertColumnto create the columns. Then use ListView_SetItemTextto insert items.

正如 Kornel 所建议的,列表视图控件可能更合适。创建列表视图控件后,使用ListView_InsertColumn创建列。然后使用ListView_SetItemText插入项目。

EDIT: My apoligies; you should use ListView_InsertItemto insert an item (a row) and then use ListView_SetItemTextto alter the subitems (columns). If the list view is still just a blank box without any headings, have you initialised common controls? This can be done using InitCommonControlsEx, specifying the ICC_LISTVIEW_CLASSESconstant. This should be done before creating the control.

编辑:我的 apoligies; 您应该使用ListView_InsertItem插入一个项目(一行),然后使用ListView_SetItemText更改子项目(列)。如果列表视图仍然只是一个没有任何标题的空白框,您是否初始化了常用控件?这可以使用InitCommonControlsEx来完成,指定ICC_LISTVIEW_CLASSES常量。这应该在创建控件之前完成。

See Microsoft's documentation on list view controls.

请参阅Microsoft 有关列表视图控件的文档

回答by Kornel Kisielewicz

Don't use a List Box, use a List Controlwith LVS_REPORT style.

不要使用列表框,使用带有 LVS_REPORT 样式的列表控件

回答by eomeroff

Maybe to use DataGridView with object as data source.

也许将 DataGridView 与对象一起用作数据源。

回答by Santhosh Nagasanthosh

Three important parameters to be checked are

要检查的三个重要参数是

  1. List Box or the List Control (List Control is to be used)
  2. View parameter must be Report mode
  3. Owner Data must be set to False The screenshot shows these enter image description here
  1. 列表框或列表控件(要使用列表控件)
  2. 查看参数必须是Report模式
  3. 所有者数据必须设置为 False 屏幕截图显示了这些 在此处输入图片说明

The programming flow to add data to list control are Change the list Control to extended list view(ListView_SetExtendedListViewStyle), Create the layout(By adding columns), Add Item data (for each row needed) & add finally add sub-item to each column (for each item data added previously).

将数据添加到列表控件的编程流程是将列表控件更改为扩展列表视图(ListView_SetExtendedListViewStyle)、创建布局(通过添加列)、添加项目数据(对于需要的每一行)以及最后将子项目添加到每列(对于之前添加的每个项目数据)。