odbcdemo2view.cpp

来自「一个很好的VC++ 用ODBC编的」· C++ 代码 · 共 428 行

CPP
428
字号
// ODBCDemo2View.cpp : implementation of the CODBCDemo2View class
//

#include "stdafx.h"
#include "ODBCDemo2.h"

#include "ODBCDemo2Doc.h"
#include "ODBCDemo2View.h"
#include "CommonRs.h"


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

/////////////////////////////////////////////////////////////////////////////
// CODBCDemo2View

IMPLEMENT_DYNCREATE(CODBCDemo2View, CListView)

BEGIN_MESSAGE_MAP(CODBCDemo2View, CListView)
	//{{AFX_MSG_MAP(CODBCDemo2View)
	ON_COMMAND(ID_VIEW_CUSTOMER, OnViewCustomer)
	ON_WM_DESTROY()
	ON_WM_RBUTTONDOWN()
	ON_WM_LBUTTONDBLCLK()
	ON_COMMAND(ID_SALE_PRODUCT, OnSaleProduct)
	ON_COMMAND(ID_SALE_EMPLOYEE, OnSaleEmployee)
	ON_COMMAND(ID_SALE_CUSTOMER, OnSaleCustomer)
	ON_UPDATE_COMMAND_UI(ID_SALE_PRODUCT, OnUpdateSaleProduct)
	ON_UPDATE_COMMAND_UI(ID_SALE_CUSTOMER, OnUpdateSaleCustomer)
	ON_UPDATE_COMMAND_UI(ID_SALE_EMPLOYEE, OnUpdateSaleEmployee)
	ON_COMMAND(ID_EXPLOYEE, OnExployee)
	ON_COMMAND(ID_VIEW_PRODUCT, OnViewProduct)
	ON_COMMAND(ID_VIEW_TRANSPORTOR, OnViewTransportor)
	ON_COMMAND(ID_VIEW_PROVIDER, OnViewProvider)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CListView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CListView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CListView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CODBCDemo2View construction/destruction

CODBCDemo2View::CODBCDemo2View()
{
	// TODO: add construction code here
	m_pCommonRS = NULL;
}

CODBCDemo2View::~CODBCDemo2View()
{
}

BOOL CODBCDemo2View::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
	cs.style |= LVS_REPORT;

	return CListView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CODBCDemo2View drawing

void CODBCDemo2View::OnDraw(CDC* pDC)
{
	CODBCDemo2Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here

}

void CODBCDemo2View::OnInitialUpdate()
{
	CListView::OnInitialUpdate();

	CListCtrl& ctrlList = (CListCtrl&) GetListCtrl();
	ctrlList.SetExtendedStyle(LVS_EX_FULLROWSELECT); 
	

	// TODO: You may populate your ListView with items by directly accessing
	//  its list control through a call to GetListCtrl().
	m_pCommonRS = new CCommonRs;
}

/////////////////////////////////////////////////////////////////////////////
// CODBCDemo2View printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CODBCDemo2View diagnostics

#ifdef _DEBUG
void CODBCDemo2View::AssertValid() const
{
	CListView::AssertValid();
}

void CODBCDemo2View::Dump(CDumpContext& dc) const
{
	CListView::Dump(dc);
}

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

/////////////////////////////////////////////////////////////////////////////
// CODBCDemo2View message handlers
void CODBCDemo2View::OnViewCustomer() 
{
	// TODO: Add your command handler code here
	CListCtrl& ctrlList = (CListCtrl&) GetListCtrl();
	ctrlList.DeleteAllItems();
	while(ctrlList.DeleteColumn(0));
	UpdateWindow();

	CString strSQL(_T("select * from 客户"));
	if(!ShowInformation(strSQL))	AfxMessageBox("数据获取失败!");
	m_uStatus = TABLE_CUSTOMERS;
}

void CODBCDemo2View::OnDestroy() 
{
	CListView::OnDestroy();
	
	// TODO: Add your message handler code here
	if(m_pCommonRS->IsOpen()) m_pCommonRS->Close();  
}

BOOL CODBCDemo2View::ShowInformation(CString strSQL)
{
	CRect rect;
	CListCtrl& ctrlList = (CListCtrl&) GetListCtrl();
	ctrlList.GetWindowRect(rect);

	try{
		// get recordset field information
		BeginWaitCursor();
		if(m_pCommonRS->IsOpen()) m_pCommonRS->Close();  
		m_pCommonRS->Open(CRecordset::dynaset, strSQL);
		if(!m_pCommonRS->IsEOF()){
			m_pCommonRS->MoveLast(); 
			m_pCommonRS->MoveFirst(); 
		}
		int nFieldCount = m_pCommonRS->GetODBCFieldCount();		
		CODBCFieldInfo fieldinfo; 
		for(int n=0;n<nFieldCount;n++){
			m_pCommonRS->GetODBCFieldInfo(n, fieldinfo);
			int nWidth = ctrlList.GetStringWidth(fieldinfo.m_strName) + 15;
			ctrlList.InsertColumn(n, fieldinfo.m_strName, LVCFMT_LEFT, nWidth);
		}
		// get recordset data information
		CString strValue; 
		m_pCommonRS->MoveFirst(); 
		int nCount = 0;

		while(!m_pCommonRS->IsEOF()){
			ctrlList.InsertItem(nCount, strValue);
			for(int j=0;j<nFieldCount;j++){
				m_pCommonRS->GetFieldValue(j, strValue);
				ctrlList.SetItemText(nCount, j, strValue);
			}
			m_pCommonRS->MoveNext(); 
			nCount ++;
		}
		EndWaitCursor();
	}

	catch(CDBException *e){
		e->ReportError();
		EndWaitCursor();
		return FALSE;
	}

	return TRUE;
}

void CODBCDemo2View::OnRButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CListView::OnRButtonDown(nFlags, point);
	
	//
	POINT pp;
	pp.x = point.x; pp.y = point.y;
	ClientToScreen(&pp);

	TrackPopupMenu(m_menuPopup, TPM_LEFTALIGN, 
		pp.x, pp.y, 0, this->GetSafeHwnd(), NULL);
}

void CODBCDemo2View::OnLButtonDblClk(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	//
	CListView::OnLButtonDblClk(nFlags, point);
}

void CODBCDemo2View::OutputReport(CDC* pDC, CPrintInfo* pInfo)
{
	if(!m_pCommonRS) return;
	if(!m_pCommonRS->IsOpen()) return;  

	CString line;
	TEXTMETRIC metrics;	
	int y = 0;
	CFont TitleFont, HeadingFont, DetailFont, FooterFont;
	int TabStops[] = {100, 400, 700, 1000, 1300, 1600, 1900, 2200, 2500, 2800, 3100};
	int FooterTabStops[] = {350};
	if (!pInfo || pInfo->m_nCurPage == 1) {
		m_pCommonRS->Requery();
	}
	TitleFont.CreateFont(m_uSizeTitle, 0, 0, 0, FW_BOLD, FALSE, FALSE, 0,
		ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
		DEFAULT_QUALITY, DEFAULT_PITCH | FF_ROMAN, 
		m_strFontTitle);
	HeadingFont.CreateFont(m_uSizeContentHead, 0, 0, 0, FW_BOLD, FALSE, TRUE, 0,
		ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
		DEFAULT_QUALITY, DEFAULT_PITCH | FF_ROMAN, 
		m_strFontContentHead);
	DetailFont.CreateFont(m_uSizeContent, 0, 0, 0, FW_NORMAL, FALSE, FALSE, 0,
		ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
		DEFAULT_QUALITY, DEFAULT_PITCH | FF_ROMAN, 
		m_strFontContent);
	FooterFont.CreateFont(m_uSizeFooter, 0, 0, 0, FW_NORMAL, FALSE, FALSE, 0,
		ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
		DEFAULT_QUALITY, DEFAULT_PITCH | FF_ROMAN, 
		m_strFontFooter);
	// Title
	CFont* OldFont = pDC->SelectObject(&TitleFont);
	pDC->GetTextMetrics(&metrics);
	int LineHeight = metrics.tmHeight + metrics.tmExternalLeading;
	y += LineHeight;	
	pDC->TextOut(200, 0, m_strReportTitle);
	// Head
	pDC->SelectObject(&HeadingFont);
	if(0<m_saSelectedFieldsTitle.GetSize()){
		for(int i=0;i<m_saSelectedFieldsTitle.GetSize()-1;i++){
			line += m_saSelectedFieldsTitle.GetAt(i);
			line += _T("\t");
		}
		line += m_saSelectedFieldsTitle.GetAt(i);
	}
	pDC->TabbedTextOut(0, y, line, m_saSelectedFieldsTitle.GetSize(), TabStops, 0);
	// detail
	LineHeight = metrics.tmHeight + metrics.tmExternalLeading;
	y += LineHeight;
	pDC->SelectObject(&DetailFont);
	pDC->GetTextMetrics(&metrics);
	LineHeight = metrics.tmHeight + metrics.tmExternalLeading;
	while (!m_pCommonRS->IsEOF()) {
		if (pInfo && abs(y) > 1000) {
			pInfo->SetMaxPage(pInfo->m_nCurPage + 1);
			break;
		}
		//Format the detail line
		line.Empty(); 
		CString strValue;
		if(0<m_saSelectedFieldsTitle.GetSize()){
			for(int j=0;j<m_saSelectedFields.GetSize()-1;j++){
				m_pCommonRS->GetFieldValue(m_saSelectedFields.GetAt(j), strValue);
				line += strValue;
				line += _T("\t");
			}
			m_pCommonRS->GetFieldValue(m_saSelectedFields.GetAt(j), strValue);
			line += strValue;
		}
		pDC->TabbedTextOut(0, y, line, m_saSelectedFields.GetSize(), TabStops, 0);
		y += LineHeight;	//Adjust y position
		m_pCommonRS->MoveNext();
	}
	// footer
	if (pInfo) {
		pDC->SelectObject(&FooterFont);
		line.Format("%s \tPage: %d \tJackie", m_strReportFooter, pInfo->m_nCurPage);
		pDC->TabbedTextOut(0, 1025, line, 2, FooterTabStops, 0);
	}
	// Restore default settings
	pDC->SelectObject(OldFont);
}

void CODBCDemo2View::OnPrint(CDC* pDC, CPrintInfo* pInfo) 
{
	// TODO: Add your specialized code here and/or call the base class
	OutputReport(pDC, pInfo);
	
	CListView::OnPrint(pDC, pInfo);
}

void CODBCDemo2View::OnSaleProduct() 
{
	// TODO: Add your command handler code here
	// clear all item and columns
	CListCtrl& ctrlList = (CListCtrl&) GetListCtrl();
	ctrlList.DeleteAllItems();
	while(ctrlList.DeleteColumn(0));
	UpdateWindow();

	CString strSQL;
	strSQL = _T("SELECT * FROM SalesByProduct;");

	if(!ShowInformation(strSQL))	AfxMessageBox("数据获取失败!");
	m_uStatus = TABLE_SPRODUCTS;
}

void CODBCDemo2View::OnSaleEmployee() 
{
	// TODO: Add your command handler code here
	CListCtrl& ctrlList = (CListCtrl&) GetListCtrl();
	ctrlList.DeleteAllItems();
	while(ctrlList.DeleteColumn(0));
	UpdateWindow();

	CString strSQL(_T("select * from SalesByEmployee"));
	if(!ShowInformation(strSQL))	AfxMessageBox("数据获取失败!");
	m_uStatus = TABLE_SIMPLOYEES;
}

void CODBCDemo2View::OnSaleCustomer() 
{
	// TODO: Add your command handler code here	
	CListCtrl& ctrlList = (CListCtrl&) GetListCtrl();
	ctrlList.DeleteAllItems();
	while(ctrlList.DeleteColumn(0));
	UpdateWindow();

	CString strSQL(_T("select * from SalesByCustomer"));
	if(!ShowInformation(strSQL))	AfxMessageBox("数据获取失败!");
	m_uStatus = TABLE_SCUSTOMERS;
}

void CODBCDemo2View::OnUpdateSaleProduct(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_uStatus == TABLE_SPRODUCTS); 	
}

void CODBCDemo2View::OnUpdateSaleCustomer(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_uStatus == TABLE_SCUSTOMERS);
}

void CODBCDemo2View::OnUpdateSaleEmployee(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_uStatus == TABLE_SIMPLOYEES);
}

void CODBCDemo2View::OnExployee() 
{
	// TODO: Add your command handler code here
	CListCtrl& ctrlList = (CListCtrl&) GetListCtrl();
	ctrlList.DeleteAllItems();
	while(ctrlList.DeleteColumn(0));
	UpdateWindow();

	CString strSQL(_T("select * from 雇员"));
	if(!ShowInformation(strSQL))	AfxMessageBox("数据获取失败!");
	m_uStatus = TABLE_IMPLOYEES;
}

void CODBCDemo2View::OnViewProduct() 
{
	// TODO: Add your command handler code here
	CListCtrl& ctrlList = (CListCtrl&) GetListCtrl();
	ctrlList.DeleteAllItems();
	while(ctrlList.DeleteColumn(0));
	UpdateWindow();

	CString strSQL(_T("select * from 产品;"));
	if(!ShowInformation(strSQL))	AfxMessageBox("数据获取失败!");
	m_uStatus = TABLE_PRODUCTS;
}

void CODBCDemo2View::OnViewTransportor() 
{
	// TODO: Add your command handler code here
	CListCtrl& ctrlList = (CListCtrl&) GetListCtrl();
	ctrlList.DeleteAllItems();
	while(ctrlList.DeleteColumn(0));
	UpdateWindow();

	CString strSQL(_T("select * from 运货商"));
	if(!ShowInformation(strSQL))	AfxMessageBox("数据获取失败!");
	m_uStatus = TABLE_PRODUCTS;
}

void CODBCDemo2View::OnViewProvider() 
{
	// TODO: Add your command handler code here
	CListCtrl& ctrlList = (CListCtrl&) GetListCtrl();
	ctrlList.DeleteAllItems();
	while(ctrlList.DeleteColumn(0));
	UpdateWindow();

	CString strSQL(_T("select * from 供应商"));
	if(!ShowInformation(strSQL))	AfxMessageBox("数据获取失败!");
	m_uStatus = TABLE_PRODUCTS;
}

⌨️ 快捷键说明

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