📄 catalogview.cpp
字号:
// CatalogView.cpp : implementation of the CCatalogView class
//
#include "stdafx.h"
#include "Catalog.h"
#include "CatalogDoc.h"
#include "CatalogView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CCatalogView
IMPLEMENT_DYNCREATE(CCatalogView, CFormView)
BEGIN_MESSAGE_MAP(CCatalogView, CFormView)
//{{AFX_MSG_MAP(CCatalogView)
ON_COMMAND(ID_VIEW_PREV, OnViewPrev)
ON_COMMAND(ID_VIEW_NEXT, OnViewNext)
ON_COMMAND(ID_APPEND, OnAppend)
ON_BN_CLICKED(IDC_SUBMISSION, OnSubmission)
ON_WM_MOUSEMOVE()
ON_COMMAND(ID_DELETE_LAST_AND_WRITE, OnDeleteLastAndWrite)
ON_COMMAND(ID_READ_EXISTING_DATA, OnReadExistingData)
ON_COMMAND(ID_APP_EXIT, OnAppExit)
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CCatalogView construction/destruction
CCatalogView::CCatalogView()
: CFormView(CCatalogView::IDD)
{
Is_Clicked_Firstly=false;
curIdx=-1;
NumOfItems=0;
// m_ID_VIS.SetReadOnly(TRUE);
tipsGiven=false;
//{{AFX_DATA_INIT(CCatalogView)
m_szID = _T("");
m_szItem = _T("");
m_fPrice = 0.0f;
m_cur = 1;
m_sum = 0;
m_act = _T("输入");
//}}AFX_DATA_INIT
// TODO: add construction code here
}
CCatalogView::~CCatalogView()
{
}
void CCatalogView::DoDataExchange(CDataExchange* pDX)
{
CFormView::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCatalogView)
DDX_Control(pDX, IDC_EDIT_ID, m_ID_VIS);
DDX_Text(pDX, IDC_EDIT_ID, m_szID);
DDV_MaxChars(pDX, m_szID, 8);
DDX_Text(pDX, IDC_EDIT_ITEM, m_szItem);
DDV_MaxChars(pDX, m_szItem, 10);
DDX_Text(pDX, IDC_EDIT_PRICE, m_fPrice);
DDX_Text(pDX, IDC_CUR, m_cur);
DDX_Text(pDX, IDC_SUM, m_sum);
DDX_Text(pDX, IDC_ACT, m_act);
//}}AFX_DATA_MAP
}
BOOL CCatalogView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CFormView::PreCreateWindow(cs);
}
void CCatalogView::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
ResizeParentToFit();
}
/////////////////////////////////////////////////////////////////////////////
// CCatalogView printing
BOOL CCatalogView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CCatalogView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CCatalogView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
void CCatalogView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
{
// TODO: add customized printing code here
}
/////////////////////////////////////////////////////////////////////////////
// CCatalogView diagnostics
#ifdef _DEBUG
void CCatalogView::AssertValid() const
{
CFormView::AssertValid();
}
void CCatalogView::Dump(CDumpContext& dc) const
{
CFormView::Dump(dc);
}
CCatalogDoc* CCatalogView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CCatalogDoc)));
return (CCatalogDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CCatalogView message handlers
void CCatalogView::OnViewPrev()
{
// TODO: Add your command handler code here
CCatalogDoc*pDoc=GetDocument();
//UpdateData(TRUE);
if(curIdx==-1||curIdx==0)
{
MessageBox("已到达第一条商品记录,无法再向前!","友情提示^_^",MB_OKCANCEL);
return;
}
m_cur = curIdx;
m_act = _T("查看");
//否则就读取最邻近的上一条信息
m_szID=(pDoc->comdtArr[curIdx-1]).m_szID;
m_szItem=(pDoc->comdtArr[curIdx-1]).m_szItem;
m_fPrice=(pDoc->comdtArr[curIdx-1]).m_fPrice;
curIdx--;//当前指标移动到上一条
UpdateData(FALSE);
}
void CCatalogView::OnViewNext()
{
// TODO: Add your command handler code here
CCatalogDoc*pDoc=GetDocument();
//UpdateData(TRUE);
if(curIdx==NumOfItems-1)
{
MessageBox("已到达最后一条商品记录,无法再向后!","友情提示^_^",MB_OKCANCEL);
return;
}
m_cur = curIdx+2;
m_act = _T("查看");
//否则就读取最邻近的上一条信息
m_szID=(pDoc->comdtArr[curIdx+1]).m_szID;
m_szItem=(pDoc->comdtArr[curIdx+1]).m_szItem;
m_fPrice=(pDoc->comdtArr[curIdx+1]).m_fPrice;
curIdx++;//当前指标移动到上一条
UpdateData(FALSE);
}
void CCatalogView::OnAppend()
{
// TODO: Add your command handler code here
CCatalogDoc*pDoc=GetDocument();
curIdx=NumOfItems++;//准备添加下一个,同时总数增加1
UpdateData(TRUE);
pDoc->comdtArr[curIdx].m_szID=m_szID;
pDoc->comdtArr[curIdx].m_szItem=m_szItem;
pDoc->comdtArr[curIdx].m_fPrice=m_fPrice;
m_cur = NumOfItems+1;
m_sum = NumOfItems;
m_act = _T("输入");
// UpdateData(FALSE);
m_szID = _T("");
m_szItem = _T("");
m_fPrice = 0.0f;
UpdateData(FALSE);
}
void CCatalogView::OnSubmission()
{
// TODO: Add your control notification handler code here
}
void CCatalogView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CFormView::OnMouseMove(nFlags, point);
if(!tipsGiven)
{
MessageBox("请选择“商品记录—从商品信息文件中读取数据到数组中”,系统将自动为您读取商品信息; 如果暂时没有商品信息文件,请先按原来的操作方法输入一些新的商品信息。","友情提示^_^",MB_OKCANCEL);
tipsGiven=true;
}
}
void CCatalogView::OnDeleteLastAndWrite()
{
// TODO: Add your command handler code here
//NumOfItems--;//删除数组中的最后一个商品信息
CCatalogDoc*pDoc=GetDocument();
FILE *fp;
if((fp=fopen("D:\\文件 W .txt","wb+"))==NULL)
{
exit(1);
}
fprintf(fp,"现有商品总数是:%d\n",NumOfItems);
for(int curIdx=0; curIdx<NumOfItems; curIdx++)//循环输出每一个商品信息
{
char *tmpID=new char[100];
strcpy(tmpID,(LPCTSTR)pDoc->comdtArr[curIdx].m_szID);
char *tmpItem=new char[100];
strcpy(tmpItem,(LPCTSTR)pDoc->comdtArr[curIdx].m_szItem);
fprintf(fp,"第%d条信息:商品编号:%s 商品名称:%s 商品价格:%d\n",curIdx+1,tmpID,tmpItem,pDoc->comdtArr[curIdx].m_fPrice);
}
fclose(fp);
}
void CCatalogView::OnReadExistingData()
{
// TODO: Add your command handler code here
Is_Clicked_Firstly=true;
//每次重新建立一个输出流
char *m_cSzID=new char[100];
char *m_cSzItem=new char[100];
char tmpC;
// ofstream out2;
// out2.open("D:\\这是保存了一些将要被读入到内存中的商品信息数据的文件 R .txt",ios::out);
CCatalogDoc*pDoc=GetDocument();
FILE *fp;
if((fp=fopen("D:\\文件 W .txt","rb+"))==NULL)
{
exit(1);
}
fscanf(fp,"现有商品总数是:%d\n",&NumOfItems);
// out2<<NumOfItems<<'\n';
int i;
for(curIdx=0; curIdx<NumOfItems; curIdx++)//循环输出每一个商品信息
{
fscanf(fp,"第%d条信息:商品编号:%s 商品名称:%s 商品价格:%d\n",&i,m_cSzID,m_cSzItem,&pDoc->comdtArr[curIdx].m_fPrice);
pDoc->comdtArr[curIdx].m_szID=_T(m_cSzID);
pDoc->comdtArr[curIdx].m_szItem=_T(m_cSzItem);
//out2<<(LPCTSTR)m_szID<<(LPCTSTR)m_szItem<<pDoc->comdtArr[curIdx].m_fPrice;
}
// out2.close();
curIdx--;
m_szID = m_cSzID;
m_szItem = m_cSzItem;
m_fPrice =pDoc->comdtArr[curIdx].m_fPrice;
fclose(fp);
UpdateData(FALSE);
}
void CCatalogView::OnAppExit()
{
// TODO: Add your command handler code here
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -