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

📄 setparadlg.cpp

📁 这是一个比较典型的vc做的智能安防报警系统
💻 CPP
字号:
// SetParaDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Alert.h"
#include "SetParaDlg.h"
#include "SetPara2Dlg.h"
#include "bufDlg.h"

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

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


CSetParaDlg::CSetParaDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSetParaDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSetParaDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	m_nSel=0;
	m_nAct=0;
}


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


BEGIN_MESSAGE_MAP(CSetParaDlg, CDialog)
	//{{AFX_MSG_MAP(CSetParaDlg)
	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()

/////////////////////////////////////////////////////////////////////////////
// CSetParaDlg message handlers

BOOL CSetParaDlg::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();
	CAlertApp* pApp=(CAlertApp*)::AfxGetApp();
	r->Open(bs,(IDispatch*)pApp->m_pConnection,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 CSetParaDlg::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 CSetParaDlg::OnAdd() 
{
	// TODO: Add your control notification handler code here
	m_nSel=-1;
	m_nAct=0;
	CBufDlg dlg(this);
	dlg.DoModal();
}

void CSetParaDlg::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;
	CBufDlg dlg(this);
	dlg.DoModal();
}

void CSetParaDlg::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
	{
		CAlertApp* pApp=(CAlertApp*)::AfxGetApp();
		r->Open(bs,(IDispatch*)pApp->m_pConnection,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 CSetParaDlg::OnFinish() 
{
	// TODO: Add your control notification handler code here
	CDialog::OnCancel();
}

void CSetParaDlg::OnNext() 
{
	// TODO: Add your control notification handler code here
	ShowWindow(SW_HIDE);
	CSetPara2Dlg dlg(this);
	dlg.DoModal();
}

⌨️ 快捷键说明

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