www.pudn.com > smsinterface.rar > SmsEditView.cpp


// SmsEditView.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "smsinterface.h" 
#include "SmsEditView.h" 
#include "SerialPort.h" 
#include "MainFrm.h" 
#include  
 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CSmsEditView 
 
IMPLEMENT_DYNCREATE(CSmsEditView, CFormView) 
 
CSmsEditView::CSmsEditView() 
	: CFormView(CSmsEditView::IDD) 
{ 
	//{{AFX_DATA_INIT(CSmsEditView) 
	m_name = _T(""); 
	m_phonenum = _T(""); 
	//}}AFX_DATA_INIT 
	isoperating=FALSE; 
	isselected=FALSE; 
	 
} 
 
CSmsEditView::~CSmsEditView() 
{ 
} 
 
void CSmsEditView::DoDataExchange(CDataExchange* pDX) 
{ 
	CFormView::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CSmsEditView) 
	DDX_Control(pDX, IDC_BUTTON_CANCEL, m_btncancel); 
	DDX_Control(pDX, IDC_BUTTON_EDIT, m_btnedit); 
	DDX_Control(pDX, IDC_BUTTON_DEL, m_btndel); 
	DDX_Control(pDX, IDC_BUTTON_ADD, m_btnadd); 
	DDX_Control(pDX, IDC_LIST_PHONEBOOK, m_lstphonebook); 
	DDX_Text(pDX, IDC_EDIT_NAME, m_name); 
	DDX_Text(pDX, IDC_EDIT_PHONE, m_phonenum); 
	//}}AFX_DATA_MAP 
} 
 
 
BEGIN_MESSAGE_MAP(CSmsEditView, CFormView) 
	//{{AFX_MSG_MAP(CSmsEditView) 
	ON_BN_CLICKED(IDC_BUTTON_ADD, OnAdd) 
	ON_BN_CLICKED(IDC_BUTTON_DEL, OnDel) 
	ON_BN_CLICKED(IDC_BUTTON_EDIT, OnEdit) 
	ON_LBN_SELCHANGE(IDC_LIST_PHONEBOOK, OnSelchangeListPhonebook) 
	ON_LBN_SETFOCUS(IDC_LIST_PHONEBOOK, OnSetfocusListPhonebook) 
	ON_BN_CLICKED(IDC_BUTTON_CANCEL, OnButtonCancel) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CSmsEditView diagnostics 
 
#ifdef _DEBUG 
void CSmsEditView::AssertValid() const 
{ 
	CFormView::AssertValid(); 
} 
 
void CSmsEditView::Dump(CDumpContext& dc) const 
{ 
	CFormView::Dump(dc); 
} 
 
CSmsinterfaceDoc* CSmsEditView::GetDocument() // non-debug version is inline 
{ 
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CSmsinterfaceDoc))); 
	return (CSmsinterfaceDoc*)m_pDocument; 
} 
#endif //_DEBUG 
 
///////////////////////////////////////////////////////////////////////////// 
// CSmsEditView message handlers 
 
void CSmsEditView::OnInitialUpdate()  
{ 
	CFormView::OnInitialUpdate(); 
	GetParentFrame()->RecalcLayout(); 
	ResizeParentToFit(); 
	// TODO: Add your specialized code here and/or call the base class 
	CEdit* editwnd; 
	editwnd=(CEdit*)this->GetDlgItem(IDC_EDIT_NAME); 
	::EnableWindow(editwnd->m_hWnd,FALSE); 
	CEdit* editwnd2; 
	editwnd2=(CEdit*)this->GetDlgItem(IDC_EDIT_PHONE); 
	::EnableWindow(editwnd2->m_hWnd,FALSE); 
	m_btncancel.EnableWindow(FALSE); 
	 
} 
 
BOOL CSmsEditView::PreCreateWindow(CREATESTRUCT& cs)  
{ 
	// TODO: Add your specialized code here and/or call the base class 
	 
	return CFormView::PreCreateWindow(cs); 
} 
 
