📄 bookquerydlg.cpp
字号:
// BookQueryDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "library.h"
#include "book.h"
#include "BookQueryDlg.h"
// BookQueryDlg 对话框
IMPLEMENT_DYNAMIC(BookQueryDlg, CDialog)
BookQueryDlg::BookQueryDlg(CWnd* pParent /*=NULL*/)
: CDialog(BookQueryDlg::IDD, pParent)
, m_checkyear(FALSE)
, m_checkprice(FALSE)
, m_BookName(_T(""))
, m_BookPress(_T(""))
, m_BookCategory(_T(""))
, m_BookAuthor(_T(""))
, m_BookYear_Low(_T(""))
, m_BookYear_High(_T(""))
, m_BookPrice_Low(_T(""))
, m_BookPrice_High(_T(""))
{
}
BookQueryDlg::~BookQueryDlg()
{
}
void BookQueryDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Check(pDX, IDC_CHECK1, m_checkyear);
DDX_Check(pDX, IDC_CHECK2, m_checkprice);
DDX_Control(pDX, IDC_LIST_QUERY_RESULT, m_QueryResult);
DDX_Text(pDX, IDC_BookName, m_BookName);
DDX_Text(pDX, IDC_Press, m_BookPress);
DDX_Text(pDX, IDC_Category, m_BookCategory);
DDX_Text(pDX, IDC_Author, m_BookAuthor);
DDX_Text(pDX, IDC_LowYear, m_BookYear_Low);
DDX_Text(pDX, IDC_HighYear, m_BookYear_High);
DDX_Text(pDX, IDC_LowPrice, m_BookPrice_Low);
DDX_Text(pDX, IDC_HighPrice, m_BookPrice_High);
}
BEGIN_MESSAGE_MAP(BookQueryDlg, CDialog)
ON_BN_CLICKED(IDOK, &BookQueryDlg::OnBnClickedOk)
END_MESSAGE_MAP()
// BookQueryDlg 消息处理程序
BOOL BookQueryDlg::OnInitDialog()
{
BOOL res = CDialog::OnInitDialog();
DWORD dwStyle, ExStyle;
dwStyle = ::GetWindowLong(m_QueryResult.m_hWnd,GWL_STYLE);
SetWindowLong(m_QueryResult.m_hWnd,GWL_STYLE,dwStyle|LVS_REPORT);
ExStyle = m_QueryResult.GetExtendedStyle();
m_QueryResult.SetExtendedStyle(ExStyle|LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
m_QueryResult.InsertColumn(0, _T("书号"), LVCFMT_LEFT, 60, -1);
m_QueryResult.InsertColumn(1, _T("分类"), LVCFMT_LEFT, 80, -1);
m_QueryResult.InsertColumn(2, _T("书名"), LVCFMT_LEFT, 150, -1);
m_QueryResult.InsertColumn(3, _T("出版社"), LVCFMT_LEFT, 150, -1);
m_QueryResult.InsertColumn(4, _T("出版年份"), LVCFMT_LEFT, 80, -1);
m_QueryResult.InsertColumn(5, _T("作者"), LVCFMT_LEFT, 80, -1);
m_QueryResult.InsertColumn(6, _T("价格"), LVCFMT_LEFT, 60, -1);
m_QueryResult.InsertColumn(7, _T("总藏书量"), LVCFMT_LEFT, 80, -1);
m_QueryResult.InsertColumn(8, _T("库存量"), LVCFMT_LEFT, 80, -1);
return res;
}
void BookQueryDlg::OnBnClickedOk()
{
// TODO: 在此添加控件通知处理程序代码
BookQueryDlg BookQueryDlg;
CString QueryCondition[6]={(CString)"",(CString)"",(CString)"",(CString)"",(CString)"",(CString)""};
CString strFilter=(CString)("");
CString tmp;
int Item;
this->UpdateData();
if(this->m_BookName!="")
QueryCondition[0]=(CString)("title like '%") + this->m_BookName + (CString)("%'");
if(this->m_BookPress!="")
QueryCondition[1]=(CString)("press like '%")+ this->m_BookPress+(CString)("%'");
if(this->m_BookCategory!="")
QueryCondition[2]=(CString)("category like'%") +this->m_BookCategory+(CString)("%'");
if(this->m_BookAuthor!="")
QueryCondition[3]=(CString)("author like'%")+this->m_BookAuthor+(CString)("%'");
//区间查询年份
//if(this->m_BookYear_Low!="")
//{
if(this->m_checkyear)
{
//上限为控
if(this->m_BookYear_High==""&&this->m_BookYear_Low!="")
QueryCondition[4]=(CString)("year >=")+this->m_BookYear_Low;
else if(this->m_BookYear_High!=""&&this->m_BookYear_Low=="")
QueryCondition[4]=(CString)("year <=")+this->m_BookYear_High;
else if(this->m_BookYear_High!=""&&this->m_BookYear_Low!="")
QueryCondition[4]=(CString)("year <=")+this->m_BookYear_High+(CString)("and year >=")+this->m_BookYear_Low;
else
QueryCondition[4]="";
}
else if(this->m_BookYear_Low!="")
QueryCondition[4]=(CString)("year =")+this->m_BookYear_Low;
//}
//if(this->m_BookPrice_Low!="")
//{
if(this->m_checkprice)
{
if(this->m_BookPrice_High==""&&this->m_BookPrice_Low!="")
QueryCondition[5]=(CString)("price >=")+this->m_BookPrice_Low;
else if(this->m_BookPrice_High!=""&&this->m_BookPrice_Low=="")
QueryCondition[5]=(CString)("price <=")+this->m_BookPrice_High;
else if(this->m_BookPrice_High!=""&&this->m_BookPrice_Low!="")
QueryCondition[5]=(CString)("price <=")+this->m_BookPrice_High+(CString)("and year >=")+this->m_BookPrice_Low;
else
QueryCondition[5]="";
}
else if(this->m_BookPrice_Low!="")
QueryCondition[5]=(CString)("price =")+this->m_BookPrice_Low;
//}
bool flag = true; // 标记是否是第一个查询语句
// 将各个查询语句连接起来
for (int i = 0; i < 6; i++)
{
if (QueryCondition[i] == "") continue;
if (flag)
{
strFilter += QueryCondition[i];
flag = false;
}
else
strFilter += (CString)" and " + QueryCondition[i];
}
if(!m_bookSet.IsOpen())
m_bookSet.Open();
m_bookSet.m_strFilter=strFilter;
m_bookSet.Requery();
Display(m_bookSet,m_QueryResult);
m_bookSet.Close();
}
void BookQueryDlg::Display(Cbook &BookRS, CListCtrl& ListCtrl)
{
if (!BookRS.IsOpen())
BookRS.Open();
BookRS.m_strSort = "bno desc";
BookRS.Requery();
CString tmp;
int Item;
// 删除原来的项目,显示查询结果
ListCtrl.DeleteAllItems();
while (!BookRS.IsEOF())
{
Item = ListCtrl.InsertItem(0,(LPCTSTR)(CString)BookRS.m_bno);
ListCtrl.SetItemText(Item, 1, (LPCTSTR)(CString)BookRS.m_category);
ListCtrl.SetItemText(Item, 2, (LPCTSTR)(CString)BookRS.m_title);
ListCtrl.SetItemText(Item, 3, (LPCTSTR)(CString)BookRS.m_press);
tmp.Format(_T("%d"), BookRS.m_year);
ListCtrl.SetItemText(Item, 4, (LPCTSTR)tmp);
ListCtrl.SetItemText(Item, 5, (LPCTSTR)(CString)BookRS.m_author);
tmp.Format(_T("%.2lf"), BookRS.m_price);
ListCtrl.SetItemText(Item, 6, (LPCTSTR)tmp);
tmp.Format(_T("%d"), BookRS.m_total);
ListCtrl.SetItemText(Item, 7, (LPCTSTR)tmp);
tmp.Format(_T("%d"), BookRS.m_stock);
ListCtrl.SetItemText(Item, 8, (LPCTSTR)tmp);
BookRS.MoveNext();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -