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

📄 dao1view.cpp

📁 Visual C++ From the Ground Up Second Edition 例程源代码
💻 CPP
字号:
// DAO1View.cpp : implementation of the CDAO1View class
//

#include "stdafx.h"
#include "DAO1.h"

#include "DAO1Set.h"
#include "DAO1Doc.h"
#include "DAO1View.h"

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

/////////////////////////////////////////////////////////////////////////////
// CDAO1View

IMPLEMENT_DYNCREATE(CDAO1View, CDaoRecordView)

BEGIN_MESSAGE_MAP(CDAO1View, CDaoRecordView)
	//{{AFX_MSG_MAP(CDAO1View)
	ON_COMMAND(ID_ROW_FIRST, OnRowFirst)
	ON_COMMAND(ID_ROW_LAST, OnRowLast)
	ON_COMMAND(ID_ROW_NEXT, OnRowNext)
	ON_COMMAND(ID_ROW_PREV, OnRowPrev)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CDaoRecordView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CDaoRecordView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CDaoRecordView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDAO1View construction/destruction

CDAO1View::CDAO1View()
	: CDaoRecordView(CDAO1View::IDD)
{
	//{{AFX_DATA_INIT(CDAO1View)
		// NOTE: the ClassWizard will add member initialization here
	m_pSet = NULL;
	//}}AFX_DATA_INIT
	// TODO: add construction code here

}

CDAO1View::~CDAO1View()
{
}

void CDAO1View::DoDataExchange(CDataExchange* pDX)
{
	CDaoRecordView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDAO1View)
	DDX_Control(pDX, IDC_MSFLEXGRID1, m_DBGrid);
	//}}AFX_DATA_MAP
}

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

	return CDaoRecordView::PreCreateWindow(cs);
}

void CDAO1View::OnInitialUpdate()
{
	m_pSet = &GetDocument()->m_dAO1Set;
	CDaoRecordView::OnInitialUpdate();
}

/////////////////////////////////////////////////////////////////////////////
// CDAO1View printing

BOOL CDAO1View::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CDAO1View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{

}

void CDAO1View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CDAO1View diagnostics

#ifdef _DEBUG
void CDAO1View::AssertValid() const
{
	CDaoRecordView::AssertValid();
}

void CDAO1View::Dump(CDumpContext& dc) const
{
	CDaoRecordView::Dump(dc);
}

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

/////////////////////////////////////////////////////////////////////////////
// CDAO1View database support
CDaoRecordset* CDAO1View::OnGetRecordset()
{
	return m_pSet;
}


/////////////////////////////////////////////////////////////////////////////
// CDAO1View message handlers

void CDAO1View::OnDraw(CDC* pDC) 
{
	CRect	oRect;						// Size of the window.
	LPTSTR	lpstrRow = "Empty0";		// Current row
	CString	oPrice = "Empty1";			// Price field.
	CString	oPurchase = "Empty2";		// Purchase field.
	LPTSTR	lpstrQuantity = "Empty3";	// Quantity field.
	LPTSTR	lpstrStorage = "Empty4";	// Storage Life field.
	int		iRowCount = 1;				// Current grid row count.

	// Resize the grid to fit in the client window.
	GetClientRect(oRect);
	m_DBGrid.MoveWindow(oRect, TRUE);
	m_DBGrid.RedrawWindow(NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW | RDW_ERASE);

	// Set the Grid to the number of rows and columns we need.
	m_DBGrid.SetCols(m_pSet->m_nFields);
	m_DBGrid.SetRows(m_pSet->GetRecordCount() + 1);

	// Set the width of the columns so the user can see all the data.
	m_DBGrid.SetColWidth(-1, 1440);

	// Create the required column headings.
	m_DBGrid.SetRow(0);
	m_DBGrid.SetCol(1);
	m_DBGrid.SetText("Food ID");
	m_DBGrid.SetCol(2);
	m_DBGrid.SetText("Name");
	m_DBGrid.SetCol(3);
	m_DBGrid.SetText("Perishable?");
	m_DBGrid.SetCol(4);
	m_DBGrid.SetText("Cost");
	m_DBGrid.SetCol(5);
	m_DBGrid.SetText("Purchased");
	m_DBGrid.SetCol(6);
	m_DBGrid.SetText("Quantity");
	m_DBGrid.SetCol(7);
	m_DBGrid.SetText("Storage Life");

	// Go to the beginning ofthe query.
	m_pSet->MoveFirst();

	// Keep looping through the database until you find all the records.
	while (!m_pSet->IsEOF())
	{
		// Move to the current display row.
		m_DBGrid.SetRow(iRowCount);

		// Display the current record number.
		m_DBGrid.SetCol(0);
		itoa(iRowCount, lpstrRow, 10);
		m_DBGrid.SetText(lpstrRow);

		// Display the Food ID and Name fields.
		m_DBGrid.SetCol(1);
		m_DBGrid.SetText(m_pSet->m_Food_ID2);
		m_DBGrid.SetCol(2);
		m_DBGrid.SetText(m_pSet->m_Name);

		// Convert the Perishable field to Yes/No and display it.
		m_DBGrid.SetCol(3);
		if (m_pSet->m_Perishable)
			m_DBGrid.SetText("Yes");
		else
			m_DBGrid.SetText("No");

		// The Price field is a COleCurrency type, so use the
		// Format() function to convert it.
		m_DBGrid.SetCol(4);
		oPrice = m_pSet->m_Price.Format(0, LANG_USER_DEFAULT);
		m_DBGrid.SetText("$" + oPrice);

		// The Purchase field is a COleDateTime type, so use the
		// Format() function to convert it.
		m_DBGrid.SetCol(5);
		oPurchase = m_pSet->m_Purchase.Format("%d %B, %Y");
		m_DBGrid.SetText(oPurchase);

		// Convert the Quality and Storage Life fields from numeric
		// types to text and then display them.
		m_DBGrid.SetCol(6);
		itoa(m_pSet->m_Quantity, lpstrQuantity, 10);
		m_DBGrid.SetText(lpstrQuantity);
		m_DBGrid.SetCol(7);
		itoa(m_pSet->m_Storage_Life, lpstrStorage, 10);
		m_DBGrid.SetText(lpstrStorage);

		// If we're not at the last row, go to the next row and record.
		if (!m_pSet->IsEOF())
		{
			m_pSet->MoveNext();
			iRowCount+=1;
		}
	}

	// Go back to the beginning of the query.
	m_pSet->MoveFirst();

	// Reset the cursor position.
	m_DBGrid.SetRow(1);
	m_DBGrid.SetCol(1);
}

BEGIN_EVENTSINK_MAP(CDAO1View, CDaoRecordView)
    //{{AFX_EVENTSINK_MAP(CDAO1View)
	ON_EVENT(CDAO1View, IDC_MSFLEXGRID1, -600 /* Click */, OnClickMsflexgrid1, VTS_NONE)
	//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()

void CDAO1View::OnClickMsflexgrid1() 
{
	int	iCurRow;	// Currently selected row.

	// Get the current cursor position.
	if (m_DBGrid.GetCol() == 0)
	{
		iCurRow = m_DBGrid.GetRow();

		// Set the row select and column select properties to 
		// highlight the entire row.
		m_DBGrid.SetRowSel(iCurRow);
		m_DBGrid.SetColSel(m_pSet->m_nFields - 1);
	}
}

void CDAO1View::OnRowFirst() 
{
	// Set the four properties required to select the first record
	// in the table.
	m_DBGrid.SetRow(1);
	m_DBGrid.SetCol(0);
	m_DBGrid.SetRowSel(1);
	m_DBGrid.SetColSel(m_pSet->m_nFields - 1);

	// Select the first record in the query.
	m_pSet->MoveFirst();
}

void CDAO1View::OnRowLast() 
{
	// Set the four properties required to select the last record
	// in the table.
	m_DBGrid.SetRow(m_pSet->GetRecordCount());
	m_DBGrid.SetCol(0);
	m_DBGrid.SetRowSel(m_pSet->GetRecordCount());
	m_DBGrid.SetColSel(m_pSet->m_nFields - 1);

	// Select the last record in the query.
	m_pSet->MoveLast();
}

void CDAO1View::OnRowNext() 
{
	int	iCurrentRow;	// Currently selected row.

	// Get the current row.
	iCurrentRow = m_DBGrid.GetRow();

	if (iCurrentRow < m_pSet->GetRecordCount())
	{
		// Set the four properties required to select the next record
		// in the table.
		m_DBGrid.SetRow(iCurrentRow + 1);
		m_DBGrid.SetCol(0);
		m_DBGrid.SetRowSel(iCurrentRow + 1);
		m_DBGrid.SetColSel(m_pSet->m_nFields - 1);

		// Select the next record in the query.
		m_pSet->MoveNext();
	}
}

void CDAO1View::OnRowPrev() 
{
	int	iCurrentRow;	// Currently selected row.

	// Get the current row.
	iCurrentRow = m_DBGrid.GetRow();

	if (iCurrentRow > 1)
	{
		// Set the four properties required to select the previous record
		// in the table.
		m_DBGrid.SetRow(iCurrentRow - 1);
		m_DBGrid.SetCol(0);
		m_DBGrid.SetRowSel(iCurrentRow - 1);
		m_DBGrid.SetColSel(m_pSet->m_nFields - 1);

		// Select the previous record in the query.
		m_pSet->MovePrev();
	}
}

