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

📄 myhomeprojectdlg.cpp

📁 我做的日常开销数据库管理系统。使用vc+SQL sever2000开发
💻 CPP
字号:
// MyHomeProjectDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MyHomeProject.h"
#include "MyHomeProjectDlg.h"
#include "BillAddDialog.h"

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

extern CMyHomeProjectApp theApp;
extern	_ConnectionPtr theConnection;
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyHomeProjectDlg dialog

CMyHomeProjectDlg::CMyHomeProjectDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMyHomeProjectDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMyHomeProjectDlg)
	m_strContentmodify = _T("(空)");
	m_strMoneymodify = _T("0.0");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);



}

void CMyHomeProjectDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMyHomeProjectDlg)
	DDX_Control(pDX, IDC_TREE1, m_tree);
	DDX_Control(pDX, IDC_LIST1, m_lsDatalist);
	DDX_Control(pDX, IDC_DATETIMEPICKER_STARTANA, m_dtpStartana);
	DDX_Control(pDX, IDC_DATETIMEPICKER_ENDANA, m_dtpEndana);
	DDX_Control(pDX, IDC_DATETIMEPICKER_MODIFY, m_dtpModify);
	DDX_Control(pDX, IDC_COMBO_SORTMODIFY, m_cbSortmodify);
	DDX_Control(pDX, IDC_COMBO_SORTANA, m_cbSortana);
	DDX_Control(pDX, IDC_COMBO_HANDLERMODIFY, m_cbHandlermodify);
	DDX_Control(pDX, IDC_COMBO_HANDLERANA, m_cbHandlerana);
	DDX_Control(pDX, IDC_COMBO_BILLANA, m_cbBillana);
	DDX_Text(pDX, IDC_EDIT_CONTENTMODIFY, m_strContentmodify);
	DDX_Text(pDX, IDC_EDIT_MONEYMODIFY, m_strMoneymodify);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMyHomeProjectDlg, CDialog)
	//{{AFX_MSG_MAP(CMyHomeProjectDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_COMMAND(IDI_BUTTONADD,OnButtonAdd)
	ON_COMMAND(IDI_BUTTONEXIT,OnButtonExit)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyHomeProjectDlg message handlers

BOOL CMyHomeProjectDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	SetWindowText("日常消费管理系统");
	// TODO: Add extra initialization here

	/*********************** 数据库连接*******************************/
	theConnection.CreateInstance(__uuidof(Connection));
	
	try
	{
		theConnection->ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Home.mdb;Persist Security Info=False";
		theConnection->Open("","","",-1);
	}
	catch(_com_error &e)
	{
		CString strError;
		strError.Format("无法连接数据库,请与管理员联系 ! %s",e.ErrorMessage());
		AfxMessageBox(strError);
		return false;
	}



	/*************************工具栏的创建****************************/

	m_toolbar.Create(TBSTYLE_FLAT|CCS_TOP|WS_CHILD|WS_VISIBLE|WS_BORDER|CCS_ADJUSTABLE|TBSTYLE_WRAPABLE,
		CRect(0,0,0,0),this,IDR_TOOLBAR);
	m_toolbar.SetBitmapSize(CSize(32,32));
	m_imagelist.Create(32,32,ILC_COLOR32|ILC_MASK,0,0);
	
	//向列表加入图标
	for(int i=0;i<4;i++)
		m_imagelist.Add(theApp.LoadIcon(i+IDI_ICON1));

	m_toolbar.SetImageList(&m_imagelist);
	TBBUTTON buttons[5];
	for(i=0;i<5;i++)				//设置按钮文字
	{
		CString str;
		int strlength;
		CCHAR* temp;
		str.LoadString(IDI_BUTTONADD+i-1);
		strlength=str.GetLength()+1;
		temp=str.GetBufferSetLength(strlength);
		temp[strlength]='\0';
		temp[strlength-1]='\0';

		if(i<1)
			buttons[i].fsStyle=TBSTYLE_SEP;
		else
			buttons[i].fsStyle=TBSTYLE_BUTTON;

		buttons[i].fsState=TBSTATE_ENABLED;
		buttons[i].dwData=0;
		buttons[i].idCommand=IDI_BUTTONADD+i-1;
		buttons[i].iString=m_toolbar.AddStrings(temp);
		buttons[i].iBitmap=i-1;
		str.ReleaseBuffer();
	}
	m_toolbar.AutoSize();
	m_toolbar.AddButtons(5,buttons);
	m_toolbar.ShowWindow(SW_SHOW);

	/********************初始化帐单信息显示*************************/
	m_cbSortana.AddString("所有类型");
	m_cbHandlerana.AddString("所有人");
	m_cbSortana.SetCurSel(0);
	m_cbHandlerana.SetCurSel(0);

	_RecordsetPtr initRst;
	initRst.CreateInstance(__uuidof(Recordset));
	
	try
	{
	initRst->Open("select 类型 from type",
		theConnection.GetInterfacePtr(),
		adOpenDynamic,
		adLockOptimistic,
		adCmdText);

	}
	catch(...)
	{
		AfxMessageBox("数据库连接出错!");
	}

	//获得类型字段,并显示到Combo中
	_variant_t var;
	CString strSort;
	while(!initRst->ADOEOF)
	{
		var=initRst->GetCollect("类型");
		if(var.vt!=VT_NULL)
			strSort=(LPCSTR)_bstr_t(var);
		m_cbSortana.AddString(strSort);
		m_cbSortmodify.AddString(strSort);
		initRst->MoveNext();
	}

	initRst->Close();

	//获得人名,并显示
	try
	{
	initRst->Open("select 姓名 from handler",
		theConnection.GetInterfacePtr(),
		adOpenDynamic,
		adLockOptimistic,
		adCmdText);

	}
	catch(...)
	{
		AfxMessageBox("数据库连接出错!");
	}

	CString strName;
	while(!initRst->ADOEOF)
	{
		var=initRst->GetCollect("姓名");
		if(var.vt!=VT_NULL)
			strName=(LPCSTR)_bstr_t(var);
		m_cbHandlerana.AddString(strName);
		m_cbHandlermodify.AddString(strName);
		initRst->MoveNext();
	}	
	initRst->Close();

	//获得帐单,并显示
	try
	{
	initRst->Open("select 帐单编号 from bill",
		theConnection.GetInterfacePtr(),
		adOpenDynamic,
		adLockOptimistic,
		adCmdText);

	}
	catch(...)
	{
		AfxMessageBox("数据库连接出错!");
	}

	CString strBill;
	CStringArray strBillArray;
//	int n,x=0;
	while(!initRst->ADOEOF)
	{
		var=initRst->GetCollect("帐单编号");     //判断帐单名称是否重复
		if(var.vt!=VT_NULL)
			strBill=(LPCSTR)_bstr_t(var);
	/*	for(n=0;n<strBillArray.GetSize();n++)
		{
			if(strBill==strBillArray[n])
				x=1;	
		}
		if(x==0)
		{*/
			m_cbBillana.AddString(strBill);
			strBillArray.Add(strBill);
	//	}
		initRst->MoveNext();
	}

	initRst->Close();
	initRst.Release();
	initRst=NULL;
	//设置组合框的初始选项
	m_cbSortmodify.SetCurSel(0);
	m_cbHandlermodify.SetCurSel(0);
	m_cbBillana.SetCurSel(0);
//	m_strContentmodify="(空)";
//	m_strMoneymodify="0.0";
	
	CTime Today=CTime::GetCurrentTime();
	LoadTree(Today.GetYear(),Today.GetMonth()-2,Today.GetMonth()+2);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMyHomeProjectDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CMyHomeProjectDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMyHomeProjectDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CMyHomeProjectDlg::OnButtonAdd()
{
	CBillAddDialog m_addDlg;
	m_addDlg.DoModal();
}

void CMyHomeProjectDlg::OnButtonExit()
{
	theConnection->Close();
	theConnection.Release();
	theConnection=NULL;
	exit(0);
}
void CMyHomeProjectDlg::LoadTree(int year, int monthFrom, int monthTo)
{
	m_tree.DeleteAllItems();
	_RecordsetPtr monthRst,dayRst,sheetRst;
	monthRst.CreateInstance(__uuidof(Recordset));
	dayRst.CreateInstance(__uuidof(Recordset));
	sheetRst.CreateInstance(__uuidof(Recordset));

	CString strTodayInfo;
	GetDlgItem(IDC_DATETIMEPICKER_STARTANA)->GetWindowText(strTodayInfo);
	CString strSQL;


	try
	{
		//已月为单位
	strSQL.Format("select  bill.日期  ,sum(content.金额)as 总金额 from bill inner join content  on bill.帐单编号=content.帐单编号 group by  日期  having year(日期)=%d and month(日期) between %d and %d",year,monthFrom,monthTo);
	monthRst->Open(_variant_t(strSQL),
		theConnection.GetInterfacePtr(),
		adOpenDynamic,
		adLockOptimistic,
		adCmdText);

	}
	catch(...)
	{
		AfxMessageBox("数据库连接出错!");
	}


	/********************以月为分类,统计每个月的总消费******************/
	/*****************************************************************
		此处算法比较繁琐,日后要改进
	******************************************************************/
	CString strValue1;
	_variant_t var1,var2;
	int i=0,n=0,numstr=0;
	HTREEITEM hMonth=NULL;
	HTREEITEM hDay=NULL;
	HTREEITEM hItem=NULL;
	CString strTemp;
	CStringArray strArray;
	double sum[12]={0};
	double moneyValue;
	double moneySum=0.0;


	while(!monthRst->ADOEOF)
	{
		var1=monthRst->GetCollect("日期");
		if(var1.vt!=VT_NULL)
			strValue1=(LPCSTR)_bstr_t(var1);
		strTemp=strValue1.GetBufferSetLength(7);
		
		var2=monthRst->GetCollect("总金额");
		if(var2.vt!=VT_NULL)
			moneyValue=(double)var2;


		if(strArray.GetSize()>0)
		{
			if(strTemp==strArray[numstr])
				moneySum+=moneyValue;
			else
			{	
				sum[numstr]=moneySum;
				numstr++;
				moneySum=0;
				
				strArray.Add(strTemp);
				moneySum+=moneyValue;
			}
		}
		else
		{		
				strArray.Add(strTemp);
				moneySum+=moneyValue;
		}
		
	//	m_strContentmodify=x;
	//	UpdateData(false);

		monthRst->MoveNext();
	}
	sum[numstr]=moneySum;
	//输入到TREE
		CString temp1;
		if(strArray.GetSize()>0)
		{
			for(n=0;n<strArray.GetSize();n++)
			{
			
				temp1.Format("%s-月¥%2.2f",strArray[n],sum[n]);

			hMonth=m_tree.InsertItem(temp1);
			}
		}

	/************添加二级目录到TREE**********************************/
		hDay=m_tree.InsertItem("ok",hMonth);
	monthRst->Close();
}

⌨️ 快捷键说明

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