⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 devicedbsdlg.cpp

📁 < Visual C+++SQL Server数据库开发与实例>>一书中的实例核心源程序提供了几个案例的编程源代码。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// DeviceDBSDlg.cpp : implementation file
//

#include "stdafx.h"
#include "DeviceDBS.h"
#include "DeviceDBSDlg.h"
#include "LendInfoDlg.h"
#include "FreqListDlg.h"
#include "DeviceNRtDlg.h"
#include "User.h"
#include "PowerHelper.h"
#include "EditProfile.h"
#include "EditUser.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
extern CDatabase m_db;
extern User user;
extern PowerHelper helper;
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()

/////////////////////////////////////////////////////////////////////////////
// CDeviceDBSDlg dialog

CDeviceDBSDlg::CDeviceDBSDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDeviceDBSDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDeviceDBSDlg)
	m_oleOperDate = COleDateTime::GetCurrentTime();
	m_oleOperTime = COleDateTime::GetCurrentTime();
	m_strBorrower = _T("");
	m_strBuyer = _T("");
	m_strDescription = _T("");
	m_strDeviceCode = _T("");
	m_strDeviceName = _T("");
	m_strSelectedName = _T("");
	m_strSelectedCode = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CDeviceDBSDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDeviceDBSDlg)
	DDX_Control(pDX, IDC_LIST_LEND, m_listLend);
	DDX_Control(pDX, IDC_LIST_DEVICE, m_listDevice);
	DDX_Control(pDX, IDC_COMBO_NAME, m_comboName);
	DDX_Control(pDX, IDC_COMBO_CODE, m_comboCode);
	DDX_DateTimeCtrl(pDX, IDC_DT_DATE, m_oleOperDate);
	DDX_DateTimeCtrl(pDX, IDC_DT_TIME, m_oleOperTime);
	DDX_Text(pDX, IDC_EDIT_BORROWER, m_strBorrower);
	DDX_Text(pDX, IDC_EDIT_BUYER, m_strBuyer);
	DDX_Text(pDX, IDC_EDIT_DESCRIPTION, m_strDescription);
	DDX_Text(pDX, IDC_EDIT_DEVICECODE, m_strDeviceCode);
	DDX_Text(pDX, IDC_EDIT_DEVICENAME, m_strDeviceName);
	DDX_CBString(pDX, IDC_COMBO_NAME, m_strSelectedName);
	DDX_CBString(pDX, IDC_COMBO_CODE, m_strSelectedCode);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDeviceDBSDlg, CDialog)
	//{{AFX_MSG_MAP(CDeviceDBSDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTN_DEVICE_ADD, OnBtnDeviceAdd)
	ON_NOTIFY(NM_CLICK, IDC_LIST_DEVICE, OnClickListDevice)
	ON_BN_CLICKED(IDC_BTN_DEVICE_MOD, OnBtnDeviceMod)
	ON_BN_CLICKED(IDC_BTN_DEVICE_DEL, OnBtnDeviceDel)
	ON_CBN_CLOSEUP(IDC_COMBO_NAME, OnCloseupComboName)
	ON_BN_CLICKED(IDC_BTN_LEND, OnBtnLend)
	ON_BN_CLICKED(IDC_BTN_RETURN, OnBtnReturn)
	ON_BN_CLICKED(IDC_BTN_LEND_LIST, OnBtnLendList)
	ON_BN_CLICKED(IDC_BTN_FREQUENCY_LIST, OnBtnFrequencyList)
	ON_BN_CLICKED(IDC_BTN_LENT_DEVICE, OnBtnLentDevice)
	ON_BN_CLICKED(IDC_SYS_EXIT, OnSysExit)
	ON_BN_CLICKED(IDC_EDIT_PROFILE, OnEditProfile)
	ON_BN_CLICKED(IDC_EDIT_USER, OnEditUser)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDeviceDBSDlg message handlers

BOOL CDeviceDBSDlg::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
	//初始化列表框控件
	InitControl();
	//初始化数据
	InitCtrlData();
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CDeviceDBSDlg::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 CDeviceDBSDlg::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 CDeviceDBSDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CDeviceDBSDlg::InitControl()
{
	//设置列表框控件扩展风格
	DWORD dwExStyle = LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES |
		LVS_EX_HEADERDRAGDROP | LVS_EX_ONECLICKACTIVATE | LVS_EX_UNDERLINEHOT;
	m_listDevice.SetExtendedStyle(dwExStyle);
	m_listLend.SetExtendedStyle(dwExStyle);	
	//初始化设备信息列表框控件
	m_listDevice.InsertColumn(0,"设备编号",LVCFMT_CENTER,80);
	m_listDevice.InsertColumn(1,"设备名称",LVCFMT_CENTER,80);
	m_listDevice.InsertColumn(2,"设备入库时间",LVCFMT_CENTER,140);
	m_listDevice.InsertColumn(3,"设备购买人",LVCFMT_CENTER,80);
	m_listDevice.InsertColumn(4,"设备描述",LVCFMT_CENTER,200);
	//初始化议题列表框控件
	m_listLend.InsertColumn(0,"借出ID",LVCFMT_CENTER,80);
	m_listLend.InsertColumn(1,"设备编号",LVCFMT_CENTER,80);
	m_listLend.InsertColumn(2,"设备名称",LVCFMT_CENTER,80);
	m_listLend.InsertColumn(3,"借出人",LVCFMT_CENTER,80);
	m_listLend.InsertColumn(4,"设备借出时间",LVCFMT_CENTER,140);
	m_listLend.InsertColumn(5,"设备归还时间",LVCFMT_CENTER,140);
}

void CDeviceDBSDlg::ConnectDB()
{
	char szPath[255];
	//获取应用程序完全路径
	::GetModuleFileName(NULL,szPath,255);
	CString strFileName = szPath;
	//获取所在的目录名称
	strFileName.Delete(strFileName.ReverseFind('\\')+1,strFileName.GetLength ()-strFileName.ReverseFind('\\')-1);
    //构造配置文件的完全路径
	strFileName += "DeviceDBS.ini";
	TCHAR sz[101];
	memset(sz,0,sizeof(TCHAR)*101);
	//获取配置文件中数据库数据源的值,如果没有,缺省值为device
	GetPrivateProfileString(_T("General"),_T("数据库数据源"),_T("device"),sz,100,strFileName);
	CString strSource(sz);
	GetPrivateProfileString(_T("General"),_T("数据库用户"),_T("dbaccess"),sz,100,strFileName);
	CString strUser(sz);
	GetPrivateProfileString(_T("General"),_T("数据库密码"),_T("dbaccess"),sz,100,strFileName);
	CString strPwd(sz);	
	//创建连接字符串.
	CString strConnect;
	strConnect.Format("DSN=%s;UID=%s;PWD=%s",strSource,strUser,strPwd);
	//打开数据库的连接,并且捕获异常
	TRY{
		m_db.OpenEx(strConnect,CDatabase::noOdbcDialog);
	}
	CATCH(CDBException,ex)
	{
		AfxMessageBox (ex->m_strError);
		AfxMessageBox (ex->m_strStateNativeOrigin);		
	}
	AND_CATCH(CMemoryException,pEx)
	{
		pEx->ReportError();
		AfxMessageBox ("memory exception");
	}
	AND_CATCH(CException,e)
	{
		TCHAR szError[100];
		e->GetErrorMessage(szError,100);
		AfxMessageBox (szError);
	}
	END_CATCH	
	InitCtrlData();	
}

void CDeviceDBSDlg::OnBtnDeviceAdd() 
{
	// TODO: Add your control notification handler code here
	//从界面控件中获取信息更新到控件变量中.
	if(!UpdateData())
		return;
	//构造入库时间
	CString strDate = m_oleOperDate.Format("%m/%d/%Y ") 
		+ " " + m_oleOperTime.Format("%H:%M:%S");
	TRY{
		CRecordset rs(&m_db);
		CString sql;
        //打开数据库,查看数据库里面是否含有相同的设备编号
		//如果有提示用户,返回界面.
		sql.Format("Select device_code from device_info_tab "
			"where device_code = '%s'",m_strDeviceCode);
		rs.Open(CRecordset::snapshot, sql);
		CString strCode = _T("");
		if(!rs.IsEOF()) {
			rs.GetFieldValue((short)0, strCode);
		}
		
		if(!strCode.IsEmpty()){
			MessageBox("已有此设备的编号,请重新输入设备的编号");
			return;
		}
        //插入新的设备记录. 
		sql.Format("Insert into device_info_tab(device_code,"
			"device_name,description,oper_date,"
			"buyer,lend_status,lend_id) "
			"VALUES("
			"'%s','%s','%s','%s'"
			",'%s',0,0)",m_strDeviceCode,m_strDeviceName,
			m_strDescription,strDate,m_strBuyer);
		TRACE(sql);
		m_db.ExecuteSQL(sql);
		//向界面中插入新的设备信息.
		InsertDeviceInfoItem(m_strDeviceCode,m_strDeviceName,
			strDate,m_strBuyer,m_strDescription) ;
		//更新设备名称和设备编号组合框的数据
		RefreshComboNameData();
	}
	CATCH(CDBException,ex)
	{
		AfxMessageBox (ex->m_strError);
		AfxMessageBox (ex->m_strStateNativeOrigin);		
	}
	AND_CATCH(CException,e)
	{
		TCHAR szError[100];
		e->GetErrorMessage(szError,100);
		AfxMessageBox (szError);
	}
	END_CATCH			
}