void CSmsEditView::OnAdd()  
{ 
	// TODO: Add your control notification handler code here 
	CEdit* editwnd; 
	CEdit* editwnd2; 
	if(!isoperating) 
		{ 
			isoperating=TRUE; 
			editwnd=(CEdit*)this->GetDlgItem(IDC_EDIT_NAME); 
			::EnableWindow(editwnd->m_hWnd,isoperating); 
			editwnd2=(CEdit*)this->GetDlgItem(IDC_EDIT_PHONE); 
			::EnableWindow(editwnd2->m_hWnd,isoperating); 
			m_btncancel.EnableWindow(isoperating); 
			editwnd2->SetFocus(); 
			editwnd2->SetSel(0,-1); 
			editwnd2->Clear(); 
			editwnd->SetFocus(); 
			editwnd->SetSel(0,-1); 
			editwnd->Clear(); 
			 
			m_lstphonebook.EnableWindow(!isoperating); 
			m_btnedit.EnableWindow(!isoperating); 
			m_btndel.EnableWindow(!isoperating); 
			m_btnadd.SetWindowText("保存设置"); 
			whichbtn=ADDENTRY; 
		} 
		else 
		{ 
			UpdateData(); 
			int len=m_name.GetLength(); 
			if(len>20) 
			{ 
				AfxMessageBox("您输入的姓名太长,请修改!",MB_OK); 
				return; 
			} 
			if(len<=0) 
			{ 
				AfxMessageBox("您输入的姓名为空,请修改!",MB_OK); 
				return; 
			} 
			len=m_phonenum.GetLength(); 
			if(len>20) 
			{ 
				AfxMessageBox("您输入的电话号码太长,请修改!",MB_OK); 
				return; 
			} 
			if(len<=0) 
			{ 
				AfxMessageBox("您输入的电话号码为空,请修改!",MB_OK); 
				return; 
			} 
			cpbw pbcpbw; 
			pbcpbw.indexstr=m_index; 
			 
			pbcpbw.namestr=m_name; 
			pbcpbw.phonestr=m_phonenum; 
			CFile gfile; 
			CFileException e; 
			if( !gfile.Open( "e:/smsinterface/phonebook.dat", CFile::modeReadWrite, &e ) ) 
			{ 
			#ifdef _DEBUG 
			     afxDump << "File could not be opened " << e.m_cause << "\n"; 
		     #endif 
			 return; 
			} 
			AddOneEntry(gfile,pbcpbw); 
		    phonebookar.RemoveAll(); 
			gfile.Seek(0,CFile::begin); 
			ReadAllEntry(gfile,phonebookar); 
			gfile.Close(); 
			GetPhoneBookList(phonebookar);   
			UpdateData(FALSE); 
			isoperating=FALSE; 
			editwnd=(CEdit*)this->GetDlgItem(IDC_EDIT_NAME); 
			::EnableWindow(editwnd->m_hWnd,isoperating); 
			editwnd2=(CEdit*)this->GetDlgItem(IDC_EDIT_PHONE); 
			::EnableWindow(editwnd2->m_hWnd,isoperating); 
			m_btncancel.EnableWindow(isoperating); 
			m_lstphonebook.EnableWindow(!isoperating); 
			m_btnedit.EnableWindow(!isoperating); 
			m_btndel.EnableWindow(!isoperating); 
			m_btnadd.SetWindowText("添      加"); 
			whichbtn=0; 
		} 
} 
 
void CSmsEditView::OnDel()  
{ 
	// TODO: Add your control notification handler code here 
	if(isselected) 
	{ 
		int res; 
	res=AfxMessageBox("您确定要删除这一项吗?",MB_YESNO); 
		if(res==IDYES) 
	{ 
		UpdateData(); 
		cpbw pbcpbw; 
		pbcpbw.indexstr=m_index; 
		pbcpbw.namestr=m_name; 
		pbcpbw.phonestr=m_phonenum; 
		CFile gfile; 
			CFileException e; 
			if( !gfile.Open( "e:/smsinterface/phonebook.dat", CFile::modeReadWrite, &e ) ) 
			{ 
			#ifdef _DEBUG 
			     afxDump << "File could not be opened " << e.m_cause << "\n"; 
		     #endif 
			 return; 
			} 
			DelOneEntry(atoi(pbcpbw.indexstr),gfile); 
		    phonebookar.RemoveAll(); 
			if( !gfile.Open( "e:/smsinterface/phonebook.dat", CFile::modeReadWrite, &e ) ) 
			{ 
			#ifdef _DEBUG 
			     afxDump << "File could not be opened " << e.m_cause << "\n"; 
		     #endif 
			 return; 
			} 
			ReadAllEntry(gfile,phonebookar); 
			gfile.Close(); 
			GetPhoneBookList(phonebookar);   
			UpdateData(FALSE);     
		m_btncancel.EnableWindow(isoperating); 
		whichbtn=0; 
	} 
	} 
} 
 
