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

📄 booksview.cpp

📁 这是我做的一个电子图书管理系统的软件程序。希望对大家有帮助!
💻 CPP
字号:
// BOOKSView.cpp : implementation of the CBOOKSView class
//

#include "stdafx.h"
#include "BOOKS.h"

#include "BOOKSSet.h"
#include "BOOKSDoc.h"
#include "BOOKSView.h"
#include "Adddoc.h"
#include "Editdoc.h"
#include "Deletedoc.h"
#include "Sortset.h"
#include "SortMag.h"
#include "Query.h"

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

/////////////////////////////////////////////////////////////////////////////
// CBOOKSView

IMPLEMENT_DYNCREATE(CBOOKSView, CRecordView)

BEGIN_MESSAGE_MAP(CBOOKSView, CRecordView)
	//{{AFX_MSG_MAP(CBOOKSView)
	ON_BN_CLICKED(IDC_ADD, OnAdd)
	ON_BN_CLICKED(IDC_EDITBOOK, OnEditbook)
	ON_BN_CLICKED(IDC_DELETEBOOK, OnDeletebook)
	ON_NOTIFY(NM_DBLCLK, IDC_TOPICLIST, OnDblclkTopiclist)
	ON_NOTIFY(NM_CLICK, IDC_TOPICLIST, OnClickTopiclist)
	ON_NOTIFY(NM_CLICK, IDC_BOOKLIST, OnClickBooklist)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_TOPIC_MANAGE, OnTopicManage)
	ON_BN_CLICKED(IDC_QUERY, OnQuery)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CRecordView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CRecordView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CRecordView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBOOKSView construction/destruction

CBOOKSView::CBOOKSView()
	: CRecordView(CBOOKSView::IDD)
{
	//{{AFX_DATA_INIT(CBOOKSView)
	m_pSet = NULL;
	m_count3 = 0;
	//}}AFX_DATA_INIT
	// TODO: add construction code here
	m_editNAME="";
	m_pSelf=(CWnd *)this;
	m_querybl=true;
	
}

CBOOKSView::~CBOOKSView()
{
}

void CBOOKSView::DoDataExchange(CDataExchange* pDX)
{
	CRecordView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CBOOKSView)
	DDX_Control(pDX, IDC_TOPICLIST, m_topiclist);
	DDX_Control(pDX, IDC_BOOKLIST, m_booklist);
	DDX_Text(pDX, IDC_EDIT1, m_count3);
	//}}AFX_DATA_MAP
}

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

	return CRecordView::PreCreateWindow(cs);
}

void CBOOKSView::OnInitialUpdate()
{
	m_pSet = &GetDocument()->m_bOOKSSet;
	CRecordView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();
	
	m_booklist.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);//LVS_EX_TRACKSELECT|LVS_EX_ONECLICKACTIVATE|LVS_EX_CHECKBOXES(复选框)|LVS_EX_TRACKSELECT(自动切换)
	m_topiclist.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);

    m_topiclist.InsertColumn(0,"类别名称",LVCFMT_LEFT,100);
    char* szColumn[8]={"书名","作者","出版社","出版时间","类别","存放路径","读书笔记地址"};
    int widths[]={120,50,100,60,50,60,80};
    LV_COLUMN lvc;
	lvc.mask=LVCF_FMT|LVCF_WIDTH|LVCF_TEXT|LVCF_SUBITEM;
    lvc.fmt=LVCFMT_LEFT;

    for(int i=0;i<7;i++) {//插入各列
    lvc.pszText=szColumn[i];
    lvc.cx=widths[i];
    lvc.iSubItem=i;
    m_booklist.InsertColumn(i,&lvc);
   }
	//::m_pbooklist=&m_booklist;

	RushListctrl(m_pSet,m_booklist,"");//刷新列表控件m_booklist


	RushListctrl(&m_pSort,m_topiclist,"");//刷新列表控件m_topiclist
//m_topiclist.InsertItem(0,"全部书籍");
//m_topiclist.InsertItem(0,"other");
//m_count3=m_pSet->GetODBCFieldCount();
UpdateData(false);
}

/////////////////////////////////////////////////////////////////////////////
// CBOOKSView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CBOOKSView diagnostics

#ifdef _DEBUG
void CBOOKSView::AssertValid() const
{
	CRecordView::AssertValid();
}

void CBOOKSView::Dump(CDumpContext& dc) const
{
	CRecordView::Dump(dc);
}

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

/////////////////////////////////////////////////////////////////////////////
// CBOOKSView database support
CRecordset* CBOOKSView::OnGetRecordset()
{
	return m_pSet;
}


/////////////////////////////////////////////////////////////////////////////
// CBOOKSView message handlers

void CBOOKSView::OnAdd() 
{
	CAdddoc dlg;
	if(m_pSort.IsOpen())
		m_pSort.Close();
	m_pSort.Open();
	//m_pSet->AddNew();
	UpdateData(false);
	//long int m_count=m_pSet->GetRecordCount();
	/*long int m_count;
	if(m_pSet->IsBOF())
	{
		m_count=1000;
	}
	else
	{
		m_pSet->MoveLast();
		m_count=m_pSet->m_ID+1;
	}*/
	m_pSet->AddNew();
	UpdateData(false);

	int m_check=dlg.DoModal();
	
	if(IDOK==m_check)
	{
		UpdateData(true);
		//m_pSet->m_ID=m_count;
		m_pSet->m_NAME=dlg.m_name;
		m_pSet->m_AUTHOR=dlg.m_author;
		m_pSet->m_PUBLISHER=dlg.m_publisher;
		m_pSet->m_SORT=dlg.m_sortword;
		m_pSet->Update();
	}
	m_pSet->Close();
	m_pSet->Open();
	UpdateData(false);

	m_booklist.DeleteAllItems();
	m_topiclist.DeleteAllItems();

	RushListctrl(m_pSet,m_booklist,"");//刷新列表控件m_booklist

	RushListctrl(&m_pSort,m_topiclist,"");//刷新列表控件m_topiclist


	// TODO: Add your control notification handler code here
	
}

void CBOOKSView::OnEditbook() 
{
	if(!m_editNAME.IsEmpty())
	{
		CString m_count=m_editNAME;
		m_pSet->Close();
		m_pSet->m_strFilter.Format(_T("NAME='%s'"),m_editNAME);
		m_pSet->Open();
		CEditdoc dlg;
		dlg.m_name=m_pSet->m_NAME;
		dlg.m_author=m_pSet->m_AUTHOR;
		dlg.m_publisher=m_pSet->m_PUBLISHER;
		dlg.m_sortword=m_pSet->m_SORT;
		m_pSet->Edit();
		UpdateData(false);
		if(IDOK==dlg.DoModal())
		{
			UpdateData(true);
			m_pSet->m_NAME=dlg.m_name;
			m_pSet->m_AUTHOR=dlg.m_author;
			m_pSet->m_PUBLISHER=dlg.m_publisher;
			m_pSet->Update();
		}
		m_pSet->Close();
		m_pSet->m_strFilter="";
		m_pSet->Open();	
		UpdateData(false);
		m_booklist.DeleteAllItems();
		m_topiclist.DeleteAllItems();

		RushListctrl(m_pSet,m_booklist,"");//刷新列表控件m_booklist

		RushListctrl(&m_pSort,m_topiclist,"");//刷新列表控件m_topiclist
	}
	
	// TODO: Add your control notification handler code here
	
}

void CBOOKSView::OnDeletebook() 
{
	if(!m_editNAME.IsEmpty())
	{
	//long int m_count=m_pSet->m_ID;
	m_pSet->Close();
	m_pSet->m_strFilter.Format(_T("NAME='%s'"),m_editNAME);
	m_pSet->Open();
	CDeletedoc dlg;
	if(IDOK==dlg.DoModal())
	{
		m_pSet->Delete();
		//m_pSet->Update();
	}
	m_pSet->Close();
	m_pSet->m_strFilter="";
	m_pSet->Open();	
	UpdateData(false);
	m_booklist.DeleteAllItems();
	m_topiclist.DeleteAllItems();

	RushListctrl(m_pSet,m_booklist,"");//刷新列表控件m_booklist

	RushListctrl(&m_pSort,m_topiclist,"");//刷新列表控件m_topiclist
	}
	// TODO: Add your control notification handler code here
	
}


void CBOOKSView::OnDblclkTopiclist(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	*pResult = 0;
}

//DEL void CBOOKSView::RushListctrl(CString m_str)
//DEL {
//DEL 	m_pSet->Close();
//DEL 	m_pSet->m_strFilter="SORT="+m_str;
//DEL 	m_pSet->Open();
//DEL 	LV_ITEM lvi;
//DEL 	lvi.mask=LVIF_TEXT|LVIF_IMAGE|LVIF_PARAM;
//DEL 	lvi.iSubItem=0;
//DEL 	//lvi.pszText=(char *)data[0];
//DEL 	lvi.iImage = 0;
//DEL 	lvi.iItem=0;
//DEL 	for(count=0;!m_pSet->IsEOF();m_pSet->MoveNext(),count++)
//DEL 	{//插入表项
//DEL 		char str[10];
//DEL 		CString data[8];
//DEL 		itoa(m_pSet->m_ID,str,10);
//DEL 		lvi.pszText=str;
//DEL 		data[0]="";
//DEL 		data[1]=m_pSet->m_NAME;
//DEL 		data[2]=m_pSet->m_AUTHOR;
//DEL 		data[3]=m_pSet->m_PUBLISHER;
//DEL 		data[5]=m_pSet->m_SORT;
//DEL 		data[6]=m_pSet->m_PATH;
//DEL 		data[7]=m_pSet->m_NOTEPATH;
//DEL 		CTime now=CTime::GetCurrentTime();
//DEL 		CString temp = now.Format("%H:%M:%S");
//DEL 		data[4]=temp.GetBuffer(1);
//DEL 		m_booklist.InsertItem(&lvi);
//DEL 		for (int j=1;j<8;j++) 
//DEL 			m_booklist.SetItemText(count,j,data[j]);
//DEL 	}
//DEL 	m_pSet->Close();
//DEL 	m_pSet->m_strFilter="";
//DEL 	m_pSet->Open();
//DEL 	m_editID=0;
//DEL }

//DEL void CBOOKSView::RushListctrl()
//DEL {
//DEL 	m_pSet->Close();
//DEL 	m_pSet->m_strFilter="";
//DEL 	m_pSet->Open();
//DEL 	LV_ITEM lvi;
//DEL 	lvi.mask=LVIF_TEXT|LVIF_IMAGE|LVIF_PARAM;
//DEL 	lvi.iSubItem=0;
//DEL 	//lvi.pszText=(char *)data[0];
//DEL 	lvi.iImage = 0;
//DEL 	lvi.iItem=0;
//DEL 	for(count=0;!m_pSet->IsEOF();m_pSet->MoveNext(),count++)
//DEL 	{//插入表项
//DEL 		char str[10];
//DEL 		CString data[8];
//DEL 		itoa(m_pSet->m_ID,str,10);
//DEL 		lvi.pszText=str;
//DEL 		data[0]="";
//DEL 		data[1]=m_pSet->m_NAME;
//DEL 		data[2]=m_pSet->m_AUTHOR;
//DEL 		data[3]=m_pSet->m_PUBLISHER;
//DEL 		data[5]=m_pSet->m_SORT;
//DEL 		data[6]=m_pSet->m_PATH;
//DEL 		data[7]=m_pSet->m_NOTEPATH;
//DEL 		CTime now=CTime::GetCurrentTime();
//DEL 		CString temp = now.Format("%H:%M:%S");
//DEL 		data[4]=temp.GetBuffer(1);
//DEL 		m_booklist.InsertItem(&lvi);
//DEL 		for (int j=1;j<8;j++) 
//DEL 			m_booklist.SetItemText(count,j,data[j]);
//DEL 	}
//DEL 	m_pSet->Close();
//DEL 	m_pSet->m_strFilter="";
//DEL 	m_pSet->Open();
//DEL 	m_editID=0;
//DEL }

void CBOOKSView::OnClickTopiclist(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	
	*pResult = 0;

	POSITION pos=m_topiclist.GetFirstSelectedItemPosition();
	CString strSort="SORT='"+m_topiclist.GetItemText(m_topiclist.GetNextSelectedItem(pos),0)+"'";
	if("''"!=strSort)
	{
		m_booklist.DeleteAllItems();
		RushListctrl(m_pSet,m_booklist,strSort);
	}
}

void CBOOKSView::OnClickBooklist(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	
	*pResult = 0;
	POSITION pos=m_booklist.GetFirstSelectedItemPosition();
	m_editNAME=m_booklist.GetItemText(m_booklist.GetNextSelectedItem(pos),0);
	//m_editID=atoi(strID);
}





void CBOOKSView::RushListctrl(CRecordset *m_pRecord,CListCtrl &m_list,CString m_str)
{
	if(m_pRecord->IsOpen())
		m_pRecord->Close();
	m_pRecord->m_strFilter=m_str;
	int num=0; 
	if(m_pRecord==m_pSet)
	{
		m_pRecord->m_strSort="NAME ASC";
		num=1;
	}
	m_pRecord->Open();
	LV_ITEM lvi;
	lvi.mask=LVIF_TEXT|LVIF_IMAGE|LVIF_PARAM;
	lvi.iSubItem=0;
	//lvi.pszText=(char *)data[0];
	lvi.iImage = 0;
	lvi.iItem=0;
	for(count=0;!m_pRecord->IsEOF();m_pRecord->MoveNext(),count++)
	{	
		//插入表项
		//char str[10];
		int k=m_pRecord->GetODBCFieldCount();
		CString data[8];
		//itoa(m_pRecord->m_ID,str,10);
		
		CDBVariant   varvalue;
		
		for(int i=num;i<k;i++)
		{
			m_pRecord->GetFieldValue((short)i,varvalue);
			if(DBVT_LONG==varvalue.m_dwType)
				data[i].Format("%d",varvalue.m_lVal);
			if(DBVT_STRING==varvalue.m_dwType)
				data[i].Format("%s",varvalue.m_pstring->GetBuffer(1));
			if(DBVT_DATE==varvalue.m_dwType)
				data[i].Format("%d-%d",varvalue.m_pdate->year,varvalue.m_pdate->month);
		}
		lvi.pszText=data[num].GetBuffer(1);
		m_list.InsertItem(&lvi);
		for (int j=1;j<k;j++) 
			m_list.SetItemText(count,j,data[j+1]);
	}
	m_pRecord->Close();
	m_pRecord->m_strFilter="";
	m_pRecord->m_strSort="";
	m_pRecord->Open();
	m_editNAME="";
}

void CBOOKSView::OnButton1() 
{
	m_booklist.DeleteAllItems();
	m_topiclist.DeleteAllItems();
	RushListctrl(m_pSet,m_booklist,"");
	RushListctrl(&m_pSort,m_topiclist,"");
	// TODO: Add your control notification handler code here	
}

void CBOOKSView::OnTopicManage() 
{
	CSortMag dlg;
	//dlg.m_pView=m_pSelf;
	//((CListCtrl *)GetDlgItem(IDC_TOPICLISTDLG))->InsertColumn(0,"类别管理",LVCFMT_LEFT,100);
	//dlg.m_topiclist.InsertColumn(0,"类别管理",LVCFMT_LEFT,100);
	//RushListctrl(&m_pSort,dlg.m_topiclist,"");
	dlg.DoModal();
	m_booklist.DeleteAllItems();
	m_topiclist.DeleteAllItems();
	RushListctrl(m_pSet,m_booklist,"");
	RushListctrl(&m_pSort,m_topiclist,"");
	// TODO: Add your control notification handler code here
	
}

void CBOOKSView::OnQuery() 
{
	static CQuery dlg;
	//m_querybl=true;
	if(dlg.m_querybl)
	{
	dlg.m_pbooklist=&m_booklist;
	dlg.Create(IDD_QUERY);
	dlg.ShowWindow(SW_SHOW);
	dlg.m_querybl=false;
	}
	// TODO: Add your control notification handler code here	
}

⌨️ 快捷键说明

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