bookamenddeletedlg.cpp

来自「采用MFC+SQLSERVER 2000的图书管理系统,ODBC方式连接数据库。」· C++ 代码 · 共 421 行

CPP
421
字号
// BookAmendDeleteDlg.cpp : implementation file
//

#include "stdafx.h"
#include "library.h"
#include "BookAmendDeleteDlg.h"
#include "Bookset.h"

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

/////////////////////////////////////////////////////////////////////////////
// CBookAmendDeleteDlg dialog


CBookAmendDeleteDlg::CBookAmendDeleteDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CBookAmendDeleteDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CBookAmendDeleteDlg)
	m_id = _T("");
	m_isbn = _T("");
	m_name = _T("");
	m_author = _T("");
	m_press = _T("");
	m_theme = _T("");
	m_classify = _T("");
	m_inds = _T("");
	m_price = _T("");
	m_seriesname = _T("");
	m_describe = _T("");
	m_tiaoma = _T("");
	//}}AFX_DATA_INIT
}


void CBookAmendDeleteDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CBookAmendDeleteDlg)
	DDX_Control(pDX, IDC_LIST1, m_ctrList);
	DDX_Text(pDX, IDC_ID, m_id);
	DDX_Text(pDX, IDC_ISBN, m_isbn);
	DDX_Text(pDX, IDC_NAME, m_name);
	DDX_Text(pDX, IDC_AUTHOR, m_author);
	DDX_Text(pDX, IDC_PRESS, m_press);
	DDX_Text(pDX, IDC_THEME, m_theme);
	DDX_Text(pDX, IDC_CLASSIFY, m_classify);
	DDX_Text(pDX, IDC_INDS, m_inds);
	DDX_Text(pDX, IDC_PRICE, m_price);
	DDX_Text(pDX, IDC_SERIESNAME, m_seriesname);
	DDX_Text(pDX, IDC_DESCRIBE, m_describe);
	DDX_Text(pDX, IDC_TIAOMA, m_tiaoma);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CBookAmendDeleteDlg, CDialog)
	//{{AFX_MSG_MAP(CBookAmendDeleteDlg)
	ON_BN_CLICKED(IDC_CHECK, OnCheck)
	ON_BN_CLICKED(IDC_DELETE, OnDelete)
	ON_BN_CLICKED(IDC_AMEND, OnAmend)
	ON_WM_CTLCOLOR()
	//}}AFX_MSG_MAP

END_MESSAGE_MAP()

BOOL CBookAmendDeleteDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
   m_brush.CreateSolidBrush(RGB(0, 255, 0)); // 生成一绿色刷子 
		m_ctrList.InsertColumn(0,"图书条码");
	m_ctrList.InsertColumn(1,"isbn号");
	m_ctrList.InsertColumn(2,"书名");
	m_ctrList.InsertColumn(3,"作者");
	m_ctrList.InsertColumn(4,"出版社");
	m_ctrList.InsertColumn(5,"主题词");
	m_ctrList.InsertColumn(6,"分类号");
	m_ctrList.InsertColumn(7,"索引号");

	m_ctrList.InsertColumn(8,"定价");
    m_ctrList.InsertColumn(9,"系列书名");
	m_ctrList.InsertColumn(10,"描述");
	m_ctrList.InsertColumn(11,"出版区条码");
	
	RECT rect;
	m_ctrList.GetWindowRect(&rect);
	int Width=rect.right-rect.left;
	m_ctrList.SetColumnWidth(0,Width/12);
	m_ctrList.SetColumnWidth(1,Width/12);
	m_ctrList.SetColumnWidth(2,Width/12);
	m_ctrList.SetColumnWidth(3,Width/12);
	m_ctrList.SetColumnWidth(4,Width/12);
	m_ctrList.SetColumnWidth(5,Width/12);
	m_ctrList.SetColumnWidth(6,Width/12);
	m_ctrList.SetColumnWidth(7,Width/12);
	m_ctrList.SetColumnWidth(8,Width/12);
	m_ctrList.SetColumnWidth(9,Width/12);
	m_ctrList.SetColumnWidth(10,Width/12);
	m_ctrList.SetColumnWidth(11,Width/12);
 
	m_ctrList.SetExtendedStyle(LVS_EX_FULLROWSELECT);
	if(!m_database.IsOpen())
	{
		//连接数据源
		if(!m_database.OpenEx("DSN=MySQL;UID=sa;PWD=123;CDatabase::noOdbcDialog"))
		{
			MessageBox("连接数据库失败!","新书登记入库",MB_OK|MB_ICONINFORMATION);
			return TRUE;
		}
	}
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}
/////////////////////////////////////////////////////////////////////////////
// CBookAmendDeleteDlg message handlers

void CBookAmendDeleteDlg::OnCheck() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
   m_id.TrimLeft();
   m_id.TrimRight();
	 if(m_id.IsEmpty())
   {
	   MessageBox("请正确填入查询数据!","图书查询");
	   return ;
   }
 	CBookSet *m_pset=new CBookSet(&m_database);  
	CString strSQL;
   strSQL.Format("select * from BOOK where Bid='%s'",m_id);
   	m_pset->Open(AFX_DB_USE_DEFAULT_TYPE,strSQL);

	while(!m_pset->IsEOF())
	{
		m_pset->MoveNext();
		m_pset->GetRecordCount();
	}
	if(m_pset->GetRecordCount()==0)
	{
		MessageBox("本馆没有这本书!","图书查询");
		m_ctrList.DeleteAllItems();
		m_pset->Close();
	 
		return;
	} 
	 
		//处理表格
	m_ctrList.DeleteAllItems();
	m_pset->MoveFirst();
	char buf[50]={'\0'};
	CDBVariant var;
	int i=0;
    CString strTemp;
	while(!m_pset->IsEOF())
	{
	 	m_ctrList.InsertItem(i,buf);
        m_id=m_pset->m_Bid;
        m_ctrList.SetItemText(i,0,m_id);

		 m_isbn=m_pset->m_Bisbn;
		 
	    m_ctrList.SetItemText(i,1,m_isbn);

        m_name=m_pset->m_Bname;
	    m_ctrList.SetItemText(i,2,m_name);

		m_author=m_pset->m_Bauthor;
	
	    m_ctrList.SetItemText(i,3,m_author);

		m_press=m_pset->m_Bpress;
 
	    m_ctrList.SetItemText(i,4,m_press);

		m_theme=m_pset->m_Btheme;
	 
	    m_ctrList.SetItemText(i,5,m_theme);

		m_classify=m_pset->m_Bclassify;
	    m_ctrList.SetItemText(i,6,m_classify);


		m_inds=m_pset->m_Binds;
	    m_ctrList.SetItemText(i,7,m_inds);


		m_price=m_pset->m_Bprice;
	    m_ctrList.SetItemText(i,8,m_price);

		m_seriesname=m_pset->m_Bseriesname;
	    m_ctrList.SetItemText(i,9,m_seriesname);

		m_describe=m_pset->m_Bdescribe;
	    m_ctrList.SetItemText(i,10,m_describe);

        m_tiaoma=m_pset->m_Btiaoma;
	    m_ctrList.SetItemText(i,11,m_tiaoma);
		i++;
		m_pset->MoveNext();
		UpdateData(FALSE);    
}  
    
	m_pset->Close();
 

}

void CBookAmendDeleteDlg::OnDelete() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	m_id.TrimLeft();
	m_id.TrimRight();
	if(m_id.IsEmpty())
	{
		MessageBox("图书条码不能为空","图书删除");
		GetDlgItem(IDC_ID)->SetFocus();
		return ;
	}
	CBookSet *m_pset=new CBookSet(&m_database);  
	CString strSQL;
   strSQL.Format("select * from BOOK where Bid='%s'",m_id);
   	m_pset->Open(AFX_DB_USE_DEFAULT_TYPE,strSQL);
    
	if(m_pset->GetRecordCount()==0)
	{
		MessageBox("本馆没有这本书!","图书修改");
		m_ctrList.DeleteAllItems();
		m_pset->Close();
		return;
	}
	else
	{
	   

        int i=0;
		char buf[50]={'\0'};
		m_ctrList.DeleteAllItems();
       CString strTemp;
	 	m_ctrList.InsertItem(i,buf);
        strTemp=m_pset->m_Bid;
        m_ctrList.SetItemText(i,0,strTemp);
		strTemp=m_pset->m_Bisbn;
	    m_ctrList.SetItemText(i,1,strTemp);
        strTemp=m_pset->m_Bname;
	    m_ctrList.SetItemText(i,2,strTemp);
		strTemp=m_pset->m_Bauthor;
	    m_ctrList.SetItemText(i,3,strTemp);
		strTemp=m_pset->m_Bpress;
	    m_ctrList.SetItemText(i,4,strTemp);
		strTemp=m_pset->m_Btheme;
	    m_ctrList.SetItemText(i,5,strTemp);
		strTemp=m_pset->m_Bclassify;
	    m_ctrList.SetItemText(i,6,strTemp);
		strTemp=m_pset->m_Binds;
	    m_ctrList.SetItemText(i,7,strTemp);
		strTemp=m_pset->m_Bprice;
	    m_ctrList.SetItemText(i,8,strTemp);
		strTemp=m_pset->m_Bseriesname;
	    m_ctrList.SetItemText(i,9,strTemp);
		strTemp=m_pset->m_Bdescribe;
	    m_ctrList.SetItemText(i,10,strTemp);
        strTemp=m_pset->m_Btiaoma;
	    m_ctrList.SetItemText(i,11,strTemp);
       
		m_pset->Delete();
		MessageBox("删除成功!","图书删除");
	 
		m_id.Empty();
		m_isbn.Empty();
		m_name.Empty();
		m_author.Empty();
		m_press.Empty();
		m_theme.Empty();
		m_classify.Empty();
		m_inds.Empty();
		m_price.Empty();
		m_seriesname.Empty();
		m_describe.Empty();
	    m_tiaoma.Empty();
		UpdateData(false);
		m_pset->Close();

	}

}



void CBookAmendDeleteDlg::OnAmend() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	m_id.TrimLeft();
	m_id.TrimRight();
	if(m_id.IsEmpty())
	{
		MessageBox("图书条码不能为空","图书修改");
		GetDlgItem(IDC_ID)->SetFocus();
		return ;

	}

	CBookSet *m_pset=new CBookSet(&m_database);  
	CString strSQL;
   strSQL.Format("select * from BOOK where Bid='%s'",m_id);
   	m_pset->Open(AFX_DB_USE_DEFAULT_TYPE,strSQL);
    
	if(m_pset->GetRecordCount()==0)
	{
		MessageBox("本馆没有这本书!","图书修改");
		m_ctrList.DeleteAllItems();
		m_pset->Close();
	 
		return;
	}

	
   	m_pset->Edit();
	m_pset->m_Bisbn=m_isbn;
	m_pset->m_Bname=m_name;
	m_pset->m_Bauthor=m_author;
	m_pset->m_Bpress=m_press;
	m_pset->m_Btheme=m_theme;
	m_pset->m_Bclassify=m_classify;
	m_pset->m_Binds=m_inds;
	m_pset->m_Bprice=m_price;
	m_pset->m_Bseriesname=m_seriesname;
	m_pset->m_Bdescribe=m_describe;
	m_pset->m_Btiaoma=m_tiaoma;
    
	m_pset->Update();
	m_pset->Requery();
	m_pset->Close();  

 	MessageBox("修改成功","图书修改");
	
        char buf[50]={'\0'};
		int i=0;
       
		m_ctrList.DeleteAllItems();
	 	m_ctrList.InsertItem(i,buf);
	
		m_ctrList.SetItemText(i,0,m_id);
       
		m_ctrList.SetItemText(i,1,m_isbn);
	 
		m_ctrList.SetItemText(i,2,m_name);

		m_ctrList.SetItemText(i,3,m_author);

		m_ctrList.SetItemText(i,4,m_press);

		m_ctrList.SetItemText(i,5,m_theme);
	 
		m_ctrList.SetItemText(i,6,m_classify);
        m_ctrList.SetItemText(i,7,m_classify);
		 
		m_ctrList.SetItemText(i,8,m_price);

		m_ctrList.SetItemText(i,9,m_seriesname);

		m_ctrList.SetItemText(i,10,m_describe);
        m_ctrList.SetItemText(i,11,m_tiaoma);

		m_id.Empty();
		m_isbn.Empty();
		m_name.Empty();
		m_author.Empty();
		m_press.Empty();
		m_theme.Empty();
		m_classify.Empty();
		m_inds.Empty();
		m_price.Empty();
		m_seriesname.Empty();
		m_describe.Empty();
	    m_tiaoma.Empty();
		UpdateData(false);
      
}

void CBookAmendDeleteDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
	if(m_database.IsOpen())
		m_database.Close();
	CDialog::OnCancel();
}

HBRUSH CBookAmendDeleteDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
	
	// TODO: Change any attributes of the DC here
    	if(nCtlColor ==CTLCOLOR_DLG)
        return m_brush; //返加绿色刷子
	if(nCtlColor==CTLCOLOR_EDIT) 
	{
        pDC->SetTextColor(RGB(0,0,255));     
		pDC->SetBkMode(TRANSPARENT); 
	}
    if(nCtlColor==CTLCOLOR_STATIC) 
	{   
		pDC->SetBkColor(RGB(0, 255, 0));
        pDC->SetTextColor(RGB(255,0,0)); 
		pDC->SetBkMode(TRANSPARENT); 
	}
	if(nCtlColor==  CTLCOLOR_LISTBOX) 
	{
       pDC->SetTextColor(RGB(0,0,255));     
		//pDC->SetBkMode(TRANSPARENT); 
	}
	// TODO: Return a different brush if the default is not desired
	return hbr;
}

⌨️ 快捷键说明

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