?? 練習dlg.cpp
字號:
// 練習Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "練習.h"
#include "練習Dlg.h"
#include "mscomm.h"
#include "ChineseCode1.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()
/////////////////////////////////////////////////////////////////////////////
// CMyDlg dialog
CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMyDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMyDlg)
m_receive = _T("");
m_CenterNum = _T("");
m_ToNum = _T("");
m_send = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMyDlg)
DDX_Control(pDX, IDC_READ, m_TAG);
DDX_Control(pDX, IDC_MSCOMM1, m_ctrlComm);
DDX_Text(pDX, IDC_EDIT1, m_receive);
DDX_Text(pDX, IDC_CenterNumber, m_CenterNum);
DDX_Text(pDX, IDC_ToNumber, m_ToNum);
DDX_Text(pDX, IDC_SEND, m_send);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
//{{AFX_MSG_MAP(CMyDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnLink)
ON_BN_CLICKED(IDC_BUTTON2, OnSend)
ON_BN_CLICKED(IDC_BUTTON3, dail)
ON_BN_CLICKED(IDC_BUTTON4, shutup)
ON_BN_CLICKED(IDC_BUTTON5, listen)
ON_BN_CLICKED(IDC_READ, OnRead)
ON_BN_CLICKED(IDC_BUTTON6, OnEXIT)
ON_BN_CLICKED(IDC_Delete, OnDelete)
ON_BN_CLICKED(IDC_BUTTON7, OnCleanUp)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyDlg message handlers
BOOL CMyDlg::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
CComboBox *combox =(CComboBox *)GetDlgItem(IDC_COMBO2);
combox->AddString("COM 1");
combox->AddString("COM 2");
combox->AddString("COM 3");
combox->AddString("COM 4");
combox->AddString("COM 5");
combox->AddString("COM 6");
combox->AddString("COM 7");
combox->SetCurSel(2);
CListCtrl *list=(CListCtrl *)GetDlgItem(IDC_LIST1);
list->InsertColumn(0,"發送方號碼",LVCFMT_LEFT,100);
list->InsertColumn(1,"接收時間",LVCFMT_LEFT,100);
list->InsertColumn(2,"短信內容",LVCFMT_LEFT,list->GetColumnWidth(2));
m_SMS_index=1;
m_CenterNum="13800731500";
m_ToNum="13467515079";
account=0;
UpdateData(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}
void CMyDlg::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 CMyDlg::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 CMyDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CMyDlg::OnLink()
{
// TODO: Add your control notification handler code here
m_ctrlComm.SetSettings("9600,n,8,1");
m_ctrlComm.SetInputMode(1);
m_ctrlComm.SetRThreshold(1);
m_ctrlComm.SetRTSEnable(TRUE);
m_ctrlComm.SetDTREnable(TRUE);
m_ctrlComm.SetInputLen(0); //設置當前接收區數據長度為0
m_ctrlComm.SetInBufferCount(0); //清空接收緩沖區
m_ctrlComm.SetOutBufferCount(0); //清空發送緩沖區
m_ctrlComm.SetInBufferSize(1024);
m_ctrlComm.SetOutBufferSize(1024);
if(m_ctrlComm.GetPortOpen()){
MessageBox("連接要關閉!");
m_ctrlComm.SetPortOpen(FALSE);
}
CComboBox *combox =(CComboBox *)GetDlgItem(IDC_COMBO2);
m_ctrlComm.SetCommPort(combox->GetCurSel()+1);
if(!m_ctrlComm.GetPortOpen()){
m_ctrlComm.SetPortOpen(TRUE);
if(m_ctrlComm.GetPortOpen())
MessageBox("連接成功!");
else
MessageBox("連接失敗!");
}
else
MessageBox("無法打開!");
m_ctrlComm.GetInput();//先預讀緩沖區以清除殘留數據
//設置短消息到達自動提示at+clip=1
// m_ctrlComm.SetOutput(COleVariant("AT+CNUM\r\n"));
// m_ctrlComm.SetOutput(COleVariant(cs));
}
void CMyDlg::OnSend()
{
// TODO: Add your control notification handler code here//
CString num="089168";
CString source="";
CString desty;
CString command="AT+CMGS=";
CString temp;
int length;
int zonglength=14;
UpdateData(TRUE);
temp=Digitcheng.RightToDiverse(m_CenterNum);
num+=temp;
num+="11000D9168";
//0891683108701305F011000D91680180F60008A9 0A 00310030003000380036
temp=Digitcheng.RightToDiverse(m_ToNum);
num+=temp;
num+="0008A9";
source=m_send;
MessageBox(m_send);
length=CCC.gsmEncodeUCS2(source,desty);
zonglength+=length+1;
temp.Format("%d",zonglength);
command+=temp+CString("\r\n");
//pdu格式發送,而不是text格式發送
m_ctrlComm.SetOutput(COleVariant("AT+CMGF=0\r\n"));
Sleep(1000);
m_ctrlComm.SetOutput(COleVariant(command));
//XXX表示PDU中TPDU的長度(不包含SMSC地址)
Sleep(1000);
num+=Digitcheng.Chenge(length);
num+=desty+CString("\x01a");
m_ctrlComm.SetOutput(COleVariant(num));
}
BEGIN_EVENTSINK_MAP(CMyDlg, CDialog)
//{{AFX_EVENTSINK_MAP(CMyDlg)
ON_EVENT(CMyDlg, IDC_MSCOMM1, 1 /* OnComm */, OnComm, VTS_NONE)
//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()
void CMyDlg::OnComm()
{
// TODO: Add your control notification handler code here
VARIANT variant_inp;
COleSafeArray safearray_inp;
LONG len,k;
BYTE rxdata[2048]; //設置BYTE數組 An 8-bit integerthat is not signed.
CString strtemp;
CString recd;
switch(m_ctrlComm.GetCommEvent())
{
case 1: // comEvSend發送數據
break;
case 2:
{
// comEvReceive讀取數據
// MessageBox(_T("讀取數據事件"), _T("TRACE"), MB_OK);
variant_inp=m_ctrlComm.GetInput(); //讀緩沖區
safearray_inp=variant_inp; //VARIANT型變量轉換為ColeSafeArray型變量
len=safearray_inp.GetOneDimSize(); //得到有效數據長度
//傳遞參數到read窗口
Sarray=safearray_inp;
// 接受數據
for(k=0; k<len;k++){
safearray_inp.GetElement(&k,rxdata+k); //轉換為BYTE型數組
BYTE bt=*(char*)(rxdata+k); //字符型
strtemp.Format("%c",bt); //將字符送入臨時變量strtemp存放
m_receive+=strtemp;
}
break;
}
default:
{
// 傳輸事件出錯
m_ctrlComm.SetOutBufferCount(0);
break;
}
}
// m_show=m_receive;
UpdateData(FALSE);
return;
}
void CMyDlg::dail()
{
// TODO: Add your control notification handler code here
// 撥打手機電話號碼
UpdateData(TRUE);
m_ctrlComm.SetOutput(COleVariant("ATD"+m_ToNum+"\r\n"));
}
void CMyDlg::shutup()
{
// TODO: Add your control notification handler code here
// 掛斷手機電話號碼
m_ctrlComm.SetOutput(COleVariant("ATH\r\n"));
}
void CMyDlg::listen()
{
// TODO: Add your control notification handler code here
// 接聽手機電話號碼
m_ctrlComm.SetOutput(COleVariant("ATA\r\n"));
}
void CMyDlg::OnRead()
{
// TODO: Add your control notification handler code here
//讀取單條短信
// m_ctrlComm.SetInBufferCount(0); //清空接收緩沖區
m_ctrlComm.SetOutput(COleVariant("AT+CMGL=1\r\n"));
// Sleep(1000);
/*
CString SMS_read;
bool tag=false;
char c;
char* temp1;
char* temp2=new char[1024];
CString cs;
cs.Format("%d",account);
// m_ctrlComm.SetOutput(COleVariant("AT+CMGR="+cs+"\r\n"));
account++;
Sleep(1000);
int length=m_receive.GetLength();
CString ds;
ds.Format("%d",length);
MessageBox(ds);
int i=1;
while((i+62<length-1)&&(m_receive.GetAt(i+62)!='\r')){
c=m_receive.GetAt(i+62);
SMS_read+=c;
i++;
if(tag==false)
tag=true;
}
if(tag==true){
SMS_read+="\0";
temp1=SMS_read.GetBuffer(SMS_read.GetLength());
MessageBox(SMS_read);
CCC.gsmDecodePdu(temp1,temp2);
SMS_read.ReleaseBuffer();
MessageBox(CString("message ")+temp2);
Sleep(1000);
m_receive="";
}
*/
}
void CMyDlg::OnEXIT()
{
// TODO: Add your control notification handler code here
if(m_ctrlComm.GetPortOpen()){
m_ctrlComm.SetPortOpen(FALSE);
MessageBox("關閉");
}
else
MessageBox("串口已關閉");
CDialog::OnCancel();
}
void CMyDlg::OnDelete()
{
// TODO: Add your control notification handler code here
// PC上的控制軟件收到一條短消息并處理后,需要將其在SIM卡上刪除,以防止SIM卡飽和。刪除短消息的指令為:
m_ctrlComm.SetOutput(COleVariant("AT+CMGD=14\r\n"));
}
void CMyDlg::OnCleanUp()
{
// TODO: Add your control notification handler code here
m_receive="";
UpdateData(FALSE);
}
//修改背景顏色
HBRUSH CMyDlg::OnCtlColor(CDC *pDC, CWnd *pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
pDC->SetBkMode(TRANSPARENT);//設置為透明模式
// TODO: Change any attributes of the DC here
hbr=CreateSolidBrush(RGB(60,111,255));//創建對話框的底色
// pDC->SetBkColor(RGB(90,249,212));
// TODO: Return a different brush if the default is not desired
return hbr;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -