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

📄 librarymdlg.cpp

📁 图书馆管理系统是典型的信息管理系统(MIS),其开发主要包括后台数据库的建立和维护以及前端应用程序的开发两个方面。对于前者要求建立起数据一致性和完整性强.数据安全性好的库。而对于后者则要求应用程序功能
💻 CPP
字号:
// LibraryMDlg.cpp : implementation file
//

#include "stdafx.h"
#include "LibraryM.h"
#include "LibraryMDlg.h"
#include "BookTypeListDlg.h"
#include "ReaderTypeListDlg.h"
#include "ReaderList.h"
#include "BorrowListDlg.h"
#include "SearchDlg.h"
#include "ReturnBookDlg.h"
#include "InsertBookDlg.h"



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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLibraryMDlg dialog

CLibraryMDlg::CLibraryMDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CLibraryMDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CLibraryMDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CLibraryMDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CLibraryMDlg)
	DDX_Control(pDX, IDC_BOOKINFO_LIST, m_BookList_Main);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CLibraryMDlg, CDialog)
	//{{AFX_MSG_MAP(CLibraryMDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BOOKTYPELIST_BTN, OnBooktypelistBtn)
	ON_BN_CLICKED(IDC_READERTYPELIST_BTN, OnReadertypelistBtn)
	ON_BN_CLICKED(IDC_READERLIST_BTN, OnReaderlistBtn)
	ON_BN_CLICKED(IDC_BORROWLIST_BTN, OnBorrowlistBtn)
	ON_BN_CLICKED(IDC_RETURN_BTN, OnReturnBtn)
	ON_BN_CLICKED(IDC_SEARCH_BTN, OnSearchBtn)
	ON_BN_CLICKED(IDC_INSERTBOOK_BTN, OnInsertbookBtn)
	ON_BN_CLICKED(IDC_MAIN_DELETE_BTN, OnMainDeleteBtn)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLibraryMDlg message handlers

BOOL CLibraryMDlg::OnInitDialog()  //初始化主界面
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);
	

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	

	//初始化数据库连接
	HRESULT result;
	try
	{
		//实例化连接对象
		result=m_pConnection.CreateInstance(_uuidof(Connection));
		if(SUCCEEDED(result))
		{
			//设置连接属性为UDL文件
			m_pConnection->ConnectionString="File Name=LIBRARYM.udl";
			//设置等待连接打开的时间为20s
			m_pConnection->ConnectionTimeout=20;
			result=m_pConnection->Open("","","",adConnectUnspecified);

			if(FAILED(result))
			{
				AfxMessageBox("open failed");
				return TRUE;
			}
		}
		else
		{
			AfxMessageBox("createinstance of connection failed!");
			return TRUE;
		}
	}
	catch(_com_error e)
	{
		//输出异常信息
		_bstr_t bstrSource(e.Source());
		_bstr_t bstrDescription(e.Description());
		AfxMessageBox(bstrSource+bstrDescription);
		return TRUE;
	}

	// TODO: Add extra initialization here
	//初始化藏书的信息列表
	m_BookList_Main.InsertColumn(0,"ID",LVCFMT_LEFT,100);
	m_BookList_Main.InsertColumn(1,"BookName",LVCFMT_CENTER,100);
	m_BookList_Main.InsertColumn(2,"TypeID",LVCFMT_CENTER,100);
	m_BookList_Main.InsertColumn(3,"Author",LVCFMT_CENTER,100);
	m_BookList_Main.InsertColumn(4,"Press",LVCFMT_CENTER,100);
	m_BookList_Main.InsertColumn(5,"PrintDate",LVCFMT_CENTER,100);
	m_BookList_Main.InsertColumn(6,"Price",LVCFMT_CENTER,70);
	m_BookList_Main.InsertColumn(7,"Page",LVCFMT_CENTER,70);
	m_BookList_Main.InsertColumn(8,"KeyWord",LVCFMT_CENTER,100);
	m_BookList_Main.InsertColumn(9,"Available",LVCFMT_CENTER,80);
	m_BookList_Main.InsertColumn(10,"PS",LVCFMT_CENTER,100);
	//选中列表的一行
	m_BookList_Main.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);

	InitListCtrl();
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CLibraryMDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CLibraryMDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CLibraryMDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CLibraryMDlg::OnBooktypelistBtn()   //打开书的类型列表
{
	CBookTypeListDlg dlg;
	if(dlg.DoModal()==IDOK)
	{
		return;
	}
}

