树形控件添加学生信息应用——MFC基于对话框的应用(4)

新建一个基于mfc对话框的程序:

设计界面为:

树形控件添加学生信息应用——MFC基于对话框的应用(4)_第1张图片

如果List Contrl 的控件不是以上格式,而是

树形控件添加学生信息应用——MFC基于对话框的应用(4)_第2张图片

则右键属性,点开View选择Report(报表)即可。

树形控件添加学生信息应用——MFC基于对话框的应用(4)_第3张图片

为九个方框添加变量并为每个控件修改符合(自己喜欢)的ID:

树形控件添加学生信息应用——MFC基于对话框的应用(4)_第4张图片

在主程序的cpp文件下,找到OnInitDialog函数为树形控件和可视化列表框控件设置风格和初始值

添加代码如下:

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);

 双击Add按钮,添加代码:

(注意:添加代码必须包含在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:

树形控件添加学生信息应用——MFC基于对话框的应用(4)_第5张图片

编辑代码为:

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:

树形控件添加学生信息应用——MFC基于对话框的应用(4)_第6张图片

平均分的显示:单击学生信息时,点击平均成绩可显示该生的三科成绩的平均分:

为List Contrl 添加事件LVN_ITEMCHANGED:

树形控件添加学生信息应用——MFC基于对话框的应用(4)_第7张图片

双击"平均分"按钮,添加代码:

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 的使用

你可能感兴趣的:(MFC程序应用)