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

📄 systemdlg.cpp

📁 家庭财务管理系统毕业论文,含源码。基于ACCESS数据库的运用。用了VC++ 6.0 ADO。
💻 CPP
字号:
// SystemDlg.cpp : implementation file
//

#include "stdafx.h"
#include "HomeRes.h"
#include "SystemDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CSystemDlg dialog


CSystemDlg::CSystemDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSystemDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSystemDlg)
	m_fMoney = 0.0f;
	m_nType = -1;
	m_nHandler = -1;
	m_nModel = -1;
	//}}AFX_DATA_INIT
	m_lCurID = 0;
}


void CSystemDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSystemDlg)
	DDX_Control(pDX, IDC_DATETIMEPICKER_DATE, m_Date);
	DDX_Control(pDX, IDC_COMBO1, m_cobModel);
	DDX_Control(pDX, IDC_TREE_BUDGET, m_Tree);
	DDX_Control(pDX, IDC_COMBO_HANDLE, m_cobHandle);
	DDX_Control(pDX, IDC_COMBO_TYPE, m_cobType);
	DDX_Text(pDX, IDC_EDIT_MONEY, m_fMoney);
	DDX_CBIndex(pDX, IDC_COMBO_TYPE, m_nType);
	DDX_CBIndex(pDX, IDC_COMBO_HANDLE, m_nHandler);
	DDX_CBIndex(pDX, IDC_COMBO1, m_nModel);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CSystemDlg, CDialog)
	//{{AFX_MSG_MAP(CSystemDlg)
	ON_BN_CLICKED(IDC_BTN_ADD, OnBtnAdd)
	ON_BN_CLICKED(IDC_BTN_DEL, OnBtnDel)
	ON_NOTIFY(TVN_SELCHANGED, IDC_TREE_BUDGET, OnSelchangedTreeBudget)
	ON_BN_CLICKED(IDC_BTN_MODIFY, OnBtnModify)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSystemDlg message handlers

void CSystemDlg::OnBtnAdd() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	_RecordsetPtr	m_pRecordset;
	m_pRecordset.CreateInstance(__uuidof(Recordset));
	try
	{
		m_pRecordset->Open("SELECT * FROM Budget",                // 查询DemoTable表中所有字段
			theApp.m_pConnection.GetInterfacePtr(),	 // 获取库接库的IDispatch指针
			adOpenDynamic,
			adLockOptimistic,
			adCmdText);
	}
	catch(_com_error *e)
	{
		AfxMessageBox(e->ErrorMessage());
	}  

	CString strDate,strType,strHandler,strContent,straddtype;
	GetDlgItem(IDC_DATETIMEPICKER_DATE)->GetWindowText(strDate);
	m_cobType.GetWindowText(strType);
	m_cobHandle.GetWindowText(strHandler);
	m_cobModel.GetWindowText(strContent);
	
	//添加新记录
	if(strContent == "收入")
	{
		straddtype = "薪水";
		m_pRecordset->AddNew();
		m_pRecordset->PutCollect("DateA",_variant_t(strDate));
		m_pRecordset->PutCollect("Content",_variant_t(strContent));
		m_pRecordset->PutCollect("AddMoney", _variant_t(m_fMoney));
		m_pRecordset->PutCollect("Handler", _variant_t(strHandler));
		m_pRecordset->PutCollect("Type", _variant_t(straddtype));
		m_pRecordset->Update();
	}
	else
	{
		m_pRecordset->AddNew();
		m_pRecordset->PutCollect("DateA",_variant_t(strDate));
		m_pRecordset->PutCollect("Content",_variant_t(strContent));
		m_pRecordset->PutCollect("MoneyA", _variant_t(m_fMoney));
		m_pRecordset->PutCollect("Handler", _variant_t(strHandler));
		m_pRecordset->PutCollect("Type", _variant_t(strType));
		m_pRecordset->Update();
	}

	m_pRecordset->Close();
	m_pRecordset = NULL;

	CTime Today = CTime::GetCurrentTime();
	Load(Today.GetMonth(),Today.GetMonth() + 1,Today.GetYear());
	m_fMoney = 0;
	UpdateData(FALSE);
}