void CDAO1View::OnPrint(CDC* pDC, CPrintInfo* pInfo) 
{
	int			iRowCount = 1;				// Current print row count.
	LPSTR		lpstrRow = "EMPTY1";		// Text form of row count.
	CString		cLine;						// One line of printed output.
	CPen		oPen;						// Pen for drawing.
	CBrush		oBrush;						// Brush for shading.
	CFont		oTextFont;					// Font used for displaying text.
	CFont		oHeadFont;					// Font used to display the heading.
	CFont		oColFont;					// Font used to display column headings.
	LOGFONT		lfFont;						// Font characteristic structure.
	CSize		oFontSize;					// Size of a font.
	COLORREF	clrRef;						// Color structure.
	int			iRowPos = 90;				// Row position on printed page.
	int			iTextHeight = 0;			// Current text height.
	CString		oPrice = "Empty1";			// Price field.
	CString		oPurchase = "Empty2";		// Purchase field.
	LPTSTR		lpstrQuantity = "Empty3";	// Quantity field.
	LPTSTR		lpstrStorage = "Empty4";	// Storage Life field.

	// Create a pen and select it into our device context.
	clrRef = 0x00000000;
	oPen.CreatePen(PS_SOLID, 2, clrRef);
	pDC->SelectObject(&oPen);

	// Create a brush and select it into our device context.
	clrRef = 0x00C0C0C0;
	oBrush.CreateSolidBrush(clrRef);
	pDC->SelectObject(&oBrush);

	// Create a heading font and select it into our device context.
	oHeadFont.CreatePointFont(240, "Arial", pDC);
	pDC->SelectObject(&oHeadFont);

	// Display our heading.
	pDC->Ellipse(500, iRowPos - 50, 2300, iRowPos + 190);
	pDC->SetBkMode(TRANSPARENT);
	oFontSize = pDC->GetOutputTextExtent("The ABC Company");
	pDC->TextOut((2800 - oFontSize.cx) / 2, iRowPos, "The ABC Company");
	pDC->SetBkMode(OPAQUE);

	// Create the appropriate space.
	oHeadFont.GetLogFont(&lfFont);
	iRowPos = abs(lfFont.lfHeight) + 175;

	// Create a text font.
	oTextFont.CreatePointFont(120, "Arial", pDC);

	// Get the current text font height.
	oTextFont.GetLogFont(&lfFont);
	iTextHeight = abs(lfFont.lfHeight) + 10;

	// Create a font for displaying column headings.
	lfFont.lfWeight = 700;	// Make it bold, normal is 400.
	oColFont.CreateFontIndirect(&lfFont);
	pDC->SelectObject(&oColFont);

	// Display the column headings.
	pDC->TextOut(40, iRowPos, "#");
	pDC->TextOut(150, iRowPos, "Food ID");
	pDC->TextOut(650, iRowPos, "Name");
	pDC->TextOut(1000, iRowPos, "Perishable");
	pDC->TextOut(1350, iRowPos, "Price");
	pDC->TextOut(1575, iRowPos,	"Purchase Date");
	pDC->TextOut(2150, iRowPos, "Quantity");
	pDC->TextOut(2450, iRowPos, "Storage Life");

	// Create a space between the column heading and the text.
	iRowPos += iTextHeight;
	pDC->MoveTo(40, iRowPos);
	pDC->LineTo(2850, iRowPos);
	iRowPos += 20;

	// Go to the first record for printing.
	m_pSet->MoveFirst();

	// Select our text font into the device context.
	pDC->SelectObject(&oTextFont);

	// Print the records in a loop.
	while (!m_pSet->IsEOF())
	{
		// Display the current record number.
		itoa(iRowCount, lpstrRow, 10);
		cLine = lpstrRow;
		pDC->TextOut(40, iRowPos, cLine);

		// Display the Food ID and Name fields.
		pDC->TextOut(150, iRowPos, m_pSet->m_Food_ID2);
		pDC->TextOut(650, iRowPos, m_pSet->m_Name);

		// Convert the Perishable field to Yes/No and display it.
		if (m_pSet->m_Perishable)
			pDC->TextOut(1000, iRowPos, "Yes");
		else
			pDC->TextOut(1000, iRowPos, "No");

		// The Price field is a COleCurrency type, so use the
		// Format() function to convert it.
		oPrice = m_pSet->m_Price.Format(0, LANG_USER_DEFAULT);
		pDC->TextOut(1350, iRowPos, "$" + oPrice);

		// The Purchase field is a COleDateTime type, so use the
		// Format() function to convert it.
		oPurchase = m_pSet->m_Purchase.Format("%d %B, %Y");
		pDC->TextOut(1575, iRowPos, oPurchase);

		// Convert the Quality and Storage Life fields from numeric
		// types to text and then display them.
		itoa(m_pSet->m_Quantity, lpstrQuantity, 10);
		cLine = lpstrQuantity;
		pDC->TextOut(2150, iRowPos, cLine);
		itoa(m_pSet->m_Storage_Life, lpstrStorage, 10);
		cLine = lpstrStorage;
		pDC->TextOut(2450, iRowPos, cLine);

		// If we're not at the last row, go to the next row and record.
		if (!m_pSet->IsEOF())
		{
			m_pSet->MoveNext();
			iRowCount++;
			iRowPos += iTextHeight;
		}
	}
	
	CDaoRecordView::OnPrint(pDC, pInfo);
}

⌨️ 快捷键说明

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