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

📄 aspdlg.cpp

📁 一款基于串口的自动报警系统的程序
💻 CPP
字号:
// SetParaDlg.cpp : implementation file
//

#include "stdafx.h"
#include "alarm.h"
#include "ASPDlg.h"
#include "BspDlg.h"
#include "FubDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// SetParaDlg dialog


CASPDlg::CASPDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CASPDlg::IDD, pParent)
{

	// TODO: Add extra validation here
	
	
	//{{AFX_DATA_INIT(CASPDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
//	m_nSel=0;
//	m_nAct=0;
}
void CASPDlg::OnOK() 
{
	// TODO: Add extra validation here
	if (!UpdateData(TRUE))
	{
		TRACE0("UpdateData failed during dialog termination.\n");
		// the UpdateData routine will set focus to correct item
		return;
	}

	//数据检查并给以提示
	if(!DataCheck())
	{
		::AfxMessageBox("数据不完整或不符合要求,请检查");
		return;
	}
	//根据不同操作采取不同的action
	if(m_nAct==0)
	{
		AddNew();
	}
	if(m_nAct==1)
	{
		Editbf();
	}

	EndDialog(IDOK);
}
 CASPDlg::FillRecord(_RecordsetPtr r)
{
	r->PutCollect("cjname",m_chjName.AllocSysString());
	r->PutCollect("property",m_chjProperty.AllocSysString());
	r->PutCollect("number",m_chjPort.AllocSysString());

	CString act;
	if(m_nActType==1)
	{
		act="发码";
		r->PutCollect("infocode",m_chjCode.AllocSysString());
	}
	else if(m_nActType==2)
	{
		act="拨号";
		long telnumber=atoi(m_chjTelNum.GetBuffer(50));
		m_chjTelNum.ReleaseBuffer();
		r->PutCollect("telnumber",COleVariant(telnumber));
		r->PutCollect("infocode",CString("-").AllocSysString());
	}
	else
		act="-";
	
	r->PutCollect("typeact",act.AllocSysString());
}
BOOL CASPDlg::DataCheck()
{
	if((m_bfCode=="") || (m_bfName=="") ||(m_bfNumber=="") ||(m_bfInUse=="")||(m_bfProperty==""))
		return FALSE;
	CASPDlg* pWnd=(CASPDlg*)GetParent();
	for(int i=0; i< pWnd->m_bfList.GetItemCount();i++)
	{
		//注意,此处逻辑与vb程序不同
		if(i==pWnd->m_nSel) continue;
		CString bfCode,bfProperty;
		pWnd->m_bfList.GetItemText(i,1,bfCode.GetBuffer(100),100);
		pWnd->m_bfList.GetItemText(i,2,bfProperty.GetBuffer(100),100);
		bfCode.ReleaseBuffer();
		bfProperty.ReleaseBuffer();

		if((binstr2i(bfCode)==binstr2i(m_bfCode))&&(bfProperty==m_bfProperty))
			return FALSE;
	}
	return TRUE;
}

void CASPDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CASPDlg)
	DDX_Control(pDX, IDC_LIST1, m_bfList);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CASPDlg, CDialog)
	//{{AFX_MSG_MAP(CASPDlg)
	ON_BN_CLICKED(IDC_ADD, OnAdd)
	ON_BN_CLICKED(IDC_EDIT, OnEdit)
	ON_BN_CLICKED(IDC_DELETE, OnDelete)
	ON_BN_CLICKED(IDFINISH, OnFinish)
	ON_BN_CLICKED(IDNEXT, OnNext)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CASPDlg message handlers

BOOL CASPDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	//初始化
	m_bfList.InsertColumn(0,"布控名称");
	m_bfList.InsertColumn(1,"事件编码");
	m_bfList.InsertColumn(2,"端口属性");
	m_bfList.InsertColumn(3,"端口编号");
	m_bfList.InsertColumn(4,"是否启用");

	CRect rect;
	m_bfList.GetClientRect(&rect);
	m_bfList.SetColumnWidth(0,rect.Width()/5);
	m_bfList.SetColumnWidth(1,rect.Width()/5);
	m_bfList.SetColumnWidth(2,rect.Width()/5);
	m_bfList.SetColumnWidth(3,rect.Width()/5);
	m_bfList.SetColumnWidth(4,rect.Width()/5);

	_RecordsetPtr r;
	r.CreateInstance(_uuidof(Recordset));
	CString s="select * from BufangSet";
	_bstr_t bs=s.AllocSysString();
	CalarmApp* pApp=(CalarmApp*)::AfxGetApp();
	r->Open(bs,(IDispatch*)pApp->m_dConnTd,adOpenDynamic,adLockOptimistic,adCmdText);

	m_bfList.DeleteAllItems();
	if(addListItem(r))
		m_nSel=0;
//		m_bfList.SetSelectionMark(m_nSel);

	r->Close();
	r=NULL;

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE    
}

//将打开的表r加入listview的item中,如果r不为空集的话,则返回true,反之为false
BOOL CASPDlg::addListItem(_RecordsetPtr r)
{
	//如果r为空集,则不用加入list control中
	if(r->adoEOF || r->BOF)
		return FALSE;

	r->MoveFirst();
	while(!r->adoEOF)
	{
		//将bfname、bfcode、property、Number、promote赋成打开的记录集的字段值(按顺序)
		CString bfname,property,Number,promote,temp;
		CString bfcode;
		BOOL sf;

		bfname = LPTSTR(_bstr_t(r->GetCollect("bfname")));
		if(!bfname)
		{
			::AfxMessageBox("此纪录无主键");
			continue;
		}

		temp= LPTSTR(_bstr_t(r->GetCollect("bfcode")));
		if(!temp)
			bfcode="-";
		else
		{
			BYTE code= r->GetCollect("bfcode");
			itoa(code,bfcode.GetBuffer(50),2);
			bfcode.ReleaseBuffer();
			while(bfcode.GetLength()<8)
				bfcode.Insert(0,'0');
		}
		
		property=LPTSTR(_bstr_t(r->GetCollect("property")));
		if(!property)
			property="-";

		Number=LPTSTR(_bstr_t(r->GetCollect("Number")));
		if(!Number)
			Number="-";

		temp= LPTSTR(_bstr_t(r->GetCollect("promote")));
		if(!temp)
			promote="-";
		else
		{
			sf= (BYTE)r->GetCollect("promote");
			if(sf)
				promote="启用";
			else
				promote="禁用";
		}

		int nItem = m_bfList.InsertItem(m_bfList.GetItemCount(),bfname);
		m_bfList.SetItemText(nItem,1,bfcode);
		m_bfList.SetItemText(nItem,2,property);
		m_bfList.SetItemText(nItem,3,Number);
		m_bfList.SetItemText(nItem,4,promote);

		r->MoveNext();
	}
	return TRUE;
}

void CASPDlg::OnAdd() 
{
	// TODO: Add your control notification handler code here
	m_nSel=-1;
	m_nAct=0;
	CASPDlg dlg(this);
	dlg.DoModal();
}

void CASPDlg::OnEdit() 
{
	// TODO: Add your control notification handler code here
	m_nSel= m_bfList.GetNextItem( -1, LVNI_ALL | LVNI_SELECTED);
	if(m_nSel==-1)
	{
		::AfxMessageBox("请选择一条记录");
		return;
	}
	m_nAct=1;
	CFubDlg dlg(this);
	dlg.DoModal();
}

