📄 mainfrm.cpp
字号:
// MainFrm.cpp : implementation of the CMainFrame class
//
#include "stdafx.h"
#include "SMS.h"
#include "MainFrm.h"
#include <stdlib.h>
extern CSMSApp theApp;
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMainFrame
IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
ON_WM_CREATE()
ON_WM_SHOWWINDOW()
ON_COMMAND(ID_ORDER_FORM_MANAGER, OnOrderFormManager)
ON_COMMAND(ID_USER_MANAGER, OnUserManager)
ON_COMMAND(ID_LOG_MANAGER, OnLogManager)
ON_COMMAND(ID_COM_SETTING, OnComSetting)
ON_UPDATE_COMMAND_UI(ID_COM_CONNECTION, OnUpdateComConnection)
ON_UPDATE_COMMAND_UI(ID_COM_DISCONNECTION, OnUpdateComDisconnection)
ON_COMMAND(ID_COM_CONNECTION, OnComConnection)
ON_COMMAND(ID_COM_DISCONNECTION, OnComDisconnection)
ON_COMMAND(ID_ServeCenterSetting, OnServeCenterSetting)
ON_WM_TIMER()
//}}AFX_MSG_MAP
//自定义消息
ON_MESSAGE(WM_SHOWNOTEMESSAGE,OnShowNoteMessage)//收到短消息后调用函数
END_MESSAGE_MAP()
static UINT indicators[] =
{
ID_SEPARATOR, // status line indicator
ID_INDICATOR_CAPS,
ID_INDICATOR_NUM,
ID_INDICATOR_SCRL,
};
/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction
CMainFrame::CMainFrame()
{
// TODO: add member initialization code here
m_IsConnection=FALSE;
}
CMainFrame::~CMainFrame()
{
}
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
{
TRACE0("Failed to create toolbar\n");
return -1; // fail to create
}
if (!m_wndStatusBar.Create(this) ||
!m_wndStatusBar.SetIndicators(indicators,
sizeof(indicators)/sizeof(UINT)))
{
TRACE0("Failed to create status bar\n");
return -1; // fail to create
}
// TODO: Delete these three lines if you don't want the toolbar to
// be dockable
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndToolBar);
return 0;
}
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CFrameWnd::PreCreateWindow(cs) )
return FALSE;
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
cs.style &= ~FWS_ADDTOTITLE;//去掉程序自动添加的标题,系统自动添加的标题 为 (打开的文件名+软件名)
cs.lpszName = "短信群发平台 SMS";
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics
#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
CFrameWnd::AssertValid();
}
void CMainFrame::Dump(CDumpContext& dc) const
{
CFrameWnd::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers
BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
// TODO: Add your specialized code here and/or call the base class
CRect rc1;
GetClientRect(rc1);
CSize paneSize1(rc1.Width()/3-16,rc1.Height()/3-16);
m_SplitterWnd1.CreateStatic(this,1,2);
m_SplitterWnd1.CreateView(0,0,RUNTIME_CLASS(CSMSTreeView),paneSize1,pContext);
m_SplitterWnd2.CreateStatic(&m_SplitterWnd1,2,1,WS_CHILD|WS_VISIBLE,m_SplitterWnd1.IdFromRowCol(0,1));
CSize paneSize2(rc1.Width()/2-16,rc1.Height()/3-16);
m_SplitterWnd2.CreateView(0,0,RUNTIME_CLASS(CSMSListView),paneSize2,pContext);
m_SplitterWnd2.CreateView(1,0,RUNTIME_CLASS(CSMSFormView),paneSize2,pContext);
//return CFrameWnd::OnCreateClient(lpcs, pContext); //要把这行注释掉,以防止再调用基类下边的函数创建子窗口
return TRUE;
}
void CMainFrame::OnShowWindow(BOOL bShow, UINT nStatus)
{
CFrameWnd::OnShowWindow(bShow, nStatus);
//先通过折分对象获取指定窗格中的窗口对象,然后再调用显示TreeView控件的内容
((CSMSTreeView *)(m_SplitterWnd1.GetPane(0,0)))->InitTreeView();
//显示ListView控件的内容
((CSMSListView *)(m_SplitterWnd2.GetPane(0,0)))->InitListView();
// TODO: Add your message handler code here
}
void CMainFrame::OnOrderFormManager()
{
// TODO: Add your command handler code here
m_OrderFormManager.DoModal();
}
void CMainFrame::OnUserManager()
{
// TODO: Add your command handler code here
m_UserManager.DoModal();
}
void CMainFrame::OnLogManager()
{
// TODO: Add your command handler code here
m_LogManager.DoModal();
}
void CMainFrame::OnComSetting()
{
// TODO: Add your command handler code here
if(IDOK==m_COMSetting.DoModal())
{
CSMSTreeView * treeView=(CSMSTreeView *)(((CMainFrame *)AfxGetMainWnd())->m_SplitterWnd1.GetPane(0,0));
treeView->ShowTree();
}
}
void CMainFrame::OnUpdateComConnection(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->Enable(!m_IsConnection);
}
void CMainFrame::OnUpdateComDisconnection(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->Enable(m_IsConnection);
//////////////////////////////////////////////////////////////////////////
//这里很要关键
// if(theApp.m_SmsSerialPort.hCom!=NULL)
// {
// theApp.m_SmsSerialPort.CloseSerialPort();
// }
}
void CMainFrame::OnComConnection()
{
// CTime currentTime = CTime::GetCurrentTime();
// CString nowTime = currentTime.Format("%Y-%m-%d %H:%M:%S");
// CTime tTime(m_beginData.GetYear(),m_beginData.GetMonth(),m_beginData.GetDay(),
// m_beginTime.GetHour(),m_beginTime.GetMinute(),m_beginTime.GetSecond());
// TODO: Add your command handler code here
//////////////////////////////////////////////////////////////////////////
//COM口连接
//////////////////////////////////////////////////////////////////////////
//判断是否能正常打开串口
int parity=0;//奇偶校验
int baudRate=atoi(theApp.m_ComConfigInfo.BaudRate);
int byteSize=atoi(theApp.m_ComConfigInfo.ByteSize);
int stopBits=atof(theApp.m_ComConfigInfo.StopBits);
if(1==stopBits)
{
stopBits=ONESTOPBIT;
}
if(1.5==stopBits)
{
stopBits=ONE5STOPBITS;
}
if(2==stopBits)
{
stopBits=TWOSTOPBITS;
}
if(theApp.m_ComConfigInfo.Parity=="无")
{
parity=NOPARITY;
}
if(theApp.m_ComConfigInfo.Parity=="标记")
{
parity=MARKPARITY;
}
if(theApp.m_ComConfigInfo.Parity=="空格")
{
parity=SPACEPARITY;
}
if(theApp.m_ComConfigInfo.Parity=="奇校验")
{
parity=ODDPARITY;
}
if(theApp.m_ComConfigInfo.Parity=="偶校验")
{
parity=EVENPARITY;
}
CString comPortName=theApp.m_ComPortName;
if(theApp.m_SmsSerialPort.OpenCOMPort(comPortName,baudRate,byteSize,parity,stopBits))
{
//判断是否能连接短信猫成功
if(!theApp.m_SmsSerialPort.IsExistModen())
{
MessageBox("请检查短信猫是否连接正确");
theApp.m_SmsSerialPort.CloseSerialPort();
return;
}
//m_SmsSerialPort.StartThread();
}
else
{
MessageBox("打开串口失败,别一个程序正在使用该串口\n请先关闭使用该串口的应用程序");
return;
//MessageBox("打开串口失败,请先关闭使用该串口的其它应用程序");
}
//////////////////////////////////////////////////////////////////////////
//发送
//////////////////////////////////////////////////////////////////////////
// SM_PARAM Note=m_SmsSerialPort.NoteMessage("8613800591500","8613459112022","你好,SELECT * FORM TABLE,TT 我的这个测试有没有成功啊");
// m_SmsSerialPort.PutToSendMessageList(Note);
// if(TRUE==m_SmsSerialPort.SendNoteMessage())
// {
// MessageBox("Send success");
// }
// else
// {
// MessageBox("send faild111");
// }
// m_SmsSerialPort.CloseSerialPort();
theApp.m_SmsSerialPort.StartThread();
m_IsConnection=TRUE;
//////////////////////////////////////////////////////////////////////////
//定时器
m_timeID=SetTimer(1,1000,NULL); //第一个参数代表定时器的编号
}
void CMainFrame::OnComDisconnection()
{
// TODO: Add your command handler code here
if(theApp.m_SmsSerialPort.hCom!=NULL)
{
theApp.m_SmsSerialPort.CloseSerialPort();
}
m_IsConnection=FALSE;
// AfxMessageBox("关闭连接");
theApp.m_SmsSerialPort.StopThread();
//////////////////////////////////////////////////////////////////////////
//关闭掉定时器
KillTimer(m_timeID);
}
void CMainFrame::OnShowNoteMessage(WPARAM wparam,LPARAM lparam)
{
//AfxMessageBox("接收到一条消息");
NoteMsgInfo noteMsgInfo;
int row=0;//行
int column=0;//列
TRACE("%d",m_SplitterWnd2.GetPane(row,column));
CListCtrl & listView=((CSMSListView *)(m_SplitterWnd2.GetPane(row,column)))->GetListCtrl();
theApp.m_SmsSerialPort.GetNoteMessageInfoFromShowMessageList(noteMsgInfo);
// EnterCriticalSection(&(theApp.m_SmsSerialPort.m_ShowNoteMessageListCritical));
// noteMsgInfo=theApp.m_SmsSerialPort.m_ShowNoteMessageList.front();
// theApp.m_SmsSerialPort.m_ShowNoteMessageList.pop_front();
// LeaveCriticalSection(&(theApp.m_SmsSerialPort.m_ShowNoteMessageListCritical));
//取出所有短消息直到缓冲区为空
int itemNum=0;
TRACE("%d",listView.GetItemCount());
itemNum=listView.GetItemCount();//获取记录总数
listView.InsertItem(itemNum, noteMsgInfo.time);//时间
listView.SetItemText(itemNum,1, noteMsgInfo.phoneNumber);//手机号码
listView.SetItemText(itemNum,2,noteMsgInfo.action);//动作
listView.SetItemText(itemNum,3,noteMsgInfo.state);//状态
listView.SetItemText(itemNum,4,noteMsgInfo.content);//内容
}
void CMainFrame::OnServeCenterSetting()
{
// TODO: Add your command handler code here
CServeConter serveConter;
if(IDOK==serveConter.DoModal())
{
CSMSTreeView * treeView=(CSMSTreeView *)(((CMainFrame *)AfxGetMainWnd())->m_SplitterWnd1.GetPane(0,0));
treeView->ShowTree();
}
}
void CMainFrame::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
CTime currentTime = CTime::GetCurrentTime();
CString time= currentTime.Format("%Y-%m-%d %H:%M:%S");
int orderID;
CString content;
CString endTime;
CString phoneNumber;
SM_PARAM sm_Param;
//////////////////////////////////////////////////////////////////////////
///数据库操作
CSMSApp * pCSMSApp=(CSMSApp *)AfxGetApp();//取得App类对象的指针
CSMSADO &Ado=pCSMSApp->m_SMSAdo;
CString sql;
sql.Format("select id,content,orderend from orders where orderstart=to_date('%s','yyyy-mm-dd hh24:mi:ss')",time);
TRACE(sql);
SQLRECORDSET orderRS=Ado.ExecuteWithRecordset(sql);
if(orderRS->BOF)
{
//AfxMessageBox("没有查找到消息")
return;
}
//AfxMessageBox("发送时间到");
while(!orderRS->rsEOF)
{
//取出订单ID
orderID=Ado.GetIntegerFieldVal(orderRS,"id");
content=Ado.GetTextFieldVal(orderRS,"content");
endTime=Ado.GetDateTimeFeildTextValue(orderRS,"orderend");
sql.Empty();
sql.Format("select pnumber from phonenumber where orderid=%d",orderID);
SQLRECORDSET phoneNumberRS=Ado.ExecuteWithRecordset(sql);
while(!phoneNumberRS->rsEOF)
{
phoneNumber=Ado.GetTextFieldVal(phoneNumberRS,"pnumber");
memset(&sm_Param,0,sizeof(sm_Param));
sm_Param=theApp.m_SmsSerialPort.NoteMessage(theApp.m_ServeCenterNumber,phoneNumber,content,orderID);
theApp.m_SmsSerialPort.PutToSendMessageList(sm_Param);
phoneNumberRS->MoveNext();
}
orderRS->MoveNext();
}
CFrameWnd::OnTimer(nIDEvent);
}
/*
std::vector<CString> CMainFrame::InterceptSendNoteMessage(CString sourceStr,int InterceptNum,int strLength)
{
std::vector<CString> sendList;
if(sourceStr.GetLength()<strLength)
{
sendList.push_back(sourceStr);
}
else
{
CString strTemp=sourceStr.Left(strLength);
CString sreturn;
int i=0;
for(i=0;i<strLength-1;)
{
if (strTemp[i]>127 ||strTemp[i]<0)
i+=2;
else
i++;
}
if (i==strLength-1)
sreturn=strTemp;
else
sreturn=sTemp.Left(qlen-1);
str.Delete(0,sreturn.GetLength());
}
}
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -