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

📄 booksdlg.cpp

📁 Windows CE Database Programming using EDB
💻 CPP
字号:
// BooksDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Books.h"
#include "BooksDlg.h"
#include "BookDetailsDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CBooksDlg dialog

CBooksDlg::CBooksDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CBooksDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CBooksDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_BOOKS, m_wndBooks);
}

BEGIN_MESSAGE_MAP(CBooksDlg, CDialog)
	ON_WM_SIZE()
	ON_COMMAND(ID_MENU_ABOUT, &CBooksDlg::OnMenuAbout)
	ON_COMMAND(ID_MENU_ADD, &CBooksDlg::OnMenuAdd)
	ON_COMMAND(ID_MENU_DELETE, &CBooksDlg::OnMenuDelete)
	ON_COMMAND(ID_MENU_EDIT, &CBooksDlg::OnMenuEdit)
	ON_NOTIFY(NM_DBLCLK, IDC_BOOKS, &CBooksDlg::OnNMDblclkRecords)

	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


// CBooksDlg message handlers

BOOL CBooksDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	SetIcon(m_hIcon, TRUE);
	SetIcon(m_hIcon, FALSE);

	PerpareListControl(&m_wndBooks);
	CreateMenu(IDR_MENU);

	if(!OpenBooksDatabase())
	{
		AfxMessageBox(TEXT("Could not open/create database.\r\nThe application will not work correctly"),
					  MB_OK | MB_ICONERROR);
	}
	else
	{
		PopulateRecords(&m_wndBooks);
	}
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CBooksDlg::OnSize(UINT nType, int cx, int cy)
{
	CDialog::OnSize(nType,cx,cy);

	if(m_wndBooks.GetSafeHwnd())
	{
		CWnd*	pWnd	= GetDlgItem(IDC_STATIC_BOOKS);
		int		x		= DRA::SCALEX(X_OFFSET);
		int		y		= DRA::SCALEY(Y_OFFSET);
		int		nWidth	= DRA::SCALEX(cx - X_OFFSET * 2);
		int		nHeight	= DRA::SCALEX(cy - Y_OFFSET * 2);
		CRect	rc;

		if(pWnd)
		{
			pWnd->GetClientRect(&rc);

			pWnd->SetWindowPos(NULL,x,y,nWidth,rc.Height(),SWP_NOZORDER);

			y		+= (rc.Height() + 2);
			nHeight	-= (rc.Height() + 2);
		}

		m_wndBooks.SetWindowPos(NULL,x,y,nWidth,nHeight,SWP_NOZORDER);
	}
}

void CBooksDlg::PerpareListControl(CListCtrl* pList)
{
	pList->ModifyStyle(0,LVS_REPORT | LVS_SHOWSELALWAYS);
	pList->SetExtendedStyle(LVS_EX_FULLROWSELECT);
	pList->InsertColumn(0,BOOKS_TITLE,LVCFMT_LEFT,BOOKS_TITLE_LEN);
	pList->InsertColumn(1,BOOKS_ISBN,LVCFMT_LEFT,BOOKS_ISBN_LEN);
	pList->InsertColumn(2,BOOKS_AUTHORS,LVCFMT_LEFT,BOOKS_AUTHORS_LEN);
}

void CBooksDlg::CreateMenu(UINT uMenuId)
{
	SHMENUBARINFO info; 
	
	info.cbSize			= sizeof(info); 
	info.hwndParent		= m_hWnd; 
	info.dwFlags		= SHCMBF_HMENU | SHCMBF_HIDESIPBUTTON; 
	info.nToolBarId		= uMenuId; 
	info.hInstRes		= ::AfxGetInstanceHandle(); 
	info.nBmpId			= 0; 
	info.cBmpImages		= 0; 
	
	SHCreateMenuBar(&info);
}

void CBooksDlg::OnMenuAbout()
{
	AfxMessageBox(TEXT("Book Database V1.0\r\nKrishnaraj Varma"),MB_OK | MB_ICONINFORMATION);
}

void CBooksDlg::OnMenuAdd()
{
	CBookDetailsDlg		dlgBook;

	if(IDOK == dlgBook.DoModal())
	{
		CString		strTitle;
		CString		strISBN;
		CString		strAuthors;
		CString		strDesc;

		dlgBook.GetDetails(strTitle,strISBN,strAuthors,strDesc);

		CBookRecord	rec;

		rec.Initialize(strTitle,strISBN,strAuthors,strDesc);

		m_db.Write(rec);

		PopulateRecords(&m_wndBooks);
	}
}

void CBooksDlg::OnMenuEdit()
{
	POSITION		pos		= m_wndBooks.GetFirstSelectedItemPosition();

	if(!pos)
		return;

	int				nSel	= m_wndBooks.GetNextSelectedItem(pos);
	int				oid		= m_wndBooks.GetItemData(nSel);

	if(!oid)
		return;

	CBookDetailsDlg	dlgBook;
	CBookRecord		rec;

	m_db.MoveTo(oid);
	
	if(!m_db.Read(rec))
	{
		return;
	}

	dlgBook.SetDetails(rec.GetTitle(),rec.GetISBN(),rec.GetAuthors(),rec.GetDescription());

	if(IDOK == dlgBook.DoModal())
	{
		CString		strTitle;
		CString		strISBN;
		CString		strAuthors;
		CString		strDesc;

		dlgBook.GetDetails(strTitle,strISBN,strAuthors,strDesc);

		CBookRecord	rec;

		rec.Initialize(strTitle,strISBN,strAuthors,strDesc);

		m_db.Write(oid,rec);

		PopulateRecords(&m_wndBooks);
	}
}

void CBooksDlg::OnMenuDelete()
{
	POSITION		pos		= m_wndBooks.GetFirstSelectedItemPosition();

	if(!pos)
		return;

	int				nSel	= m_wndBooks.GetNextSelectedItem(pos);
	int				oid		= m_wndBooks.GetItemData(nSel);

	if(!oid)
		return;

	CBookRecord		rec;

	m_db.MoveTo(oid);
	
	if(!m_db.Read(rec))
	{
		return;
	}

	CString			strMessage;

	strMessage.Format(TEXT("Are you sure delete the book:\r\nTitle: %s\r\nISBN: %s\r\nAuthor(s): %s"),
					  rec.GetTitle(),rec.GetISBN(),rec.GetAuthors());

	if(IDYES == AfxMessageBox(strMessage,MB_YESNO | MB_ICONQUESTION))
	{
		m_db.Delete(oid);

		PopulateRecords(&m_wndBooks);
	}
}

void CBooksDlg::OnNMDblclkRecords(NMHDR *pNMHDR, LRESULT *pResult)
{
	OnMenuEdit();

	*pResult = 0;
}

BOOL CBooksDlg::OpenBooksDatabase()
{
	return m_db.Open();
}

void CBooksDlg::PopulateRecords(CListCtrl* pList)
{
	pList->DeleteAllItems();
	m_db.MoveFirst();

	CBookRecord	rec;
	CEOID		oid		= m_db.Read(rec);
	int			nRecord	= 0;
	int			nItem	= 0;

	while(oid)
	{
		nItem	= pList->InsertItem(nRecord,rec.GetTitle(),1);

		pList->SetItemData(nItem,oid);

		pList->SetItemText(nItem,1,rec.GetISBN());
		pList->SetItemText(nItem,2,rec.GetAuthors());

		oid		= m_db.Read(rec);
		
		++nRecord;
	}

	m_db.MoveFirst();
}

⌨️ 快捷键说明

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