MFC---SaleSystem项目(下)
  5Z6Aj0LQwRjK 2023年11月14日 25 0


8 销售管理窗口

8.1 ui设计

1)添加对话框资源(ID修改为DIALOG_SELL),添加所需控件。

MFC---SaleSystem项目(下)_#include

在窗口属性中,Border改为None,Style改为Child:

MFC---SaleSystem项目(下)_控件_02


MFC---SaleSystem项目(下)_mfc_03

2)选中对话框 -> 右击 -> 添加类 -> 类名:CSellDlg,基类选择CFormView

MFC---SaleSystem项目(下)_mfc_04

3)根据需求,控件关联所需变量

商品名组合框关联CComboBox m_combo,单价编辑框关联int m_price,

个数编辑框关联int m_num,销售信息编辑框关联CString m_sellInfo。

MFC---SaleSystem项目(下)_组合框_05

8.2 界面挂载

在CMainFrame类中OnMyChange函数,添加如下代码:

case NM_B:
{
//CSellDlg类需要包含头文件#include “SellDlg.h”
Context.m_pNewViewClass = RUNTIME_CLASS(CSellDlg);
Context.m_pCurrentFrame = this;
Context.m_pLastView = (CFormView *)m_spliter.GetPane(0, 1);
m_spliter.DeleteView(0, 1);
m_spliter.CreateView(0, 1, RUNTIME_CLASS(CSellDlg), CSize(600, 0), &Context);
CSellDlg *pNewView = (CSellDlg *)m_spliter.GetPane(0, 1);
m_spliter.RecalcLayout();
pNewView->OnInitialUpdate();
m_spliter.SetActivePane(0, 1);
}
break;

程序运行效果图:

MFC---SaleSystem项目(下)_c++_06

8.3功能实现

1)在对话框类中,重写 OnInitDialog 函数,进行初始化。

MFC---SaleSystem项目(下)_控件_07

void CSellDlg::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();

	// TODO:  在此添加专用代码和/或调用基类

	//读取文件,获取商品名,给组合框添加字符串
	//需要包含#include "InfoFile.h"
	CInfoFile file;
	file.ReadDocline(); //读取商品信息
	for (list<msg>::iterator it = file.ls.begin(); it != file.ls.end(); it++)
	{
		m_combo.AddString((CString)it->name.c_str());
	}

	file.ls.clear(); //清空list的内容

	//将第一个商品名设为默认选中项
	m_combo.SetCurSel(0);
}

2)处理组合框所需控制事件

MFC---SaleSystem项目(下)_组合框_08

void CSellDlg::OnCbnSelchangeCombo1()
{
	// TODO:  在此添加控件通知处理程序代码

	CString text;
	//获取当前选中项
	int index = m_combo.GetCurSel();
	//获取当前内容
	m_combo.GetLBText(index, text);

	//需要包含#include "InfoFile.h"
	CInfoFile file;
	file.ReadDocline(); //读取商品信息
	for (list<msg>::iterator it = file.ls.begin(); it != file.ls.end(); it++)
	{
		if (text == it->name.c_str())
		{
			m_price = it->price;
			m_num = 0;
			UpdateData(FALSE); //内容更新到对应的控件
		}
	}

	file.ls.clear(); //清空list的内容
}

3)购买按钮功能实现

void CSellDlg::OnBnClickedButton1()
{
	// TODO:  在此添加控件通知处理程序代码
	
	//获取控件上的内容,更新到对应关联的变量中
	UpdateData(TRUE);

	if (m_num == 0)
	{
		MessageBox(TEXT("个数不能为0"));
		return;
	}

	CString type;
	//获取当前选中项
	int index = m_combo.GetCurSel();
	//获取组合框当前内容
	m_combo.GetLBText(index, type);

	CString str;
	str.Format(_T("商品:%s \r\n单价:%d \r\n个数:%d \r\n总价:%d"), type, m_price, m_num, m_price*m_num);

	m_sellInfo = str; //销售信息
	UpdateData(FALSE);
	MessageBox(str);


	//需要包含#include "InfoFile.h"
	CInfoFile file;
	file.ReadDocline(); //读取商品信息
	for (list<msg>::iterator it = file.ls.begin(); it != file.ls.end(); it++)
	{
		if (type == it->name.c_str())
		{
			it->num = it->num - m_num;
		}
	}
	file.WirteDocline(); //更新文件内容

	file.ls.clear(); //清空list的内容

	m_sellInfo.Empty();
	m_num = 0;
	UpdateData(FALSE); //更新到对应的控件
}

4)取消按钮功能实现

void CSellDlg::OnBnClickedButton3()
{
	// TODO:  在此添加控件通知处理程序代码

	m_combo.SetCurSel(0); //选择第0项目
	m_sellInfo = "";
	m_num = 0;
	OnCbnSelchangeCombo1();
}

9 库存信息窗口

9.1 ui设计

1)添加对话框资源(ID修改为DIALOG_INFO),添加所需控件。

MFC---SaleSystem项目(下)_c++_09

在窗口属性中,Border改为None,Style改为Child:

MFC---SaleSystem项目(下)_mfc_10

View 属性为 Report(报表表模式):

MFC---SaleSystem项目(下)_c++_11

2)选中对话框 -> 右击 -> 添加类 -> 类名:CInfoDlg,基类选择CFormView

MFC---SaleSystem项目(下)_组合框_12

3)根据需求,控件关联所需变量

列表控件关联CListCtrl m_list:

MFC---SaleSystem项目(下)_控件_13

9.2 界面挂载
在CMainFrame类中OnMyChange函数,添加如下代码:

case NM_C:
{
	//CInfoDlg类需要包含头文件#include "InfoDlg.h"
	Context.m_pNewViewClass = RUNTIME_CLASS(CInfoDlg);
	Context.m_pCurrentFrame = this;
	Context.m_pLastView = (CFormView *)m_spliter.GetPane(0, 1);
	m_spliter.DeleteView(0, 1);
	m_spliter.CreateView(0, 1, RUNTIME_CLASS(CInfoDlg), CSize(600, 0), &Context);
	CInfoDlg *pNewView = (CInfoDlg *)m_spliter.GetPane(0, 1);
	m_spliter.RecalcLayout();
	pNewView->OnInitialUpdate();
	m_spliter.SetActivePane(0, 1);
}
	break;

程序运行效果图:

MFC---SaleSystem项目(下)_#include_14

9.3功能实现

在对话框类中,重写 OnInitDialog 函数,进行商品信息初始化:

MFC---SaleSystem项目(下)_组合框_15

void CInfoDlg::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();

	// TODO:  在此添加专用代码和/或调用基类
	// 设置扩展风格
	//LVS_EX_FULLROWSELECT选中整行,LVS_EX_GRIDLINES网格
	m_list.SetExtendedStyle(m_list.GetExtendedStyle() | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);

	// 初始化表头
	CString field[] = { _T("商品ID"), _T("商品名称"), _T("商品价格"), _T("库存数量") };
	for (int i = 0; i < sizeof(field) / sizeof(field[0]); ++i)
	{
		m_list.InsertColumn(i, field[i], LVCFMT_CENTER, 90);
	}

	//需要包含#include "InfoFile.h"
	CInfoFile file;
	file.ReadDocline(); //读取商品信息

	//添加数据
	int i = 0;
	CString str;
	for (list<msg>::iterator it = file.ls.begin(); it != file.ls.end(); it++)
	{
		str.Format(_T("%d"), it->id);
		m_list.InsertItem(i, str);
		int column = 1;
		m_list.SetItemText(i, column++, (CString)it->name.c_str());
		str.Format(_T("%d"), it->price);
		m_list.SetItemText(i, column++, str);
		str.Format(_T("%d"), it->num);
		m_list.SetItemText(i, column++, str);
		i++;
	}

	
}

10 库存添加窗口

10.1 ui设计

1)添加对话框资源(ID修改为DIALOG_ADD),添加所需控件。

MFC---SaleSystem项目(下)_组合框_16

在窗口属性中,Border改为None,Style改为Child:

MFC---SaleSystem项目(下)_组合框_17


MFC---SaleSystem项目(下)_控件_18

2)选中对话框 -> 右击 -> 添加类 -> 类名:CAddDlg,基类选择CFormView

MFC---SaleSystem项目(下)_组合框_19

3)根据需求,控件关联所需变量
添加个数:
商品组合框关联CComboBox m_combo,单价编辑框关联int m_price1,
个数编辑框关联int m_num1。

添加新产品:

商品组合框关联CString m_name2,单价编辑框关联int m_price2,

个数编辑框关联int m_num2。

MFC---SaleSystem项目(下)_控件_20

10.2 界面挂载

在CMainFrame类中OnMyChange函数,添加如下代码:

MFC---SaleSystem项目(下)_mfc_21

10.3功能实现

1)在对话框类中,重写 OnInitDialog 函数,进行商品信息初始化:

MFC---SaleSystem项目(下)_mfc_22

