📄 cmsdlg.cpp
字号:
// CMSDlg.cpp : implementation file
//
#include "stdafx.h"
#include "CMS.h"
#include "CMSDlg.h"
#include "AddUserDlg.h"
#include "DeleteUserDlg.h"
#include "ModifyUserDlg.h"
#include "SelectUserDlg.h"
#include "UserDAO.h"
#include "LogDAO.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define LedType 2 // 控制卡类型,1-4
#define LedNum 0 // 控制卡编号,由控制卡上的跳线设定
#define LedWidth 384 // 屏体宽度,由CL2005节目管理系统设定
#define LedHeight 128 // 屏体高度,由CL2005节目管理系统设定
#define LedColor 1 // 颜色, 0:单色,1:双色, 由CL2005节目管理系统设定
#define ComPort 1 // 串行口
#define ComBaudRate 38400 // 通讯速率,由控制卡跳线设定
#define ComDelay 1000 // 等待应答的延时,由通讯质量及速率和数据包大小决定
// 一般至少取800以上
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
inline void TESTHR(HRESULT x) {if FAILED(x) _com_issue_error(x);};
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()
/////////////////////////////////////////////////////////////////////////////
// CCMSDlg dialog
CCMSDlg::CCMSDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCMSDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCMSDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CCMSDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCMSDlg)
DDX_Control(pDX, IDC_LOG_LIST, m_listLog);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CCMSDlg, CDialog)
//{{AFX_MSG_MAP(CCMSDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_COMMAND(ID_ADD_USER_MENUITEM, OnAddUserMenuitem)
ON_COMMAND(ID_DELETE_USER_MENUITEM, OnDeleteUserMenuitem)
ON_COMMAND(ID_MODIFY_USER_MENUITEM, OnModifyUserMenuitem)
ON_COMMAND(ID_SELECT_USER_MENUITEM, OnSelectUserMenuitem)
ON_COMMAND(ID_ON_MENUITEM, OnOnMenuitem)
ON_COMMAND(ID_OFF_MENUITEM, OnOffMenuitem)
ON_WM_TIMER()
ON_WM_DESTROY()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCMSDlg message handlers
BOOL CCMSDlg::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
CMenu NewMenu;
NewMenu.LoadMenu(IDR_MENU);
SetMenu(&NewMenu);
//打开读写器
if(m_rfid.Open() == false)
{
MessageBox("打开读写器失败!", "消息",MB_OK);
}
//连接数据库
try
{
TESTHR(m_pConnection.CreateInstance(__uuidof(Connection)));
HRESULT hr = m_pConnection->Open("driver={SQL Server};Server=WANYONGCHAO; \
DATABASE=CMS; UID=sa; PWD=chenlidong;","","",adModeUnknown);
}
catch(_com_error e)
{
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
MessageBox((LPCSTR)bstrDescription,(LPCSTR)bstrSource,MB_OK);
}
rs232.Connect();
//
m_listLog.InsertColumn(0,"用户号",LVCFMT_LEFT, 100);
m_listLog.InsertColumn(1,"姓名",LVCFMT_LEFT, 100);
m_listLog.InsertColumn(2,"卡号",LVCFMT_LEFT, 120);
m_listLog.InsertColumn(3,"时间",LVCFMT_LEFT, 200);
m_listLog.InsertColumn(4,"类型",LVCFMT_LEFT, 100);
return TRUE; // return TRUE unless you set the focus to a control
}
void CCMSDlg::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 CCMSDlg::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 CCMSDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CCMSDlg::OnAddUserMenuitem()
{
// TODO: Add your command handler code here
KillTimer(m_nTimer);
CAddUserDlg dlgAddUser;
if(dlgAddUser.DoModal() == IDOK)
{
CUserDAO userDao(m_pConnection);
userDao.InsertUser(dlgAddUser.m_strUserID, dlgAddUser.m_strCardID,\
dlgAddUser.m_strName, dlgAddUser.m_dSurplus);
MessageBox("添加用户成功","消息",MB_OK);
}
}
void CCMSDlg::OnDeleteUserMenuitem()
{
// TODO: Add your command handler code here
KillTimer(m_nTimer);
CDeleteUserDlg dlgDeleteUser;
if( dlgDeleteUser.DoModal() == IDOK)
{
CUserDAO userDao(m_pConnection);
userDao.DeleteUser(dlgDeleteUser.m_strUserID);
MessageBox("删除成功","消息",MB_OK);
}
}
void CCMSDlg::OnModifyUserMenuitem()
{
// TODO: Add your command handler code here
KillTimer(m_nTimer);
CModifyUserDlg dlgModifyUser;
if( dlgModifyUser.DoModal() == IDOK)
{
CUserDAO userDao(m_pConnection);
userDao.UpdateUser(dlgModifyUser.m_strUserID, dlgModifyUser.m_strName, dlgModifyUser.m_dSurplus);
MessageBox("修改成功", "消息",MB_OK);
}
}
void CCMSDlg::OnSelectUserMenuitem()
{
// TODO: Add your command handler code here
KillTimer(m_nTimer);
CSelectUserDlg dlgSelectUser;
if( dlgSelectUser.DoModal() == IDOK)
{
CUserDAO userDao(m_pConnection);
// userDao.SelectUser(dlgSelectUser.m_strUserID);
}
}
void CCMSDlg::OnOnMenuitem()
{
//打开计时器
m_nTimer = SetTimer(1, 100, 0);
}
void CCMSDlg::OnOffMenuitem()
{
// TODO: Add your command handler code here
//关闭定时器
KillTimer(m_nTimer);
}
void CCMSDlg::OnTimer(UINT nIDEvent)
{
//检测是否有卡在附近
CString strUserID;
CString strCardID;
CString strName;
CString strTime;
CString strType;
if(m_rfid.ReadID(strCardID) == true)
{
//添加到数据库
CUserDAO userDAO(m_pConnection);
bool flag = userDAO.SelectByCard(strCardID,strUserID,strName);
if(flag == true)
{
CLogDAO logDao(m_pConnection);
logDao.InsertLog(strUserID);
logDao.SelectLogType(strType);
//用户界面更新
CTime t = CTime::GetCurrentTime();
strTime = t.Format("%Y年%m月%d日 %H时%M分%S秒");
int row = m_listLog.InsertItem(0,strUserID);
m_listLog.SetItemText(row,1,strName);
m_listLog.SetItemText(row,2,strCardID);
m_listLog.SetItemText(row,3,strTime);
m_listLog.SetItemText(row,4,strType);
//显示模块
char str[256];
BOOL bOK;
strcpy(str,"`01红`02绿`03黄");
bOK = m_CL2005.ComInitial(ComPort,ComBaudRate,ComDelay);
if (!bOK)
{
MessageBox("串口初始化失败","显示文字",MB_OK);
}
else
{
bOK = m_CL2005.SetLEDProperty(LedType,LedNum,LedWidth,LedHeight,LedColor,1);
if (bOK) bOK = m_CL2005.SwitchToBank(0);
if (bOK) bOK = m_CL2005.ShowString(1,LedWidth-3*16,0,0,(long)str);
if (bOK) bOK = m_CL2005.SwitchToBank(1);
m_CL2005.CloseCL2005();
if (bOK) MessageBox("发送成功","显示文字",MB_OK);
else MessageBox("发送失败","显示文字",MB_OK);
}
rs232.Rotate(0xaa);
m_rfid.Clear();
rs232.Rotate(0x55);
}
else
{
m_rfid.Clear();
MessageBox("该车未登记!","消息", MB_OK);
}
}
CDialog::OnTimer(nIDEvent);
}
void CCMSDlg::OnDestroy()
{
CDialog::OnDestroy();
//关闭与数据库的连接
m_pConnection->Close();
//关闭读写器
if(m_rfid.Close() == false)
{
MessageBox("关闭读写器失败!", "消息",MB_OK);
}
rs232.Close();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -