📄 accountmanagement.cpp
字号:
// AccountManagement.cpp : implementation file
//
#include "stdafx.h"
#include "Financing_Master_Hand.h"
#include "AccountManagement.h"
#include "Register_InOutcome_AccountDlg.h" //包含注册帐号对话框类
#include "Financing_Master_HandDoc.h" //包含文档类
#include "Financing_Master_HandView.h" //包含,当读帐号的时候要初始化此类
#ifdef _DEBUG //的m_accountArray
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAccountManagement
IMPLEMENT_DYNCREATE(CAccountManagement, CListView)
CAccountManagement::CAccountManagement()
{
m_nAccountNum = 0;
for(int i=0;i<6;i++)
m_bAccountFlag[i] = 0;
m_nCurrentItem = -1;
}
CAccountManagement::~CAccountManagement()
{
}
BEGIN_MESSAGE_MAP(CAccountManagement, CListView)
//{{AFX_MSG_MAP(CAccountManagement)
ON_COMMAND(ID_ADD_ACCOUNT, OnAddAccount)
ON_COMMAND(ID_DELETE_ACCOUNT, OnDeleteAccount)
ON_WM_LBUTTONDOWN()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CAccountManagement drawing
void CAccountManagement::OnDraw(CDC* pDC)
{
CFinancing_Master_HandDoc* pDoc = GetDocument();
// TODO: add draw code here
}
/////////////////////////////////////////////////////////////////////////////
// CAccountManagement diagnostics
#ifdef _DEBUG
void CAccountManagement::AssertValid() const
{
CListView::AssertValid();
}
void CAccountManagement::Dump(CDumpContext& dc) const
{
CListView::Dump(dc);
}
CFinancing_Master_HandDoc* CAccountManagement::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CFinancing_Master_HandDoc)));
return (CFinancing_Master_HandDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CAccountManagement message handlers
void CAccountManagement::OnInitialUpdate()
{
CListView::OnInitialUpdate();
// TODO: Add your specialized code here and/or call the base class
this->GetListCtrl().SetExtendedStyle(GetListCtrl().GetExtendedStyle() | LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT);
//设置列表的列头
this->GetListCtrl().InsertColumn(0,_T("Account"),LVCFMT_LEFT,96);
GetListCtrl().InsertColumn(1,_T("Name"),LVCFMT_LEFT,96);
GetListCtrl().InsertColumn(2,_T("RemainMoney"),LVCFMT_LEFT,96);
GetListCtrl().InsertColumn(3,_T("Date"),LVCFMT_LEFT,96);
GetListCtrl().InsertColumn(4,_T("Remark"),LVCFMT_LEFT,96);
GetListCtrl().InsertColumn(5,_T("AccountType"),LVCFMT_LEFT,96);
}
BOOL CAccountManagement::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Add your specialized code here and/or call the base class
cs.style &= ~LVS_TYPEMASK;
cs.style |= LVS_REPORT; //初始化为列表显示方式
return CListView::PreCreateWindow(cs);
}
void CAccountManagement::addItem(int i,CAccounts &temp) //添加一项记录
{
GetListCtrl().InsertItem(i,(LPCTSTR)temp.m_account.m_sAccount,i);
GetListCtrl().SetItemText(i,1,(LPCTSTR)temp.m_account.m_sName);
CString s;
s.Format(_T("%.2f"),temp.m_account.m_fRemainMoney);
GetListCtrl().SetItemText(i,2,(LPCTSTR)s);
GetListCtrl().SetItemText(i,3,(LPCTSTR)temp.m_account.m_sDate);
GetListCtrl().SetItemText(i,4,(LPCTSTR)temp.m_account.m_sRemark);
int j,n,type = temp.m_nNum;
BOOL haveFlag = FALSE;
if(type == 2) //收入支出类型
{
s = "In/Outcome";
m_bAccountFlag[2]++;
CFinancing_Master_HandApp *TheApp =(CFinancing_Master_HandApp*) AfxGetApp();
CFinancing_Master_HandView *pFirstView = (CFinancing_Master_HandView*)(TheApp->m_pFirstView);
n = pFirstView->m_accountArray.GetSize();
for(j=0;j<n;j++)
{
if(pFirstView->m_accountArray[j] == temp.m_account.m_sAccount)
{
haveFlag = TRUE;
break;
}
}
if(!haveFlag) pFirstView->m_accountArray.Add(temp.m_account.m_sAccount);
}
else if(type == 3) //投资管理
{
s= "Invest";
m_bAccountFlag[3]++;
}
else if(type == 4) //借贷管理
{
s= "Loan";
m_bAccountFlag[4]++;
}
else
{
s = "Securities"; //证券管理
m_bAccountFlag[5]++;
}
GetListCtrl().SetItemText(i,5,(LPCTSTR)s);
// AfxMessageBox(_T("insert type=")+s);
}
void CAccountManagement::SerializeInfo()
{
CFile file; //并行化帐号
CFinancing_Master_HandApp *TheApp =(CFinancing_Master_HandApp*) AfxGetApp();
if(file.Open(TheApp->m_sName,CFile::modeRead))
{
GetDocument()->m_nTypeFlag = 1; //设置并行化文件类型
CArchive ar(&file,CArchive::load);
// AfxMessageBox(_T("oninitupdate caccountmanagement.cpp"));
GetDocument()->Serialize(ar);
// AfxMessageBox(_T("load succed"));
GetDocument()->m_nTypeFlag = 0; //恢复初始值
}
else //打开文件失败
{
if(TheApp->m_sName == _T("admin")) //创建admin文件
{
if(!file.Open(TheApp->m_sName,CFile::modeCreate|CFile::modeWrite)) //创建文件失败
{
// AfxMessageBox(_T("Create file failed1"));
PostMessage(WM_CLOSE);
}
else
{
// AfxMessageBox(_T("admin accountmanagement.cpp"));
GetDocument()->m_nTypeFlag = 1;
CArchive ar(&file,CArchive::store);
GetDocument()->Serialize(ar);
// AfxMessageBox(_T("hello serialize caccountmamagemeng"));
GetDocument()->m_nTypeFlag = 0;
// AfxMessageBox(_T("failed hello"));
}
// AfxMessageBox(_T("end if"));
}
else
{
AfxMessageBox(_T("why"));
PostMessage(WM_CLOSE); //失败时,程序结束
}
}
// AfxMessageBox(_T("accountmanagement oninit return "));
file.Close();
// AfxMessageBox(_T("failed return"));
}
void CAccountManagement::OnAddAccount()
{
// TODO: Add your command handler code here
CRegister_InOutcome_AccountDlg dlg;
if(dlg.DoModal() == IDOK)
{
CAccounts temp;
temp.m_account.m_sAccount = dlg.m_sAccountName;
temp.m_account.m_sName =((CFinancing_Master_HandApp*) AfxGetApp())->m_sName;
temp.m_account.m_sRemark = dlg.m_AccountRemark;
temp.m_account.m_fRemainMoney=0; //有待修改
CTime time = CTime::GetCurrentTime();
temp.m_account.m_sDate.Format(_T("%04d-%02d-%02d"),time.GetYear(),
time.GetMonth(),time.GetDay());; //时间
temp.m_nNum = dlg.m_nAccountType;
// AfxMessageBox(_T("%d"),dlg.m_nAccountType);
addItem(GetListCtrl().GetItemCount(),temp);
m_nAccountNum++;
}
}
void CAccountManagement::OnDeleteAccount()
{
// TODO: Add your command handler code here
if( m_nCurrentItem == -1)
return;
CString account; //存储将要删除的那一项的帐号
CString strtype; //存储删除的帐号的类型
int i,type;
account=GetListCtrl().GetItemText(m_nCurrentItem,0); //获得帐号
strtype = GetListCtrl().GetItemText(m_nCurrentItem,5); //帐号类型
type = GetAccountType(strtype);
int nAccountNum = 0;
CFinancing_Master_HandApp *TheApp =(CFinancing_Master_HandApp*) AfxGetApp();
if(type == 2) //删除现金类型的帐号
{
CFinancing_Master_HandView *pFirstView =
(CFinancing_Master_HandView *)(TheApp->m_pFirstView);//获得帐号管理视图指针
nAccountNum = pFirstView->m_accountArray.GetSize();
if(m_bAccountFlag[2]>0) m_bAccountFlag[2]--;
for(i=0;i<nAccountNum;i++)
if(pFirstView->m_accountArray[i] == account) //找到改帐号
{
pFirstView->m_accountArray.RemoveAt(i);
}
}
else if(type == 3) {} //以下为未实现的管理类型
else if(type == 4) {}
else if(type == 5) {}
else {}
GetListCtrl().DeleteItem(m_nCurrentItem);
m_nAccountNum--;
}
void CAccountManagement::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);
}
int CAccountManagement::GetAccountType(CString strType)
{
if(strType=="In/Outcome") //收入支出类型
return 2;
else if(strType=="Invest")
return 3; //投资管理
else if(strType=="Loan") //借贷管理
return 4;
else if(strType=="Securities") //证券管理
return 5;
else return 0; //类型出错
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -