📄 smspiritdlg.cpp
字号:
// SMSpiritDlg.cpp : implementation file
//
#include "stdafx.h"
#include "SMSpirit.h"
#include "SMSpiritDlg.h"
#include "SerialPort.h"
#include "SMS.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CSMSpiritDlg dialog
CSMSpiritDlg::CSMSpiritDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSMSpiritDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSMSpiritDlg)
m_strMessage = _T("Hello, World!");
m_strReceiver = _T("13521086031");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
CSMSpiritDlg::~CSMSpiritDlg()
{
delete m_pPort;
delete m_pSms;
}
void CSMSpiritDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSMSpiritDlg)
DDX_Control(pDX, IDC_OUTBOX, m_OutBox);
DDX_Control(pDX, IDC_INBOX, m_InBox);
DDX_Control(pDX, IDC_ADDRESS_BOOK, m_AddressBook);
DDX_Text(pDX, IDC_MESSAGE, m_strMessage);
DDX_Text(pDX, IDC_RECEIVER, m_strReceiver);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSMSpiritDlg, CDialog)
//{{AFX_MSG_MAP(CSMSpiritDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_SEND, OnSend)
ON_BN_CLICKED(IDC_BUTTON_REFRESH, OnButtonRefresh)
ON_NOTIFY(LVN_KEYDOWN, IDC_INBOX, OnKeydownInbox)
ON_NOTIFY(LVN_KEYDOWN, IDC_OUTBOX, OnKeydownOutbox)
ON_NOTIFY(NM_DBLCLK, IDC_ADDRESS_BOOK, OnDblclkAddressBook)
//}}AFX_MSG_MAP
ON_MESSAGE(USERMSG_RECEIVE_SM, ReceivedNewMessage)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSMSpiritDlg message handlers
BOOL CSMSpiritDlg::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
//
// Our initialize
//
m_OutBox.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
m_InBox.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
m_AddressBook.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
RECT rect;
m_OutBox.GetClientRect(&rect);
m_OutBox.InsertColumn(0, "status", LVCFMT_LEFT, 60);
m_OutBox.InsertColumn(1, "receiver", LVCFMT_LEFT, 120);
m_OutBox.InsertColumn(2, "message", LVCFMT_LEFT, rect.right - 260);
m_OutBox.InsertColumn(3, "time", LVCFMT_LEFT, 80);
m_InBox.GetClientRect(&rect);
m_InBox.InsertColumn(0, "status", LVCFMT_LEFT, 60);
m_InBox.InsertColumn(1, "sender", LVCFMT_LEFT, 120);
m_InBox.InsertColumn(2, "message", LVCFMT_LEFT, rect.right - 260);
m_InBox.InsertColumn(3, "time", LVCFMT_LEFT, 80);
m_AddressBook.GetClientRect(&rect);
m_AddressBook.InsertColumn(0, "name", LVCFMT_LEFT, 100);
m_AddressBook.InsertColumn(1, "number", LVCFMT_LEFT, rect.right - 100);
m_pPort = new CSerialPort();
m_pPort->OpenPort("COM1");
m_pPort->ConfigurePort();
m_pPort->SetCommunicationTimeouts();
m_pPort->SelectEvents();
m_pSms = new CSMS(m_pPort);
m_pSms->initialize();
FillAddressBook();
FillOutbox();
FillInbox();
return TRUE;
}
void CSMSpiritDlg::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 CSMSpiritDlg::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 CSMSpiritDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CSMSpiritDlg::OnSend()
{
char szTel[32], szMessage[192] = {0};
int start = 0, end;
UpdateData(true);
//strcpy(szMessage, (LPCTSTR) m_strMessage);
int msglen = m_strMessage.GetLength();
memcpy(szMessage, (LPCTSTR) m_strMessage.GetBuffer(0), msglen > 160 ? 160 : msglen);
int msg_type = MSG_ENGLISH;
bool fOverlen;
do
{
msg_type = MSG_ENGLISH;
fOverlen = msglen > 160 ? true:false;
int len = strlen(szMessage);
for(int i = 0; i < len; i++)
{
if(i == 140 && msg_type == MSG_CHINESE)
{
memset(szMessage+i, 0, len - i);
fOverlen = true;
break;
}
if(msg_type == MSG_ENGLISH && mblen(szMessage + i, len - i) == -1) // MB_CUR_MAX
{
msg_type = MSG_CHINESE;
}
}
if(m_strReceiver[m_strReceiver.GetLength() - 1] != ',')
m_strReceiver += ",";
while((end = m_strReceiver.Find(',', start)) > 0)
{
strcpy(szTel, (LPCTSTR) m_strReceiver.Mid(start, end - start));
m_pSms->send(szTel, szMessage, msg_type);
start = end + 1;
}
msglen -= i;
memset(szMessage, 0, sizeof(szMessage));
if(msglen > 0)
memcpy(szMessage, (LPCTSTR) m_strMessage.GetBuffer(0) + i, msglen > 160 ? 160 : msglen);
} while(fOverlen && msglen);
}
void CSMSpiritDlg::FillAddressBook()
{
char *szName[512], *szTel[512];
int number, i;
// szName will be allocated in read_phone_book, so free it after call
number = m_pSms->read_phone_book(szName, szTel);
for(i = 0; i < number; i++)
{
m_AddressBook.InsertItem(i, szName[i]);
free(szName[i]);
m_AddressBook.SetItemText(i, 1, szTel[i]);
free(szTel[i]);
}
}
void CSMSpiritDlg::FillOutbox()
{
char *szTime[512], *szTel[512], *szMessage[512];
int location[512], number, i;
number = m_pSms->read_message_by_type(2, szTel, szTime, szMessage, location);
for(i = 0; i < number; i++)
{
m_OutBox.InsertItem(i, "Un Sent");
m_OutBox.SetItemText(i, 1, szTel[i]);
free(szTel[i]);
m_OutBox.SetItemText(i, 2, szMessage[i]);
free(szMessage[i]);
m_OutBox.SetItemText(i, 3, szTime[i]);
free(szTime[i]);
m_OutBox.SetItemData(i, location[i]);
}
number = m_pSms->read_message_by_type(3, szTel, szTime, szMessage, location);
for(i = 0; i < number; i++)
{
m_OutBox.InsertItem(i, "Sent");
m_OutBox.SetItemText(i, 1, szTel[i]);
free(szTel[i]);
m_OutBox.SetItemText(i, 2, szMessage[i]);
free(szMessage[i]);
m_OutBox.SetItemText(i, 3, szTime[i]);
free(szTime[i]);
m_OutBox.SetItemData(i, location[i]);
}
}
void CSMSpiritDlg::FillInbox()
{
char *szTime[512], *szTel[512], *szMessage[512];
int location[512], number, i;
number = m_pSms->read_message_by_type(0, szTel, szTime, szMessage, location);
for(i = 0; i < number; i++)
{
m_InBox.InsertItem(i, "Un Read");
m_InBox.SetItemText(i, 1, szTel[i]);
free(szTel[i]);
m_InBox.SetItemText(i, 2, szMessage[i]);
free(szMessage[i]);
m_InBox.SetItemText(i, 3, szTime[i]);
free(szTime[i]);
m_InBox.SetItemData(i, location[i]);
}
number = m_pSms->read_message_by_type(1, szTel, szTime, szMessage, location);
for(i = 0; i < number; i++)
{
m_InBox.InsertItem(i, "Read");
m_InBox.SetItemText(i, 1, szTel[i]);
free(szTel[i]);
m_InBox.SetItemText(i, 2, szMessage[i]);
free(szMessage[i]);
m_InBox.SetItemText(i, 3, szTime[i]);
free(szTime[i]);
m_InBox.SetItemData(i, location[i]);
}
}
void CSMSpiritDlg::OnButtonRefresh()
{
m_AddressBook.DeleteAllItems();
FillAddressBook();
m_InBox.DeleteAllItems();
FillInbox();
m_OutBox.DeleteAllItems();
FillOutbox();
}
LRESULT CSMSpiritDlg::ReceivedNewMessage(WPARAM wParam, LPARAM lParam)
{
SHORT_MESSAGE *pMsg = (SHORT_MESSAGE*) wParam;
int iItem = m_InBox.GetItemCount();
m_InBox.InsertItem(iItem, "UN READ");
m_InBox.SetItemText(iItem, 1, pMsg->szTel);
m_InBox.SetItemText(iItem, 2, pMsg->szMessage);
m_InBox.SetItemText(iItem, 3, pMsg->szTime);
return 0;
}
void CSMSpiritDlg::OnKeydownInbox(NMHDR* pNMHDR, LRESULT* pResult)
{
LV_KEYDOWN* pLVKeyDow = (LV_KEYDOWN*)pNMHDR;
// TODO: Add your control notification handler code here
if(pLVKeyDow->wVKey == VK_DELETE)
{
POSITION pos = m_InBox.GetFirstSelectedItemPosition();
if(pos) {
int nItem = m_InBox.GetNextSelectedItem(pos);
//int location = m_InBox.GetItemData(nItem);
//m_pSms->remove(location);
if(MessageBox("确定要删除该短信吗?", "警告", MB_ICONEXCLAMATION|MB_YESNO) == IDYES)
m_InBox.DeleteItem(nItem);
}
}
*pResult = 0;
}
void CSMSpiritDlg::OnKeydownOutbox(NMHDR* pNMHDR, LRESULT* pResult)
{
LV_KEYDOWN* pLVKeyDow = (LV_KEYDOWN*)pNMHDR;
// TODO: Add your control notification handler code here
if(pLVKeyDow->wVKey == VK_DELETE)
{
POSITION pos = m_OutBox.GetFirstSelectedItemPosition();
if(pos) {
int nItem = m_OutBox.GetNextSelectedItem(pos);
//int location = m_OutBox.GetItemData(nItem);
//m_pSms->remove(location);
if(MessageBox("确定要删除该短信吗?", "警告", MB_ICONEXCLAMATION|MB_YESNO) == IDYES)
m_OutBox.DeleteItem(nItem);
}
}
*pResult = 0;
}
void CSMSpiritDlg::OnDblclkAddressBook(NMHDR* pNMHDR, LRESULT* pResult)
{
POSITION pos = m_AddressBook.GetFirstSelectedItemPosition();
if(pos) {
int nItem = m_AddressBook.GetNextSelectedItem(pos);
CString strTel = m_AddressBook.GetItemText(nItem, 1);
m_strReceiver += "," + strTel;
UpdateData(false);
}
*pResult = 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -