?? librarymdlg.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 + -