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

📄 shopinglistview.cpp

📁 ppWizard has created this ShopingList application for you. This application not only demonstrates t
💻 CPP
字号:
// ShopingListView.cpp : implementation of the CShopingListView class
//

#include "stdafx.h"
#include "ShopingList.h"

#include "ShopingListDoc.h"
#include "ShopingListView.h"

// ja - included for dlgs
#include "InsertDlg.h"
#include "ChgStrNameDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CShopingListView

IMPLEMENT_DYNCREATE(CShopingListView, CFormView)

BEGIN_MESSAGE_MAP(CShopingListView, CFormView)
	//{{AFX_MSG_MAP(CShopingListView)
	ON_COMMAND(ID_INSERT, OnInsert)
	ON_COMMAND(ID_STORE_NAMES, OnStoreNames)
	ON_NOTIFY(NM_CLICK, IDC_LIST1, OnClickList)
	ON_COMMAND(ID_DELETE, OnDelete)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CShopingListView construction/destruction

CShopingListView::CShopingListView()
	: CFormView(CShopingListView::IDD)
{
	//{{AFX_DATA_INIT(CShopingListView)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// TODO: add construction code here

}

CShopingListView::~CShopingListView()
{
	nListBoxIndex = 0;
}

void CShopingListView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CShopingListView)
	DDX_Control(pDX, IDC_LIST1, m_ListBox);
	//}}AFX_DATA_MAP
}

BOOL CShopingListView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CFormView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CShopingListView diagnostics

#ifdef _DEBUG
void CShopingListView::AssertValid() const
{
	CFormView::AssertValid();
}

void CShopingListView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

CShopingListDoc* CShopingListView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CShopingListDoc)));
	return (CShopingListDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CShopingListView message handlers

void CShopingListView::OnInitialUpdate() 
{
	CFormView::OnInitialUpdate();
	
	CreateListBoxHeader();
}

void CShopingListView::UpdateListBox()
{
	
	// ja - clear out all items in list box
	m_ListBox.DeleteAllItems();
	
	CString sProductName, sTemp;
	int     nQty;
	float   fStore1Price;
	float   fStore2Price;
	float   fStore3Price;
	float   fStore4Price;
	
	int nIndex = GetDocument()->m_sProductName.GetSize();
	
	// ja - this must be done outside out main loop
	for (int t = 0; t <= nIndex; t++){
		m_ListBox.InsertItem(t, NULL);
	}

	// ja - fill the list box with data
	for (int i = 0; i < nIndex; i++){
		
		nQty         = GetDocument()->m_Qty[i];		
		fStore1Price = GetDocument()->m_fStore1Price[i];
		fStore2Price = GetDocument()->m_fStore2Price[i];
		fStore3Price = GetDocument()->m_fStore3Price[i];
		fStore4Price = GetDocument()->m_fStore4Price[i];
		sProductName = GetDocument()->m_sProductName[i];
		
		// ja - convert to CString and put into list box
		m_ListBox.SetItemText(i, 0, sProductName); 
		
		sTemp.Format(_T("%i"),nQty);
		m_ListBox.SetItemText(i, 1, sTemp); 
				
		sTemp.Format(_T("%.2f"),fStore1Price);
		m_ListBox.SetItemText(i, 2, sTemp); 

		sTemp.Format(_T("%.2f"),fStore2Price);
		m_ListBox.SetItemText(i, 3, sTemp); 
		
		sTemp.Format(_T("%.2f"),fStore3Price);
		m_ListBox.SetItemText(i, 4, sTemp); 
		
		sTemp.Format(_T("%.2f"),fStore4Price);
		m_ListBox.SetItemText(i, 5, sTemp); 

	}	

	CreateTotals();
}

void CShopingListView::CreateListBoxHeader()
{
	// ja - get the store names from out document
	CString sStore1Name = GetDocument()->m_sStoreNames[0];
	CString sStore2Name = GetDocument()->m_sStoreNames[1];
	CString sStore3Name = GetDocument()->m_sStoreNames[2];
	CString sStore4Name = GetDocument()->m_sStoreNames[3];
	
	// ja - create headers for out list box
	// ja - we have to delete the cols before we recreate them
	m_ListBox.DeleteColumn(0);
	m_ListBox.InsertColumn( 0, _T("Product Name") , LVCFMT_LEFT, 175 );
	m_ListBox.DeleteColumn(1);
	m_ListBox.InsertColumn( 1, _T("Qty")		, LVCFMT_LEFT, 50 );
	m_ListBox.DeleteColumn(2);
	m_ListBox.InsertColumn( 2, sStore1Name , LVCFMT_RIGHT, 95 );
	m_ListBox.DeleteColumn(3);
	m_ListBox.InsertColumn( 3, sStore2Name , LVCFMT_RIGHT, 95 );
	m_ListBox.DeleteColumn(4);
	m_ListBox.InsertColumn( 4, sStore3Name , LVCFMT_RIGHT, 95 );
	m_ListBox.DeleteColumn(5);
	m_ListBox.InsertColumn( 5, sStore4Name , LVCFMT_RIGHT, 95 );

	UpdateListBox();
	
}


void CShopingListView::OnInsert() 
{
	CInsertDlg dlg;
	if (dlg.DoModal() == IDOK){
		//ja - get values from dlg
		int      nQty = dlg.m_nQty;
		CString  sProductName = dlg.m_sPdtName;
		float fStore1Price = dlg.m_fStore1;
		float fStore2Price = dlg.m_fStore2;
		float fStore3Price = dlg.m_fStore3;
		float fStore4Price = dlg.m_fStore4;
		
		GetDocument()->m_Qty.Add(nQty);
		GetDocument()->m_sProductName.Add(sProductName);
		GetDocument()->m_fStore1Price.Add(fStore1Price);
		GetDocument()->m_fStore2Price.Add(fStore2Price);
		GetDocument()->m_fStore3Price.Add(fStore3Price);
		GetDocument()->m_fStore4Price.Add(fStore4Price);
	}
	
	GetDocument()->SetModifiedFlag(TRUE);
	
	UpdateListBox();
	
}

void CShopingListView::OnStoreNames() 
{
	CChgStrNameDlg dlg;

	// ja - fill the dlg with current names 
	dlg.m_sStore1Name = GetDocument()->m_sStoreNames[0];
	dlg.m_sStore2Name = GetDocument()->m_sStoreNames[1];
	dlg.m_sStore3Name = GetDocument()->m_sStoreNames[2];
	dlg.m_sStore4Name = GetDocument()->m_sStoreNames[3];

	if (dlg.DoModal() == IDOK){
		// ja - get the store names from out document
		
		GetDocument()->m_sStoreNames[0] = dlg.m_sStore1Name;
		GetDocument()->m_sStoreNames[1] = dlg.m_sStore2Name;
		GetDocument()->m_sStoreNames[2] = dlg.m_sStore3Name;
		GetDocument()->m_sStoreNames[3] = dlg.m_sStore4Name;
	}

	CreateListBoxHeader();
}

// ja - we have to change the first param to a TBNOTIY
void CShopingListView::OnClickList(TBNOTIFY* pNMHDR, LRESULT* pResult) 
{
	nListBoxIndex = pNMHDR->iItem;
	
	*pResult = 0;
}

void CShopingListView::OnDelete() 
{
	
	// ja - test the position in the array
	if (nListBoxIndex <= GetDocument()->m_sProductName.GetSize() && nListBoxIndex >= 0){
		if (AfxMessageBox(_T("Are you sure you want to delete " 
			+ GetDocument()->m_sProductName[nListBoxIndex] + " ?"),MB_OKCANCEL) == IDOK){
		
			// ja - delete from the array
			GetDocument()->m_Qty.RemoveAt(nListBoxIndex,1);
			GetDocument()->m_sProductName.RemoveAt(nListBoxIndex,1);
			GetDocument()->m_fStore1Price.RemoveAt(nListBoxIndex,1);
			GetDocument()->m_fStore2Price.RemoveAt(nListBoxIndex,1);
			GetDocument()->m_fStore3Price.RemoveAt(nListBoxIndex,1);
			GetDocument()->m_fStore4Price.RemoveAt(nListBoxIndex,1);

			GetDocument()->SetModifiedFlag(TRUE);
			UpdateListBox();
		}	
	}		
}

void CShopingListView::CreateTotals()
{
	int nIndex = GetDocument()->m_sProductName.GetSize();

	// ja - first we will add up all of the totals
	CString sTemp;
	int nTotalQty = 0;
	float fTotalStore1Price = 0;
	float fTotalStore2Price = 0;
	float fTotalStore3Price = 0;
	float fTotalStore4Price = 0;
	
	for (int i = 0; i < nIndex; i++){
		
		nTotalQty    += GetDocument()->m_Qty[i];	
		fTotalStore1Price += GetDocument()->m_fStore1Price[i];
		fTotalStore2Price += GetDocument()->m_fStore2Price[i];
		fTotalStore3Price += GetDocument()->m_fStore3Price[i];
		fTotalStore4Price += GetDocument()->m_fStore4Price[i];	
	}
	
	m_ListBox.SetItemText(nIndex, 0, _T("TOTALS")); 
	
	sTemp.Format(_T("%i"),nTotalQty);
	m_ListBox.SetItemText(nIndex, 1, sTemp); 
				
	sTemp.Format(_T("%.2f"),fTotalStore1Price);
	m_ListBox.SetItemText(nIndex, 2, sTemp); 

	sTemp.Format(_T("%.2f"),fTotalStore2Price);
	m_ListBox.SetItemText(nIndex, 3, sTemp); 
		
	sTemp.Format(_T("%.2f"),fTotalStore3Price);
	m_ListBox.SetItemText(nIndex, 4, sTemp); 
		
	sTemp.Format(_T("%.2f"),fTotalStore4Price);
	m_ListBox.SetItemText(nIndex, 5, sTemp); 

}


⌨️ 快捷键说明

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