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

📄 mobileassistantview.cpp

📁 一个收发手机短信的源代码
💻 CPP
字号:
// MobileAssistantView.cpp : implementation of the CMobileAssistantView class
//

#include "stdafx.h"
#include "MobileAssistant.h"
#include "MainFrm.h"
#include "MobileAssistantDoc.h"
#include "MobileAssistantView.h"
#include "PMPacket.h"

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

CMobileAssistantView* g_pCMobileAssistantView = NULL;
LRESULT Proc_ReceivedSMS ( SM_PARAM *pSMRecv, WPARAM wParam, LPARAM lParam )
{
	CMobileAssistantView *pMobileAssistantView = reinterpret_cast<CMobileAssistantView*>(wParam);
	ASSERT ( pMobileAssistantView );
	return pMobileAssistantView->Proc_ReceivedSMS ( pSMRecv );
}

/////////////////////////////////////////////////////////////////////////////
// CMobileAssistantView

IMPLEMENT_DYNCREATE(CMobileAssistantView, CFormView)

BEGIN_MESSAGE_MAP(CMobileAssistantView, CFormView)
	//{{AFX_MSG_MAP(CMobileAssistantView)
	ON_COMMAND(ID_BUTTON_SendSMS, OnBUTTONSendSMS)
	ON_WM_DESTROY()
	ON_COMMAND(ID_BUTTON_OpenPort, OnBUTTONOpenPort)
	ON_COMMAND(ID_BUTTON_Clear, OnBUTTONClear)
	ON_COMMAND(ID_BUTTON_Exit, OnBUTTONExit)
	ON_COMMAND(ID_BUTTON_RecvSMS, OnBUTTONRecvSMS)
	ON_COMMAND(ID_BUTTON_ReadSMS, OnBUTTONReadSMS)
	ON_COMMAND(ID_BUTTON_ReadPhoneBook, OnBUTTONReadPhoneBook)
	ON_BN_CLICKED(IDC_BUTTON_AddPhoneBook, OnBUTTONAddPhoneBook)
	ON_WM_SIZE()
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMobileAssistantView construction/destruction

CMobileAssistantView::CMobileAssistantView()
	: CFormView(CMobileAssistantView::IDD)
	, m_MobileControl ( Log )
	, m_nComNo ( 1 )
{
	//{{AFX_DATA_INIT(CMobileAssistantView)
	m_csReceiverMobileNO = _T("");
	m_csSMSContent = _T("");
	m_csBaudRate = _T("9600");
	m_csSMSC = _T("");
	m_csCountryCode = _T("");
	m_csMobileNOHead = _T("");
	//}}AFX_DATA_INIT
	// TODO: add construction code here
	g_pCMobileAssistantView = this;

}

CMobileAssistantView::~CMobileAssistantView()
{
	
}

void CMobileAssistantView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMobileAssistantView)
	DDX_Control(pDX, IDC_EDIT_Log, m_editLog);
	DDX_Text(pDX, IDC_EDIT_ReceiverMobileNO, m_csReceiverMobileNO);
	DDX_Text(pDX, IDC_EDIT_SMSContent, m_csSMSContent);
	DDX_CBString(pDX, IDC_COMBO_BaudRate, m_csBaudRate);
	DDX_Text(pDX, IDC_EDIT_SMSC, m_csSMSC);
	DDX_Text(pDX, IDC_EDIT_CountryCode, m_csCountryCode);
	DDX_Text(pDX, IDC_EDIT_MobileNOHead, m_csMobileNOHead);
	//}}AFX_DATA_MAP
}

BOOL CMobileAssistantView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CFormView::PreCreateWindow(cs);
}

void CMobileAssistantView::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();
	SetCtrlValue ();
	this->SetForegroundWindow ();
	SetTimer ( 1, 1000, NULL );
}

/////////////////////////////////////////////////////////////////////////////
// CMobileAssistantView printing

BOOL CMobileAssistantView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CMobileAssistantView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CMobileAssistantView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

void CMobileAssistantView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
{
	// TODO: add customized printing code here
}

/////////////////////////////////////////////////////////////////////////////
// CMobileAssistantView diagnostics

#ifdef _DEBUG
void CMobileAssistantView::AssertValid() const
{
	CFormView::AssertValid();
}

void CMobileAssistantView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

CMobileAssistantDoc* CMobileAssistantView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMobileAssistantDoc)));
	return (CMobileAssistantDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMobileAssistantView message handlers

void CMobileAssistantView::Align()
{
	if ( !::IsWindow ( m_editLog.m_hWnd ) )
		return;

	CRect rcClient;
	CWnd *pWndParent = GetParent ();
	GetWindowRect ( &rcClient );
	pWndParent->ScreenToClient ( &rcClient );
	MoveWindow ( &rcClient );

	CRect rc;
	m_editLog.GetWindowRect ( &rc );
	ScreenToClient ( &rc );

	GetClientRect ( &rcClient );
	CRect rcLog = ::hwMoveWindow ( this, &m_editLog,
		0, rc.top, rcClient.right, rcClient.bottom );

}

void CMobileAssistantView::AddLogStr(LPCTSTR lpszLog)
{
	if ( !::IsWindow ( m_editLog.m_hWnd ) )
		return;
	CString csLog;
	m_editLog.GetWindowText ( csLog );
	int nTextLen = csLog.GetLength ();
	CString csTemp = GET_SAFE_STRING ( lpszLog );
	if ( ::IsWindow ( m_editLog.m_hWnd ) )
	{
		m_editLog.SetSel ( nTextLen, -1, TRUE );
		m_editLog.ReplaceSel( csTemp );
	}
}

void CMobileAssistantView::SetCtrlValue()
{
	m_csReceiverMobileNO = AfxGetApp()->GetProfileString ( "SMS", "ReceiverMobileNO", "13510517717" );
	m_csCountryCode = AfxGetApp()->GetProfileString ( "SMS", "CountryCode", "+86" );
	m_csMobileNOHead = AfxGetApp()->GetProfileString ( "SMS", "MobileNOHead", "13" );
	m_csSMSContent = AfxGetApp()->GetProfileString ( "SMS", "SMSContent", "hello!" );
	m_csBaudRate = AfxGetApp()->GetProfileString ( "SMS", "BaudRate", "9600" );
	m_nComNo = AfxGetApp()->GetProfileInt ( "SMS", "COM", 1 );

	CComboBox *pcombo = (CComboBox*) GetDlgItem(IDC_COMBO_COM);
	ASSERT ( pcombo );
	for ( int i=0; i<16; i++ )
	{
		CString csCOM;
		csCOM.Format ( "COM%d", i+1 );
		pcombo->AddString ( csCOM );
	}
	if ( m_nComNo > 0 )
		pcombo->SetCurSel ( m_nComNo-1 );

	pcombo = (CComboBox*) GetDlgItem(IDC_COMBO_BaudRate);
	ASSERT ( pcombo );
	pcombo->AddString ( "1200" );
	pcombo->AddString ( "2400" );
	pcombo->AddString ( "4800" );
	pcombo->AddString ( "9600" );
	pcombo->AddString ( "19200" );
	pcombo->AddString ( "38400" );
	pcombo->AddString ( "57600" );
	pcombo->AddString ( "115200" );

	pcombo = (CComboBox*) GetDlgItem(IDC_COMBO_PhoneBook);
	pcombo->AddString ( "ME" );
	pcombo->AddString ( "SM" );
	pcombo->AddString ( "FD" );
	pcombo->AddString ( "DC" );
	pcombo->AddString ( "MC" );
	pcombo->AddString ( "RC" );
	pcombo->SetCurSel ( 0 );

	UpdateData ( FALSE );
}

void CMobileAssistantView::GetCtrlValue()
{
	UpdateData ( TRUE );
	CComboBox *pcombo = (CComboBox*) GetDlgItem(IDC_COMBO_COM);
	ASSERT ( pcombo );
	m_nComNo = pcombo->GetCurSel () + 1;
}

void CMobileAssistantView::OnDestroy() 
{	
	GetCtrlValue ();
	AfxGetApp()->WriteProfileString ( "SMS", "ReceiverMobileNO", m_csReceiverMobileNO );
	AfxGetApp()->WriteProfileString ( "SMS", "CountryCode", m_csCountryCode );
	AfxGetApp()->WriteProfileString ( "SMS", "MobileNOHead", m_csMobileNOHead );
	AfxGetApp()->WriteProfileString ( "SMS", "SMSContent", m_csSMSContent );
	AfxGetApp()->WriteProfileString ( "SMS", "BaudRate", m_csBaudRate );
	AfxGetApp()->WriteProfileInt ( "SMS", "COM", m_nComNo );

	CFormView::OnDestroy();
}

void CMobileAssistantView::OnBUTTONOpenPort() 
{
	ToolBarEnableButton ( ID_BUTTON_OpenPort, FALSE );
	GetCtrlValue ();
	m_MobileControl.SetParameter ( ::Proc_ReceivedSMS, WPARAM(this), NULL );
	m_MobileControl.ConfigParameter ( m_csMobileNOHead );
	if ( !m_MobileControl.Init ( TRUE, TRUE, g_hEvt_TerminateSys, m_nComNo, atoi(m_csBaudRate) ) )
		ToolBarEnableButton ( ID_BUTTON_OpenPort, TRUE );
}

void CMobileAssistantView::OnBUTTONClear() 
{
	m_editLog.SetWindowText ( "" );
}

void CMobileAssistantView::OnBUTTONExit() 
{
	AfxGetMainWnd ()-> PostMessage ( WM_CLOSE );
}

LRESULT Proc_SendSMSEnd ( ENUM_ERROR_TYPE eResult, WPARAM wParam, LPARAM lParam )
{
	CMobileAssistantView *pMobileAssistantView = reinterpret_cast<CMobileAssistantView*> ( wParam );
	ASSERT ( pMobileAssistantView );
	return pMobileAssistantView->Proc_SendSMSEnd ( eResult, wParam, lParam );
}

BOOL CMobileAssistantView::Proc_SendSMSEnd ( ENUM_ERROR_TYPE eResult, WPARAM wParam, LPARAM lParam )
{
	t_PktSMS *pPktSMS = reinterpret_cast<t_PktSMS*> ( wParam );
	t_SendSMSResult *pSendSMSResult = reinterpret_cast<t_SendSMSResult*> ( lParam );
	LogML ( L_NORMAL, "Send SMS [%s] to(#4) [%s] %s", pPktSMS->szByteText, pPktSMS->szMobileNO,
		(eResult==ENUM_ERROR_TYPE_SUCCESS)?"successfully":g_pErrorTypeDesc[eResult] );
	return TRUE;
}

void CMobileAssistantView::OnBUTTONSendSMS() 
{
	GetCtrlValue ();
	m_MobileControl.SetParameter ( ::Proc_ReceivedSMS, WPARAM(this), NULL );
	m_MobileControl.ConfigParameter ( m_csMobileNOHead );

	LogML ( L_VERBOSE, "Send SMS to(#4) [%s] ...", m_csReceiverMobileNO );
	if ( m_csReceiverMobileNO.GetLength () < 1|| m_csSMSContent.GetLength () < 1 )
	{
		MsgBoxML ( this, NULL, 0, "Enter error" );
		return;
	}

	CStringArray *pStrAry_SMSContent = PartMessengerOrSMSText ( m_csSMSContent, ENUM_MsgPeerType_Mobile, ENUM_MsgType_Private );
	if ( !pStrAry_SMSContent )
	{
		::AfxThrowMemoryException ();
		return;
	}
	for ( int i=0; i<pStrAry_SMSContent->GetSize(); i++ )
	{
		CString csOneSMSContent = pStrAry_SMSContent->GetAt ( i );
		WCHAR wzSMSContent[256] = {0};
		::MultiByteToWideChar(CP_ACP, 0, csOneSMSContent, csOneSMSContent.GetLength(), wzSMSContent, sizeof(wzSMSContent) );
		m_MobileControl.SendSMS ( m_csReceiverMobileNO, csOneSMSContent, wzSMSContent,
			INVALID_PMACCOUNT, ::Proc_SendSMSEnd, WPARAM(this) );
	}
	DELETE_ARRAY ( &pStrAry_SMSContent );	
}

void CMobileAssistantView::OnBUTTONRecvSMS() 
{
}

void CMobileAssistantView::OnBUTTONReadSMS() 
{
}

void CMobileAssistantView::OnBUTTONReadPhoneBook() 
{
	t_Ary_PhoneBook *pAry_PhoneBook = m_MobileControl.ReadPhoneBook ();
	if ( !pAry_PhoneBook )
	{
		LogML ( L_WARNING, "Read PhoneBook failed" );
		return;
	}

	for ( int i=0; i<pAry_PhoneBook->GetSize(); i++ )
	{
		t_PhoneBook &PhoneBook = pAry_PhoneBook->GetAt ( i );
		LogML ( L_NORMAL, "电话本信息:NO.%d - [%s] - %s - %s", PhoneBook.nIndex,
			PhoneBook.szPhoneBookName, PhoneBook.szName, PhoneBook.szPhoneNO );
	}

	DbgLog ( L_DEBUG, "读取电话本已经全部结束, 共 %d 条纪录", pAry_PhoneBook->GetSize() );
	DELETE_ARRAY ( &pAry_PhoneBook );
}

BOOL CMobileAssistantView::Proc_ReceivedSMS ( SM_PARAM *pSMRecv )
{
	ASSERT ( pSMRecv );
	if ( !pSMRecv ) return FALSE;
	char szRecvTime[DATETIME_TYPE_LENGTH+1] = {0};
	m_MobileControl.Convert_SCTS2Standard ( pSMRecv->TP_SCTS, szRecvTime, sizeof(szRecvTime) );
	LogML ( L_NORMAL, "\r\nReceived SMS : NO.%d - %s - [%s]\r\n$%s$\r\n", pSMRecv->index, pSMRecv->TPA, szRecvTime, pSMRecv->TP_UD_Byte );
	return TRUE;
}

void CMobileAssistantView::OnBUTTONAddPhoneBook() 
{
	CString csName, csPhoneNO, csPhoneBook;
	GetDlgItemText ( IDC_EDIT_Name, csName );
	GetDlgItemText ( IDC_EDIT_PhoneNO, csPhoneNO );
	GetDlgItemText ( IDC_COMBO_PhoneBook, csPhoneBook );
	if ( csName.GetLength () < 1 || csPhoneNO.GetLength () < 1 || csPhoneBook.GetLength () < 1 )
	{
		MsgBoxML ( NULL, NULL, 0, "Enter Phone Datum Error" );
		return;
	}

	if ( !m_MobileControl.InsertPhoneBook ( csName, csPhoneNO, csPhoneBook ) )
	{
		MsgBoxML ( this, NULL, 0, "Insert PhoneBook Failed" );
	}
}

void CMobileAssistantView::OnSize(UINT nType, int cx, int cy) 
{
	CFormView::OnSize(nType, cx, cy);
	
	Align ();
}

BOOL CMobileAssistantView::ToolBarEnableButton(int nID, BOOL bEnable)
{
	CToolBarCtrl &ToolBarCtrl = ( (CMainFrame*)AfxGetMainWnd() )->m_wndToolBar.GetToolBarCtrl ();
	return ToolBarCtrl.EnableButton ( nID, bEnable );
}

void CMobileAssistantView::OnTimer(UINT nIDEvent) 
{
	if ( m_csSMSC.GetLength() <= 0 )
	{
		CString csSMSC = m_MobileControl.RetrievesSMSC ();
		if ( csSMSC.GetLength() > 0 )
		{
			KillTimer ( nIDEvent );
			UpdateData ( TRUE );
			m_csSMSC = csSMSC;
			UpdateData ( FALSE );
		}
	}
	CFormView::OnTimer(nIDEvent);
}

⌨️ 快捷键说明

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