void CDeviceDBSDlg::InsertDeviceInfoItem(CString code, CString name, CString date, CString buyer, CString description)
{
	//获取当前的纪录条数.
	int nIndex = m_listDevice.GetItemCount();
	LV_ITEM lvItem;
	lvItem.mask = LVIF_TEXT ;	
	lvItem.iItem = nIndex;				//行数
	lvItem.iSubItem = 0;
	lvItem.pszText = (char*)(LPCTSTR)code;		//第一列
	//在最后一行插入记录值.
	m_listDevice.InsertItem(&lvItem);	
	//设置该行的其他列的值.
	m_listDevice.SetItemText(nIndex,1,name);
	m_listDevice.SetItemText(nIndex,2,date);	
	m_listDevice.SetItemText(nIndex,3,buyer);	
	m_listDevice.SetItemText(nIndex,4,description);	
}

void CDeviceDBSDlg::InsertLendInfoItem(int id,CString code,CString name,CString borrower,CString lendDate,CString returnDate)
{
	//获取当前的纪录条数.
	int nIndex = m_listLend.GetItemCount();
	LV_ITEM lvItem;
	lvItem.mask = LVIF_TEXT ;	
	lvItem.iItem = nIndex;				//行数
	lvItem.iSubItem = 0;
	CString temp ;
	temp.Format("%d",id);
	lvItem.pszText = (char*)(LPCTSTR)temp;		//第一列
	//在最后一行插入记录值.
	m_listLend.InsertItem(&lvItem);	
	//设置该行的其他列的值.
	m_listLend.SetItemText(nIndex,1,code);
	m_listLend.SetItemText(nIndex,2,name);	
	m_listLend.SetItemText(nIndex,3,borrower);	
	m_listLend.SetItemText(nIndex,4,lendDate);	
	m_listLend.SetItemText(nIndex,5,returnDate);	
}

void CDeviceDBSDlg::InitCtrlData(){
	if(!m_db.IsOpen()){
		MessageBox("数据库未打开");
		return;
	}
	m_listDevice.DeleteAllItems();
	TRY{
		CRecordset rs(&m_db);
		//打开所有的设备信息记录.
		rs.Open(CRecordset::dynaset, "select * from device_info_tab");
		while (!rs.IsEOF()) {
			CString strCode,strName,strBuyer,strDate,strDescription;
			//获取设备编号字段值
			rs.GetFieldValue((short)0, strCode);
			//获取设备名称字段值
			rs.GetFieldValue(1, strName);
			//获取设备描述字段值
			rs.GetFieldValue(2, strDescription);
			//获取入库时间字段值
			rs.GetFieldValue(3, strDate);
			//获取设备购买人字段值
			rs.GetFieldValue(4, strBuyer);
			//向会议列表框控件中加入一条新的记录信息.
			//向界面中插入新的设备信息.
			InsertDeviceInfoItem(strCode,strName,
				strDate,strBuyer,strDescription) ;
			rs.MoveNext();	
		}
		rs.Close();
		//更新设备名称和设备编号组合框的数据
		RefreshComboNameData();
	}
	CATCH(CDBException,ex)
	{
		AfxMessageBox (ex->m_strError);
		AfxMessageBox (ex->m_strStateNativeOrigin);		
	}
	AND_CATCH(CMemoryException,pEx)
	{
		pEx->ReportError();
		AfxMessageBox ("memory exception");
	}
	AND_CATCH(CException,e)
	{
		TCHAR szError[100];
		e->GetErrorMessage(szError,100);
		AfxMessageBox (szError);
	}
	END_CATCH   			
}

void CDeviceDBSDlg::OnClickListDevice(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	//获取已选择的记录项.
	int nItem = m_listDevice.GetNextItem(-1, LVNI_SELECTED);
	if(nItem != -1){
		//从列表框控件的第nItem+1行获取数据,以将这些数据显示到设备参数的控件中.
		m_strDeviceCode = m_listDevice.GetItemText(nItem,0);
		m_strDeviceName = m_listDevice.GetItemText(nItem,1); 
		CString strDT = m_listDevice.GetItemText(nItem,2);
		m_strBuyer = m_listDevice.GetItemText(nItem,3);
		m_strDescription = m_listDevice.GetItemText(nItem,4);
		m_oleOperDate.ParseDateTime(strDT,0,VAR_DATEVALUEONLY);
		m_oleOperTime.ParseDateTime(strDT,0,VAR_TIMEVALUEONLY);
		//把设备列表框控件中当前已选择行的数据,更新到设备参数的控件中.
		UpdateData(FALSE);
	}	
	*pResult = 0;
}

void CDeviceDBSDlg::OnBtnDeviceMod() 
{
	// TODO: Add your control notification handler code here
	// TODO: Add your control notification handler code here
	int nItem = m_listDevice.GetNextItem(-1, LVNI_SELECTED);
	//如果没有选择要修改的纪录,返回.
	if(nItem == -1){
		AfxMessageBox("没有选择要修改的设备信息");
		return;
	}
	CString strDeviceCode = m_listDevice.GetItemText(nItem,0);
	if(!UpdateData())
		return;
	//构造入库时间
	CString strDate = m_oleOperDate.Format("%m/%d/%Y ") 
		+ " " + m_oleOperTime.Format("%H:%M:%S");
	TRY{
		CString sql;
		//更新数据库中的记录值. 

⌨️ 快捷键说明

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