📄 smsdemodlg.cpp
字号:
// SMSdemoDlg.cpp : implementation file
//
#include "stdafx.h"
#include "SMSdemo.h"
#include "SMSdemoDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CWinThread* pSockThread;
int receivemsg(SM_PARAM * sm_param_temp);
/////////////////////////////////////////////////////////////////////////////
// CSMSdemoDlg dialog
CSMSdemoDlg::CSMSdemoDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSMSdemoDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSMSdemoDlg)
m_COM = _T("COM3");
m_smsmsg = _T("");
m_phone = _T("13488898030");
m_sendstate = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
InitializeCriticalSection(&m_csSend);
InitializeCriticalSection(&m_csRecv);
}
void CSMSdemoDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSMSdemoDlg)
DDX_Control(pDX, IDC_BUTTON6, m_6);
DDX_Control(pDX, IDC_BUTTON5, m_5);
DDX_Control(pDX, IDC_BUTTON4, m_4);
DDX_Control(pDX, IDC_BUTTON3, m_3);
DDX_Control(pDX, IDC_COMBO2, m_BoxPhone);
DDX_Control(pDX, IDC_BUTTON2, m_close);
DDX_Control(pDX, IDC_BUTTON1, m_contant);
DDX_Control(pDX, IDC_LIST2, m_listsms);
DDX_Control(pDX, IDC_EDIT1, m_SMSmsg);
DDX_CBString(pDX, IDC_COMBO1, m_COM);
DDX_Text(pDX, IDC_EDIT1, m_smsmsg);
DDV_MaxChars(pDX, m_smsmsg, 140);
DDX_CBString(pDX, IDC_COMBO2, m_phone);
DDX_Text(pDX, IDC_EDIT2, m_sendstate);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSMSdemoDlg, CDialog)
//{{AFX_MSG_MAP(CSMSdemoDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
ON_BN_CLICKED(IDC_BUTTON6, OnButton6)
ON_CBN_SELENDCANCEL(IDC_COMBO2, OnSelendcancelCombo2)
ON_CBN_DBLCLK(IDC_COMBO2, OnDblclkCombo2)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSMSdemoDlg message handlers
BOOL CSMSdemoDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
BindRes2CtrlbyHWND(99,m_hWnd);
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE);
m_listsms.SetExtendedStyle( LVS_EX_GRIDLINES|LVS_EX_FULLROWSELECT|LVS_EX_CHECKBOXES);
m_listsms.InsertColumn(0,_T("发送号码"),LVCFMT_LEFT,100,100);
m_listsms.InsertColumn(1,_T("时间"),LVCFMT_LEFT,100,100);
m_listsms.InsertColumn(2,_T("内容"),LVCFMT_LEFT,1000,100);
m_3.EnableWindow(FALSE);
m_4.EnableWindow(FALSE);
m_5.EnableWindow(FALSE);
m_6.EnableWindow(FALSE);
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
// 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 CSMSdemoDlg::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 CSMSdemoDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CSMSdemoDlg::OnButton1()
{
// TODO: Add your control notification handler code here
UpdateData();
if (OpenComm(m_COM,115200,NOPARITY,8, ONE5STOPBITS))
{
SetSendModule(0);
m_contant.EnableWindow(FALSE);
m_3.EnableWindow(TRUE);
m_4.EnableWindow(TRUE);
m_5.EnableWindow(TRUE);
m_6.EnableWindow(TRUE);
pSockThread = AfxBeginThread(readmessage,this);
}
}
void CSMSdemoDlg::OnButton3()
{
// TODO: Add your control notification handler code here
SM_PARAM *sm_param_temp;
sm_param_temp= new SM_PARAM;
CString phone;
BOOL same=FALSE;
// pSockThread->SuspendThread();
SetCursor(LoadCursor(NULL,IDC_WAIT));
m_sendstate = "";
UpdateData();
if ( SendShortMessage(m_phone,m_smsmsg,sm_param_temp) == 0)
{
m_sendstate = "发送成功!!!";
m_BoxPhone.GetWindowText(phone);
for (int i = 0; i<=m_BoxPhone.GetCount(); i++)
{
if (phone == m_BoxPhone.GetItemData(i))
{
same = TRUE;
}
}
}
else
m_sendstate = "发送失败!!!";
if (!same)
{
m_BoxPhone.AddString(phone);
}
UpdateData(FALSE);
SetCursor(LoadCursor(NULL,IDC_ARROW));
pSockThread->ResumeThread();
}
void CSMSdemoDlg::OnButton2()
{
// TODO: Add your control notification handler code here
if(CloseComm())
{
m_contant.EnableWindow(TRUE);
m_3.EnableWindow(FALSE);
m_4.EnableWindow(FALSE);
m_5.EnableWindow(FALSE);
m_6.EnableWindow(FALSE);
}
}
void CSMSdemoDlg::OnButton4()
{
// TODO: Add your control notification handler code here
SetCursor(LoadCursor(NULL,IDC_WAIT));
UpdateData();
m_listsms.DeleteAllItems();
SM_PARAM *sm_param_temp=new SM_PARAM[70];
int temp=ReceiveAllMessage(sm_param_temp);
int i=0;
while(i<temp)
{
m_listsms.InsertItem(LVIF_TEXT|LVIF_STATE,i,sm_param_temp->TPA,(i%2)==0?LVIS_SELECTED:0,LVIS_SELECTED,0,0);
m_listsms.SetItemText(i,1, InvertTime(sm_param_temp->TP_SCTS));
m_listsms.SetItemText(i,2, sm_param_temp->TP_UD);
sm_param_temp++;
i++;
}
SetCursor(LoadCursor(NULL,IDC_ARROW));
UpdateData(FALSE);
}
CString CSMSdemoDlg::InvertTime(char temp[])
{
CString strResult="20";
CString temp1;
int i;
for(i=0;i<6;i=i+2)
{
temp1.Format("%c",temp[i]);
strResult+=temp1;
temp1.Format("%c",temp[i+1]);
strResult+=temp1;
if(i<4)
strResult+="-";
}
strResult+=" ";
for(i=6;i<14;i=i+2)
{
temp1.Format("%c",temp[i]);
strResult+=temp1;
temp1.Format("%c",temp[i+1]);
strResult+=temp1;
if(i<12)
strResult+=":";
}
return strResult;
}
void CSMSdemoDlg::OnButton5()
{
// TODO: Add your control notification handler code here
SetCursor(LoadCursor(NULL,IDC_WAIT));
// UpdateData();
// m_listsms.DeleteAllItems();
SM_PARAM *sm_param_temp=new SM_PARAM[15];
int temp=ReceiveUnreadMessage(sm_param_temp);
int i=0;
while(i<temp)
{
m_listsms.InsertItem(LVIF_TEXT|LVIF_STATE,i,sm_param_temp->TPA,(i%2)==0?LVIS_SELECTED:0,LVIS_SELECTED,0,0);
m_listsms.SetItemText(i,1, InvertTime(sm_param_temp->TP_SCTS));
m_listsms.SetItemText(i,2, sm_param_temp->TP_UD);
sm_param_temp++;
i++;
}
SetCursor(LoadCursor(NULL,IDC_ARROW));
// UpdateData(FALSE);
}
void CSMSdemoDlg::OnButton6()
{
// TODO: Add your control notification handler code here
SetCursor(LoadCursor(NULL,IDC_WAIT));
UpdateData();
m_listsms.DeleteAllItems();
for (int i = 1; i<=70; i++)
{
if(!DeleteMessage(i))
{
MessageBox("删除失败","删除所有短消息",MB_OK);
return;
}
}
SetCursor(LoadCursor(NULL,IDC_ARROW));
UpdateData(FALSE);
}
UINT CSMSdemoDlg::readmessage(LPVOID pParam)
{
while (1)
{
int temp;
CSMSdemoDlg * dlg = (CSMSdemoDlg *)pParam;
Sleep(5000);
SM_PARAM *sm_param_temp=new SM_PARAM[15];
temp = 0;
temp=receivemsg(sm_param_temp);
int i=0;
while(i<temp)
{
dlg->m_listsms.InsertItem(LVIF_TEXT|LVIF_STATE,i,sm_param_temp->TPA,(i%2)==0?LVIS_SELECTED:0,LVIS_SELECTED,0,0);
dlg->m_listsms.SetItemText(i,1, dlg->InvertTime(sm_param_temp->TP_SCTS));
dlg->m_listsms.SetItemText(i,2, sm_param_temp->TP_UD);
sm_param_temp++;
i++;
}
}
return 1;
}
int receivemsg(SM_PARAM * sm_param_temp)
{
int temp=ReceiveUnreadMessage(sm_param_temp);
return temp;
}
void CSMSdemoDlg::UpdateDataTrue()
{
EnterCriticalSection(&m_csRecv);
UpdateData();
LeaveCriticalSection(&m_csRecv);
}
void CSMSdemoDlg::UpdateDataFalse()
{
EnterCriticalSection(&m_csRecv);
UpdateData(FALSE);
LeaveCriticalSection(&m_csRecv);
}
void CSMSdemoDlg::OnSelendcancelCombo2()
{
// int i =m_BoxPhone.GetCurSel();
// m_BoxPhone.DeleteString(i); // TODO: Add your control notification handler code here
}
void CSMSdemoDlg::OnDblclkCombo2()
{
// TODO: Add your control notification handler code here
}
//DEL void CSMSdemoDlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
//DEL {
//DEL // TODO: Add your message handler code here and/or call default
//DEL CString in;
//DEL CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
//DEL }
BOOL CSMSdemoDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if (pMsg->wParam == VK_DELETE)
{
int i =m_BoxPhone.GetCurSel();
m_BoxPhone.DeleteString(i);
}
return CDialog::PreTranslateMessage(pMsg);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -