This blog briefly introduces the use of CListBox and CListCtrl tools.
Use the MFC wizard to create a dialog program, add a CListBox and CListCtrl resource to the dialog box in the resource editing window and associate a control object.
Add the following code to the member function OnInitDialog
- CRect rect;
- GetClientRect(&rect);
- //The selected item is highlighted and grid lines are drawn
- m_listctrl.SetExtendedStyle(m_listctrl.GetExtendedStyle()|LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
- //Create 3 column objects
- m_listctrl.InsertColumn(0,"语言",LVCFMT_LEFT,70);
- m_listctrl.InsertColumn(1,"2010排名",LVCFMT_LEFT,70);
- m_listctrl.InsertColumn(2,"2011排名",LVCFMT_LEFT,70);
- m_listctrl.InsertItem(0, "java" ); //Create a row object
- m_listctrl.SetItemText(0,1, "1" ); //Create child object members
- m_listctrl.SetItemText(0,2,"2");
- m_listctrl.InsertItem(1,"c++");
- m_listctrl.SetItemText(1,1,"3");
- m_listctrl.SetItemText(1,2,"3");
- m_listctrl.InsertItem(2,"c");
- m_listctrl.SetItemText(2,1,"2");
- m_listctrl.SetItemText(2,2,"1");
- m_listbox.AddString("string one");
- m_listbox.AddString("string two");
- m_listbox.AddString("string three");
Click Run to see the initialized data in the control.
Reprinted from: http://blog.csdn.net/gk_2014/article/details/46353189