void CAddDlg::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();

	// TODO:  在此添加专用代码和/或调用基类

	//读取文件,获取商品名,给组合框添加字符串
	//需要包含#include "InfoFile.h"
	CInfoFile file;
	file.ReadDocline(); //读取商品信息
	for (list<msg>::iterator it = file.ls.begin(); it != file.ls.end(); it++)
	{
		m_combo.AddString((CString)it->name.c_str());
	}

	file.ls.clear(); //清空list的内容

	//将第一个商品名设为默认选中项
	m_combo.SetCurSel(0);
}

2)处理组合框所需控制事件

MFC---SaleSystem项目(下)_mfc_23

void CAddDlg::OnCbnSelchangeCombo2()
{
	// TODO:  在此添加控件通知处理程序代码

	CString text;
	//获取当前选中项
	int index = m_combo.GetCurSel();
	//获取当前内容
	m_combo.GetLBText(index, text);

	//需要包含#include "InfoFile.h"
	CInfoFile file;
	file.ReadDocline(); //读取商品信息
	for (list<msg>::iterator it = file.ls.begin(); it != file.ls.end(); it++)
	{
		if (text == it->name.c_str())
		{
			m_price1 = it->price;
			m_num1 = 0;
			UpdateData(FALSE); //内容更新到对应的控件
		}
	}

	file.ls.clear(); //清空list的内容
}

3)添加个数按钮实现

void CAddDlg::OnBnClickedButton1()
{
	// TODO:  在此添加控件通知处理程序代码

	//获取控件上的内容,更新到对应关联的变量中
	UpdateData(TRUE);

	if (m_num1 == 0)
	{
		MessageBox(TEXT("个数不能为0"));
		return;
	}

	CString type;
	//获取当前选中项
	int index = m_combo.GetCurSel();
	//获取组合框当前内容
	m_combo.GetLBText(index, type);

	CString str;
	str.Format(_T("添加了 商品:%s \r\n单价:%d \r\n个数:%d"), type, m_price1, m_num1);
	MessageBox(str);


	//需要包含#include "InfoFile.h"
	CInfoFile file;
	file.ReadDocline(); //读取商品信息
	for (list<msg>::iterator it = file.ls.begin(); it != file.ls.end(); it++)
	{
		if (type == it->name.c_str())
		{
			it->num +=  m_num1;
		}
	}
	file.WirteDocline(); //更新文件内容

	file.ls.clear(); //清空list的内容

	m_num1 = 0;
	UpdateData(FALSE); //更新到对应的控件
}

4)添加个数取消按钮实现

void CAddDlg::OnBnClickedButton2()
{
	// TODO:  在此添加控件通知处理程序代码

	m_combo.SetCurSel(0);
	m_num1 = 0;
	OnCbnSelchangeCombo2();
}

5)添加新商品按钮实现

void CAddDlg::OnBnClickedButton4()
{
	// TODO:  在此添加控件通知处理程序代码

	UpdateData(TRUE); //获取控件内容

	if (m_num2 <= 0 || m_price2 <= 0 || m_name2.IsEmpty())
	{
		MessageBox(TEXT("输入信息有误"));
		return;
	}

	//需要包含#include "InfoFile.h"
	CInfoFile file;
	file.ReadDocline(); //读取商品信息
	file.Addline(m_name2, m_num2, m_price2); //添加商品
	file.WirteDocline(); //写文件
	file.ls.clear(); //清空list的内容
	MessageBox(_T("添加成功"));

	m_name2.Empty();
	m_num2 = 0;
	m_price2 = 0;
	UpdateData(FALSE);
}

6)添加新商品取消按钮实现

void CAddDlg::OnBnClickedButton5()
{
	// TODO:  在此添加控件通知处理程序代码
	m_name2.Empty();
	m_num2 = 0;
	m_price2 = 0;
	UpdateData(FALSE);
}

11 库存删除窗口

11.1 ui设计

1)添加对话框资源(ID修改为DIALOG_DEL),添加所需控件。

MFC---SaleSystem项目(下)_控件_24

在窗口属性中,Border改为None,Style改为Child:

MFC---SaleSystem项目(下)_#include_25


MFC---SaleSystem项目(下)_组合框_26

2)选中对话框 -> 右击 -> 添加类 -> 类名:CDelDlg,基类选择CFormView

MFC---SaleSystem项目(下)_mfc_27

3)根据需求,控件关联所需变量

商品名组合框关联CComboBox m_combo,单价编辑框关联int m_price,

个数编辑框关联int m_num。

