?? rentdlg.cpp
字號:
// rentDlg.cpp : 實(shí)現(xiàn)文件
//
#include "stdafx.h"
#include "rent.h"
#include "rentDlg.h"
#include "RentDVDDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// 用于應(yīng)用程序“關(guān)于”菜單項的 CAboutDlg 對話框
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// 對話框數(shù)據(jù)
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 實(shí)現(xiàn)
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();
// 將“關(guān)于...”菜單項添加到系統(tǒng)菜單中。
// IDM_ABOUTBOX 必須在系統(tǒng)命令范圍內(nèi)。
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);
}
}
// 設(shè)置此對話框的圖標(biāo)。當(dāng)應(yīng)用程序主窗口不是對話框時,框架將自動
// 執(zhí)行此操作
SetIcon(m_hIcon, TRUE); // 設(shè)置大圖標(biāo)
SetIcon(m_hIcon, FALSE); // 設(shè)置小圖標(biāo)
// 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,"收費(fèi)站名稱",LVCFMT_LEFT,200,-1);
m_rentinfoList.InsertColumn(4,"收費(fèi)日期",LVCFMT_LEFT,160,-1);
m_rentinfoList.InsertColumn(5,"收費(fèi)標(biāo)準(zhǔn)",LVCFMT_LEFT,160,-1);
//設(shè)置list的行被選中時是全行選中
m_rentinfoList.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
//日期顯示格式
m_DateFrom.SetFormat("yyyyMMdd");
m_DateTo.SetFormat("yyyyMMdd");
//日期查詢初始時是不選中的,設(shè)置起始日期的控件不可用
m_check_Date=FALSE;
m_DateFrom.EnableWindow(FALSE);
m_DateTo.EnableWindow(FALSE);
//選擇日期條件來查詢,添加IDC_CHECK_DATE(Check Box)控件的響應(yīng)函數(shù),選中該復(fù)選框時,使用日期來查詢,使IDC_DATETIMEPICKER_FROM和....TO控件都可使用
//初始化數(shù)據(jù)庫連接
HRESULT hr;
try
{
//實(shí)例化連接對象
hr=m_pConnection.CreateInstance(_uuidof(Connection));
if(SUCCEEDED(hr))
//設(shè)置連接串屬性為UDL文件
{//m_pConnection->ConnectionString="File Name=my_data1.udl";
//m_pConnection->ConnectionString="Provider=SQLOLEDB;DataSource=local;InitialCatalog=影碟出租系統(tǒng);User ID=sa;Password=123456;";
//_bstr_t strConnect = "Provider=SQLOLEDB; Server=(local); Database=影碟出租系統(tǒng); 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=車牌識別系統(tǒng);Data Source=(local);","","",adModeUnknown);
//設(shè)置等待連接打開時間為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("連接數(shù)據(jù)庫失敗!\r\n錯誤信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///顯示錯誤信息
// Connection.Close();
return TRUE;
}
//獲得本月的出租記錄,并顯示在List控件中
InitListCtr();
InitComboCtr();
return TRUE; // 除非將焦點(diǎn)設(shè)置到控件,否則返回 TRUE
}
void CrentDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// 如果向?qū)υ捒蛱砑幼钚』粹o,則需要下面的代碼
// 來繪制該圖標(biāo)。對于使用文檔/視圖模型的 MFC 應(yīng)用程序,
// 這將由框架自動完成。
void CrentDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于繪制的設(shè)備上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 使圖標(biāo)在工作矩形中居中
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;
// 繪制圖標(biāo)
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
//當(dāng)用戶拖動最小化窗口時系統(tǒng)調(diào)用此函數(shù)取得光標(biāo)顯示。
//
HCURSOR CrentDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CrentDlg::OnEnChangeEdit1()
{
// TODO: 如果該控件是 RICHEDIT 控件,則它將不會
// 發(fā)送該通知,除非重寫 CDialog::OnInitDialog()
// 函數(shù)并調(diào)用 CRichEditCtrl().SetEventMask(),
// 同時將 ENM_CHANGE 標(biāo)志“或”運(yùn)算到掩碼中。
// 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 序號,車牌號, 車主姓名,收費(fèi)站名稱,收費(fèi)日期,收費(fèi)標(biāo)準(zhǔn) from 收費(fèi)信息,車主信息,收費(fèi)站 where 收費(fèi)信息.收費(fèi)站ID=收費(fèi)站.收費(fèi)站ID and 收費(fèi)信息.車主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("收費(fèi)站名稱");
if(var.vt != VT_NULL)
strValue = (LPCSTR)_bstr_t(var);
//需要根據(jù)該DVDID查詢tbDVDInfo,獲得DVDname,插入list控件中
//m_rentInfoList.SetItemText(curItem,2,QueryDVDName(strValue));
//使用調(diào)用存儲過程函數(shù)QueryDVDNameSP
m_rentinfoList.SetItemText(curItem,3,strValue);
var = pRentRecordset->GetCollect("收費(fèi)日期");
if(var.vt != VT_NULL)
strValue = (LPCSTR)_bstr_t(var);
m_rentinfoList.SetItemText(curItem,4,strValue);
var = pRentRecordset->GetCollect("收費(fèi)標(biāo)準(zhǔn)");
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)
{
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -