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

📄 childview.cpp

📁 windows mobile ado simple 开发环境:evc4.0
💻 CPP
字号:
// ChildView.cpp : implementation of the CChildView class
//

#include "stdafx.h"
#include "AdoSample.h"
#include "ChildView.h"
#include "ado.h"

#include "CePropertySheet.h"
#include "AuthorPage.h"

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


//---------------------------------------------------------------------------
//
//	CChildView
//
//---------------------------------------------------------------------------


CChildView::CChildView()
{
}


CChildView::~CChildView()
{
}


BEGIN_MESSAGE_MAP(CChildView, CListCtrl)
	//{{AFX_MSG_MAP(CChildView)
	ON_WM_CREATE()
	ON_COMMAND(ID_FILE_NEW, OnFileNew)
	ON_NOTIFY_REFLECT(NM_CLICK, OnClick)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


//---------------------------------------------------------------------------
//
//	CChildView operations
//
//---------------------------------------------------------------------------


// CChildView::RecordExists
//
//		Checks if a record exists
//
BOOL CChildView::RecordExists(int nID)
{
	CADORecordset	rs(&g_db);
	CString			strSelect;
	BOOL			bExists;

	strSelect.Format(_T("SELECT authID FROM Author WHERE authID=%d"), nID);

	bExists = rs.Open(strSelect, CADORecordset::openQuery);

	if(bExists)
		bExists = rs.IsEof();

	return bExists;
}


// CChildView::EditRecord
//
//		Edits a record existing or new
//
BOOL CChildView::EditRecord(int nID)
{
	CADORecordset		rs(&g_db);
	CString				strSelect;
	CCePropertySheet	sheet(_T("Author"));
	CAuthorPage			page;
	COleDateTime		odtNow(COleDateTime::GetCurrentTime()),
						odtDate(odtNow.GetYear(),
								odtNow.GetMonth(),
								odtNow.GetDay(), 0, 0, 0);
	BOOL				bOk = FALSE;

	//
	// Check if we are editing a new or existing record
	//
	if(nID)
	{
		//
		// Existing record : load it
		//
		strSelect.Format(_T("SELECT * FROM Author WHERE authID=%d"), nID);

		bOk = rs.Open(strSelect, CADORecordset::openQuery);
		if(bOk)
		{
			rs.GetFieldValue(_T("authID"),		page.m_nAuthorID	);
			rs.GetFieldValue(_T("authName"),	page.m_strAuthorName);
			rs.GetFieldValue(_T("authDate"),	page.m_odtAuthorDate);
		}
		else
		{
			CString	strMsg;

			strMsg.Format(_T("Cannot load Author with ID=%d"), nID);
			MessageBox(strMsg, _T("Error"), MB_OK | MB_ICONSTOP);
			return FALSE;
		}
	}
	else
	{
		//
		// New record
		//
		page.m_nAuthorID		= 0;
		page.m_odtAuthorDate	= odtDate;
	}

	//
	// Show the dialog
	//
	sheet.AddPage(&page);
	sheet.DoModal();

	//
	// Save only if the ID > 0
	//
	if(page.m_nAuthorID > 0)
	{
		//
		// Check if we are saving a new or existing record
		//
		if(nID)
		{
			//
			// This is an existing record.
			// If the user changed the ID, check if it exists
			//
			if(nID != page.m_nAuthorID)
			{
				if(RecordExists(page.m_nAuthorID))
				{
					MessageBox(_T("This Author ID already exists: cannot change."),
								_T("Error"), MB_OK | MB_ICONSTOP);
					return FALSE;
				}
			}

			//
			// Issue a SELECT statement for its ID (the one from the dialog
			// because the user may have changed it).
			//
			strSelect.Format(_T("SELECT * FROM Author WHERE authID=%d"), page.m_nAuthorID);
			bOk = rs.Open(strSelect, CADORecordset::openUpdate);

			//
			// The record must exist, and we must put the recordset in edit mode.
			//
			if(bOk)
			{
				bOk = !rs.IsEof();	
				rs.Edit();
			}
		}
		else
		{
			//
			// This is a new record.
			// Check if the ID already exists.
			// Pocket Access does not support unique indexes
			//
			if(RecordExists(page.m_nAuthorID))
			{
				MessageBox(_T("An author with the same ID already exists"),
							_T("Error"), MB_OK | MB_ICONSTOP);
				return FALSE;
			}

			//
			// The ID is unique in the table. We can insert the new record.
			// The record is inserted by opening a recordset in table mode.
			//
			bOk = rs.Open(_T("Author"), CADORecordset::openTable);

			if(bOk)
			{
				if(!rs.AddNew())
				{
					MessageBox(_T("AddNew failed."),
								_T("Error"), MB_OK | MB_ICONSTOP);
					return FALSE;
				}
			}
		}

		//
		// Check if we managed to create / locate the record
		//
		if(bOk)
		{
			//
			// Good, now set the field values.
			//
			rs.SetFieldValue(_T("authID"),		page.m_nAuthorID	);
			rs.SetFieldValue(_T("authName"),	page.m_strAuthorName);
			rs.SetFieldValue(_T("authDate"),	page.m_odtAuthorDate);

			//
			// Now, update the record
			//
			bOk = rs.Update();
			if(!bOk)
				MessageBox(_T("Update failed"),
							_T("Error"), MB_OK | MB_ICONSTOP);
		}
	}

	return bOk;
}


// CChildView::Refresh
//
//		Refreshes the list
//
void CChildView::Refresh()
{
	CADORecordset	rs(&g_db);
	int				iItem,
					i,
					nID;
	CString			strID,
					strName,
					strDate;

	//
	// Try to open the recordset
	//
	if(rs.Open(_T("SELECT * FROM Author"), CADORecordset::openQuery))
	{
		SetRedraw(FALSE);

		//
		// Clear the list
		//
		DeleteAllItems();

		//
		// Loop through all records
		//
		for(i = 0, rs.MoveFirst(); !rs.IsEof(); rs.MoveNext(), ++i)
		{
			rs.GetFieldValue(_T("authID"),		strID	);
			rs.GetFieldValue(_T("authID"),		nID		);
			rs.GetFieldValue(_T("authName"),	strName	);
			rs.GetFieldValue(_T("authDate"),	strDate	);

			iItem = InsertItem(i, strID);
			if(iItem != -1)
			{
				SetItemText(iItem, 1, strName);
				SetItemText(iItem, 2, strDate);

				//
				// Store the ID for later
				//
				SetItemData(iItem, nID);
			}
		}

		SetRedraw(TRUE);
		rs.Close();
	}
}


//---------------------------------------------------------------------------
//
//	CChildView virtual methods
//
//---------------------------------------------------------------------------


BOOL CChildView::PreCreateWindow(CREATESTRUCT& cs) 
{
	if(!CListCtrl::PreCreateWindow(cs))
		return FALSE;

	return TRUE;
}


//---------------------------------------------------------------------------
//
//	CChildView message handlers
//
//---------------------------------------------------------------------------


// CChildView::OnCreate
//
//		The list is being created
//
int CChildView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	LOGFONT	lf;
	DWORD	dwExStyle;

	if(CListCtrl::OnCreate(lpCreateStruct) == -1)
		return -1;

	//
	// Full row selection
	//
	dwExStyle = GetExtendedStyle();
	SetExtendedStyle(dwExStyle | LVS_EX_FULLROWSELECT);

	//
	// Set the Tahoma 8 font
	//
	// Points  8 : -11
	//         9 : -12
	//		  10 : -13
	//
	lf.lfHeight			= -11;	//(LONG)(-8 * pDC.GetDeviceCaps(LOGPIXELSY) / 72);
	lf.lfWidth			= 0;
	lf.lfEscapement		= 0;
	lf.lfOrientation	= 0;
	lf.lfWeight			= FW_NORMAL;
	lf.lfItalic			= FALSE;
	lf.lfUnderline		= FALSE;
	lf.lfStrikeOut		= 0;
	lf.lfCharSet		= ANSI_CHARSET;
	lf.lfOutPrecision	= OUT_DEFAULT_PRECIS;
	lf.lfClipPrecision	= CLIP_DEFAULT_PRECIS;
	lf.lfQuality		= DEFAULT_QUALITY;
	lf.lfPitchAndFamily = DEFAULT_PITCH | FF_SWISS;
	lstrcpy(lf.lfFaceName, TEXT("Tahoma"));
	m_font.CreateFontIndirect(&lf);

	SetFont(&m_font);
	
	//
	// Insert the columns
	//
	InsertColumn(0, _T("ID"),	LVCFMT_LEFT,  50, -1);
	InsertColumn(1, _T("Name"),	LVCFMT_LEFT, 100,  1);
	InsertColumn(2, _T("Date"),	LVCFMT_LEFT,  70,  2);

	//
	// Insert the data
	//
	Refresh();
	
	return 0;
}


// CChildView::OnFileNew
//
//		Intercepts the ID_FILE_NEW command forwarded by the app
//
void CChildView::OnFileNew() 
{
	if(EditRecord())
		Refresh();
}


// CChildView::OnClick
//
//		The user clicked the list
//
void CChildView::OnClick(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NMLISTVIEW*	pNMLV = (NMLISTVIEW*) pNMHDR;
	int			iItem = pNMLV->iItem,
				nID;

	//
	// Check if we clicked an item
	//
	if(iItem != -1)
	{
		nID = (int) GetItemData(iItem);

		if(EditRecord(nID))
			Refresh();
	}
	
	*pResult = 0;
}

⌨️ 快捷键说明

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