⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 financing_master_handview.cpp

📁 实时系统的实习作业
💻 CPP
字号:
// Financing_Master_HandView.cpp : implementation of the CFinancing_Master_HandView class
//

#include "stdafx.h"
#include "Financing_Master_Hand.h"

#include "Financing_Master_HandDoc.h"
#include "Financing_Master_HandView.h"
#include "Register_InOutcome_AccountDlg.h"            //包含注册现金管理帐号对话框类
#include "AccountManagement.h"                        //包含帐号管理类
#include "AddCashRecordDlg.h"                         //包含添加记录对话框类
//#include <wchar.h>                                    //包含wcstod()
#include <stdlib.h>
#include "resource.h"                                 //包含资源头文件,弹出菜单
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CFinancing_Master_HandView

IMPLEMENT_DYNCREATE(CFinancing_Master_HandView, CListView)

BEGIN_MESSAGE_MAP(CFinancing_Master_HandView, CListView)
	//{{AFX_MSG_MAP(CFinancing_Master_HandView)
	ON_COMMAND(ID_ADD_CASH_RECORD, OnAddCashRecord)
	ON_COMMAND(ID_DELETE_CASH_RECORD, OnDeleteCashRecord)
	ON_COMMAND(ID_CASH_MODIFY, OnCashModify)
	ON_COMMAND(ID_CASH_DELETE, OnCashDelete)
	ON_NOTIFY_REFLECT(NM_RCLICK, OnRclick)
	ON_WM_LBUTTONDOWN()
	ON_COMMAND(ID_CASH_ADD, OnCashAdd)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFinancing_Master_HandView construction/destruction

CFinancing_Master_HandView::CFinancing_Master_HandView()
{
	// TODO: add construction code here
	//初始化变量
	m_nNum=0;                //记录的数量
	m_fAllMoney=0;           //所有余额

//	m_nRecordNum=0;          //记录号
	m_sAccount="";
	m_sDate="";              //记录的日期
	m_bFlag = FALSE;         //默认为收入
	m_fMoney = 0;
//	m_fincome=0;             //收入
//	m_foutcome=0;            //支出
	m_fRemainMoney=0;        //余额
	m_sRemark="";            //备注
	m_nCurrentItem = -1;     //当前选中的项的id号,用来右键时修改或删除
}

CFinancing_Master_HandView::~CFinancing_Master_HandView()
{
}

BOOL CFinancing_Master_HandView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
	cs.style &= ~LVS_TYPEMASK;
	cs.style |= LVS_REPORT;                  //初始化为列表显示方式
	return CListView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CFinancing_Master_HandView drawing

void CFinancing_Master_HandView::OnDraw(CDC* pDC)
{
	CFinancing_Master_HandDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	// TODO: add draw code for native data here
}

void CFinancing_Master_HandView::OnInitialUpdate()
{
	CListView::OnInitialUpdate();

	// TODO: You may populate your ListView with items by directly accessing
	//  its list control through a call to GetListCtrl().
	GetListCtrl().SetExtendedStyle(GetListCtrl().GetExtendedStyle() | LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT);
	//设置列表的列头
	GetListCtrl().InsertColumn(0,_T("Account"),LVCFMT_LEFT,96);
	GetListCtrl().InsertColumn(1,_T("Record Date"),LVCFMT_LEFT,192);
	GetListCtrl().InsertColumn(2,_T("Income"),LVCFMT_LEFT,96);
	GetListCtrl().InsertColumn(3,_T("Outcome"),LVCFMT_LEFT,96);
	GetListCtrl().InsertColumn(4,_T("RemainMoney"),LVCFMT_LEFT,96);
	GetListCtrl().InsertColumn(5,_T("Remark"),LVCFMT_LEFT,96);
	
	
}

/////////////////////////////////////////////////////////////////////////////
// CFinancing_Master_HandView diagnostics

#ifdef _DEBUG
void CFinancing_Master_HandView::AssertValid() const
{
	CListView::AssertValid();
}

void CFinancing_Master_HandView::Dump(CDumpContext& dc) const
{
	CListView::Dump(dc);
}

CFinancing_Master_HandDoc* CFinancing_Master_HandView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CFinancing_Master_HandDoc)));
	return (CFinancing_Master_HandDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CFinancing_Master_HandView message handlers

void CFinancing_Master_HandView::SerializeInfo()
{
	//查看是否已经有记录,如果有记录,就导入记录;如果没有记录,就创建文件,添加记录
	CFile file;
	CFinancing_Master_HandApp *TheApp =(CFinancing_Master_HandApp*) AfxGetApp();
//	AfxMessageBox(_T("handview file"));
	CString fileName = TheApp->m_sName+_T("2"); //收入支出文件命名方式:用户名_2
	BOOL flag = FALSE;
//	AfxMessageBox(fileName);
	if(file.Open(fileName,CFile::modeRead))
	{
	//	AfxMessageBox(_T("have file")+fileName);
		GetDocument()->m_nTypeFlag = 2;         //设置并行化文件方式
		CArchive ar(&file,CArchive::load);
		GetDocument()->Serialize(ar);
	//	AfxMessageBox(_T("load")+fileName+_T("succeed"));
		GetDocument()->m_nTypeFlag = 0;         //恢复初始值
		flag = TRUE;
//		file.Close();
//	AfxMessageBox(_T("hand view init problem"));
	}
	else 
	{
		/*AfxMessageBox(_T("You now do not have an account,please register one."));
		CRegister_InOutcome_AccountDlg dlg;          //注册收入支出帐号
		dlg.m_sAccountName = "admin";
		if(dlg.DoModal() == IDOK)                    //这册成功
		{
			CFinancing_Master_HandApp *TheApp=(CFinancing_Master_HandApp*)AfxGetApp();
			CAccountManagement * pAnotherView;
			pAnotherView =(CAccountManagement *) TheApp->m_pAnotherView;
			CAccounts temp;
			temp.m_account.m_sAccount = dlg.m_sAccountName;
			temp.m_account.m_sName = TheApp->m_sName;
			temp.m_account.m_sRemark = dlg.m_AccountRemark;
			temp.m_account.m_fRemainMoney=0;            //有待修改
			temp.m_account.m_sDate = _T("today");           //有待修改
			temp.m_nNum = 2;
		//	pAnotherView->addItem(pAnotherView->m_nAccountNum,temp);
			//pAnotherView->m_nAccountNum=(pAnotherView->m_nAccountNum)+1;
		}*/
	}
	if(flag) file.Close();
}

void CFinancing_Master_HandView::OnAddCashRecord() 
{
	// TODO: Add your command handler code here
	CAddCashRecordDlg dlg;
	
	////////////////以下4行初始化时间///////////////////
	CTime currentTime = CTime::GetCurrentTime();
	dlg.m_nYear = currentTime.GetYear();
	dlg.m_nMonth = currentTime.GetMonth();
	dlg.m_nDay = currentTime.GetDay();
	///////////////初始化时间结束///////////////////////

	if(dlg.DoModal() == IDOK)          //添加记录
	{
	//	AfxMessageBox(_T("you have choose")+dlg.m_sAccount);
		CString s,temp;
		int n;
		float addMoney,result;    //添加一项记录时,需要增加的金额
		s.Format(_T("%4d-%02d-%02d"),dlg.m_nYear,dlg.m_nMonth,dlg.m_nDay);//格式化时间
		n = m_nNum - 1;
		for(;n>=0;n--)
		{
			if( GetListCtrl().GetItemText(n,1) <= s )
				break;
		}
		n++;
		GetListCtrl().InsertItem(n,(LPCTSTR)dlg.m_sAccount,n); //添加一项
		GetListCtrl().SetItemText(n,1,(LPCTSTR)s);                  //记录日期
		if(dlg.m_bIncome)                 //收入
		{
			s.Format(_T("%.2f"),dlg.m_fMoney);
			GetListCtrl().SetItemText(n,2,(LPCTSTR)s);               //收入
			s = "";
			GetListCtrl().SetItemText(n,3,(LPCTSTR)s);               //支出
			addMoney = dlg.m_fMoney;
		}
		else
		{
			s = "";
			GetListCtrl().SetItemText(n,2,(LPCTSTR)s);               //收入
			s.Format(_T("%.2f"),dlg.m_fMoney);
			GetListCtrl().SetItemText(n,3,(LPCTSTR)s);               //支出
			addMoney = -dlg.m_fMoney;
		}
		s = _T("0");
		if(n == 0)                            //第一项记录
			s.Format(_T("%.2f"),addMoney);
		else                                  //不是第一项记录要根据上一项的记录更新
		{
			temp = GetListCtrl().GetItemText(n-1,4);
			result = (float)wcstod(temp,NULL);
			result += addMoney;
			s.Format(_T("%.2f"),result);
		}
		GetListCtrl().SetItemText(n,4,(LPCTSTR)s);                  //余额
		GetListCtrl().SetItemText(n,5,(LPCTSTR)dlg.m_sRemark);      //备注
		++m_nNum;
		n++;
		for(;n<m_nNum;n++)            //更新添加的记录后面的余额
		{
			temp = GetListCtrl().GetItemText(n,4);
			result = (float)wcstod(temp,NULL);
			result += addMoney;
			s.Format(_T("%.2f"),result);
			GetListCtrl().SetItemText(n,4,(LPCTSTR)s);
		}
		m_fAllMoney = result;
	}
}

void CFinancing_Master_HandView::OnDeleteCashRecord() 
{
	// TODO: Add your command handler code here
	OnCashDelete();
}

void CFinancing_Master_HandView::OnCashModify() 
{
	// TODO: Add your command handler code here
	if(m_nCurrentItem == -1)
		return ;
	CAddCashRecordDlg dlg;
	CString date,strmoney;
	TCHAR *temp;      //以下初始化弹出对话框
	BOOL cashType = TRUE; //初始化为收入
	date = GetListCtrl().GetItemText(m_nCurrentItem,1);
	dlg.m_nYear =(int) wcstol(date,&temp,10);
	dlg.m_nMonth = -((int) wcstol(temp,&temp,10));
	dlg.m_nDay = -( (int)wcstol(temp,&temp,10) );
	dlg.m_comboBox.SetWindowText(GetListCtrl().GetItemText(m_nCurrentItem,0));
	if( (strmoney=GetListCtrl().GetItemText(m_nCurrentItem,2) ) == "")  //如果是支出
	{
		strmoney = GetListCtrl().GetItemText(m_nCurrentItem,3);
		cashType = FALSE;
	}                                                     //否则是收入
	dlg.m_fMoney=(float)wcstod(strmoney,NULL);
	dlg.m_sRemark = GetListCtrl().GetItemText(m_nCurrentItem,5);
	dlg.m_bIncome = cashType;
	if(dlg.DoModal() == IDOK)                              //修改成功
	{//修改方法,现删除一项,再添加一项;代码拷贝自OnAddCashRecord
		OnCashDelete();
		CString s,temp;
		int n;
		float addMoney,result;    //添加一项记录时,需要增加的金额
		s.Format(_T("%4d-%02d-%02d"),dlg.m_nYear,dlg.m_nMonth,dlg.m_nDay);//格式化时间
		n = m_nNum - 1;
		for(;n>=0;n--)
		{
			if( GetListCtrl().GetItemText(n,1) <= s )
				break;
		}
		n++;
		GetListCtrl().InsertItem(n,(LPCTSTR)dlg.m_sAccount,n); //添加一项
		GetListCtrl().SetItemText(n,1,(LPCTSTR)s);                  //记录日期
		if(dlg.m_bIncome)                 //收入
		{
			s.Format(_T("%.2f"),dlg.m_fMoney);
			GetListCtrl().SetItemText(n,2,(LPCTSTR)s);               //收入
			s = "";
			GetListCtrl().SetItemText(n,3,(LPCTSTR)s);               //支出
			addMoney = dlg.m_fMoney;
		}
		else
		{
			s = "";
			GetListCtrl().SetItemText(n,2,(LPCTSTR)s);               //收入
			s.Format(_T("%.2f"),dlg.m_fMoney);
			GetListCtrl().SetItemText(n,3,(LPCTSTR)s);               //支出
			addMoney = -dlg.m_fMoney;
		}
		s = _T("0");
		if(n == 0)                            //第一项记录
			s.Format(_T("%.2f"),addMoney);
		else                                  //不是第一项记录要根据上一项的记录更新
		{
			temp = GetListCtrl().GetItemText(n-1,4);
			result = (float)wcstod(temp,NULL);
			result += addMoney;
			s.Format(_T("%.2f"),result);
		}
		GetListCtrl().SetItemText(n,4,(LPCTSTR)s);                  //余额
		GetListCtrl().SetItemText(n,5,(LPCTSTR)dlg.m_sRemark);      //备注
		++m_nNum;
		n++;
		for(;n<m_nNum;n++)            //更新添加的记录后面的余额
		{
			temp = GetListCtrl().GetItemText(n,4);
			result = (float)wcstod(temp,NULL);
			result += addMoney;
			s.Format(_T("%.2f"),result);
			GetListCtrl().SetItemText(n,4,(LPCTSTR)s);
		}
		m_fAllMoney = result;
	}           
}

void CFinancing_Master_HandView::OnCashDelete() 
{
	// TODO: Add your command handler code here
	int i,n = m_nNum;
	float fdiffMoney;                  //存储删除的记录涉及的金额,收入为正,支出为负
	float fRemainMoney;                    //余额
	CString smoney = GetListCtrl().GetItemText(m_nCurrentItem,2);
	if( smoney =="" ){                  //支出
		smoney = GetListCtrl().GetItemText(m_nCurrentItem,3);
		fdiffMoney =-(float) wcstod(smoney,NULL);
	}
	else {
		fdiffMoney = (float) wcstod(smoney,NULL);
	}
	for(i=m_nCurrentItem;i<m_nNum;i++) {
		smoney = GetListCtrl().GetItemText(i,4);
		fRemainMoney = (float) wcstod(smoney,NULL);
		fRemainMoney = fRemainMoney - fdiffMoney;
		smoney.Format(_T("%.2f"),fRemainMoney);
		GetListCtrl().SetItemText(i,4,(LPCTSTR)smoney);
	}
	m_fAllMoney = m_fAllMoney - fdiffMoney;
	GetListCtrl().DeleteItem(m_nCurrentItem);
	m_nNum--;
}



BOOL CFinancing_Master_HandView::PreTranslateMessage(MSG* pMsg) 
{
	// TODO: Add your specialized code here and/or call the base class

	return CListView::PreTranslateMessage(pMsg);
}

void CFinancing_Master_HandView::OnRclick(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	
	CPoint pt;
    GetCursorPos(&pt);
    GetListCtrl().ScreenToClient(&pt);       //命中测试
	int nIndex;
	if( (nIndex = GetListCtrl().HitTest(pt)) != -1)
	{
		//GetListCtrl().Set
	//	CString s=GetListCtrl().GetItemText(nIndex,1);
	//	AfxMessageBox(s);
		m_nCurrentItem = nIndex;
		CMenu menu;
		menu.LoadMenu(IDR_CONTEXTMENU_CASH);
		CMenu *pMenu;
		pMenu=menu.GetSubMenu(0);
		pMenu->TrackPopupMenu(TPM_LEFTALIGN,pt.x,pt.y,this);
	}
	*pResult = 0;
}

void CFinancing_Master_HandView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	////当点击左键时,进行命中测试,并初始化m_nCurrentItem,用来响应删除操作
	CPoint ps;
	ps = point;
	GetListCtrl().ScreenToClient(&ps);
	int nIndex;
	if( (nIndex = GetListCtrl().HitTest(ps)) != -1 )
		m_nCurrentItem = nIndex;
	CListView::OnLButtonDown(nFlags, point);
}

void CFinancing_Master_HandView::OnCashAdd() //右击时响应,用来添加一项记录
{
	// TODO: Add your command handler code here
	OnAddCashRecord();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -