模块汇总
MFC中包含的类:App、Doc、MainFrame、View、CAboutDlg。执行的顺序也是这样。
一、MFC的消息与消息队列
操作系统封装消息–>消息进入消息队列–>应用程序取消息—>应用程序给操作系统发送消息—>窗口过程
二、设计
1.外部依赖类-CInfoFile
struct msg
{
int id; //商品id
string name; //商品名
int price; //商品价格
int num; //商品个数
};
class CInfoFile
{
public:
CInfoFile();
~CInfoFile();
//读取登陆信息,通过ifstream先打开文件,读取两行的内容存入char类型数组
//设计到一个char*转CString的问题
void ReadLogin(CString &name, CString &pwd);
//修改密码,将内容写入文件,涉及到一个CString转char*的问题,在传参的前期完成工作
void WritePwd(char* name, char* pwd);
// 读取商品数据,将商品信息从文件中读入到list<msg>容器中,涉及到拆分字符串的问题
//strtok(buf, "|")函数,以|为分隔符进行切割
void ReadDocline();
//商品写入文件
//将链表中的数据写入到文件中
void WirteDocline();
//添加新商品
//在链表后端添加,push_back,涉及到CString向char*转换的问题,先将CString转向CStringA,
//再通过str.GetBuffer()函数将CStringA换成char*
void Addline(CString name, int num, int price);
list<msg> ls; //存储商品容器
int num; //用来记录商品个数
};
2.主框架设计MainFrame类、Doc类
图标、标题、窗口大小
主框架图标、窗口大小的在主框架的Oncreat函数中
//设置图标,IDI_ICON_WIN为图标资源ID,此为WINAPI函数
SetClassLong(m_hWnd, GCL_HICON, (LONG)AfxGetApp()->LoadIconW(IDI_ICON_WIN));
//设置窗口的位置和大小:CWnd::MoveWindow
//0, 0, 起点坐标x和y
//800, 500, 窗口宽度和高度
MoveWindow(0, 0, 800, 500);
//将窗口移动到屏幕中央,CWnd::CenterWindow
CenterWindow();
主框架的窗口标题在DOC类的OnNewDocument类中实现
//设置窗口标题,CDocument::SetTitle
SetTitle(TEXT("销售管理系统"));
3.登陆界面设置
3.1界面设计
设计界面控件布局,关联变量,对于 登陆按钮点击控件有以下的要求:
void CLoginDlg::OnBnClickedButton1()
{
UpdateData(TRUE); //更新控件的数据到对应的变量
CInfoFile file; //创建操作文件类对象,需要头文件#include "InfoFile.h"
CString user, pwd;
//读取配置文件,获取用户名密码,参数为引用传递
file.ReadLogin(user, pwd);
if (m_user == user)//用户名相等
{
if (m_pwd != pwd)//密码不同
{
MessageBox(_T("密码错误"));
m_user.Empty(); //清空
m_pwd.Empty();
}
else//密码相同
{
CDialogEx::OnOK();//CDialog确定按钮
}
}
else//用户名不同
{
MessageBox(_T("用户名不存在"));
m_user.Empty();
m_pwd.Empty();
}
}
设计过程结束之后,要注释掉本来的OnOk(),避免按回车键直接进去主框架内,同时可以增加Onclose函数,进行退出设置
3.2对象创建
由于登陆框是在主框架之前创建的,因而在主框架程序入口之前需要创建这个对象。
4.静态拆分窗口
自定义两个类:CSelectView和CDispalyView(它的基类必须是视图类)。
4.1CMainFrame类中,声明CSplitterWnd类型的对象
- 加入私有CSplitterWnd类型变量,用于切分窗口
- 重写框架类CMainFrame的OnCreateClient函数
把OnCreateClient()函数的返回值改为Return TRUE
//切分窗口,首先设置切分窗口的行列总数,继而分别创建窗口,通过指定框体的大小,加载不同大小的类
BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
// TODO: 在此添加专用代码和/或调用基类
// 静态拆分窗口,1行2列,CSplitterWnd::CreateStatic
m_spliter.CreateStatic(this, 1, 2);
// 创建视图:CSplitterWnd::CreateView
//0, 0 : 放在第0行第0列的位置
//RUNTIME_CLASS(CSelectView) :需要头文件#include "SelectView.h", CSelectView在SelectView.h中声明
// CSize(250, 500):指定视图宽度和高度
//pContext : 为OnCreateClient()最后一个形参
m_spliter.CreateView(0, 0, RUNTIME_CLASS(CSelectView), CSize(200, 500), pContext);
//0, 1: 放在第0行第1列的位置
//CDispalyView,需要头文件#include "DispalyView.h"
m_spliter.CreateView(0, 1, RUNTIME_CLASS(CDispalyView), CSize(600, 500), pContext);
//return CFrameWnd::OnCreateClient(lpcs, pContext);
return TRUE;
}
4.2 CSelectView继承于CTreeView
CSelectView的类,作用于切分窗口左侧第一列,继承于CTreeView类,可以利用树型控件,实现列表式的选项进行选择,进而分发消息,实现右侧第二列窗口的刷新挂载
private:
CTreeCtrl *m_treeCtrl; //树控件
CImageList m_imageList; //图标列表
3)重写CSelectView的OnInitUpdate函数
void CSelectView::OnInitialUpdate()
{
CTreeView::OnInitialUpdate();
//图标资源的加载 CWinApp::LoadIcon
//IDI_ICON_RE为图标资源ID
HICON icon = AfxGetApp()->LoadIconW(IDI_ICON_RE);
//图片列表的创建 CImageList::Create
//30, 30:指定图标的宽度和高度
//ILC_COLOR32:图标格式
//1, 1:有多少图标就写多少
m_imageList.Create(30, 30, ILC_COLOR32, 1, 1);
//图片列表追加图标 CImageList::Add
m_imageList.Add(icon);
//获取数视图中的树控件 CTreeView::GetTreeCtrl
m_treeCtrl = &GetTreeCtrl();
//数控件设置图片列表 CTreeCtrl::SetImageList
m_treeCtrl->SetImageList(&m_imageList, TVSIL_NORMAL);
//树控件设置节点 CTreeCtrl::InsertItem
m_treeCtrl->InsertItem(TEXT("个人信息"), 0, 0, NULL);
m_treeCtrl->InsertItem(TEXT("销售管理"), 0, 0, NULL);
m_treeCtrl->InsertItem(TEXT("库存信息"), 0, 0, NULL);
m_treeCtrl->InsertItem(TEXT("库存添加"), 0, 0, NULL);
m_treeCtrl->InsertItem(TEXT("库存删除"), 0, 0, NULL);
}
功能节点相应消息处理
void CSelectView::OnTvnSelchanged(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMTREEVIEW pNMTreeView = reinterpret_cast<LPNMTREEVIEW>(pNMHDR);
// TODO: 在此添加控件通知处理程序代码
*pResult = 0;
//获取当前节点选中项目 CTreeCtrl::GetSelectedItem
HTREEITEM item = m_treeCtrl->GetSelectedItem();
//获取选中项的文本内容 CTreeCtrl::GetItemText
CString str = m_treeCtrl->GetItemText(item);
//MessageBox(str);
if (str == TEXT("个人信息"))
{
//需要包含框架类头文件#include "MainFrm.h"
//CWnd::PostMessage 将一个消息放入窗口的消息队列
//AfxGetMainWnd():框架窗口对象的指针
//AfxGetMainWnd()->GetSafeHwnd():获取返回窗口的句柄,CWnd::GetSafeHwnd
//NM_A:发送自定义消息
//(WPARAM)NM_A:指定了附加的消息信息
//(LPARAM)0:指定了附加的消息信息,此参数这里没有意义
::PostMessage(AfxGetMainWnd()->GetSafeHwnd(), NM_A, (WPARAM)NM_A, (LPARAM)0);
}
else if (str == TEXT("销售管理"))
{
::PostMessage(AfxGetMainWnd()->GetSafeHwnd(), NM_B, (WPARAM)NM_B, (LPARAM)0);
}
else if (str == TEXT("库存信息"))
{
::PostMessage(AfxGetMainWnd()->GetSafeHwnd(), NM_C, (WPARAM)NM_C, (LPARAM)0);
}
else if (str == TEXT("库存添加"))
{
::PostMessage(AfxGetMainWnd()->GetSafeHwnd(), NM_D, (WPARAM)NM_D, (LPARAM)0);
}
else if (str == TEXT("库存删除"))
{
::PostMessage(AfxGetMainWnd()->GetSafeHwnd(), NM_E, (WPARAM)NM_E, (LPARAM)0);
}
}
4.3 CDispalyView继承于CFormView
5.个人信息管理窗口
5.1 窗口设计、类创建、按钮功能实现、窗口风格是child、继承于基类CFormView
包括一系列的初始化,关联变量,从文件中读取数据,同步到变量中,按下修改按钮之后进行以下的操作:
void CUserDlg::OnBnClickedButton()
{
// TODO: 在此添加控件通知处理程序代码
UpdateData(TRUE);//更新控件内容到对应的变量中
if (m_newPwd.IsEmpty() || m_surePwd.IsEmpty())
{
MessageBox(TEXT("输入密码不能为空"));
return;
}
if (m_newPwd != m_surePwd)
{
MessageBox(TEXT("输入密码和确定密码不相等"));
return;
}
CInfoFile file; //需要头文件#include "InfoFile.h"
CString name, pwd;
file.ReadLogin(name, pwd); //读取文件的用户名和密码
if (m_surePwd == pwd)
{
MessageBox(TEXT("输入密码和旧密码相等"));
return;
}
//把用户名和密码的CString类型转为char *
char *tmpName, *tmpPwd;
//用户名
CStringA tmp1;
tmp1 = name;
tmpName = tmp1.GetBuffer();
//密码
CStringA tmp2;
tmp2 = m_surePwd;
tmpPwd = tmp2.GetBuffer();
file.WritePwd(tmpName, tmpPwd); //修改密码
MessageBox(TEXT("密码修改成功"));
//输入框内容清空
m_surePwd.Empty();
m_newPwd.Empty();
UpdateData(FALSE); //把数据更新到控件上
}
5.2界面挂载
1)自定义消息宏,自定义消息宏在主框架类中实现
//WM_USER 是用户自定义消息的一个起始值
//WM_USER+100是为了区分系统消息和用户消息,避免冲突
#define NM_A (WM_USER + 100)
#define NM_B (WM_USER + 101)
#define NM_C (WM_USER + 102)
#define NM_D (WM_USER + 103)
#define NM_E (WM_USER + 104)
2)在CMainFrame框架类中添加自定义消息处理函数
/自定义消息处理函数
afx_msg LRESULT OnMyChange(WPARAM wParam, LPARAM lParam);
3)在CMainFrame框架类BEGIN_MESSAGE_MAP和END_MESSAGE_MAP
之间添加自定义消息入口,与自定义消息处理函数绑定。
//ON_MESSAGE响应的是自定义消息
//产生NM_X消息,自动调用OnMyChange函数
ON_MESSAGE(NM_A, OnMyChange)
ON_MESSAGE(NM_B, OnMyChange)
ON_MESSAGE(NM_C, OnMyChange)
ON_MESSAGE(NM_D, OnMyChange)
ON_MESSAGE(NM_E, OnMyChange)
4)发送消息。
在CSelectView的OnTvnSelchanged函数中,发送自定义信号。
::PostMessage(AfxGetMainWnd()->GetSafeHwnd(),自定义消息宏, (WPARAM)自定义消息宏, (LPARAM)0);
6)自定义信息处理
在CMainFrame框架类OnMyChange函数中处理相应消息
LRESULT CMainFrame::OnMyChange(WPARAM wParam, LPARAM lParam)
{
CCreateContext Context;
switch (wParam)
{
case NM_A:
{
//CUserDlg类需要包含头文件#include "UserDlg.h"
Context.m_pNewViewClass = RUNTIME_CLASS(CUserDlg);
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(CUserDlg), CSize(600,500), &Context);
CUserDlg *pNewView = (CUserDlg *)m_spliter.GetPane(0, 1);
m_spliter.RecalcLayout();
pNewView->OnInitialUpdate();
m_spliter.SetActivePane(0, 1);
}
break;
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;
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;
case NM_D:
{
Context.m_pNewViewClass = RUNTIME_CLASS(CAddDlg);
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(CAddDlg), CSize(600, 0), &Context);
CAddDlg *pNewView = (CAddDlg *)m_spliter.GetPane(0, 1);
m_spliter.RecalcLayout();
pNewView->OnInitialUpdate();
m_spliter.SetActivePane(0, 1);
}
break;
break;
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;
default:
MessageBox(_T("error"));
}
return 0;
}
6.销售管理窗口
6.1 设计窗口、添加控件,关联变量、窗口风格是child、继承于基类CFormView
初始化中,需要将,所有链表中存放的销售商品信息,添加进下拉列表的插入。
void CSellDlg::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());
}
file.ls.clear(); //清空list的内容
//将第一个商品名设为默认选中项
m_combo.SetCurSel(0);
}
//添加控件选择改变的事件处理函数,获取当前控件的索引,获取索引里的字符串,同时读取文件中匹配于该字符串的内容,将list链表中的
//信息拿出来,并且更新到相应的控件之中。
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的内容
}
//按下购买按键时,更新list链表中的内容,同时将信息更新到文件中。
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); //更新到对应的控件
}
7.库存信息窗口
7.1窗口设计,继承于基类CFormView,窗口风格为FromView
主要涉及到一个列表控件的设计:
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++;
}
}
8.库存添加窗口的设计
8.1 初始化函数中,需要将list链表中所有的信息存入下拉列表中
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);
}
8.2 添加下拉框选择改变的消息控制函数
获取当前的控件选项的索引,获取当前控件中的字符串,在list中查找相同的字符串,同时将其他信息同步到其他控件中。
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的内容
}
8.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); //更新到对应的控件
}
8.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);
}
9.库存删除
9.1 重写初始化函数:
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);
}
9.2 添加下拉框选择改变的消息控制函数
void CDelDlg::OnCbnSelchangeCombo1()
{
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); //内容更新到对应的控件
}
}
}
9.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); //更新到对应的控件
}