程序的运行:
首先来看登录界面:
这个登录界面对应ManageSystemDlg类,在ManageSystemDlg.cpp中主要就是一个登录的逻辑代码,如果用户名和密码输入正确当前主窗口隐藏,子窗口弹出,子窗口SysytemDig就是程序的主界面类:
关键代码:
void CManageSystemDlg::OnBnClickedButtonLgn()
{
// TODO: Add your control notification handler code here
CString sun, spwd;
GetDlgItemText(IDC_EDIT_UNAME, sun);
GetDlgItemText(IDC_EDIT_PWD, spwd);
//登录的条件
BOOL isSuccess = !sun.CollateNoCase(_T("admin")) && !spwd.CollateNoCase(_T("123456"));
//isSuccess = true;
if (isSuccess){
//当前的主窗口隐藏 子窗口出现
ShowWindow(SW_HIDE);
//创建一个对话框
SystemDig dig;
//弹出来
dig.DoModal();
}else{
MessageBox(_T("用户名或密码不正确"),_T("登陆失败"));
}
}
接下来就是程序的主界面类:SystemDig
SystemDig.h代码:
#pragma once
#include "afxcmn.h"
// SystemDig dialog
class SystemDig : public CDialogEx
{
DECLARE_DYNAMIC(SystemDig)
public:
SystemDig(CWnd* pParent = NULL); // standard constructor
virtual ~SystemDig();
// Dialog Data
#ifdef AFX_DESIGN_TIME
enum {
IDD = IDD_DIALOG1 };
#endif
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnClose();
virtual BOOL OnInitDialog();
// 这是显示捐款收据的列表
CListCtrl m_List;
afx_msg void OnBnClickedButton1();
afx_msg void OnBnClickedButtonReverse();
afx_msg void OnBnClickedButton3();
afx_msg void OnBnClickedButton4();
afx_msg void OnBnClickedButton5();
afx_msg void OnBnClickedButtonSave();
afx_msg void OnBnClickedButtonOpen();
afx_msg void OnNMDblclkList1(NMHDR *pNMHDR, LRESULT *pResult);
};
// SystemDig.cpp : implementation file
//
#include "stdafx.h"
#include "ManageSystem.h"
#include "SystemDig.h"
#include "afxdialogex.h"
#include "PropertyDlg.h"
// SystemDig dialog
IMPLEMENT_DYNAMIC(SystemDig, CDialogEx)
SystemDig::SystemDig(CWnd* pParent /*=NULL*/)
: CDialogEx(IDD_DIALOG1, pParent)
{
}
SystemDig::~SystemDig()
{
}
void SystemDig::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_LIST1, m_List);
}
BEGIN_MESSAGE_MAP(SystemDig, CDialogEx)
ON_WM_CLOSE()
ON_BN_CLICKED(IDC_BUTTON1, &SystemDig::OnBnClickedButton1)
ON_BN_CLICKED(IDC_BUTTON_REVERSE, &SystemDig::OnBnClickedButtonReverse)
ON_BN_CLICKED(IDC_BUTTON3, &SystemDig::OnBnClickedButton3)
ON_BN_CLICKED(IDC_BUTTON4, &SystemDig::OnBnClickedButton4)
ON_BN_CLICKED(IDC_BUTTON5, &SystemDig::OnBnClickedButton5)
ON_BN_CLICKED(IDC_BUTTON_SAVE, &SystemDig::OnBnClickedButtonSave)
ON_BN_CLICKED(IDC_BUTTON_OPEN, &SystemDig::OnBnClickedButtonOpen)
ON_NOTIFY(NM_DBLCLK, IDC_LIST1, &SystemDig::OnNMDblclkList1)
END_MESSAGE_MAP()
// SystemDig message handlers
//窗口关闭时执行
void SystemDig::OnClose()
{
// TODO: Add your message handler code here and/or call default
//希望关闭主窗口时 子窗口也关闭 获取主窗口的指针
CDialog* pdlg=(CDialog*)AfxGetMainWnd();
pdlg->DestroyWindow();
CDialogEx::OnClose();
}
//程序初始化时执行
BOOL SystemDig::OnInitDialog()
{
CDialogEx::OnInitDialog();
// TODO: Add extra initialization here
//设置扩展样式 整行显示 复选框等等
m_List.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_CHECKBOXES);
//添加表头 列
m_List.InsertColumn(0,_T("捐款类型"),0,200);
m_List.InsertColumn(1, _T("捐款时间"),0,200);
m_List.InsertColumn(2, _T("捐款金额"),0,200);
//创建行
CString itemName,sdate,sn;
for (int i = 0; i < 10; i++) {
itemName.Format(_T("itemName=%d"),i);
sdate.Format(_T("2008-05-13 09:0%d"),i);
sn.Format(_T("%d00"),i);
m_List.InsertItem(i,itemName);
m_List.SetItemText(i,1,sdate);
m_List.SetItemText(i, 2, sn);
}
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
//全选
void SystemDig::OnBnClickedButton1()
{
// TODO: Add your control notification handler code here
for (int i = 0; i < m_List.GetItemCount(); i++) {
m_List.SetCheck(i, TRUE);
}
}
//反选
void SystemDig::OnBnClickedButtonReverse()
{
// TODO: Add your control notification handler code here
for (int i = 0; i < m_List.GetItemCount(); i++) {
m_List.SetCheck(i, !m_List.GetCheck(i));
}
}
//增加
void SystemDig::OnBnClickedButton3()
{
// TODO: Add your control notification handler code here
PropertyDlg dig;
//弹出窗口
dig.DoModal();
int nCount=m_List.GetItemCount();
m_List.InsertItem(nCount,dig.sType);
m_List.SetItemText(nCount,1,dig.sDate);
m_List.SetItemText(nCount, 2, dig.sMoney);
}
//删除
void SystemDig::OnBnClickedButton4()
{
// TODO: Add your control notification handler code here
for (int i = 0; i < m_List.GetItemCount(); i++) {
BOOL state=m_List.GetCheck(i);
if (state) {
m_List.DeleteItem(i);
i--;
}
}
}
//修改
void SystemDig::OnBnClickedButton5()
{
// TODO: Add your control notification handler code here
for (int i = 0; i < m_List.GetItemCount(); i++) {
BOOL state = m_List.GetCheck(i);
if (state) {
PropertyDlg dig;
dig.DoModal();
m_List.SetItemText(i,0,dig.sType);
m_List.SetItemText(i, 1, dig.sDate);
m_List.SetItemText(i, 2, dig.sMoney);
}
}
}
//保存
void SystemDig::OnBnClickedButtonSave()
{
// TODO: Add your control notification handler code here
CFileDialog fDig(FALSE,".dat","saveData",OFN_OVERWRITEPROMPT,"数据文件(*.dat)|*.dat|所有文件(*.*)|*.*||",NULL);
if (fDig.DoModal() == IDOK) {
CStdioFile file(fDig.GetPathName(), CFile::modeWrite | CFile::modeCreate);
CString cs;
for (int i = 0; i < m_List.GetItemCount(); i++) {
cs = m_List.GetItemText(i, 0)+"#";
//拼接 +=
cs += m_List.GetItemText(i, 1)+"#";
cs += m_List.GetItemText(i, 2)+"\n";
file.WriteString(cs);
}
file.Close();
}
}
//打开
void SystemDig::OnBnClickedButtonOpen()
{
// TODO: Add your control notification handler code here
CFileDialog fDig(TRUE, ".dat", "saveData", OFN_OVERWRITEPROMPT, "数据文件(*.dat)|*.dat|所有文件(*.*)|*.*||", NULL);
if (fDig.DoModal() == IDOK) {
CStdioFile file(fDig.GetPathName(), CFile::modeRead);
CString cs;
int index = 0;
while (file.ReadString(cs)) {
int x = cs.Find("#");
int x2 = cs.Find("#", x + 1);
int m = x + 1;
int n = x2 - x - 1;
int y = cs.GetLength() - x2 - 1;
m_List.InsertItem(index, cs.Left(x));
m_List.SetItemText(index, 1, cs.Mid(m, n));
m_List.SetItemText(index, 2, cs.Right(y));
index++;
}
}
}
//右击事件添加的事件处理
void SystemDig::OnNMDblclkList1(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
// TODO: Add your control notification handler code here
*pResult = 0;
int i=pNMItemActivate->iItem;
PropertyDlg dig;
dig.DoModal();
m_List.SetItemText(i,0,dig.sType);
m_List.SetItemText(i,1,dig.sDate);
m_List.SetItemText(i,2,dig.sMoney);
}
这个其中有一个PropertyDlg类,他就是为了修改的,界面如下:
PropertyDlg.h:
#pragma once
// PropertyDlg dialog
class PropertyDlg : public CDialogEx
{
DECLARE_DYNAMIC(PropertyDlg)
public:
PropertyDlg(CWnd* pParent = NULL); // standard constructor
virtual ~PropertyDlg();
// Dialog Data
#ifdef AFX_DESIGN_TIME
enum {
IDD = IDD_DIALOG2 };
#endif
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
DECLARE_MESSAGE_MAP()
public:
//创建对象
CString sType;
CString sDate;
CString sMoney;
afx_msg void OnBnClickedButtonOk();
};
PropertyDlg.cpp:
// PropertyDlg.cpp : implementation file
//
#include "stdafx.h"
#include "ManageSystem.h"
#include "PropertyDlg.h"
#include "afxdialogex.h"
// PropertyDlg dialog
IMPLEMENT_DYNAMIC(PropertyDlg, CDialogEx)
PropertyDlg::PropertyDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(IDD_DIALOG2, pParent)
{
}
PropertyDlg::~PropertyDlg()
{
}
void PropertyDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(PropertyDlg, CDialogEx)
ON_BN_CLICKED(IDC_BUTTON_OK, &PropertyDlg::OnBnClickedButtonOk)
END_MESSAGE_MAP()
// PropertyDlg message handlers
//确定
void PropertyDlg::OnBnClickedButtonOk()
{
// TODO: Add your control notification handler code here
GetDlgItemText(IDC_EDIT_TYPE,sType);
GetDlgItemText(IDC_EDIT_DATE, sDate);
GetDlgItemText(IDC_EDIT_MONEY, sMoney);
EndDialog(0);
}
这个是一个比较简单的项目,但对于新手往往看不懂MFC框架的知识点,我推荐大家还是先去看看Windows api的知识,再来学习MFC就比较容易了,另外给大家推荐一本书作者好像叫孙鑫,书名叫VC++深入好像,我感觉这是本对初学者作用十分大的书,纯小白可能看不懂