新建一个基于mfc对话框的程序:
如果List Contrl 的控件不是以上格式,而是
则右键属性,点开View选择Report(报表)即可。
添加代码如下:
CString strFont[3] = { _T("一年级"),_T("二年级"),_T("三年级") };//年级的下拉设置
for (int i = 0; i < 3; i++)
{
m_Grade.InsertString(i, strFont[i]);
}
this->m_TreeCon.ModifyStyle(0, TVS_HASBUTTONS | TVS_HASLINES | TVS_LINESATROOT);
root = this->m_TreeCon.InsertItem(_T("年级"));
root1 = this->m_TreeCon.InsertItem(_T("一年级"), root);
root2 = this->m_TreeCon.InsertItem(_T("二年级"), root);
root3 = this->m_TreeCon.InsertItem(_T("三年级"), root);
//this->m_Grade.SetCurSel(0);
m_Inquiry.SetExtendedStyle(LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT | LVS_EX_ONECLICKACTIVATE);
m_Inquiry.InsertColumn(0, _T("姓名"), LVCFMT_LEFT, 40);
m_Inquiry.InsertColumn(1, _T("学号"), LVCFMT_LEFT, 80);
m_Inquiry.InsertColumn(2, _T("年级"), LVCFMT_LEFT, 80);
m_Inquiry.InsertColumn(3, _T("Sub1"), LVCFMT_LEFT, 80);
m_Inquiry.InsertColumn(4, _T("Sub2"), LVCFMT_LEFT, 80);
m_Inquiry.InsertColumn(5, _T("Sub3"), LVCFMT_LEFT, 80);
(注意:添加代码必须包含在UpdateData(true);和UpdateData(false);之间,否则会进行错误的数据添加)
void COOPEx51Dlg::OnBnClickedButton2()
{
// TODO: 在此添加控件通知处理程序代码
UpdateData(true);
CString grade;
switch (this->m_Grade.GetCurSel())
{
case 0:
this->m_TreeCon.InsertItem(this->m_Name, root1);
this->m_TreeCon.Expand(root, TVE_EXPAND);
this->m_TreeCon.Expand(root1, TVE_EXPAND);
grade = "一年级";
break;
case 1:
this->m_TreeCon.InsertItem(this->m_Name, root2);
this->m_TreeCon.Expand(root, TVE_EXPAND);
this->m_TreeCon.Expand(root2, TVE_EXPAND);
grade = "二年级";
break;
case 2:
this->m_TreeCon.InsertItem(this->m_Name, root3);
this->m_TreeCon.Expand(root, TVE_EXPAND);
this->m_TreeCon.Expand(root3, TVE_EXPAND);
grade = "三年级";
break;
}
//存在另一个类里面
stu[count].name = this->m_Name;
stu[count].id = this->m_ID;
stu[count].grade = grade;
stu[count].sub1 = this->m_Sub1;
stu[count].sub2 = this->m_Sub2;
stu[count].sub3 = this->m_Sub3;
stu[count].average = (this->m_Sub1 + this->m_Sub2 + this->m_Sub3)/3.0;
count++;
////更新日志
CTime CurrentTime = CTime::GetCurrentTime();
CString strTime;
strTime.Format(_T("%d:%d:%d"), CurrentTime.GetHour(), CurrentTime.GetMinute(), CurrentTime.GetSecond());
CString log;
log = "";
log += strTime;
log += " DATA ADD";
this->m_Log.AddString(log);
UpdateData(false);
}
双击ReSet按钮,添加代码:
m_Grade.ResetContent();
CString strFont[3] = { _T("一年级"),_T("二年级"),_T("三年级") };
for (int i = 0; i < 3; i++)
{
m_Grade.InsertString(i, strFont[i]);
}
this->m_ID = "";
this->m_Name = "";
this->m_Sub1 = 0.0;
this->m_Sub2 = 0.0;
this->m_Sub3 = 0.0;
//增加日志信息
CTime CurrentTime = CTime::GetCurrentTime();
CString strTime;
strTime.Format(_T("%d:%d:%d"), CurrentTime.GetHour(), CurrentTime.GetMinute(), CurrentTime.GetSecond());
CString log;
log = "";
log += strTime;
log += " DATA RESET";
m_Log.AddString(log);
UpdateData(false);
双击Clear按钮,添加代码:
void COOPEx51Dlg::OnBnClickedClear()
{
// TODO: 在此添加控件通知处理程序代码
CString log;
log = "";
this->m_Log.ResetContent();
}
右键单击树形控件,选择添加事件NM_DBLCLK:
编辑代码为:
void COOPEx51Dlg::OnNMDblclkTree1(NMHDR *pNMHDR, LRESULT *pResult)
{
// TODO: 在此添加控件通知处理程序代码
HTREEITEM temp = this->m_TreeCon.GetSelectedItem();
CString text = this->m_TreeCon.GetItemText(temp);
for (int i = 0; i < count; i++)
{
if (text == stu[i].name)
{
this->m_Inquiry.InsertItem(0, stu[i].name);
this->m_Inquiry.SetItemText(0, 1, stu[i].id);
this->m_Inquiry.SetItemText(0, 2, stu[i].grade);
CString str;
str.Format(_T("%lf"), stu[i].sub1);
this->m_Inquiry.SetItemText(0, 3, str);
str.Format(_T("%lf"), stu[i].sub2);
this->m_Inquiry.SetItemText(0, 4, str);
str.Format(_T("%lf"), stu[i].sub3);
this->m_Inquiry.SetItemText(0, 5, str);
break;
}
}
//增加日志信息
CTime CurrentTime = CTime::GetCurrentTime();
CString strTime;
strTime.Format(_T("%d:%d:%d"), CurrentTime.GetHour(), CurrentTime.GetMinute(), CurrentTime.GetSecond());
CString log;
log = "";
log += strTime;
log += " TREE DOUBLE CLICKED";
this->m_Log.AddString(log);
*pResult = 0;
}
右键单击树形控件,选择添加事件TVN_ITEMEXPANDING:
为List Contrl 添加事件LVN_ITEMCHANGED:
双击"平均分"按钮,添加代码:
int i = m_Inquiry.GetSelectionMark();//获得选中List Contrl行的行标
CString s1, s2, s3;
s1 = m_Inquiry.GetItemText(i, 3); //i是行,j是列。m_Inquiry是个list control 对象
s2 = m_Inquiry.GetItemText(i, 4);
s3 = m_Inquiry.GetItemText(i, 5);
double a = _wtof(s1.GetBuffer()); //转换为double类型的数据
double b = _wtof(s2.GetBuffer());
double c = _wtof(s3.GetBuffer());
double d = (a + b + c)/3; //进行计算
CString str;
str.Format(_T("%lf"), d); //再转换
CString SS;
SS = "";
SS += "平均成绩为:";
SS += str;
MessageBox(SS);
(该树形控件显示实验就此完成,如果该篇bolg对大家有帮助的话,别忘了对BZ进行打赏噢~~~~~(其实,点赞就好了~~~~))
最后是BZ参考和查询的一些博客和链接等资料:
CString 转换为 double 类型
MFC的list control控件,如何得到某一行某列的字符串内容?
VC简单入门系列实验及知识点总结
MFC ListContrl 的使用