void CSystemDlg::Load(int nFrom, int nTo, int nYear)
{
	m_Tree.DeleteAllItems();

	CString strValue;
	CString strTody;
	CString strContent;
	m_cobModel.GetWindowText(strContent);
	GetDlgItem(IDC_DATETIMEPICKER_DATE)->GetWindowText(strTody);
	CString strSql;
	_variant_t var;
	_RecordsetPtr pMonthRes,pTodayRes;
	pTodayRes.CreateInstance(__uuidof(Recordset));
	pMonthRes.CreateInstance(__uuidof(Recordset));
	try
	{		
		strSql.Format("SELECT DateA,Sum(MoneyA) as MoneySum FROM Budget group by DateA having year(DateA)=%d and month(DateA) between %d and %d" ,
			nYear,nFrom,nTo);
		//rSql="SELECT DateA,Sum(MoneyA) as MoneySum FROM Budget group by DateA having DateA>=#2004-8-1# and DateA<=#2004-8-2#";
		pMonthRes->Open(_variant_t(strSql),                // 查询DemoTable表中所有字段
		theApp.m_pConnection.GetInterfacePtr(),	 // 获取库接库的IDispatch指针
			adOpenDynamic,
			adLockOptimistic,
			adCmdText);
	}
	catch(_com_error *e)
	{
		AfxMessageBox(e->ErrorMessage());
	}  
	
	int i = 0;
	long lID = 0;
	HTREEITEM hDay = NULL;
	HTREEITEM hDayItem = NULL;
	while(!pMonthRes->adoEOF)
	{	
		//
		var = pMonthRes->GetCollect("DateA");
		if(var.vt != VT_NULL)
			strValue = (LPCSTR)_bstr_t(var);
		
		double dMoneySum;
		var = pMonthRes->GetCollect((long)1);
		if(var.vt != VT_NULL)
			dMoneySum = (double)var;
		CString strDayLable;
		strDayLable.Format("%s/总支出:¥%2.2f",strValue,dMoneySum);
		
		hDay = m_Tree.InsertItem(strDayLable);
		int nYear,nMonth,nDay;
		nYear = atoi(strtok(strValue.GetBuffer(strValue.GetLength()),"-"));
		nMonth = atoi(strtok(NULL,"-"));
		nDay = atoi(strtok(NULL,"-"));
		strValue.ReleaseBuffer();
		strSql.Format("SELECT * FROM Budget where Year(DateA)=%d and Month(DateA)=%d and Day(DateA)=%d",
			nYear,nMonth,nDay);
		//strSql = "SELECT * FROM Budget where ID=15";
		pTodayRes->Open(_variant_t(strSql),                // 查询DemoTable表中所有字段
						theApp.m_pConnection.GetInterfacePtr(),	 // 获取库接库的IDispatch指针
						adOpenDynamic,
						adLockOptimistic,
						adCmdText);

			while(!pTodayRes->adoEOF)
			{
				//
				var = pTodayRes->GetCollect("Content");
				if(var.vt != VT_NULL)
					strValue = (LPCSTR)_bstr_t(var);
				if(strValue == "收入")
				{
					strValue += '/';
					var = pTodayRes->GetCollect("Type");
					if(var.vt != VT_NULL)
						strValue += (LPCSTR)_bstr_t(var);
					strValue += "/¥";
					//
					var = pTodayRes->GetCollect("AddMoney");
					if(var.vt != VT_NULL)
						strValue += (LPCSTR)_bstr_t(var);
					strValue += '/';
					//
					var = pTodayRes->GetCollect("Handler");
					if(var.vt != VT_NULL)
						strValue += (LPCSTR)_bstr_t(var);
					
					var = pTodayRes->GetCollect("ID");
					if(var.vt != VT_NULL)
						lID = (long)var;
				}
				else
				{
					strValue += '/';
					var = pTodayRes->GetCollect("Type");
					if(var.vt != VT_NULL)
						strValue += (LPCSTR)_bstr_t(var);
					strValue += "/¥";
					//
					var = pTodayRes->GetCollect("MoneyA");
					if(var.vt != VT_NULL)
						strValue += (LPCSTR)_bstr_t(var);
					strValue += '/';

					var = pTodayRes->GetCollect("Handler");
					if(var.vt != VT_NULL)
						strValue += (LPCSTR)_bstr_t(var);
					
					var = pTodayRes->GetCollect("ID");
					if(var.vt != VT_NULL)
						lID = (long)var;
				}

				hDayItem = m_Tree.InsertItem(strValue,hDay);
				m_Tree.SetItemData(hDayItem,lID);
				
				pTodayRes->MoveNext();
			}
		pTodayRes->Close();
		
		pMonthRes->MoveNext();
		i++;
	}

	pMonthRes->Close();
	pMonthRes = NULL;

	m_Tree.Expand(hDay,TVE_EXPAND);
}

BOOL CSystemDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
// 	m_cobHandle.ResetContent();//清除控件中的所有字符串条目
	GetSystemTime(&m_CurTime);
	_RecordsetPtr	pHandlerRecordset;
	pHandlerRecordset.CreateInstance(__uuidof(Recordset));
	try
	{
		pHandlerRecordset->Open("SELECT * FROM Handler",                // 查询DemoTable表中所有字段
		theApp.m_pConnection.GetInterfacePtr(),	 // 获取库接库的IDispatch指针
		adOpenDynamic,
		adLockOptimistic,
		adCmdText);
	}
	catch(_com_error *e)
	{
		AfxMessageBox(e->ErrorMessage());
	}  
				
	_variant_t var;
	CString strName;
	while(!pHandlerRecordset->adoEOF)
	{
		var = pHandlerRecordset->GetCollect("Handler");
		if(var.vt != VT_NULL)
			strName = (LPCSTR)_bstr_t(var);
		m_cobHandle.AddString(strName);	
		pHandlerRecordset->MoveNext();
	}
	pHandlerRecordset->Close();
	pHandlerRecordset.Release();
	pHandlerRecordset = NULL;
	m_cobHandle.SetCurSel(0);
	
	CString strType;
	_RecordsetPtr pTypeRecordset;
	pTypeRecordset.CreateInstance(__uuidof(Recordset));
	try
	{
		pTypeRecordset->Open("SELECT * FROM Type",                // 查询DemoTable表中所有字段
			theApp.m_pConnection.GetInterfacePtr(),	 // 获取库接库的IDispatch指针
			adOpenDynamic,
			adLockOptimistic,
			adCmdText);
	}
	catch(_com_error *e)
	{
		AfxMessageBox(e->ErrorMessage());
	}  

	while(!pTypeRecordset->adoEOF)
	{
		var = pTypeRecordset->GetCollect("Type");
		if(var.vt != VT_NULL)
			strType = (LPCSTR)_bstr_t(var);
		m_cobType.AddString(strType);
		pTypeRecordset->MoveNext();
	}
	pTypeRecordset->Close();
	pTypeRecordset.Release();
	pTypeRecordset = NULL;
	m_cobType.SetCurSel(0);
	m_cobModel.SetCurSel(1);
	//读入当月的记录
	//
	CTime Today = CTime::GetCurrentTime();
	Load(Today.GetMonth(),Today.GetMonth() + 1,Today.GetYear());
	m_nHandler = 0;
	m_nType = 0;
	UpdateData(FALSE);
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CSystemDlg::OnBtnDel() 
{
	// TODO: Add your control notification handler code here
	if(m_lCurID > 0 && AfxMessageBox("是否删除?",MB_YESNO) == IDYES)
	{
		CString strSql;
		strSql.Format("delete from Budget where ID=%d",m_lCurID);
		_variant_t RecordsAffected;
		theApp.m_pConnection->Execute(_bstr_t(strSql),&RecordsAffected,adCmdText);
		CTime Today = CTime::GetCurrentTime();
		Load(Today.GetMonth(),Today.GetMonth() + 1,Today.GetYear());
	}
}

void CSystemDlg::OnSelchangedTreeBudget(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
	HTREEITEM hCurItem = pNMTreeView->itemNew.hItem;
	CString m_strContent;
	m_cobModel.GetWindowText(m_strContent);
	if(m_Tree.GetParentItem(hCurItem) != NULL)
	{
		CString strDate = m_Tree.GetItemText(m_Tree.GetParentItem(hCurItem));
		int nPos = strDate.Find('/');
		strDate = strDate.Left(nPos);
		m_CurTime.wYear = atoi(strtok(strDate.GetBuffer(strDate.GetLength()),"-"));
		m_CurTime.wMonth = atoi(strtok(NULL,"-"));
		m_CurTime.wDay = atoi(strtok(NULL,"-"));
		m_Date.SetTime(m_CurTime);
		strDate.ReleaseBuffer();

		CString strText = m_Tree.GetItemText(hCurItem);
		m_lCurID = m_Tree.GetItemData(hCurItem);
		m_nModel = m_cobModel.SelectString(-1,strtok(strText.GetBuffer(strText.GetLength()),"/"));
		m_nType = m_cobType.SelectString(-1,strtok(NULL,"/"));
		CString strMoney = strtok(NULL,"/");
		strMoney = strMoney.Right(strMoney.GetLength() - 2);
		m_fMoney = (float)atof(strMoney);
		m_nHandler = m_cobHandle.SelectString(-1,strtok(NULL,"/"));
		
		strText.ReleaseBuffer();
		UpdateData(FALSE);
	}	
	else
	{
		m_lCurID = 0;
	}
	*pResult = 0;
}

void CSystemDlg::OnBtnModify() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	if(m_lCurID > 0)
	{
		int nCurSel = 0;
		int nID = 0;
		CString strDate,strType,strHandler,strContent;
		m_Date.GetWindowText(strDate);
		m_cobType.GetWindowText(strType);
		m_cobHandle.GetWindowText(strHandler);
		m_cobModel.GetWindowText(strContent);
		CString strSql;
		strSql.Format("update Budget set DateA='%s',Content='%s',MoneyA=%f,Handler='%s',Type='%s' where ID=%d",
			strDate,strContent,m_fMoney,strHandler,strType,m_lCurID);
		_variant_t vAffected;
		theApp.m_pConnection->Execute(_bstr_t(strSql),&vAffected,adCmdText);
		
		CTime Today = CTime::GetCurrentTime();
		Load(Today.GetMonth(),Today.GetMonth() + 1,Today.GetYear());

	}
}

⌨️ 快捷键说明

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