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

📄 smssenddlg.cpp

📁 通过GSM模块发送接收短消息的程序
💻 CPP
字号:
// SMSSendDlg.cpp : implementation file
//

#include "stdafx.h"
#include "SMSSend.h"
#include "SMSSendDlg.h"
#include "SettingsDlg.h"
#include "Comm.h"


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

#define WM_MY_TRAY_NOTIFICATION WM_USER+0
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSMSSendDlg dialog

CSMSSendDlg::CSMSSendDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSMSSendDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSMSSendDlg)
	m_content = _T("");
	m_number = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CSMSSendDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSMSSendDlg)
	DDX_Control(pDX, IDC_HAND, m_hand_ctrl);
	DDX_Control(pDX, IDC_LIST1, ListCtrl);
	DDX_Control(pDX, IDC_NUMBER, m_ctrl_number);
	DDX_Control(pDX, IDC_CONTENT, m_ctrl_content);
	DDX_CBString(pDX, IDC_CONTENT, m_content);
	DDX_CBString(pDX, IDC_NUMBER, m_number);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSMSSendDlg, CDialog)
	//{{AFX_MSG_MAP(CSMSSendDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_SEND, OnSend)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDSETING, OnSeting)
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_HAND, OnHand)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSMSSendDlg message handlers

BOOL CSMSSendDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	m_strSmsc="13800240500";

	m_pSmsTraffic = new CSmsTraffic;
	ListCtrl.InsertColumn(0, "号码", LVCFMT_LEFT, 100);
	ListCtrl.InsertColumn(1, "时间", LVCFMT_LEFT, 140);
	ListCtrl.InsertColumn(2, "消息内容", LVCFMT_LEFT, 1000);

	SetTimer(101, 1000, NULL);
	m_hand_ctrl.SetCheck(0);

/*	m_bShowTrayNotifications = TRUE;//zxn
	m_bShutdown = FALSE;//zxn

	m_trayIcon.SetNotificationWnd(this, WM_MY_TRAY_NOTIFICATION);
	m_iWhichIcon = 1;
	m_trayIcon.SetIcon(IDI_MYICON);
*/	
	this->ShowWindow(SW_MINIMIZE);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CSMSSendDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CSMSSendDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CSMSSendDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CSMSSendDlg::OnSend() 
{
	// TODO: Add your control notification handler code here

	CString strSmsc;
	CString strNumber;
	CString strContent;
	UpdateData(1);
	strSmsc = m_strSmsc;
	strNumber=m_number;
	strContent=m_content;


	// 检查号码
	if(strNumber.GetLength() < 11)
	{
		AfxMessageBox("请输入正确的号码!");
		return;
	}

	// 检查短消息内容是否空,或者超长
	CString strUnicode;
	WCHAR wchar[1024];
	int nCount = ::MultiByteToWideChar(CP_ACP, 0, strContent, -1, wchar, 1024);
	if(nCount <= 1)
	{
		AfxMessageBox("请输入消息内容!");
		return;
	}
	else if(nCount > 70)		// 我们决定全部用UCS2编码,最大70个字符(半角/全角)
	{
		AfxMessageBox("消息内容太长,无法发送!");
		return;
	}

//	if(AfxMessageBox("确定发送吗?", MB_YESNO) == IDYES)
	{
		SM_PARAM SmParam;

		memset(&SmParam, 0, sizeof(SM_PARAM));

		// 去掉号码前的"+"
		if(strSmsc[0] == '+')  strSmsc = strSmsc.Mid(1);
		if(strNumber[0] == '+')  strNumber = strNumber.Mid(1);

		// 在号码前加"86"
		if(strSmsc.Left(2) != "86")  strSmsc = "86" + strSmsc;
		if(strNumber.Left(2) != "86")  strNumber = "86" + strNumber;

		// 填充短消息结构
		strcpy(SmParam.SCA, strSmsc);
		strcpy(SmParam.TPA, strNumber);
		strcpy(SmParam.TP_UD, strContent);
		SmParam.TP_PID = 0;
		if(m_hand)
			SmParam.TP_DCS = GSM_UCS20;
		else
			SmParam.TP_DCS = GSM_UCS2;

		// 发送短消息
//		for(int i=0;i<10;i++)
		{
		m_pSmsTraffic->PutSendMessage(&SmParam);
		// 列表中加入新串
		if(m_ctrl_number.FindStringExact(-1, strNumber)<0)  m_ctrl_number.InsertString(0, strNumber);
		if(m_ctrl_content.FindStringExact(-1, strContent)<0)  m_ctrl_content.InsertString(0, strContent);
		}
	}
	
}

void CSMSSendDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	if(nIDEvent == 101)
	{
		SM_PARAM SmParam;
		CString strTime;
		CString strNumber;
		CString strContent;
		
		//		CListCtrl& ListCtrl = GetListCtrl();
		
		// 取接收到的短消息
		if(m_pSmsTraffic->GetRecvMessage(&SmParam))
		{
			// 取短消息信息
			strNumber = SmParam.TPA;
			strContent = SmParam.TP_UD;
			strTime = "20" + CString(&SmParam.TP_SCTS[0],2) 
				+ "-" + CString(&SmParam.TP_SCTS[2],2) 
				+ "-" + CString(&SmParam.TP_SCTS[4],2)
				+ " " + CString(&SmParam.TP_SCTS[6],2) 
				+ ":" + CString(&SmParam.TP_SCTS[8],2) 
				+ ":" + CString(&SmParam.TP_SCTS[10],2);
			
			// 去掉号码前的"86"
			if(strNumber.Left(2) == "86")  strNumber = strNumber.Mid(2);
			
			// 最多保留200条
			int nItemCount = ListCtrl.GetItemCount();
			if(nItemCount >= 200)
			{
				ListCtrl.DeleteItem(0);
				nItemCount--;
			}
			
			// 插入新消息
			ListCtrl.InsertItem(nItemCount, strNumber);
			ListCtrl.SetItemText(nItemCount, 1, strTime);
			ListCtrl.SetItemText(nItemCount, 2, strContent);
			ListCtrl.EnsureVisible(nItemCount, FALSE);
			CFile fp;
			if(fp.Open(receive_file,fp.modeReadWrite|fp.typeBinary))
			{
				char cc[5];
				fp.Seek(0,fp.end);
				cc[0]=0x0d;cc[1]=0x0a;
				fp.Write(cc,2);
				strcpy(cc,"    ");
				fp.Write(strNumber,strNumber.GetLength());
				fp.Write(cc,5);
				fp.Write(strTime,strTime.GetLength());
				fp.Write(cc,5);
				fp.Write(strContent,strContent.GetLength());
				fp.Close();
			}
			else
			{
				if(fp.Open(receive_file,fp.modeWrite|fp.modeCreate|fp.typeBinary))
				{
					char cc[5];
					cc[0]=0x0d;cc[1]=0x0a;
					fp.Write(cc,2);
					strcpy(cc,"    ");
					fp.Write(strNumber,strNumber.GetLength());
					fp.Write(cc,5);
					fp.Write(strTime,strTime.GetLength());
					fp.Write(cc,5);
					fp.Write(strContent,strContent.GetLength());
					fp.Close();
				}
			}		
		}
	}
	CFile fp;
	if(fp.Open(message_file,fp.modeReadWrite|fp.typeBinary))
	{
		int i=0,k=0,m,l;
		char num[100][13],cc,txt[500];//,txttmp[70];
		while(1)
		{
			fp.Read(&cc,1);
			if(cc>='0'&&cc<='9'&&k<12)
			{
				num[i][k++]=cc;
			}
			else
			{
				num[i][k]=0;
				if(cc==',')
				{
					k=0;
					if(i<100) i++;
				}
				else
				{
					if(i<100) i++;
					break;
				}
			}
		}
		while(cc==0x0d||cc==0x0a)
		fp.Read(&cc,1);
		k=0;
		while(1)
		{
			if(k<450)
			{
				txt[k++]=cc;
			}
			else
			{
				if(k>=450)
					break;
			}
			if(fp.Read(&cc,1)!=1) 
			{
				txt[k]=0;
				break;
			}
		}
		fp.Close();
		DeleteFile(message_file);
		CString strSmsc;
		CString strNumber;
		CString strContent;
		CString strContent_tmp;
		CString strUnicode;
		WCHAR wchar[1024];
		
		strSmsc = m_strSmsc;
		strContent_tmp=txt;
		l=strlen(txt);
		m=0;
		while(l>m)
		{
			strContent=strContent_tmp.Right(strContent_tmp.GetLength()-m);
			if(::MultiByteToWideChar(CP_ACP, 0, strContent, -1, wchar, 1024)>70)
			{
				while(::MultiByteToWideChar(CP_ACP, 0, strContent, -1, wchar, 1024)>70)
				{
					strContent=strContent.Left(strContent.GetLength()-1);
				}
				int m_num=0,m_len,ch;
				m_len=strContent.GetLength();
				for(k=0;k<m_len;k++)
				{
					ch=strContent.GetAt(k)&0xff;
					if(ch<0x80)
						m_num=0;
					else m_num++;
				}
				if(m_num%2)
					strContent=strContent.Left(strContent.GetLength()-1);
				m+=strContent.GetLength();
			}
			else
				m=l;
			k=0;
			while(k<i)
			{
				strNumber=num[k++];
				
				// 检查号码
				if(strNumber.GetLength() < 11)
				{
					AfxMessageBox("请输入正确的号码!");
					break;
				}
				// 检查短消息内容是否空,或者超长
				int nCount = ::MultiByteToWideChar(CP_ACP, 0, strContent, -1, wchar, 1024);
				if(nCount <= 1)
				{
					AfxMessageBox("请输入消息内容!");
					break;
				}
				else if(nCount > 70)		// 我们决定全部用UCS2编码,最大70个字符(半角/全角)
				{
					AfxMessageBox("消息内容太长,无法发送!");
					break;
				}
				
				SM_PARAM SmParam;
				
				memset(&SmParam, 0, sizeof(SM_PARAM));
				
				// 去掉号码前的"+"
				if(strSmsc[0] == '+')  strSmsc = strSmsc.Mid(1);
				if(strNumber[0] == '+')  strNumber = strNumber.Mid(1);
				
				// 在号码前加"86"
				if(strSmsc.Left(2) != "86")  strSmsc = "86" + strSmsc;
				if(strNumber.Left(2) != "86")  strNumber = "86" + strNumber;
				
				// 填充短消息结构
				strcpy(SmParam.SCA, strSmsc);
				strcpy(SmParam.TPA, strNumber);
				strcpy(SmParam.TP_UD, strContent);
				SmParam.TP_PID = 0;
				if(m_hand)
					SmParam.TP_DCS = GSM_UCS20;
				else
					SmParam.TP_DCS = GSM_UCS2;
				
				// 发送短消息
				m_pSmsTraffic->PutSendMessage(&SmParam);
			}
		}
	}
	CDialog::OnTimer(nIDEvent);
}
		
void CSMSSendDlg::OnSeting() 
{
	// TODO: Add your control notification handler code here
	CSettingsDlg dlg;
	dlg.m_strPort = m_strPort;
	dlg.m_strRate = m_strRate;

	if(dlg.DoModal() == IDOK)
	{
		if(m_strPort != dlg.m_strPort)
		{
			AfxMessageBox("端口设置在下次启动程序时生效");
		}
		m_strPort = dlg.m_strPort;
		m_strRate = dlg.m_strRate;
	}
}

void CSMSSendDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
	if (m_pSmsTraffic != NULL)
	{
		delete m_pSmsTraffic;
	}
	
	CloseComm();
	CFile fp;
	if(fp.Open(system_file,fp.modeWrite|fp.modeCreate|fp.typeBinary))
	{
		fp.Write(m_strPort,4);
		fp.Write(m_strRate,m_strRate.GetLength());
		fp.Close();
	}
	
}

void CSMSSendDlg::OnHand() 
{
	// TODO: Add your control notification handler code here
	m_hand=m_hand_ctrl.GetCheck();
}

⌨️ 快捷键说明

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