void CLibraryMDlg::OnReadertypelistBtn()  //打开读者类型列表按钮
{
	CReaderTypeListDlg dlg;
	if(dlg.DoModal()==IDOK)
	{
		return;
	}
}

void CLibraryMDlg::OnReaderlistBtn()  //打开读者信息列表按钮
{
	CReaderList dlg;
	if(dlg.DoModal()==IDOK)
	{
		return;
	}
}

void CLibraryMDlg::OnBorrowlistBtn()   //打开已借书信息列表
{
	CBorrowListDlg dlg;
	if(dlg.DoModal()==IDOK)
	{
		return;
	}
}

void CLibraryMDlg::OnReturnBtn()     //打开还书窗口
{
	CReturnBookDlg dlg;
	if(dlg.DoModal()==IDOK)
	{
		m_BookList_Main.DeleteAllItems();  //更新书列表
		InitListCtrl();
		return;
	}
}

void CLibraryMDlg::OnSearchBtn()    //打开搜索书目列表
{
	CSearchDlg dlg;
	if(dlg.DoModal()==IDOK)
	{
		m_BookList_Main.DeleteAllItems(); //更新书列表
		InitListCtrl();
		return;
	}
}

void CLibraryMDlg::OnInsertbookBtn()     //打开插入新书信息窗口
{
	CInsertBookDlg dlg;
	if(dlg.DoModal()==IDOK)
	{
		m_BookList_Main.DeleteAllItems();  //更新
		InitListCtrl();
	}
		return;
}

void CLibraryMDlg::InitListCtrl()     //图书列表信息控件
{
	HRESULT hr;
	//定义记录集指针
	_RecordsetPtr pBookListset;
	//实例化记录指针
	hr=pBookListset.CreateInstance(_uuidof(Recordset));
	//判断实例是否创建成功
	if(FAILED(hr))
	{
		AfxMessageBox("createinstance of recordset failed!\ncan't initiate the list control!");
		return;
	}

	//定义字符串存储SQL语句
	CString strSql;
	//定义_variant_t变量存储从数据库读取的字段
	_variant_t getvar;
	//定义存储字符串_variant_t 变量中的字符串
	CString strValue;
	//list控件中记录的序号
	int curItem=0;
	//初始化SQL语句字符串,获得BookInfo表中的数据
	strSql="select * from BookInfo";
	try 
	{
		//利用open函数执行SQL命令,获得查询结果记录集
		//需要把CString类型转换为_variant_t类型
		hr=pBookListset->Open(_variant_t(strSql),m_pConnection.GetInterfacePtr(),
			adOpenDynamic,adLockOptimistic,adCmdText);
		if(SUCCEEDED(hr))
		{
			//判断记录集是否到末尾,对每条记录,把字段值插入list control的每一行中
			while(!pBookListset->adoEOF)
			{
				//获得记录集中当前的第一字段"BookID"的值
				getvar=pBookListset->GetCollect("BookID");
				if(getvar.vt!=VT_NULL)
				{
					strValue=(LPCSTR)_bstr_t(getvar);
				}
				else 
				{	strValue=" ";}
				m_BookList_Main.InsertItem(curItem,strValue);
				
				//获得记录集中BookName的值
				getvar=pBookListset->GetCollect("BookName");
				if(getvar.vt!=VT_NULL)
				{
					strValue=(LPCSTR)_bstr_t(getvar);
				}
				else
				{
					strValue=" ";
				}
				m_BookList_Main.SetItemText(curItem,1,strValue);

				//获得记录集中BookTypeID的值
				getvar=pBookListset->GetCollect("BookTypeID");
				if(getvar.vt!=VT_NULL)
				{
					strValue=(LPCSTR)_bstr_t(getvar);
				}
				else
				{
					strValue=" ";
				}
				m_BookList_Main.SetItemText(curItem,2,strValue);

				//获得记录集中Author的值
				getvar=pBookListset->GetCollect("Author");
				if(getvar.vt!=VT_NULL)
				{
					strValue=(LPCSTR)_bstr_t(getvar);
				}
				else
				{
					strValue=" ";
				}
				m_BookList_Main.SetItemText(curItem,3,strValue);

				//获得记录集中Press的值
				getvar=pBookListset->GetCollect("Press");
				if(getvar.vt!=VT_NULL)
				{
					strValue=(LPCSTR)_bstr_t(getvar);
				}
				else
				{
					strValue=" ";
				}
				m_BookList_Main.SetItemText(curItem,4,strValue);

				//获得记录集中PrintDate的值
				getvar=pBookListset->GetCollect("PrintDate");
				if(getvar.vt!=VT_NULL)
				{
					strValue=(LPCSTR)_bstr_t(getvar);
				}
				else
				{
					strValue=" ";
				}
				m_BookList_Main.SetItemText(curItem,5,strValue);

				//获得记录集中Price的值
				getvar=pBookListset->GetCollect("Price");
				if(getvar.vt!=VT_NULL)
				{
					strValue=(LPCSTR)_bstr_t(getvar);
				}
				else
				{
					strValue=" ";
				}
				m_BookList_Main.SetItemText(curItem,6,strValue);

				//获得记录集中Page的值
				getvar=pBookListset->GetCollect("Page");
				if(getvar.vt!=VT_NULL)
				{
					strValue=(LPCSTR)_bstr_t(getvar);
				}
				else
				{
					strValue=" ";
				}
				m_BookList_Main.SetItemText(curItem,7,strValue);

				//获得记录集中Keyword的值
				getvar=pBookListset->GetCollect("Keyword");
				if(getvar.vt!=VT_NULL)
				{
					strValue=(LPCSTR)_bstr_t(getvar);
				}
				else
				{
					strValue=" ";
				}
				m_BookList_Main.SetItemText(curItem,8,strValue);

				//获得记录集中Available的值
				getvar=pBookListset->GetCollect("Available");
				if(getvar.vt!=VT_NULL)
				{
					strValue=(LPCSTR)_bstr_t(getvar);
				}
				else
				{
					strValue=" ";
				}
				m_BookList_Main.SetItemText(curItem,9,strValue);

				//获得记录集中PS的值
				getvar=pBookListset->GetCollect("PS");
				if(getvar.vt!=VT_NULL)
				{
					strValue=(LPCSTR)_bstr_t(getvar);
				}
				else
				{
					strValue=" ";
				}
				m_BookList_Main.SetItemText(curItem,10,strValue);
				
				//移动当前记录到下一条记录
				pBookListset->MoveNext();
				curItem++;
				
			}
		}
		else
		{
			AfxMessageBox("open recordset failed!");
		}
	}
	catch(_com_error *e)  //捕捉不能打开数据库发生的错误
	{
		AfxMessageBox(e->ErrorMessage()); //显示错误
		return ;
	}
	pBookListset->Close();
	pBookListset=NULL;
}

void CLibraryMDlg::OnMainDeleteBtn()     //删除图书记录
{
	//从选中的列中得到数据将数据从数据库和列表里删除
	int select=m_BookList_Main.GetSelectionMark();
	if(select>=0 )
	{
		if(AfxMessageBox("are you sure to delete this record ?",MB_YESNO)!=IDNO)
		{
			//得到要删除元组的主码
			CString strID=m_BookList_Main.GetItemText(select,0);
			//先删除数据库里面的数据再删除控件上显示的数据	

			HRESULT hr;
			CString delsqlbook="delete from BookInfo where ";

			_RecordsetPtr pset;
			hr=pset.CreateInstance(_uuidof(Recordset));
			if(FAILED(hr))
			{
				AfxMessageBox("failed to createinstance !");
				return ;
			}
			try 
			{
				CString temp3;
				temp3.Format("BookID='%s'",strID);
				delsqlbook+=temp3;

				pset->Open(_variant_t(delsqlbook),m_pConnection.GetInterfacePtr(),
					adOpenDynamic,adLockOptimistic,adCmdText);

				AfxMessageBox("successfully deleted the record!");
			}
			catch(_com_error *e)
			{
				AfxMessageBox(e->ErrorMessage());
				return ;
			}
			m_BookList_Main.DeleteItem(select); //从控件上删除
		}
		else
			return;
	}
	else if(select<0)
		AfxMessageBox("no record has been selected!");
}

⌨️ 快捷键说明

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