void CASPDlg::OnDelete() 
{
	m_nSel= m_bfList.GetNextItem( -1, LVNI_ALL | LVNI_SELECTED);
	if(m_nSel==-1)
	{
		::AfxMessageBox("请选择一条记录");
		return;
	}
	int res=::AfxMessageBox("确定删除该条记录?",MB_YESNO);
	if(res==IDNO)
		return;

	_RecordsetPtr r;
	r.CreateInstance(_uuidof(Recordset));
	CString tempbfName;
	m_bfList.GetItemText(m_nSel,0,tempbfName.GetBuffer(100),100);
	tempbfName.ReleaseBuffer();
	CString s="select * from BufangSet where bfname = '" +tempbfName + "'";
	_bstr_t bs=s.AllocSysString();
	TRY
	{
		CalarmApp* pApp=(CalarmApp*)::AfxGetApp();
		r->Open(bs,(IDispatch*)pApp->m_dConnTd,adOpenDynamic,adLockOptimistic,adCmdText);
		if(!r->adoEOF && !r->BOF)
		{
			r->Delete(adAffectCurrent);
			r->Update();
		}
	}
	CATCH(CException,e)
	{
		::AfxMessageBox("请再次检查数据");
		e->ReportError();
	}
	END_CATCH
	
	m_bfList.DeleteItem(m_nSel);	//从listctrl中删除该条记录

	r->Close();
	r=NULL;

	// TODO: Add your control notification handler code here
}

void CASPDlg::OnFinish() 
{
	// TODO: Add your control notification handler code here
	CDialog::OnCancel();
}
void CASPDlg::Editbf()
{
	_RecordsetPtr r;
	r.CreateInstance(_uuidof(Recordset));
	
	CASPDlg* pWnd=(CASPDlg*)GetParent();
	CString tempbfName;
	pWnd->m_bfList.GetItemText(pWnd->m_nSel,0,tempbfName.GetBuffer(100),100);
	tempbfName.ReleaseBuffer();
	CString s="select * from BufangSet where bfname = '" +tempbfName + "'";
	_bstr_t bs=s.AllocSysString();
	TRY
	{
		CalarmApp* pApp=(CalarmApp*)::AfxGetApp();
		r->Open(bs,(IDispatch*)pApp->m_dConnTd,adOpenDynamic,adLockOptimistic,adCmdText);
		FillRecord(r);
		r->Update();
	
		//将list更新
		CASPDlg* pWnd=(CASPDlg*)GetParent();
		pWnd->m_bfList.SetItemText(pWnd->m_nSel,0,m_bfName);
		pWnd->m_bfList.SetItemText(pWnd->m_nSel,1,m_bfCode);
		pWnd->m_bfList.SetItemText(pWnd->m_nSel,2,m_bfProperty);
		pWnd->m_bfList.SetItemText(pWnd->m_nSel,3,m_bfNumber);
		pWnd->m_bfList.SetItemText(pWnd->m_nSel,4,m_bfInUse);
	}
	CATCH(CException,e)
	{
		::AfxMessageBox("请再次检查数据");
		e->ReportError();
	}
	END_CATCH
	r->Close();
	r=NULL;
}
void CASPDlg::OnNext() 
{
	// TODO: Add your control notification handler code here
	ShowWindow(SW_HIDE);
	CBSPDlg dlg(this);
	dlg.DoModal();
}
void CASPDlg::AddNew()
{
	_RecordsetPtr r;
	r.CreateInstance(_uuidof(Recordset));
	CString s="select * from bufangset";
	_bstr_t bs=s.AllocSysString();
	TRY
	{
		CalarmApp* pApp=(CalarmApp*)::AfxGetApp();
		r->Open(bs,(IDispatch*)pApp->m_dConnTd,adOpenDynamic,adLockOptimistic,adCmdText);
		r->AddNew();
		FillRecord(r);
		r->Update();
	
		//将list更新
		CASPDlg* pWnd=(CASPDlg*)GetParent();
		int nItem = pWnd->m_bfList.InsertItem(pWnd->m_bfList.GetItemCount(),m_bfName);
		pWnd->m_bfList.SetItemText(nItem,1,m_bfCode);
		pWnd->m_bfList.SetItemText(nItem,2,m_bfProperty);
		pWnd->m_bfList.SetItemText(nItem,3,m_bfNumber);
		pWnd->m_bfList.SetItemText(nItem,4,m_bfInUse);
	}
	CATCH(CException,e)
	{
		e->ReportError();
		::AfxMessageBox("请再次检查数据");
	}
	END_CATCH
	r->Close();
	r=NULL;
}


⌨️ 快捷键说明

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