1.ListCtrl效果
2.操作步骤
(1)在VS2008中新建对话框IDD_WORD,在对话框上添加ListCtrl控件IDC_LIST_WORD
(2)为对话框IDD_WORD建立关联类WordResult类
在头文件中加入
//添加ListCtrl类变量 自定义变量
CListCtrl m_listCtrl;
map<string, int> m_wordMap;
//重载初始化方法
virtual BOOL OnInitDialog();
void SetParam(map<string, int> temp_map);
在类定义中加入
void WordResult::DoDataExchange(CDataExchange* pDX)
{
//建立ListCtrl变量与控件的映射
DDX_Control(pDX, IDC_LIST_WORD, m_listCtrl);
}
(3)重点在于初始化函数
BOOL WordResult::OnInitDialog()//domodal回调显示窗口,用于显示
{
CDialog::OnInitDialog();
//初始化控件格式
int total=0;
CString temp;
/* 设置listctrl当前风格 */
long lStyle;
lStyle = GetWindowLong(m_listCtrl.m_hWnd, GWL_STYLE);//获取当前窗口style
lStyle &= ~LVS_TYPEMASK; //清除显示方式位
lStyle |= LVS_REPORT; //设置style
SetWindowLong(m_listCtrl.m_hWnd, GWL_STYLE, lStyle);//设置style*/
/* 设置listctrl扩展风格 */
DWORD dwExStyle=LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES|LVS_EX_HEADERDRAGDROP|LVS_EX_ONECLICKACTIVATE|LVS_EX_UNDERLINEHOT;
m_listCtrl.SetExtendedStyle(dwExStyle);
//插入列名称
m_listCtrl.InsertColumn(0,"Id",LVCFMT_LEFT,100);
m_listCtrl.InsertColumn(1,"Frequency",LVCFMT_CENTER,100);
m_listCtrl.InsertColumn(2,"Word",LVCFMT_CENTER,100);
int count = 1;
map<string, int>::iterator iter = m_wordMap.begin();
int nIndex=m_listCtrl.GetItemCount();
LV_ITEM lvItem;
for(; iter != m_wordMap.end(); iter++)
{
lvItem.mask=LVIF_TEXT;//设置掩码
lvItem.iItem=nIndex;//项编号
lvItem.iSubItem=0;
temp.Format("%d", count++);
lvItem.pszText=(char*)(LPCTSTR)(temp);//项的显示文本
//在最后一行插入记录值M
m_listCtrl.InsertItem(&lvItem);
/*设置该行的其他列的值*/
m_listCtrl.SetItemText(nIndex,1,(iter->first).c_str());
temp.Format("%d",iter->second);
m_listCtrl.SetItemText(nIndex,2,temp);
nIndex=m_listCtrl.GetItemCount();
}
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
3.在实例中应用:在调用类中
//弹出对话框,显示内容
WordResult wordDlg;
wordDlg.SetParam(temp_map);
wordDlg.DoModal();