MFC-第四节 案例:销售系统(二)

销售管理窗口

  1. ui设计
    1. 创建一个对话框,ID为(DIALOG_SELL),在窗口属性中,Border改为None,Style改为Child:
    2. 右击对话框,添加一个类为CSellDlg,基类为CFormView
    3. 布局页面
      1. 拖一个static text 编辑为”销售订单”,点击销售订单,水平居中
      2. 拖2个group box 分别编辑为”购买/订单信息”,都选中,顶部对齐
      3. 左边groupbox 拖入4个static text分别为,商品名/库存/单价/个数,点击右对齐
      4. 选择一个commbox,与商品名对齐,顶部对齐,右击属性->sort 设置为FALSE,type设置为下拉列表
      5. 拖3个Edit control 分别于库存、单价、个数对齐,库存、单价属性readonly设置为ture
      6. 拖1个Edit control放到右边groupbox中,属性readonly设置为ture,mutiline设置为ture,want return 设置为ture,auto vscroll为ture,vertical scroll 设置为ture,horizontal设置为ture
      7. 添加两个按钮:购买、取消
      8. 如下图

        图4

    4. 根据需求,控件关联所需变量
      1. 商品名组合框关联CComboBox m_combo,库存编辑关联框 int m_left, 单价编辑框关联int m_price,个数编辑框关联int m_num,销售信息编辑框关联CString m_sellInfo。
      2. 注意:右击单价->添加变量->类别选择 值(value),变量类型选择 int,确定
  2. 界面挂载
    1. 在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;
      
  3. 功能实现
    1. 将实现准备stock.txt拖入到项目根目录
    2. 在对话框类中,重写 OnInitialUpdate 函数,进行初始化。

       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);
       	//手动更新
       	OnCbnSelchangeCombo1();
       }
      
    3. 处理组合框所需控制事件
      1. 右击商品名commbox->属性->点击事件图标
      2. 找到CBN_SELECHANGE->add

                    
         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_left = it->num;
         			m_price = it->price;
         			m_num = 0;
         			UpdateData(FALSE); //内容更新到对应的控件
         		}
         	}
                    
         	file.ls.clear(); //清空list的内容
         }
        
    4. 购买按钮功能实现

       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;
       			m_left = it->num;
       		}
       	}
       	file.WirteDocline(); //更新文件内容
              
       	file.ls.clear(); //清空list的内容
              
       	m_sellInfo.Empty();
       	m_num = 0;
       	UpdateData(FALSE); //更新到对应的控件
       }
      
    5. 取消按钮功能实现

       void CSellDlg::OnBnClickedButton3()
       {
       	// TODO:  在此添加控件通知处理程序代码
              
       	m_combo.SetCurSel(0); //选择第0项目
       	m_sellInfo = "";
       	m_num = 0;
       	OnCbnSelchangeCombo1();
       }
      

其他窗口(略)

菜单栏

  1. 资源视图->Menu->双击打开IDR_MAINFRAME,删掉所有默认,“帮助”除外
  2. 在框中输入“菜单”、“开始”,添加2个item
    1. 在菜单下分别输入:个人信息、销售管理、库存信息、库存添加、库存删除 5个子item
    2. 在开始下输入:退出 一个子item
  3. 点击“开始”->”退出”右击->添加事件处理程序->消息类型选择COMMAND->列表类选择CMainFrame->添加编辑
  4. 在类CMainFrame中生成函数的实现,然后在函数中写代码:

     //退出程序
     void CMainFrame::On32775()
     {
     	// TODO: 在此添加命令处理程序代码
     	exit(0);
     }
    
  5. 同理给“菜单”->个人信息,也添加一个处理函数
  6. 在函数中发送通知,跟CSelectView类中一样

     //点击菜单-》个人信息
     void CMainFrame::On32771()
     {
     	// TODO: 在此添加命令处理程序代码
     	::PostMessage(AfxGetMainWnd()->GetSafeHwnd(), NM_A, (WPARAM)NM_A, (LPARAM)0);
     }
    
Table of Contents