📄 configmsgdlg.cpp
字号:
// ConfigMsgDlg.cpp : implementation file
//
#include "stdafx.h"
#include "MsgDemo.h"
#include "ConfigMsgDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CConfigMsgDlg dialog
CConfigMsgDlg::CConfigMsgDlg(CWnd* pParent /*=NULL*/)
: CDialog(CConfigMsgDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CConfigMsgDlg)
m_Data = _T("");
m_Id = _T("");
m_Name = _T("");
m_numTel = _T("");
//}}AFX_DATA_INIT
}
void CConfigMsgDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CConfigMsgDlg)
DDX_Control(pDX, IDC_TELPHONE_NUM, m_ctrlTelNum);
DDX_Control(pDX, IDC_SEL_DATE, m_ctrlSelDate);
DDX_Control(pDX, IDC_DEFAULT, m_Default);
DDX_Control(pDX, IDC_USER_DEFINE, m_UserDefine);
DDX_Control(pDX, IDC_ID, m_ctrlId);
DDX_Control(pDX, IDC_NAME, m_ctrlName);
DDX_Control(pDX, IDC_DATA, m_ctrlData);
DDX_Control(pDX, IDC_LIST1, m_List);
DDX_CBString(pDX, IDC_DATA, m_Data);
DDX_Text(pDX, IDC_ID, m_Id);
DDX_CBString(pDX, IDC_NAME, m_Name);
DDX_Text(pDX, IDC_TELPHONE_NUM, m_numTel);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CConfigMsgDlg, CDialog)
//{{AFX_MSG_MAP(CConfigMsgDlg)
ON_BN_CLICKED(IDC_USER_DEFINE, OnUserDefine)
ON_BN_CLICKED(IDC_DEFAULT, OnDefault)
ON_WM_CLOSE()
ON_CBN_SELCHANGE(IDC_NAME, OnSelchangeName)
ON_BN_CLICKED(IDC_ADD_NEW_MSG, OnAddNewMsg)
ON_BN_CLICKED(IDC_ADD_TIME, OnAddTime)
ON_BN_CLICKED(IDC_DELETE_TIME, OnDeleteTime)
ON_NOTIFY(NM_CLICK, IDC_LIST1, OnClickList1)
ON_BN_CLICKED(IDC_EDIT_MSG, OnEditMsg)
ON_BN_CLICKED(IDC_DELETE_MSG, OnDeleteMsg)
ON_BN_CLICKED(IDC_LOAD_MSG, OnLoadMsg)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CConfigMsgDlg message handlers
BOOL CConfigMsgDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
m_HeaderCtrl.SetTitleText("新增信息记录");
m_HeaderCtrl.SetDescText("本操作就是向短信发送队列中增加计量流记录.");
m_HeaderCtrl.SetIconHandle(AfxGetApp()->LoadIcon(IDI_HARDDRIVE));
m_HeaderCtrl.SetIconHandle(::LoadIcon(AfxGetResourceHandle(),MAKEINTRESOURCE(IDI_TOOLBOX)));
m_HeaderCtrl.Init(this);
m_HeaderCtrl.MoveCtrls(this);
// TODO: Add extra initialization here
this->m_List.DeleteAllItems();
//设置list控件的文字和背景颜色
m_List.SetBkColor(RGB(255,255,255));
m_List.SetTextBkColor(RGB(255,255,255));
//清空list控件的数据
for(int delcolumn=100;delcolumn>=0;delcolumn--)
m_List.DeleteColumn(delcolumn);
//设置list对话框的列
DWORD dwStyle;
LV_COLUMN lvc;
dwStyle = m_List.GetStyle();
dwStyle |= LVS_EX_GRIDLINES |LVS_EX_FULLROWSELECT |LVS_SHOWSELALWAYS |LVS_EDITLABELS ;
m_List.SetExtendedStyle(dwStyle);
lvc.mask = LVCF_TEXT | LVCF_SUBITEM | LVCF_WIDTH |LVCF_FMT;
lvc.fmt=LVCFMT_LEFT;
lvc.iSubItem = 0;
lvc.pszText = _T("列队编号");
lvc.cx = 80;
m_List.InsertColumn(1,&lvc);
lvc.iSubItem = 1;
lvc.pszText = _T("信息内容");
lvc.cx = 150;
m_List.InsertColumn(2,&lvc);
lvc.iSubItem = 2;
lvc.pszText = _T("呼叫号码");
lvc.cx = 90;
m_List.InsertColumn(3,&lvc);
lvc.iSubItem = 3;
lvc.pszText = _T("发送级别");
lvc.cx = 80;
m_List.InsertColumn(4,&lvc);
lvc.iSubItem = 4;
lvc.pszText = _T("备注");
lvc.cx = 150;
m_List.InsertColumn(5,&lvc);
//get view point
CMainFrame *pFrm=(CMainFrame *)AfxGetApp()->GetMainWnd();
m_pView=pFrm->GetActiveView();
InsertComName();//插入COMBO名称选择
this->m_UserDefine.SetCheck(TRUE);
m_SysCon=USERDEFINE;
ReadIniFileVal();
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CConfigMsgDlg::OnUserDefine()
{
// TODO: Add your control notification handler code here
this->m_ctrlData.EnableWindow(TRUE);
m_SysCon=USERDEFINE;
}
void CConfigMsgDlg::OnDefault()
{
// TODO: Add your control notification handler code here
this->m_ctrlData.EnableWindow(FALSE);
m_SysCon=DEFAULT;
}
void CConfigMsgDlg::InsertComName()//插入COMBO名称选择
{
HRESULT hr;
try
{
hr = m_pConnection.CreateInstance("ADODB.Connection");//创建Connection对象
if(SUCCEEDED(hr))
{
hr = m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=JHWY.mdb","","",adModeUnknown);///连接数据库
///上面一句中连接字串中的Provider是针对ACCESS2000环境的,对于ACCESS97,需要改为:Provider=Microsoft.Jet.OLEDB.3.51; }
}
}
catch(_com_error e)///捕捉异常
{
CString errormessage;
errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
CDialog::OnCancel();
}
//read data from database and save to ini file
_RecordsetPtr m_pRecordset;
_variant_t RecordsAffected;
m_pRecordset.CreateInstance(__uuidof(Recordset));
m_pConnection->put_ConnectionTimeout(long(5));
CString strVal;
// m_pRecordset =m_pConnection->Execute("select name from Realanaquantity where warn='warn'",
// &RecordsAffected,adCmdText);
//模拟量表
m_pRecordset->Open("select * from RealElecenergy",// 查询DemoTable表中所有字段
m_pConnection.GetInterfacePtr(), // 获取库接库的IDispatch指针
adOpenDynamic,
adLockOptimistic,
adCmdText);
int nCount=1;
while(!m_pRecordset->adoEOF)
{
_variant_t var = m_pRecordset->GetCollect("name");
if(var.vt != VT_NULL)
strVal = (LPCSTR)_bstr_t(var);
if(strVal!="undefine")
{
this->m_ctrlName.AddString(strVal);
strName[nCount]=strVal;
var = m_pRecordset->GetCollect("ID");
strId[nCount]=(LPCSTR)_bstr_t(var);
nCount++;
}
m_pRecordset->MoveNext();
}
m_pRecordset->Close();
// m_pRecordset->Release();
}
void CConfigMsgDlg::OnClose()
{
// TODO: Add your message handler code here and/or call default
m_pConnection->Close();
m_pConnection.Release();
CDialog::OnClose();
}
void CConfigMsgDlg::OnSelchangeName()
{
// TODO: Add your control notification handler code here
CString strVal;
int nCur=this->m_ctrlName.GetCurSel();
this->m_ctrlName.GetLBText(nCur,strVal);
for(int i=1;i<100;i++)
{
if(!strName[i].IsEmpty())
{
if(strName[i]==strVal)
{
this->m_ctrlId.SetWindowText(strId[i]);
this->m_ctrlSelDate.ResetContent();
break;
}
}
}
}
void CConfigMsgDlg::OnAddNewMsg()
{
// TODO: Add your control notification handler code here
CString strDes,szTemp;
int nCount;
BOOL bExist=FALSE;
UpdateData(TRUE);
for(int i=0;i<this->m_ctrlSelDate.GetCount();i++)
{
this->m_ctrlSelDate.GetLBText(i,szTemp);
strDes+=szTemp;
strDes+=",";
}
strDes=strDes.Left(strDes.GetLength()-1);
nCount=this->m_List.GetItemCount();
for(i=0;i<nCount;i++)
{
szTemp=this->m_List.GetItemText(i,0);
if(strcmp(szTemp,this->m_Id)==0)
{
bExist=TRUE;
::AfxMessageBox("该编号已经被占用,添加失败!");
}
}
if(!bExist)
{
this->m_List.InsertItem(nCount,this->m_Id);
this->m_List.SetItemText(nCount,1,this->m_Name);
this->m_List.SetItemText(nCount,2,this->m_numTel);
this->m_List.SetItemText(nCount,3,"发送");
this->m_List.SetItemText(nCount,4,strDes);
}
}
void CConfigMsgDlg::OnAddTime()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
if(m_SysCon==DEFAULT)
{
if(this->m_Name.IsEmpty() || this->m_Id.IsEmpty() || this->m_numTel.IsEmpty())
::AfxMessageBox("输入信息不完整,情输入完整信息");
else
{
this->m_ctrlSelDate.ResetContent();
for(int i=0;i<24;i++)
{
CString strTime;
strTime.Format("%.2d",i);
this->m_ctrlSelDate.AddString(strTime);
this->m_ctrlSelDate.SetWindowText(strTime);
}
}
}
else
{
if(this->m_Name.IsEmpty() || this->m_Id.IsEmpty() || this->m_Data.IsEmpty()
|| this->m_numTel.IsEmpty())
::AfxMessageBox("输入信息不完整,情输入完整信息");
else
{
CString strVal;
BOOL bExist=FALSE;
for(int i=0;i<this->m_ctrlSelDate.GetCount();i++)
{
this->m_ctrlSelDate.GetLBText(i,strVal);
if(strcmp(strVal,m_Data)==0)
{
bExist=TRUE;
break;
}
}
if(!bExist)
{
this->m_ctrlSelDate.AddString(m_Data);
this->m_ctrlSelDate.SetWindowText(m_Data);
}
}
}
}
void CConfigMsgDlg::OnDeleteTime()
{
// TODO: Add your control notification handler code here
CString strVal,szTemp;
this->m_ctrlSelDate.GetWindowText(strVal);
for(int i=0;i<this->m_ctrlSelDate.GetCount();i++)
{
this->m_ctrlSelDate.GetLBText(i,szTemp);
if(strcmp(strVal,szTemp)==0)
{
this->m_ctrlSelDate.DeleteString(i);
if(this->m_ctrlSelDate.GetCount())
this->m_ctrlSelDate.GetLBText(0,szTemp);
else
szTemp.Empty();
this->m_ctrlSelDate.SetWindowText(szTemp);
break;
}
}
}
void CConfigMsgDlg::OnClickList1(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
// TODO: Add your command handler code here
int iOption;
CString strId,strNum,strbSend,strContent,strDes;
iOption = m_List.GetSelectedCount();
//得到当前选中的行
POSITION pos = m_List.GetFirstSelectedItemPosition();
//如果选中一行
if(pos)
{
int nItem = m_List.GetNextSelectedItem(pos);
strId=m_List.GetItemText(nItem,0);
strContent=m_List.GetItemText(nItem,1);
strNum=m_List.GetItemText(nItem,2);
strbSend=m_List.GetItemText(nItem,3);
strDes=m_List.GetItemText(nItem,4);
///////
DataCString(strDes);
this->m_ctrlId.SetWindowText(strId);
this->m_ctrlName.SetWindowText(strContent);
this->m_ctrlTelNum.SetWindowText(strNum);
}
*pResult = 0;
}
void CConfigMsgDlg::DataCString(CString strVal)
{
CString szTemp;
szTemp=strVal;
CString str[30];
int nSize=1;
while(1)
{
int nCount=szTemp.Find(',');
if(nCount==-1)
break;
str[nSize]=szTemp.Left(nCount);
szTemp=szTemp.Right(szTemp.GetLength()-nCount-1);
nSize++;
}
str[nSize]=szTemp;
//Insert ListCtrl Content
this->m_ctrlSelDate.ResetContent();
for(int i=0;i<30;i++)
{
if(!str[i].IsEmpty())
{
this->m_ctrlSelDate.AddString(str[i]);
this->m_ctrlSelDate.SetWindowText(str[i]);
}
}
}
void CConfigMsgDlg::OnEditMsg()
{
// TODO: Add your control notification handler code here
CString strDes,szTemp;
int nCount;
UpdateData(TRUE);
for(int i=0;i<this->m_ctrlSelDate.GetCount();i++)
{
this->m_ctrlSelDate.GetLBText(i,szTemp);
strDes+=szTemp;
strDes+=",";
}
strDes=strDes.Left(strDes.GetLength()-1);
nCount=this->m_List.GetItemCount();
for(i=0;i<nCount;i++)
{
szTemp=this->m_List.GetItemText(i,0);
if(strcmp(szTemp,this->m_Id)==0)
{
this->m_List.SetItemText(i,1,this->m_Name);
this->m_List.SetItemText(i,2,this->m_numTel);
this->m_List.SetItemText(i,3,"发送");
this->m_List.SetItemText(i,4,strDes);
return;
}
}
::AfxMessageBox("没有找到相应编号得记录,更新失败!");
}
void CConfigMsgDlg::OnDeleteMsg()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
CString szTemp;
if(!this->m_Id.IsEmpty())
{
int nCount=this->m_List.GetItemCount();
for(int i=0;i<nCount;i++)
{
szTemp=this->m_List.GetItemText(i,0);
if(strcmp(szTemp,this->m_Id)==0)
{
if(MessageBox("确定要删除编号为"+this->m_Id+"的记录吗?","询问信息",MB_OKCANCEL|MB_ICONQUESTION)==IDOK)
{
this->m_List.DeleteItem(i);
return;
}
else
return;
}
}
::AfxMessageBox("没有找到相应编号得记录,更新失败!");
}
}
void CConfigMsgDlg::OnOK()
{
// TODO: Add extra validation here
for(int i=0;i<50;i++)
{
szId[i].Empty();
szName[i].Empty();
szDes[i].Empty();
szNum[i].Empty();
}
///
for(i=0;i<this->m_List.GetItemCount();i++)
{
szId[i+1]=this->m_List.GetItemText(i,0);
szName[i+1]=this->m_List.GetItemText(i,1);
szNum[i+1]=this->m_List.GetItemText(i,2);
szDes[i+1]=this->m_List.GetItemText(i,4);
}
//
CIniFile IniFile;
CString str,path;
path=AfxGetApp()->m_pszHelpFilePath;
str=AfxGetApp()->m_pszExeName;
path=path.Left(path.GetLength()-str.GetLength()-4);
IniFile.SetName("SetTimerList");
IniFile.SetPath(path);
BOOL bExist=IniFile.OpenIniFileForWrite();
if(bExist)
{
IniFile.WriteSection("Data");
for(int i=0;i<50;i++)
{
CString strId,strTolVal;
strId.Format("%.4d",i+1);
if(szId[i+1].IsEmpty())
strTolVal=";;;;;";
else
strTolVal=szId[i+1]+";"+szName[i+1]+";"+szNum[i+1]+";"+"发送"
+";"+szDes[i+1];
IniFile.WriteItemString(strId,strTolVal);
}
}
CDialog::OnOK();
}
void CConfigMsgDlg::ReadIniFileVal()
{
CIniFile IniFile;
CString str,path;
path=AfxGetApp()->m_pszHelpFilePath;
str=AfxGetApp()->m_pszExeName;
path=path.Left(path.GetLength()-str.GetLength()-4);
IniFile.SetName("SetTimerList");
IniFile.SetPath(path);
BOOL bExist=IniFile.OpenIniFileForRead();
if(bExist)
{
CString strId,strTolVal;
this->m_List.DeleteAllItems();
for(int i=0;i<50;i++)
{
strId.Format("%.4d",i+1);
strTolVal.Empty();
IniFile.GetItemString("Data",strId,strTolVal);
if(!strTolVal.IsEmpty())
DataCString(strTolVal,i+1);
}
}
}
void CConfigMsgDlg::DataCString(CString strVal, int nPos)
{
CString szTemp;
szTemp=strVal;
CString str[10];
int nSize=1;
while(1)
{
int nCount=szTemp.Find(';');
if(nCount==-1)
break;
str[nSize]=szTemp.Left(nCount);
szTemp=szTemp.Right(szTemp.GetLength()-nCount-1);
nSize++;
}
str[nSize]=szTemp;
//Insert ListCtrl Content
int nPos1=m_List.GetItemCount();
if(!str[1].IsEmpty())
{
this->m_List.InsertItem(nPos1,str[1]);
this->m_List.SetItemText(nPos1,1,str[2]);
this->m_List.SetItemText(nPos1,2,str[3]);
this->m_List.SetItemText(nPos1,3,str[4]);
this->m_List.SetItemText(nPos1,4,str[5]);
}
}
void CConfigMsgDlg::OnLoadMsg()
{
// TODO: Add your control notification handler code here
if(MessageBox("重新导入记录将会使得开始配置参数无效,确定要导入吗?","询问信息",MB_OKCANCEL|MB_ICONQUESTION)==IDOK)
ReadIniFileVal();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -