8 销售管理窗口
8.1 ui设计
1)添加对话框资源(ID修改为DIALOG_SELL),添加所需控件。
在窗口属性中,Border改为None,Style改为Child:
2)选中对话框 -> 右击 -> 添加类 -> 类名:CSellDlg,基类选择CFormView
3)根据需求,控件关联所需变量
商品名组合框关联CComboBox m_combo,单价编辑框关联int m_price,
个数编辑框关联int m_num,销售信息编辑框关联CString m_sellInfo。
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;
程序运行效果图:
8.3功能实现
1)在对话框类中,重写 OnInitDialog 函数,进行初始化。
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)处理组合框所需控制事件
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),添加所需控件。
在窗口属性中,Border改为None,Style改为Child:
View 属性为 Report(报表表模式):
2)选中对话框 -> 右击 -> 添加类 -> 类名:CInfoDlg,基类选择CFormView
3)根据需求,控件关联所需变量
列表控件关联CListCtrl m_list:
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;
程序运行效果图:
9.3功能实现
在对话框类中,重写 OnInitDialog 函数,进行商品信息初始化:
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),添加所需控件。
在窗口属性中,Border改为None,Style改为Child:
2)选中对话框 -> 右击 -> 添加类 -> 类名:CAddDlg,基类选择CFormView
3)根据需求,控件关联所需变量
添加个数:
商品组合框关联CComboBox m_combo,单价编辑框关联int m_price1,
个数编辑框关联int m_num1。
添加新产品:
商品组合框关联CString m_name2,单价编辑框关联int m_price2,
个数编辑框关联int m_num2。
10.2 界面挂载
在CMainFrame类中OnMyChange函数,添加如下代码:
10.3功能实现
1)在对话框类中,重写 OnInitDialog 函数,进行商品信息初始化:
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)处理组合框所需控制事件
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),添加所需控件。
在窗口属性中,Border改为None,Style改为Child:
2)选中对话框 -> 右击 -> 添加类 -> 类名:CDelDlg,基类选择CFormView
3)根据需求,控件关联所需变量
商品名组合框关联CComboBox m_combo,单价编辑框关联int m_price,
个数编辑框关联int m_num。
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;
程序运行效果图:
11.3功能实现
1)在对话框类中,重写 OnInitDialog 函数,进行初始化。
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)处理组合框所需控制事件
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,删除掉所有默认(帮助除外):
2)右键菜单栏项,添加事件处理程序,选择COMMAND 消息类型,添加至CMainFrame框架类中。
3)在事件处理函数中发送自定义信号,其它菜单操作类似。
//个人信息菜单
void CMainFrame::On32772()
{
// TODO: 在此添加命令处理程序代码
::PostMessage(AfxGetMainWnd()->GetSafeHwnd(), NM_A, (WPARAM)NM_A, (LPARAM)0);
}