ShortMessageDlg.cpp
资源名称:Mesctrl.rar [点击查看]
上传用户:wang776455
上传日期:2022-06-12
资源大小:4721k
文件大小:9k
源码类别:
手机短信编程
开发平台:
Visual C++
- // ShortMessageDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "ShortMessage.h"
- #include "ShortMessageDlg.h"
- #include "SmsTraffic.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- CSmsTraffic* m_pSmsTraffic;
- BOOL bIsOpenComm;
- /////////////////////////////////////////////////////////////////////////////
- // CAboutDlg dialog used for App About
- class CAboutDlg : public CDialog
- {
- public:
- CAboutDlg();
- // Dialog Data
- //{{AFX_DATA(CAboutDlg)
- enum { IDD = IDD_ABOUTBOX };
- CListBox m_ListBox;
- //}}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)
- virtual void OnOK();
- //}}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)
- DDX_Control(pDX, IDC_LIST1, m_ListBox);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
- //{{AFX_MSG_MAP(CAboutDlg)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CShortMessageDlg dialog
- CShortMessageDlg::CShortMessageDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CShortMessageDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CShortMessageDlg)
- m_strMessage = _T("");
- m_strCenterNumber = _T("");
- m_strMobileNo = _T("");
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
- void CShortMessageDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CShortMessageDlg)
- DDX_Control(pDX, IDC_LIST1, m_ListCtrl);
- DDX_Text(pDX, IDC_EDIT3, m_strMessage);
- DDX_Text(pDX, IDC_EDIT1, m_strCenterNumber);
- DDX_Text(pDX, IDC_EDIT2, m_strMobileNo);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CShortMessageDlg, CDialog)
- //{{AFX_MSG_MAP(CShortMessageDlg)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_WM_TIMER()
- ON_WM_DESTROY()
- ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CShortMessageDlg message handlers
- BOOL CShortMessageDlg::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
- if(OpenComm("COM1", 115200) == FALSE)
- {
- CString strError;
- strError = "无法打开串口1! ";
- MessageBox(strError, "警告", MB_OK);
- bIsOpenComm = FALSE;
- return TRUE;
- }
- else
- {
- m_pSmsTraffic = new CSmsTraffic;
- bIsOpenComm = TRUE;
- }
- m_strCenterNumber = "+8613800571500";
- /*
- m_ListCtrl.InsertColumn(0,"手机号码",LVCFMT_LEFT,80,-1);
- m_ListCtrl.InsertColumn(1,"短消息内容",LVCFMT_LEFT,620,-1);
- m_ListCtrl.InsertColumn(2,"时间",LVCFMT_LEFT,100,-1);
- */
- m_ListCtrl.InsertColumn(0,"接受短消息内容",LVCFMT_LEFT,800,-1);
- UpdateData(FALSE);
- SetTimer(1, 10001, NULL);
- return TRUE; // return TRUE unless you set the focus to a control
- }
- void CShortMessageDlg::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 CShortMessageDlg::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 CShortMessageDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
- void CShortMessageDlg::OnTimer(UINT nIDEvent)
- {
- // TODO: Add your message handler code here and/or call default
- if(nIDEvent == 1)
- {
- if(bIsOpenComm == TRUE)
- {
- SM_PARAM SmParam;
- // 取接收到的短消息
- while(m_pSmsTraffic->GetRecvMessage(&SmParam))
- {
- CString strNumber;
- CString strContent;
- CString strTemp;
- CString strTime;
- // 取短消息信息
- strNumber = SmParam.TPA;
- strContent = SmParam.TP_UD;
- strTemp = SmParam.TP_SCTS;
- strTime = "20" + strTemp.Mid(0,2)+ "-" + strTemp.Mid(2,2) + "-" + strTemp.Mid(4,2) + " " + strTemp.Mid(6,2)+ ":" + strTemp.Mid(8,2)+ ":" + strTemp.Mid(10,2);
- // 去掉号码前的"86"
- if(strNumber.Mid(0, 2) == "86")
- strNumber = strNumber.Mid(2,strNumber.GetLength() - 2);
- /*
- m_ListCtrl.InsertColumn(0,strNumber,LVCFMT_LEFT,80,-1);
- m_ListCtrl.InsertColumn(1,strContent,LVCFMT_LEFT,320,-1);
- m_ListCtrl.InsertColumn(2,strTime,LVCFMT_LEFT,100,-1);
- */
- m_ListCtrl.InsertItem(0, strNumber);
- m_ListCtrl.InsertItem(1, strContent);
- m_ListCtrl.InsertItem(2, strTime);
- m_ListCtrl.InsertItem(3, "");
- }
- }
- }
- CDialog::OnTimer(nIDEvent);
- }
- void CShortMessageDlg::SmsSend(CString centerNumber, CString mobileNo, CString message, char mode)
- {
- CString strSmsc;
- CString strNumber;
- CString strContent;
- strSmsc = centerNumber;
- strNumber = mobileNo;
- strContent = message;
- // 检查号码
- if(strNumber.GetLength() < 11)
- {
- // ShowMessage("请输入正确的号码!");
- return;
- }
- // 检查短消息内容是否空,或者超长
- CString strUnicode;
- WCHAR wchar[1024];
- int nCount = ::MultiByteToWideChar(CP_ACP, 0, strContent, -1, wchar, 1024);
- if(nCount <= 1)
- {
- // ShowMessage("请输入消息内容!");
- return;
- }
- else if(nCount > 70) // 我们决定全部用UCS2编码,最大70个字符(半角/全角)
- {
- // ShowMessage("消息内容太长,无法发送!");
- return;
- }
- SM_PARAM SmParam;
- ::memset(&SmParam, 0, sizeof(SM_PARAM));
- // 去掉号码前的"+"
- if(strSmsc[0] == '+') strSmsc = strSmsc.Mid(1, strSmsc.GetLength()-1);
- if(strNumber[0] == '+') strNumber = strNumber.Mid(1, strNumber.GetLength()-1);
- // 在号码前加"86"
- if(strSmsc.Mid(0,2) != "86") strSmsc = "86" + strSmsc;
- if(strNumber.Mid(0,2) != "86") strNumber = "86" + strNumber;
- // 填充短消息结构
- ::strcpy(SmParam.SCA, strSmsc);
- ::strcpy(SmParam.TPA, strNumber);
- ::strcpy(SmParam.TP_UD, strContent);
- SmParam.TP_PID = 0;
- //? SmParam.TP_DCS = GSM_7BIT;
- //? SmParam.TP_DCS = GSM_8BIT;
- SmParam.TP_DCS = GSM_UCS2;
- SmParam.mode = mode;
- // 发送短消息
- m_pSmsTraffic->PutSendMessage(&SmParam);
- }
- void CShortMessageDlg::OnDestroy()
- {
- CDialog::OnDestroy();
- // TODO: Add your message handler code here
- KillTimer(1);
- delete m_pSmsTraffic;
- }
- void CShortMessageDlg::OnOK()
- {
- // TODO: Add extra validation here
- //text mode
- UpdateData(TRUE);
- CString centerNumber = m_strCenterNumber;
- CString mobileNo = m_strMobileNo;
- CString message = m_strMessage;
- char mode = 1;
- SmsSend(centerNumber, mobileNo, message, mode);
- //?CDialog::OnOK();
- }
- void CShortMessageDlg::OnButton1()
- {
- // TODO: Add your control notification handler code here
- //pdu mode
- UpdateData(TRUE);
- CString centerNumber = m_strCenterNumber;
- CString mobileNo = m_strMobileNo;
- CString message = m_strMessage;
- char mode = 0;
- SmsSend(centerNumber, mobileNo, message, mode);
- }
- void CAboutDlg::OnOK()
- {
- // TODO: Add extra validation here
- CDialog::OnOK();
- }