void CSmsEditView::OnEdit()  
{ 
	// TODO: Add your control notification handler code here 
	 
	CEdit* editwnd; 
	CEdit* editwnd2; 
	if(isselected) 
		if(!isoperating) 
		{ 
			isoperating=TRUE; 
			editwnd=(CEdit*)this->GetDlgItem(IDC_EDIT_NAME); 
			::EnableWindow(editwnd->m_hWnd,isoperating); 
			editwnd2=(CEdit*)this->GetDlgItem(IDC_EDIT_PHONE); 
			::EnableWindow(editwnd2->m_hWnd,isoperating); 
			m_btncancel.EnableWindow(isoperating); 
			editwnd->SetFocus(); 
			editwnd->SetSel(0,-1); 
			m_lstphonebook.EnableWindow(!isoperating); 
			m_btnadd.EnableWindow(!isoperating); 
			m_btndel.EnableWindow(!isoperating); 
			m_btnedit.SetWindowText("保存设置"); 
			whichbtn=EDITENTRY; 
		} 
		else 
		{ 
			UpdateData(); 
			int len=m_name.GetLength(); 
			if(len>20) 
			{ 
				AfxMessageBox("您输入的姓名太长,请修改!",MB_OK); 
				return; 
			} 
			if(len<=0) 
			{ 
				AfxMessageBox("您输入的姓名为空,请修改!",MB_OK); 
				return; 
			} 
			len=m_phonenum.GetLength(); 
			if(len>20) 
			{ 
				AfxMessageBox("您输入的电话号码太长,请修改!",MB_OK); 
				return; 
			} 
			if(len<=0) 
			{ 
				AfxMessageBox("您输入的电话号码为空,请修改!",MB_OK); 
				return; 
			} 
			cpbw pbcpbw; 
			pbcpbw.indexstr=m_index; 
			 
			pbcpbw.namestr=m_name; 
			pbcpbw.phonestr=m_phonenum; 
			CFile gfile; 
			CFileException e; 
			if( !gfile.Open( "e:/smsinterface/phonebook.dat", CFile::modeReadWrite, &e ) ) 
			{ 
			#ifdef _DEBUG 
			     afxDump << "File could not be opened " << e.m_cause << "\n"; 
		     #endif 
			 return; 
			} 
			EditOneEntry(atoi(pbcpbw.indexstr),gfile,pbcpbw); 
		    phonebookar.RemoveAll(); 
			if( !gfile.Open( "e:/smsinterface/phonebook.dat", CFile::modeReadWrite, &e ) ) 
			{ 
			#ifdef _DEBUG 
			     afxDump << "File could not be opened " << e.m_cause << "\n"; 
		     #endif 
			 return; 
			} 
			ReadAllEntry(gfile,phonebookar); 
			gfile.Close(); 
			GetPhoneBookList(phonebookar);   
			UpdateData(FALSE); 
			isoperating=FALSE; 
			editwnd=(CEdit*)this->GetDlgItem(IDC_EDIT_NAME); 
			::EnableWindow(editwnd->m_hWnd,isoperating); 
			editwnd2=(CEdit*)this->GetDlgItem(IDC_EDIT_PHONE); 
			::EnableWindow(editwnd2->m_hWnd,isoperating); 
			m_btncancel.EnableWindow(isoperating); 
			m_lstphonebook.EnableWindow(!isoperating); 
			m_btnadd.EnableWindow(!isoperating); 
			m_btndel.EnableWindow(!isoperating); 
			m_btnedit.SetWindowText("编      辑"); 
			whichbtn=0; 
		} 
} 
 
LRESULT CSmsEditView::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)  
{ 
	// TODO: Add your specialized code here and/or call the base class 
	if(message==WM_GETPHONEBOOKLIST) 
	{ 
	 
		CFile gfile; 
		CFileException e; 
		 if( !gfile.Open( "e:/smsinterface/phonebook.dat", CFile::modeRead, &e ) ) 
		{ 
		   #ifdef _DEBUG 
		      afxDump << "File could not be opened " << e.m_cause << "\n"; 
		   #endif 
		} 
		phonebookar.RemoveAll(); 
		ReadAllEntry(gfile,phonebookar); 
		gfile.Close(); 
		GetPhoneBookList(phonebookar); 
	 
	} 
	return CFormView::DefWindowProc(message, wParam, lParam); 
} 
 
void CSmsEditView::GetPhoneBookList(CStringArray &phonebook) 
{ 
	int i=0; 
	int nSize=phonebook.GetSize()/3; 
	m_lstphonebook.ResetContent(); 
	i=nSize-1; 
	while(i>=0) 
	{ 
		CString indexstr,namestr,phonestr,str; 
		indexstr=phonebook[i*3]; 
		namestr=phonebook[i*3+1]; 
		phonestr=phonebook[i*3+2]; 
		str=indexstr; 
		str+=_T(": "); 
		str+=namestr; 
		str+=_T("  "); 
		str+=phonestr; 
		m_lstphonebook.AddString(str); 
		i--; 
	} 
 
} 
 
void CSmsEditView::DeCodeUni(CString sstr, CString &dstr) 
{ 
	CFile gfile; 
	CFileException e; 
    if( !gfile.Open( "e:/smsinterface/unitoansi.dit", CFile::modeRead|CFile::typeBinary, &e ) ) 
	{ 
     #ifdef _DEBUG 
         afxDump << "File could not be opened " << e.m_cause << "\n"; 
     #endif 
		 return; 
	} 
	WCHAR wch; 
	UINT result; 
	WORD tempch,ansistr; 
	unsigned char cha,chb; 
	int len,i; 
	i=0; 
	len=sstr.GetLength(); 
	while(i+1=str.GetLength())||(sindex>eindex)) 
	{ 
		substr=_T(""); 
		return; 
	} 
	startindex=sindex; 
	if(eindex>=str.GetLength()) 
		endindex=str.GetLength()-1; 
	else 
		endindex=eindex; 
	int i; 
	for(i=startindex;i<=endindex;i++) 
		substr.Insert(100,str.GetAt(i)); 
} 
 
void CSmsEditView::OnSelchangeListPhonebook()  
{ 
	// TODO: Add your control notification handler code here 
	UpdateData(); 
	CString stritem; 
	int nIndex =m_lstphonebook.GetCurSel(); 
	int nCount =m_lstphonebook.GetCount(); 
	if ((nIndex >=0 ) && (nCount >= 1)) 
	{ 
	   int n=m_lstphonebook.GetTextLen( nIndex ); 
	   m_lstphonebook.GetText(nIndex,stritem.GetBuffer(n)); 
	   stritem.ReleaseBuffer(); 
	   m_phonenum.Empty(); 
	   m_name.Empty(); 
	   m_index.Empty(); 
	   Getphbookitem(stritem,m_index,m_phonenum,m_name); 
	   isselected=TRUE; 
	} 
	UpdateData(FALSE); 
} 
 
void CSmsEditView::OnSetfocusListPhonebook()  
{ 
	// TODO: Add your control notification handler code here 
	UpdateData(); 
	CString stritem; 
	int nIndex =m_lstphonebook.GetCurSel(); 
	int nCount = m_lstphonebook.GetCount(); 
	if((nIndex >=0 ) && (nCount >= 1)) 
	{ 
	   int n=m_lstphonebook.GetTextLen( nIndex ); 
	   m_lstphonebook.GetText(nIndex,stritem.GetBuffer(n)); 
	   stritem.ReleaseBuffer(); 
	   m_phonenum.Empty(); 
	   m_name.Empty(); 
	   m_index.Empty(); 
	   Getphbookitem(stritem,m_index,m_phonenum,m_name); 
	   isselected=TRUE; 
	} 
	UpdateData(FALSE); 
} 
 
void CSmsEditView::Getphbookitem(CString stritem,CString &indexstr, CString &phonestr, CString &namestr) 
{ 
	int pos; 
	pos=stritem.Find(':',0); 
	GetSubStr(stritem,0,pos-1,indexstr); 
	int sindex,eindex; 
	sindex=pos+2; 
	pos=stritem.Find(' ',sindex); 
	eindex=pos-1; 
	GetSubStr(stritem,sindex,eindex,namestr); 
	GetSubStr(stritem,eindex+3,stritem.GetLength()-1,phonestr); 
 
} 
 
void CSmsEditView::OnButtonCancel()  
{ 
	// TODO: Add your control notification handler code here 
	CEdit* editwnd; 
	CEdit* editwnd2; 
	isoperating=FALSE; 
	editwnd=(CEdit*)this->GetDlgItem(IDC_EDIT_NAME); 
	::EnableWindow(editwnd->m_hWnd,isoperating); 
	editwnd2=(CEdit*)this->GetDlgItem(IDC_EDIT_PHONE); 
	::EnableWindow(editwnd2->m_hWnd,isoperating); 
	m_btncancel.EnableWindow(isoperating); 
	editwnd->SetFocus(); 
	editwnd->SetSel(0,-1); 
	m_lstphonebook.EnableWindow(!isoperating); 
	if(whichbtn==EDITENTRY) 
	{ 
		m_btnadd.EnableWindow(!isoperating); 
		m_btndel.EnableWindow(!isoperating);	 
		m_btnedit.SetWindowText("编      辑"); 
	} 
	if(whichbtn==ADDENTRY) 
	{ 
		m_btnedit.EnableWindow(!isoperating); 
		m_btndel.EnableWindow(!isoperating);	 
		m_btnadd.SetWindowText("添      加"); 
	} 
	if(whichbtn==EDITENTRY) 
	{ 
		m_btnadd.EnableWindow(!isoperating); 
		m_btnedit.EnableWindow(!isoperating);	 
		m_btndel.SetWindowText("删      除"); 
	} 
	whichbtn=0; 
	 
} 
 
void CSmsEditView::AnsiToUni(CString ans, CString &uni) 
{ 
	char ch[2]; 
	unsigned char cha,chb; 
	int i,len,re; 
	len=ans.GetLength(); 
	i=0; 
	while(i