?? dominexmanagerview.cpp
字號:
// DomIneXManagerView.cpp : implementation of the CDomIneXManagerView class
//
#include "stdafx.h"
#include "DomIneXManager.h"
#include "DomIneXManagerDoc.h"
#include "DomIneXManagerView.h"
#include "MemberDlg.h" // /
#include <fstream.h> // /
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDomIneXManagerView
IMPLEMENT_DYNCREATE(CDomIneXManagerView, CFormView)
BEGIN_MESSAGE_MAP(CDomIneXManagerView, CFormView)
//{{AFX_MSG_MAP(CDomIneXManagerView)
ON_BN_CLICKED(IDC_ADD, OnAdd)
ON_BN_CLICKED(IDC_DEL, OnDel)
ON_BN_CLICKED(IDC_MODIFY, OnModify)
ON_BN_CLICKED(IDC_STATISTICS, OnStatistics)
ON_BN_CLICKED(IDC_SEARCH, OnSearch)
ON_WM_CREATE()
ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
ON_COMMAND(ID_FILE_SAVE_AS, OnFileSaveAs)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDomIneXManagerView construction/destruction
CDomIneXManagerView::CDomIneXManagerView()
: CFormView(CDomIneXManagerView::IDD)
{
//{{AFX_DATA_INIT(CDomIneXManagerView)
m_money = 0.0;
m_name = _T("");
m_no = 0;
m_item = _T("");
m_time = 0;
//}}AFX_DATA_INIT
// TODO: add construction code here
}
CDomIneXManagerView::~CDomIneXManagerView()
{
}
void CDomIneXManagerView::DoDataExchange(CDataExchange* pDX)
{
CFormView::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDomIneXManagerView)
DDX_Text(pDX, IDC_MONEY, m_money);
DDX_Text(pDX, IDC_NAME, m_name);
DDX_Text(pDX, IDC_NUM, m_no);
DDX_Text(pDX, IDC_ITEM, m_item);
DDX_Text(pDX, IDC_TIME, m_time);
//}}AFX_DATA_MAP
}
BOOL CDomIneXManagerView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CFormView::PreCreateWindow(cs);
}
void CDomIneXManagerView::OnInitialUpdate() // /VC程序調用OnInitialUpdate():初始化之后,更新界面之前
{
CFormView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
ResizeParentToFit();
//初始化組合框
CComboBox *pWnd = (CComboBox*) this->GetDlgItem(IDC_TYPE); // /返回控件的指針
pWnd->AddString("收入"); // /增加列表項
pWnd->AddString("支出"); // /增加列表項
pWnd->SetCurSel(0); // /SetCurSel()設定某個列表項為選中狀態,在此為"收入"
SetDlgItemText(IDC_SEARCH_CONTENT, "請輸入編號"); // /SetDlgItemText()設置控件顯示文本
SetDlgItemText(IDC_TIME, "格式:20080808"); // /
}
/////////////////////////////////////////////////////////////////////////////
// CDomIneXManagerView printing
BOOL CDomIneXManagerView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CDomIneXManagerView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CDomIneXManagerView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
void CDomIneXManagerView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
{
// TODO: add customized printing code here
}
/////////////////////////////////////////////////////////////////////////////
// CDomIneXManagerView diagnostics
#ifdef _DEBUG
void CDomIneXManagerView::AssertValid() const
{
CFormView::AssertValid();
}
void CDomIneXManagerView::Dump(CDumpContext& dc) const
{
CFormView::Dump(dc);
}
CDomIneXManagerDoc* CDomIneXManagerView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDomIneXManagerDoc)));
return (CDomIneXManagerDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CDomIneXManagerView message handlers
void CDomIneXManagerView::UpdateListBox() // /更新列表框的顯示內容
{
CListBox *pWnd = (CListBox*) this->GetDlgItem(IDC_LIST1);
// /GetDlgItem(控件ID),返回該控件的指針
pWnd->ResetContent(); // /清除列表框中所有的列表項
CString temp; // /CString 類,提供靈活的、可調整大小的字符串對象
CString no, money, time;
for(int i=0; i<members.size(); i++)
{
no.Format("%d", members[i].m_no); //itoa
// /Format()是封裝在CString中的一個方法,它的用法和ANSIC++中的printf()相似
temp = no + " " + members[i].m_name;
temp = temp + " " + members[i].m_item;
money.Format("%f", members[i].m_money);
temp = temp + " " + money;
time.Format("%ld", members[i].m_time);
temp = temp + " " + time;
temp = temp + " " + members[i].m_type;
pWnd->AddString(temp); // /AddString(),CListBox類的成員函數,向列表框增加列表項
}
}
void CDomIneXManagerView::OnAdd() // /“添加”按鈕的消息處理函數
{
// TODO: Add your control notification handler code here
this->UpdateData(true); // /將數據從對話框的控件中傳送到對應的數據成員中
if(m_name.IsEmpty() || m_item.IsEmpty())
this->MessageBox("請輸入相應值!"); // /MESSAGEBOX(cMessageText) 顯示一個用戶自定義對話框。
else
{
m_no = members.size()+1;
CString type;
CComboBox *pWnd = (CComboBox*) this->GetDlgItem(IDC_TYPE); // /指向“類型”組合框
pWnd->GetWindowText(type); // /GetWindowText()將指定窗口的標題條文本(如果存在)拷貝到一個緩存區內
// /獲取“類型”組合框的內容:收入/支出
members.push_back(CMember(m_no, m_name, m_item, m_money, m_time, type)); // /保存記錄
this->UpdateData(false); // /將數據從數據成員中傳送到對應的控件中,主要是修改編號
}
UpdateListBox();
}
void CDomIneXManagerView::OnDel() // /“刪除”按鈕的消息處理函數
{
// TODO: Add your control notification handler code here
CListBox *pWnd = (CListBox*)this->GetDlgItem(IDC_LIST1);
int index = pWnd->GetCurSel(); // /獲得列表框中當前選擇的列表項的位置序號
//pWnd->DeleteString(index); // /方法二:DeleteString()刪除指定的列表項
if(index >= 0) // /方法一
{
members.erase(members.begin() + index); // /刪除記錄
UpdateListBox();
}
else
this->MessageBox("請選擇要刪除的條目");
}
void CDomIneXManagerView::OnModify() // /“修改”按鈕的消息處理函數
{
// TODO: Add your control notification handler code here
CListBox *pWnd = (CListBox*) this->GetDlgItem(IDC_LIST1);
int index = pWnd->GetCurSel();
if(index < 0)
{
this->MessageBox("請選擇要修改的條目");
return;
}
CMemberDlg memberDlg;
if(IDOK == memberDlg.DoModal()) /* 兩層意義:一,memberDlg.DoModal()作用是彈出CMemberDlg對話框,
二,IDOK==memberDlg.DoModal()代表點擊了對話框上的OK按鈕。
就是說你同時做了上述兩件事時就執行if語句后面的程序。 */
{
int no = members[index].m_no;
CString type = members[index].m_type;
members.erase(members.begin()+index); //沒有用UpdateData()
members.insert(members.begin()+index, CMember(no, memberDlg.m_name, memberDlg.m_item,
memberDlg.m_money, memberDlg.m_time, type));
}
UpdateListBox();
}
void CDomIneXManagerView::OnStatistics() // /“統計”按鈕的消息處理函數
{
// TODO: Add your control notification handler code here
/*
for(int i=1; i<members.size(); i++)
for(int j=0; j<members.size()-i; j++)
{
if(members[j].m_money > members[j+1].m_money)
{
CMember temp;
temp = members[j];
members[j] = members[j+1];
members[j+1] = temp;
}
}
*/
// UpdateListBox();
double moneyout = 0;
double moneyin = 0;
for(int k=0; k<members.size(); k++)
{
if(members[k].m_type.Compare("收入") == 0) // /CString類的成員函數Compare()
moneyin += members[k].m_money;
else if(members[k].m_type.Compare("支出") == 0) // /
moneyout += members[k].m_money;
}
CString str;
str.Format("收入: %f, 支出: %f", moneyin, moneyout); // /Format()成員函數,用法類似于printf()
this->MessageBox(str);
}
void CDomIneXManagerView::OnSearch() // /“查找”按鈕的消息處理函數
{
// TODO: Add your control notification handler code here
CString str;
( (CEdit*)this->GetDlgItem(IDC_SEARCH_CONTENT) )->GetWindowText(str);
int n = atoi(str); // /atoi()
CListBox *pWnd = (CListBox*)this->GetDlgItem(IDC_LIST1);
bool flag = true;
CString temp; // /用于輸出查找結果(如果存在)
CString no, money, time;
for(int i=0; i<members.size(); i++)
if(members[i].m_no == n)
{
no.Format("%d", members[i].m_no); //itoa()
temp = no + " " + members[i].m_name + " " + members[i].m_item;
money.Format("%f", members[i].m_money);
temp = temp + " " + money;
time.Format("%ld",members[i].m_time);
temp = temp + " " + time + " " + members[i].m_type;
this->MessageBox(temp);
flag = false;
break;
}
if(flag)
this->MessageBox("Not Found!");
}
int CDomIneXManagerView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFormView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
return 0;
}
void CDomIneXManagerView::OnFileOpen() // /
{
// TODO: Add your command handler code here
CFileDialog fileDlg(true);
//fileDlg.m_ofn.lpstrFilter="文本文件(*.txt)|*.txt";
if(IDOK!=fileDlg.DoModal())
return;
CFile file(fileDlg.GetFileName(),CFile::modeRead);
CArchive ar(&file,CArchive::load);
int count;
ar>>count;
if(!members.empty())
members.clear();
int no;
CString name,item,type;
double money;
long time;
for(int i=0;i<count;i++)
{
ar>>no>>name>>item>>money>>time>>type;
members.push_back(CMember(no,name,item,money,time,type));
}
//ar.Close();
//file.Close();
/*方法2
ifstream in(fileDlg.GetFileName(),ios::in); //type
if(!in)
{
MessageBox("Can not open file to read",NULL,MB_OK);
exit(-1);
}
if(!members.empty())
members.clear();
while(!in.eof())
{
int count=0;
in>>count;//多一個換行時,沒結束但也沒輸入數據!!
//if(count==0)
// break;
int no;
char name[10],item[10];
//CString name,item;
double money;
long time;
for(int i=0;i<count;i++)
{
in>>no>>name>>item>>money>>time;
members.push_back(CMember(no,name,item,money,time));
}
}*/
UpdateListBox();
}
void CDomIneXManagerView::OnFileSaveAs() // /
{
// TODO: Add your command handler code here
CFileDialog fileDlg(false);
//fileDlg.m_ofn.lpstrFilter="文本文件(*.txt)|*.txt";
if(IDOK!=fileDlg.DoModal())
return;
CFile file(fileDlg.GetFileName(),CFile::modeCreate|CFile::modeWrite|CFile::modeNoTruncate);
CArchive ar(&file,CArchive::store);
int count=members.size();
ar<<count;
for(int i=0;i<count;i++)
{
ar<<members[i].m_no<<members[i].m_name<<members[i].m_item
<<members[i].m_money<<members[i].m_time<<members[i].m_type;
}
//ar.Close();
//file.Close();
/*方法2
ofstream out(fileDlg.GetFileName(),ios::out|ios::app);
if(!out)
{
MessageBox("Can not open file to write",NULL,MB_OK);
exit(-1);
}
int count=members.size();
out<<count<<endl;
for(int i=0;i<count;i++)
{
out<<members[i].m_no<<'\t'<<
members[i].m_name<<'\t'<<
members[i].m_item<<'\t'<<
members[i].m_money<<'\t'<<
members[i].m_time;
out<<endl;
}*/
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -