📄 rentdlg.cpp
字号:
// RentDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Rent.h"
#include "RentDlg.h"
#include "logdlg.h"
#include "adddvdinfo.h"
#include "addrentinfo.h"
#include "checkdvdinfo.h"
#include "checkrentinfo.h"
#include "connectiondb.h"
#include "Rentinfodlg.h"
#include "returndlg.h"
#include "deldvdinfodialog.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()
/////////////////////////////////////////////////////////////////////////////
// CRentDlg dialog
CRentDlg::CRentDlg(CWnd* pParent /*=NULL*/)
: CDialog(CRentDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CRentDlg)
m_Note = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CRentDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CRentDlg)
DDX_Control(pDX, IDC_LIST_DVDINFO, m_DVDInfoList);
DDX_Text(pDX, IDC_EDIT_NOTE, m_Note);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CRentDlg, CDialog)
//{{AFX_MSG_MAP(CRentDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(ID_LOGIN,OnLogIn)
ON_BN_CLICKED(ID_LOGOUT,OnLogOut)
ON_BN_CLICKED(ID_ADDNEW,OnAddNew)
ON_BN_CLICKED(ID_DEL,OnDel)
ON_BN_CLICKED(ID_ADDRENT,OnAddRent)
ON_BN_CLICKED(ID_CHECK_DVDINFO,OnCheckDVDInfo)
ON_BN_CLICKED(ID_CHECK_RENTINFO,OnCheckRentInfo)
ON_BN_CLICKED(ID_ABOUT, OnAbout)
ON_BN_CLICKED(ID_RETURN,OnReturn)
ON_NOTIFY(NM_CLICK, IDC_LIST_DVDINFO, OnClickListDvdinfo)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CRentDlg message handlers
BOOL CRentDlg::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);
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);
}
}
// 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
// TODO: Add extra initialization here
m_DVDInfoList.InsertColumn(0,"ID",LVCFMT_LEFT,50,-1);
m_DVDInfoList.InsertColumn(1,"名称",LVCFMT_LEFT,120,-1);
m_DVDInfoList.InsertColumn(2,"发行日期",LVCFMT_LEFT,110,-1);
m_DVDInfoList.InsertColumn(3,"所属地",LVCFMT_LEFT,90,-1);
m_DVDInfoList.InsertColumn(4,"库存数量",LVCFMT_LEFT,70,-1);
m_DVDInfoList.InsertColumn(5,"备注",LVCFMT_LEFT,260,-1);
m_DVDInfoList.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
AfxGetMainWnd()->GetMenu()->EnableMenuItem(ID_LOGIN,MF_DISABLED|MF_BYCOMMAND|MF_GRAYED);
CMenu *pTopMenu,*psubMenu;
pTopMenu=AfxGetMainWnd()->GetMenu();
psubMenu=pTopMenu->GetSubMenu(0);
psubMenu->EnableMenuItem(ID_LOGIN,MF_ENABLED);
psubMenu->EnableMenuItem(ID_LOGOUT,MF_DISABLED|MF_BYCOMMAND|MF_GRAYED);
psubMenu=pTopMenu->GetSubMenu(1);
psubMenu->EnableMenuItem(ID_ADDNEW,MF_DISABLED|MF_BYCOMMAND|MF_GRAYED);
psubMenu->EnableMenuItem(ID_DEL,MF_DISABLED|MF_BYCOMMAND|MF_GRAYED);
psubMenu->EnableMenuItem(ID_ADDRENT,MF_DISABLED|MF_BYCOMMAND|MF_GRAYED);
psubMenu->EnableMenuItem(ID_RETURN,MF_DISABLED|MF_BYCOMMAND|MF_GRAYED);
psubMenu=pTopMenu->GetSubMenu(2);
psubMenu->EnableMenuItem(ID_CHECK_DVDINFO,MF_DISABLED|MF_BYCOMMAND|MF_GRAYED);
psubMenu->EnableMenuItem(ID_CHECK_RENTINFO,MF_DISABLED|MF_BYCOMMAND|MF_GRAYED);
psubMenu=pTopMenu->GetSubMenu(3);
psubMenu->EnableMenuItem(ID_ABOUT,MF_DISABLED|MF_BYCOMMAND|MF_GRAYED);
m_DVDInfoList.EnableWindow(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}
void CRentDlg::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 CRentDlg::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 CRentDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CRentDlg::OnInitCtrl()
{
if(m_DVDInfoList.GetItemCount()>0)
{
m_DVDInfoList.DeleteAllItems();
UpdateData(FALSE);
}
CLogDlg temp;
CConnectionDB *connectionptr=new CConnectionDB();
if(m_pConnection==NULL)
m_pConnection=connectionptr->Connectiondb();
m_pConnection=connectionptr->GetConnectionPtr();
HRESULT hr;
_RecordsetPtr m_DVDInfoRecordset;
hr=m_DVDInfoRecordset.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 DVDInfo";
try
{
hr=m_DVDInfoRecordset->Open(_variant_t(strSql),m_pConnection.GetInterfacePtr(),
adOpenDynamic,adLockOptimistic,adCmdText);
if(SUCCEEDED(hr))
{
while(!m_DVDInfoRecordset->adoEOF)
{
var=m_DVDInfoRecordset->GetCollect("DVDID");
if(var.vt!=VT_NULL)
strValue=(LPCSTR)_bstr_t(var);
m_DVDInfoList.InsertItem(curItem,strValue);
var=m_DVDInfoRecordset->GetCollect("DVDName");
if(var.vt!=VT_NULL)
strValue=(LPCSTR)_bstr_t(var);
m_DVDInfoList.SetItemText(curItem,1,strValue);
var=m_DVDInfoRecordset->GetCollect("OutDate");
if(var.vt!=VT_NULL)
strValue=(LPCSTR)_bstr_t(var);
m_DVDInfoList.SetItemText(curItem,2,strValue);
var=m_DVDInfoRecordset->GetCollect("Place");
if(var.vt!=VT_NULL)
strValue=(LPCSTR)_bstr_t(var);
m_DVDInfoList.SetItemText(curItem,3,strValue);
var=m_DVDInfoRecordset->GetCollect("DVDNum");
if(var.vt!=VT_NULL)
strValue=(LPCSTR)_bstr_t(var);
m_DVDInfoList.SetItemText(curItem,4,strValue);
var=m_DVDInfoRecordset->GetCollect("Note");
if(var.vt!=VT_NULL)
{
strValue=(LPCSTR)_bstr_t(var);
m_DVDInfoList.SetItemText(curItem,5,strValue);
}
m_DVDInfoRecordset->MoveNext();
curItem++;
}//while
}//if
else
{
AfxMessageBox("open recordset fail!");
}
}//try
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
return;
}
m_DVDInfoRecordset->Close();
m_DVDInfoRecordset=NULL;
delete connectionptr;
}
///////////////////////////////////////////////////////////////////
//菜单响应函数
void CRentDlg::OnLogIn()
{//登录菜单
UpdateData(FALSE);
CLogDlg logDlg;
CConnectionDB pconnection;
if(pconnection.GetConnectionPtr()==NULL)
pconnection.Connectiondb();
if(logDlg.DoModal()==IDOK)
{
//如果验证成功,则设置登录菜单为不可选,其他菜单为可选
m_DVDInfoList.EnableWindow(TRUE);
CMenu *pTopMenu,*psubMenu;
pTopMenu=AfxGetMainWnd()->GetMenu();
psubMenu=pTopMenu->GetSubMenu(0);
psubMenu->EnableMenuItem(ID_LOGIN,MF_DISABLED|MF_BYCOMMAND|MF_GRAYED);
psubMenu->EnableMenuItem(ID_LOGOUT,MF_ENABLED);
psubMenu=pTopMenu->GetSubMenu(1);
psubMenu->EnableMenuItem(ID_ADDNEW,MF_ENABLED);
psubMenu->EnableMenuItem(ID_DEL,MF_ENABLED);
psubMenu->EnableMenuItem(ID_ADDRENT,MF_ENABLED);
psubMenu->EnableMenuItem(ID_RETURN,MF_ENABLED);
psubMenu=pTopMenu->GetSubMenu(2);
psubMenu->EnableMenuItem(ID_CHECK_DVDINFO,MF_ENABLED);
psubMenu->EnableMenuItem(ID_CHECK_RENTINFO,MF_ENABLED);
psubMenu=pTopMenu->GetSubMenu(3);
psubMenu->EnableMenuItem(ID_ABOUT,MF_ENABLED);
OnInitCtrl();
}
}
void CRentDlg::OnLogOut()
{//注销菜单
if((MessageBox("是否注销?","提示",MB_OKCANCEL))==IDOK)
{
CMenu *pTopMenu,*psubMenu;
pTopMenu=AfxGetMainWnd()->GetMenu();
psubMenu=pTopMenu->GetSubMenu(0);
psubMenu->EnableMenuItem(ID_LOGIN,MF_ENABLED);
psubMenu->EnableMenuItem(ID_LOGOUT,MF_DISABLED|MF_BYCOMMAND|MF_GRAYED);
psubMenu=pTopMenu->GetSubMenu(1);
psubMenu->EnableMenuItem(ID_ADDNEW,MF_DISABLED|MF_BYCOMMAND|MF_GRAYED);
psubMenu->EnableMenuItem(ID_DEL,MF_DISABLED|MF_BYCOMMAND|MF_GRAYED);
psubMenu->EnableMenuItem(ID_ADDRENT,MF_DISABLED|MF_BYCOMMAND|MF_GRAYED);
psubMenu->EnableMenuItem(ID_RETURN,MF_DISABLED|MF_BYCOMMAND|MF_GRAYED);
psubMenu=pTopMenu->GetSubMenu(2);
psubMenu->EnableMenuItem(ID_CHECK_DVDINFO,MF_DISABLED|MF_BYCOMMAND|MF_GRAYED);
psubMenu->EnableMenuItem(ID_CHECK_RENTINFO,MF_DISABLED|MF_BYCOMMAND|MF_GRAYED);
psubMenu=pTopMenu->GetSubMenu(3);
psubMenu->EnableMenuItem(ID_ABOUT,MF_DISABLED|MF_BYCOMMAND|MF_GRAYED);
CConnectionDB connection;
connection.DeleteConnectionPtr();//关闭数据库连接
m_DVDInfoList.DeleteAllItems();
UpdateData(FALSE);
m_DVDInfoList.EnableWindow(FALSE);
}
else
return;
}
void CRentDlg::OnAddNew()
{//添加DVD信息菜单
CADDDVDInfo addDlg;
if(addDlg.DoModal()==IDOK)
UpdateListData();
}
void CRentDlg::OnDel(){
CDelDVDInfoDialog delDlg;
if(delDlg.DoModal()==IDOK)
UpdateListData();
}
void CRentDlg::OnAddRent()
{//添加DVD租借信息菜单
CAddRentInfo addrentDlg;
if(addrentDlg.DoModal()==IDOK)
{
CRentInfoDlg rentDlg;
rentDlg.DoModal();
UpdateListData();
}
}
void CRentDlg::OnCheckDVDInfo()
{//查找DVD信息菜单
CCheckDVDInfo dvdDlg;
dvdDlg.DoModal();
UpdateListData();
}
void CRentDlg::OnCheckRentInfo()
{//查找租借信息菜单
CCheckRentInfo rentDlg;
rentDlg.DoModal();
}
void CRentDlg::OnAbout()
{
CAboutDlg aboutDlg;
aboutDlg.DoModal();
}
void CRentDlg::OnReturn()
{//还碟信息菜单
CReturnDlg returnDlg;
if(returnDlg.DoModal()==IDOK)
UpdateListData();
}
////////////////////////////////////////////////////////////////////
void CRentDlg::UpdateListData()
{//更新列表框中的内容
// m_DVDInfoList.DeleteAllItems();
OnInitCtrl();
}
void CRentDlg::OnClickListDvdinfo(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
int sel;
CString str=_T(" ");
if(m_DVDInfoList.GetItemCount()>0)
{
sel=m_DVDInfoList.GetSelectionMark();
if(sel>=0)
str+=m_DVDInfoList.GetItemText(sel,5);
if(str==_T(" "))
m_Note="没有内容介绍";
else
m_Note=str;
UpdateData(FALSE);
}
*pResult = 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -