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

📄 ado1view.cpp

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

#include "stdafx.h"
#include "ADO1.h"

#include "ADO1Set.h"
#include "ADO1Doc.h"
#include "ADO1View.h"

// Include the C_Recordset class so that we
// can manipulate the recordset directly.
#include "_Recordset.h"

// Include the CColumn class so that we can
// modify the DataGrid1 display format.
#include "Column.h"
#include "Columns.h"

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

/////////////////////////////////////////////////////////////////////////////
// CADO1View

IMPLEMENT_DYNCREATE(CADO1View, COleDBRecordView)

BEGIN_MESSAGE_MAP(CADO1View, COleDBRecordView)
	//{{AFX_MSG_MAP(CADO1View)
	ON_WM_SIZE()
	ON_COMMAND(ID_RECORD_FIRST2, OnRecordFirst2)
	ON_COMMAND(ID_RECORD_LAST2, OnRecordLast2)
	ON_COMMAND(ID_RECORD_NEXT2, OnRecordNext2)
	ON_COMMAND(ID_RECORD_PREV2, OnRecordPrev2)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, COleDBRecordView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, COleDBRecordView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, COleDBRecordView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CADO1View construction/destruction

CADO1View::CADO1View()
	: COleDBRecordView(CADO1View::IDD)
{
	//{{AFX_DATA_INIT(CADO1View)
	m_pSet = NULL;
	//}}AFX_DATA_INIT

	// TODO: add construction code here

}

CADO1View::~CADO1View()
{
}

void CADO1View::DoDataExchange(CDataExchange* pDX)
{
	COleDBRecordView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CADO1View)
	DDX_Control(pDX, IDC_ADODC1, m_adoDC1);
	DDX_Control(pDX, IDC_DATAGRID1, m_dataGrid1);
	//}}AFX_DATA_MAP
}

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

	return COleDBRecordView::PreCreateWindow(cs);
}

void CADO1View::OnInitialUpdate()
{
	m_pSet = &GetDocument()->m_aDO1Set;
	{
		CWaitCursor wait;
		HRESULT hr = m_pSet->Open();
		if (hr != S_OK)
		{
			AfxMessageBox(_T("Record set failed to open."), MB_OK);
			// Disable the Next and Previous record commands,
			// since attempting to change the current record without an
			// open RecordSet will cause a crash.
			m_bOnFirstRecord = TRUE;
			m_bOnLastRecord = TRUE;
		}				
	}

	COleDBRecordView::OnInitialUpdate();

}

/////////////////////////////////////////////////////////////////////////////
// CADO1View printing

BOOL CADO1View::OnPreparePrinting(CPrintInfo* pInfo)
{

	// default preparation
	return DoPreparePrinting(pInfo);
}

void CADO1View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

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

/////////////////////////////////////////////////////////////////////////////
// CADO1View diagnostics

#ifdef _DEBUG
void CADO1View::AssertValid() const
{
	COleDBRecordView::AssertValid();
}

void CADO1View::Dump(CDumpContext& dc) const
{
	COleDBRecordView::Dump(dc);
}

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

/////////////////////////////////////////////////////////////////////////////
// CADO1View database support
CRowset* CADO1View::OnGetRowset()
{
	return m_pSet;
}


/////////////////////////////////////////////////////////////////////////////
// CADO1View message handlers

void CADO1View::OnSize(UINT nType, int cx, int cy) 
{
	int  iHeight, iWidth;	// Control height and width.
	CRect rect;				// Control size.

	// Perform the default action.
	COleDBRecordView::OnSize(nType, cx, cy);

	// Reposition AdoDC1.
	m_adoDC1.GetClientRect(rect);
	iHeight = rect.Height();
	iWidth = rect.Width();
	m_adoDC1.MoveWindow(cx, cy, iWidth, iHeight, TRUE);

	// Resize and reposition DataGrid1
	m_dataGrid1.MoveWindow(0, 0, cx, cy - iHeight, TRUE);
}

void CADO1View::OnRecordFirst2() 
{
	// Move to the first record.
	m_adoDC1.GetRecordset().MoveFirst();
}

void CADO1View::OnRecordLast2() 
{
	// Move to the last record.
	m_adoDC1.GetRecordset().MoveLast();
}

void CADO1View::OnRecordNext2() 
{
	// Move to the next record.
	m_adoDC1.GetRecordset().MoveNext();
}

void CADO1View::OnRecordPrev2() 
{
	// Check to see if we can move the record pointer.
	if (!m_adoDC1.GetRecordset().GetBof())
	{
		// Move to the previous record.
		m_adoDC1.GetRecordset().MovePrevious();

		// Check again for beginning of file.
		if (m_adoDC1.GetRecordset().GetBof())
		{
			// If we are at the beginning of the file,
			// advance the record pointer to the first record.
			m_adoDC1.GetRecordset().MoveNext();
		}
	}
}


void CADO1View::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 = 120;				// Row position on printed page.
    int			iTextHeight = 0;			// Current text height.
	CRect		oDrawRect;					// Drawing area for printer.
	int			iRecNumPos;					// Record number position.
	int			iFoodIDPos;					// Food ID position.
	int			iNamePos;					// Name position.
	int			iPerishablePos;				// Perishable position.
	int			iPricePos;					// Price position.
	int			iPurchasePos;				// Purchase Date position.
	int			iQuantityPos;				// Quantity position.
	int			iStoragePos;				// Storage Life position.

	// Get the drawing area for our print routine.
	oDrawRect = pInfo->m_rectDraw;
	
    // 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.
    oFontSize = pDC->GetOutputTextExtent("The ABC Company");
    pDC->Ellipse(500, 
		iRowPos - (oFontSize.cy / 2) - 10, 
		oDrawRect.Width() - 500, 
		iRowPos + (oFontSize.cy / 2) + 10);
    pDC->SetBkMode(TRANSPARENT);
    pDC->TextOut((oDrawRect.Width() - oFontSize.cx) / 2, 
		iRowPos - (oFontSize.cy / 2) - 10, 
		"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);

	// Compute the column spacings.  Set the first column to 1/2 inch.
	iRecNumPos = int(oDrawRect.Width() / 17);
	iFoodIDPos = iRecNumPos + 50 + pDC->GetOutputTextExtent("##").cx;
	iNamePos = iFoodIDPos + 50 + pDC->GetOutputTextExtent("XXX00000XXX").cx;
	iPerishablePos = iNamePos + 50 + pDC->GetOutputTextExtent("Xxxxxxxxxxxxx").cx;
	iPricePos = iPerishablePos + 50 + pDC->GetOutputTextExtent("Perishable").cx;
	iPurchasePos = iPricePos + 50 + pDC->GetOutputTextExtent("$00.00").cx;
	iQuantityPos = iPurchasePos + 50 + pDC->GetOutputTextExtent("Purchase Date").cx;
	iStoragePos = iQuantityPos + 50 + pDC->GetOutputTextExtent("Quantity").cx;

    // Display the column headings.
    pDC->TextOut(iRecNumPos, iRowPos, "#");
    pDC->TextOut(iFoodIDPos, iRowPos, "Food ID");
    pDC->TextOut(iNamePos, iRowPos, "Name");
    pDC->TextOut(iPerishablePos, iRowPos, "Perishable");
    pDC->TextOut(iPricePos, iRowPos, "Price");
    pDC->TextOut(iPurchasePos, iRowPos, "Purchase Date");
    pDC->TextOut(iQuantityPos, iRowPos, "Quantity");
    pDC->TextOut(iStoragePos, iRowPos, "Storage Life");

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

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

	// Determine the row height.
	iTextHeight = 20 + pDC->GetOutputTextExtent("Xy").cy;

	// Print the records in a loop.
	while (iRowCount < m_dataGrid1.GetVisibleRows())
	{

        // Display the current record number.
        itoa(iRowCount, lpstrRow, 10);
        cLine = lpstrRow;
        pDC->TextOut(iRecNumPos, iRowPos, cLine);

		// Print the data.
		m_dataGrid1.SetRow(iRowCount - 1);
		m_dataGrid1.SetCol(0);
		pDC->TextOut(iFoodIDPos, iRowPos, m_dataGrid1.GetText());
		m_dataGrid1.SetCol(1);
		pDC->TextOut(iNamePos, iRowPos, m_dataGrid1.GetText());
		m_dataGrid1.SetCol(2);
		if (m_dataGrid1.GetText() == "-1")
			pDC->TextOut(iPerishablePos, iRowPos, "Yes");
		else
			pDC->TextOut(iPerishablePos, iRowPos, "No");
		m_dataGrid1.SetCol(6);
		pDC->TextOut(iPricePos, iRowPos, "$" + m_dataGrid1.GetText());
		m_dataGrid1.SetCol(4);
		pDC->TextOut(iPurchasePos, iRowPos, m_dataGrid1.GetText());
		m_dataGrid1.SetCol(5);
		pDC->TextOut(iQuantityPos, iRowPos, m_dataGrid1.GetText());
		m_dataGrid1.SetCol(3);
		pDC->TextOut(iStoragePos, iRowPos, m_dataGrid1.GetText());
	
		// Advance the row.
		iRowPos += iTextHeight;
		iRowCount ++;
	}
    
	//Perform the default action
	COleDBRecordView::OnPrint(pDC, pInfo);
}

⌨️ 快捷键说明

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