MFC---SaleSystem项目(下)_组合框_28

11.2 界面挂载

在CMainFrame类中OnMyChange函数,添加如下代码:

case NM_E:
{
	//CDelDlg类需要包含头文件#include "DelDlg.h"
	Context.m_pNewViewClass = RUNTIME_CLASS(CDelDlg);
	Context.m_pCurrentFrame = this;
	Context.m_pLastView = (CFormView *)m_spliter.GetPane(0, 1);
	m_spliter.DeleteView(0, 1);
	m_spliter.CreateView(0, 1, RUNTIME_CLASS(CDelDlg), CSize(600, 0), &Context);
	CDelDlg *pNewView = (CDelDlg *)m_spliter.GetPane(0, 1);
	m_spliter.RecalcLayout();
	pNewView->OnInitialUpdate();
	m_spliter.SetActivePane(0, 1);
}
	break;

程序运行效果图:

MFC---SaleSystem项目(下)_mfc_29

11.3功能实现

1)在对话框类中,重写 OnInitDialog 函数,进行初始化。

MFC---SaleSystem项目(下)_控件_30

void CDelDlg::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();

	//读取文件,获取商品名,给组合框添加字符串
	//需要包含#include "InfoFile.h"
	CInfoFile file;
	file.ReadDocline(); //读取商品信息
	for (list<msg>::iterator it = file.ls.begin(); it != file.ls.end(); it++)
	{
		m_combo.AddString((CString)it->name.c_str());
	}


	//将第一个商品名设为默认选中项
	m_combo.SetCurSel(0);
}

2)处理组合框所需控制事件

MFC---SaleSystem项目(下)_c++_31

void CDelDlg::OnCbnSelchangeCombo1()
{
	// TODO:  在此添加控件通知处理程序代码

	CString text;
	//获取当前选中项
	int index = m_combo.GetCurSel();
	//获取当前内容
	m_combo.GetLBText(index, text);

	//需要包含#include "InfoFile.h"
	CInfoFile file;
	file.ReadDocline(); //读取商品信息
	for (list<msg>::iterator it = file.ls.begin(); it != file.ls.end(); it++)
	{
		if (text == it->name.c_str())
		{
			m_price = it->price;
			m_num = 0;
			UpdateData(FALSE); //内容更新到对应的控件
		}
	}

	
}

3)确定按钮功能实现

void CDelDlg::OnBnClickedButton1()
{
	// TODO:  在此添加控件通知处理程序代码

	//获取控件上的内容,更新到对应关联的变量中
	UpdateData(TRUE);

	if (m_num == 0)
	{
		MessageBox(TEXT("个数不能为0"));
		return;
	}

	CString type;
	//获取当前选中项
	int index = m_combo.GetCurSel();
	//获取组合框当前内容
	m_combo.GetLBText(index, type);

	CString str;
	str.Format(_T("删除商品:%s \r\n单价:%d \r\n个数:%d "), type, m_price, m_num);
	MessageBox(str);


	//需要包含#include "InfoFile.h"
	CInfoFile file;
	file.ReadDocline(); //读取商品信息
	for (list<msg>::iterator it = file.ls.begin(); it != file.ls.end(); it++)
	{
		if (type == it->name.c_str())
		{
			it->num = it->num - m_num;
		}
	}
	file.WirteDocline(); //更新文件内容

	

	m_num = 0;
	UpdateData(FALSE); //更新到对应的控件
}

4)取消按钮功能实现

void CDelDlg::OnBnClickedButton2()
{
	// TODO:  在此添加控件通知处理程序代码

	m_combo.SetCurSel(0); //选择第0项目
	m_num = 0;
	OnCbnSelchangeCombo1();
}

12 菜单栏

1)切换到资源视图的Menu,删除掉所有默认(帮助除外):

MFC---SaleSystem项目(下)_控件_32


MFC---SaleSystem项目(下)_c++_33

2)右键菜单栏项,添加事件处理程序,选择COMMAND 消息类型,添加至CMainFrame框架类中。

MFC---SaleSystem项目(下)_mfc_34

MFC---SaleSystem项目(下)_mfc_35

3)在事件处理函数中发送自定义信号,其它菜单操作类似。
//个人信息菜单

void CMainFrame::On32772()
{
	// TODO:  在此添加命令处理程序代码
::PostMessage(AfxGetMainWnd()->GetSafeHwnd(), NM_A, (WPARAM)NM_A, (LPARAM)0);
}


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月14日 0

暂无评论

推荐阅读
5Z6Aj0LQwRjK