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

📄 smssenddlg.cpp

📁 基于VC++6的GSM模块收发短消息的程序
💻 CPP
字号:
// SMSSendDlg.cpp : implementation file
//

#include "stdafx.h"
#include "SMSSend.h"
#include "SMSSendDlg.h"
#include "SettingsDlg.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("");
	m_pdu = -1;
	//}}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);
	DDX_Radio(pDX, IDC_RADIO1, m_pdu);
	//}}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_pSmsTraffic = new CSmsTraffic;
	m_pSmsTraffic1 = new CSmsTraffic;
	if(m_pSmsTraffic->OpenComm("COM1", 9600))
	{
		if (!m_pSmsTraffic->gsmInit())
		{
			AfxMessageBox("该端口上没有MODEM!或波特率不正确!");
			
		}
		
	}
	if(m_pSmsTraffic1->OpenComm("COM1", 9600))
	{
		if (!m_pSmsTraffic1->gsmInit())
		{
			AfxMessageBox("该端口上没有MODEM!或波特率不正确!");
			
		}
		
	}
	m_strSmsc="13800417500";

	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);
*/
	m_pdu=1;
	m_number="13840766662";
	m_content="1服务器发送短信和接收短信可使用各自的猫,服务器发送短信和接收短信可使用各自的猫,服务器发送短信和接收短信可使用各自的猫,服务器发送短信和接收短信可使用各自的猫,";
	UpdateData(0);
	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 
	{
		int size;
		if(m_pdu)
			size=70;
		else
			size=140;
		if(nCount > size)		// 我们决定全部用UCS2编码,最大70个字符(半角/全角)
		{
			AfxMessageBox("消息内容太长,无法发送!");
			return;
		}
	}*/
	
	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_pdu)
		SmParam.TP_DCS = GSM_UCS2;
	else
		SmParam.TP_DCS = GSM_7BIT;
	if(m_hand)
		SmParam.TP_DCS |= 0x10;
	
	m_pSmsTraffic->PutSendMessage(&SmParam);
	m_pSmsTraffic1->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);
		}
	}

	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)
	{
	m_pSmsTraffic->CloseComm();
		delete m_pSmsTraffic;
	}
	if (m_pSmsTraffic1 != NULL)
	{
	m_pSmsTraffic1->CloseComm();
		delete m_pSmsTraffic1;
	}
	
}

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


BOOL CSMSSendDlg::CheckSettings()
{
	int nRate;

	sscanf(m_strRate, "%d", &nRate);

	while (!m_pSmsTraffic->OpenComm(m_strPort, nRate))
	{
		CString strError;
		strError.Format("无法打开端口%s! 现在设置吗?", m_strPort);
		
		if (AfxMessageBox(strError, MB_YESNO) == IDNO) return FALSE;

		CSettingsDlg dlg;
	
		dlg.m_strPort = m_strPort;
		dlg.m_strRate = m_strRate;

		if (dlg.DoModal() == IDOK)
		{
			m_strPort = dlg.m_strPort;
			m_strRate = dlg.m_strRate;
		}
		else
		{
			return FALSE;
		}
	}

	if (!m_pSmsTraffic->gsmInit())
	{
		AfxMessageBox("该端口上没有MODEM!或波特率不正确!");

		return FALSE;
	}

	return TRUE;
}

⌨️ 快捷键说明

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