📄 rentdlg.cpp
字号:
// rentDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "rent.h"
#include "rentDlg.h"
#include "RentDVDDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// 用于应用程序“关于”菜单项的 CAboutDlg 对话框
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// 对话框数据
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 实现
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()
// CrentDlg 对话框
CrentDlg::CrentDlg(CWnd* pParent /*=NULL*/)
: CDialog(CrentDlg::IDD, pParent)
, m_name(_T(""))
, m_check_Date(FALSE)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CrentDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_LIST1, m_rentinfoList);
DDX_Text(pDX, IDC_EDIT1, m_name);
DDX_Control(pDX, IDC_COMBO2, m_comboDVD);
DDX_Control(pDX, IDC_EDIT1, m_editName);
DDX_Control(pDX, IDC_DATETIMEPICKER1, m_DateFrom);
DDX_Control(pDX, IDC_DATETIMEPICKER2, m_DateTo);
DDX_Check(pDX, IDC_CHECK3, m_check_Date);
}
BEGIN_MESSAGE_MAP(CrentDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_EN_CHANGE(IDC_EDIT1, &CrentDlg::OnEnChangeEdit1)
ON_BN_CLICKED(IDC_BUTTON1, &CrentDlg::wm_lbuttondown)
ON_BN_CLICKED(IDC_CHECK3, &CrentDlg::OnBnClickedCheck3)
ON_BN_CLICKED(IDOK, &CrentDlg::OnButtonRent)
// ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST1, &CrentDlg::OnLvnItemchangedList1)
//ON_BN_CLICKED(IDCANCEL, &CrentDlg::OnButtonDelete1)
ON_BN_CLICKED(IDCDELETE, &CrentDlg::OnBnClickedCdelete)
END_MESSAGE_MAP()
// CrentDlg 消息处理程序
BOOL CrentDlg::OnInitDialog()
{ CDialog::OnInitDialog();
// 将“关于...”菜单项添加到系统菜单中。
// IDM_ABOUTBOX 必须在系统命令范围内。
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
// TODO: 在此添加额外的初始化代码
m_rentinfoList.InsertColumn(0,"序号",LVCFMT_LEFT,40,-1);
m_rentinfoList.InsertColumn(1,"车牌号",LVCFMT_LEFT,40,-1);
m_rentinfoList.InsertColumn(2,"车主姓名",LVCFMT_LEFT,160,-1);
m_rentinfoList.InsertColumn(3,"收费站名称",LVCFMT_LEFT,200,-1);
m_rentinfoList.InsertColumn(4,"收费日期",LVCFMT_LEFT,160,-1);
m_rentinfoList.InsertColumn(5,"收费标准",LVCFMT_LEFT,160,-1);
//设置list的行被选中时是全行选中
m_rentinfoList.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
//日期显示格式
m_DateFrom.SetFormat("yyyyMMdd");
m_DateTo.SetFormat("yyyyMMdd");
//日期查询初始时是不选中的,设置起始日期的控件不可用
m_check_Date=FALSE;
m_DateFrom.EnableWindow(FALSE);
m_DateTo.EnableWindow(FALSE);
//选择日期条件来查询,添加IDC_CHECK_DATE(Check Box)控件的响应函数,选中该复选框时,使用日期来查询,使IDC_DATETIMEPICKER_FROM和....TO控件都可使用
//初始化数据库连接
HRESULT hr;
try
{
//实例化连接对象
hr=m_pConnection.CreateInstance(_uuidof(Connection));
if(SUCCEEDED(hr))
//设置连接串属性为UDL文件
{//m_pConnection->ConnectionString="File Name=my_data1.udl";
//m_pConnection->ConnectionString="Provider=SQLOLEDB;DataSource=local;InitialCatalog=影碟出租系统;User ID=sa;Password=123456;";
//_bstr_t strConnect = "Provider=SQLOLEDB; Server=(local); Database=影碟出租系统; uid=sa; pwd=123456;";
m_pConnection->ConnectionTimeout=20;
m_pConnection->Open("Provider=SQLOLEDB.1;Persist Security Info=True;User ID=sa1;Password=123456;Initial Catalog=车牌识别系统;Data Source=(local);","","",adModeUnknown);
//设置等待连接打开时间为20s
// hr=m_pConnection->Open("","","",adModeReadWrite);//adConnectUnspecified
if(FAILED(hr))
{
AfxMessageBox("open fail!");
return TRUE;
}
}
else
{AfxMessageBox("createinstance of Connection fail!");
return TRUE;
}
}
catch(_com_error e)
{//给出异常消息
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
AfxMessageBox(bstrSource+bstrDescription);
CString errormessage;
errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
// Connection.Close();
return TRUE;
}
//获得本月的出租记录,并显示在List控件中
InitListCtr();
InitComboCtr();
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
void CrentDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。
void CrentDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 使图标在工作矩形中居中
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;
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
//当用户拖动最小化窗口时系统调用此函数取得光标显示。
//
HCURSOR CrentDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CrentDlg::OnEnChangeEdit1()
{
// TODO: 如果该控件是 RICHEDIT 控件,则它将不会
// 发送该通知,除非重写 CDialog::OnInitDialog()
// 函数并调用 CRichEditCtrl().SetEventMask(),
// 同时将 ENM_CHANGE 标志“或”运算到掩码中。
// TODO: 在此添加控件通知处理程序代码
}
void CrentDlg::OnCheckDate()
{
if(m_check_Date==FALSE)
{m_check_Date=TRUE;
m_DateFrom.EnableWindow(TRUE);
m_DateTo.EnableWindow(TRUE);
}
else
{m_check_Date=FALSE;
m_DateFrom.EnableWindow(FALSE);
m_DateTo.EnableWindow(FALSE);
}
}
void CrentDlg::InitListCtr()
{ HRESULT hr;
_RecordsetPtr pRentRecordset;
hr=pRentRecordset.CreateInstance(__uuidof(Recordset));
if(FAILED(hr))
{
AfxMessageBox("createinstance of Recordset failed!\n can`t initiate List control!");
return;
}
CString strSql;
_variant_t var;
CString strValue;
int curItem=0;
strSql="select 序号,车牌号, 车主姓名,收费站名称,收费日期,收费标准 from 收费信息,车主信息,收费站 where 收费信息.收费站ID=收费站.收费站ID and 收费信息.车主ID=车主信息.车主ID";
try
{
hr=pRentRecordset->Open(_variant_t(strSql),
m_pConnection.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText);
if(SUCCEEDED(hr))
{
while(!pRentRecordset->adoEOF)
{
var = pRentRecordset->GetCollect("序号");
if(var.vt != VT_NULL)
strValue = (LPCSTR)_bstr_t(var);
m_rentinfoList.InsertItem(curItem,strValue);
var = pRentRecordset->GetCollect("车牌号");
if(var.vt != VT_NULL)
strValue = (LPCSTR)_bstr_t(var);
m_rentinfoList.SetItemText(curItem,1,strValue);
var = pRentRecordset->GetCollect("车主姓名");
if(var.vt != VT_NULL)
strValue = (LPCSTR)_bstr_t(var);
m_rentinfoList.SetItemText(curItem,2,strValue);
var = pRentRecordset->GetCollect("收费站名称");
if(var.vt != VT_NULL)
strValue = (LPCSTR)_bstr_t(var);
//需要根据该DVDID查询tbDVDInfo,获得DVDname,插入list控件中
//m_rentInfoList.SetItemText(curItem,2,QueryDVDName(strValue));
//使用调用存储过程函数QueryDVDNameSP
m_rentinfoList.SetItemText(curItem,3,strValue);
var = pRentRecordset->GetCollect("收费日期");
if(var.vt != VT_NULL)
strValue = (LPCSTR)_bstr_t(var);
m_rentinfoList.SetItemText(curItem,4,strValue);
var = pRentRecordset->GetCollect("收费标准");
if(var.vt != VT_NULL)
strValue = (LPCSTR)_bstr_t(var);
m_rentinfoList.SetItemText(curItem,5,strValue);
pRentRecordset->MoveNext();
curItem++;
}
}
else
{
AfxMessageBox("Open recordset fail!");
}
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
return;
}
pRentRecordset->Close();
pRentRecordset=NULL;
}
CString CrentDlg::QueryDVDName(CString DVDID)
{
_RecordsetPtr pDVDNameRecordset;
pDVDNameRecordset.CreateInstance(__uuidof(Recordset));
CString strValue;
_bstr_t vSQL;
vSQL="select DVDNAME from tbdvdinfo where DVDID="+DVDID;
try
{
pDVDNameRecordset->Open(vSQL,
m_pConnection.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText);
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
strValue="";
return strValue;
}
_variant_t var;
var = pDVDNameRecordset->GetCollect("DVDNAME");
if(var.vt != VT_NULL)
strValue = (LPCSTR)_bstr_t(var);
pDVDNameRecordset->Close();
pDVDNameRecordset=NULL;
return strValue;
}
void CrentDlg::InitComboCtr()
{
_RecordsetPtr pDVDNameRecordset;
pDVDNameRecordset.CreateInstance(__uuidof(Recordset));
try
{
pDVDNameRecordset->Open("SELECT 车牌号 FROM 车主信息",
m_pConnection.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText);
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
return;
}
_variant_t var;
CString strValue;
//可以选择空,表示不使用该查询选项
m_comboDVD.AddString("");
try{
while(!pDVDNameRecordset->adoEOF)
{
var=pDVDNameRecordset->GetCollect("车牌号");
if(var.vt != VT_NULL)
strValue = (LPCSTR)_bstr_t(var);
m_comboDVD.AddString(strValue);
pDVDNameRecordset->MoveNext();
}
pDVDNameRecordset->Close();
pDVDNameRecordset=NULL;
}
catch(_com_error *e)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -