⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 messagetestdlg.cpp

📁 手机串口收发短消息的收发界面
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// MessageTestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MessageTest.h"
#include "MessageTestDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CMessageTestDlg dialog

CMessageTestDlg::CMessageTestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMessageTestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMessageTestDlg)
	m_nREncode = _T("");
	m_nSCenterNum = _T("");
	m_nSContent = _T("");
	m_nSEncode = _T("");
	m_nSNum = _T("");
	m_nComSelect = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

	m_bConnected=FALSE;
	m_pThread=NULL;
}

CMessageTestDlg::~CMessageTestDlg()
{
	if(m_bConnected) //确保关闭串口
		OnClosecom();

	//删除事件句柄
	if(m_hMsgEvent!=NULL)
		CloseHandle(m_hMsgEvent);
	if(m_OverlappedRead.hEvent!=NULL)
		CloseHandle(m_OverlappedRead.hEvent);
	if(m_OverlappedWrite.hEvent!=NULL)
		CloseHandle(m_OverlappedWrite.hEvent);
}

void CMessageTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMessageTestDlg)
	DDX_Control(pDX, IDC_RCONTENT, m_nRContent);
	DDX_Text(pDX, IDC_RENCODE, m_nREncode);
	DDX_Text(pDX, IDC_SCENTERNUM, m_nSCenterNum);
	DDX_Text(pDX, IDC_SCONTENT, m_nSContent);
	DDV_MaxChars(pDX, m_nSContent, 140);
	DDX_Text(pDX, IDC_SENCODE, m_nSEncode);
	DDX_Text(pDX, IDC_SNUM, m_nSNum);
	DDX_CBString(pDX, IDC_COMSELECT, m_nComSelect);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMessageTestDlg, CDialog)
	//{{AFX_MSG_MAP(CMessageTestDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_DESTROY()
	ON_WM_SIZE()
	ON_BN_CLICKED(IDC_DECODE, OnDecode)
	ON_BN_CLICKED(IDC_ENCODE, OnEncode)
	ON_BN_CLICKED(IDC_OPENCOM, OnOpencom)
	ON_BN_CLICKED(IDC_CLOSECOM, OnClosecom)
	ON_BN_CLICKED(IDC_SENDCOM, OnSendcom)	
	ON_BN_CLICKED(IDC_SETCENTERNUM, OnSetcenternum)
	ON_BN_CLICKED(IDC_GETCENTERNUM, OnGetcenternum)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMessageTestDlg message handlers

BOOL CMessageTestDlg::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
	/*	CFile mFile;
	CString strDir=GetCurDir();
	if(mFile.Open((GetCurDir()+"dlgStatus.sav"), CFile::modeRead) && mFile.GetLength()>0)
	{
		CArchive ar(&mFile, CArchive::load);
		mapStrToRect.Serialize(ar);
		ar.Close();
		mFile.Close();
		CString strWindow;
		GetWindowText(strWindow);
		CRect rectDlg;
		if(mapStrToRect.Lookup(strWindow, rectDlg))
			MoveWindow(rectDlg);
	}
*/
	CRect rectWnd;
	GetWindowRect(&rectWnd);
	listRect.AddTail(&rectWnd);
	CWnd *pWndChild = GetWindow(GW_CHILD);
	while(pWndChild)
	{
		pWndChild->GetWindowRect(&rectWnd);
		listRect.AddTail(&rectWnd);
		pWndChild = pWndChild->GetNextWindow();
	}
//	screen.nScreenX=GetSystemMetrics(SM_CXSCREEN);
//	screen.nScreenY=GetSystemMetrics(SM_CYSCREEN);
		
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMessageTestDlg::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 CMessageTestDlg::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 CMessageTestDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

CString CMessageTestDlg::GetCurDir()
{
	TCHAR sDrive[_MAX_DRIVE];
	TCHAR sDir[_MAX_DIR];
	TCHAR sFilename[_MAX_FNAME],Filename[_MAX_FNAME];
	TCHAR sExt[_MAX_EXT];

	GetModuleFileName(AfxGetInstanceHandle(), Filename, _MAX_PATH);
	_tsplitpath(Filename, sDrive, sDir, sFilename, sExt);
	CString homeDir(CString(sDrive) + CString(sDir));
	int nLen = homeDir.GetLength();
	if(homeDir.GetAt(nLen-1) != _T('\\'))
		homeDir += _T('\\');
	return homeDir;
}


void CMessageTestDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
/*	CRect rectDlg;
	GetWindowRect(&rectDlg);
	CString strWindow;
	GetWindowText(strWindow);
	mapStrToRect.SetAt(strWindow, rectDlg);
	CFile mFile;
	mFile.Open(GetCurDir()+"dlgStatus.sav",CFile::modeCreate|CFile::modeWrite);
	CArchive ar(&mFile, CArchive::store);
	mapStrToRect.Serialize(ar);
	ar.Close();
	mFile.Close(); */
}

void CMessageTestDlg::OnSize(UINT nType, int cx, int cy) 
{
	CDialog::OnSize(nType, cx, cy);
	
	// TODO: Add your message handler code here
	int nCount=listRect.GetCount();
	if(listRect.GetCount()>0)
	{
		CRect rectDlgNow;
		GetWindowRect(&rectDlgNow);
		POSITION mP = listRect.GetHeadPosition();
		CRect rectDlgSaved;
		rectDlgSaved = listRect.GetNext(mP);
		ScreenToClient(rectDlgSaved);
		ScreenToClient(rectDlgNow);
		float fRateScaleX = (float)(rectDlgNow.right-rectDlgNow.left)/(rectDlgSaved.right-rectDlgSaved.left);
		float fRateScaleY = (float)(rectDlgNow.bottom-rectDlgNow.top)/(rectDlgSaved.bottom-rectDlgSaved.top);
		ClientToScreen(rectDlgSaved);
		ClientToScreen(rectDlgNow);
		LOGFONT stFont;
		::GetObject((HFONT)GetStockObject(DEFAULT_GUI_FONT), sizeof(stFont), &stFont);
		strcpy(stFont.lfFaceName,"Times New Roman");
		stFont.lfHeight = (long)(fRateScaleY*14);
		stFont.lfWidth = (long)(fRateScaleX*7);
		CFont * m_pCFont;
		m_pCFont = new CFont;
		m_pCFont->CreateFontIndirect(&stFont);
		CRect rectChildSaved;
		CWnd *pWndChild = GetWindow(GW_CHILD);
		while(pWndChild)
		{
			rectChildSaved = listRect.GetNext(mP);
			rectChildSaved.left = rectDlgNow.left+(int)((rectChildSaved.left-rectDlgSaved.left)* fRateScaleX);
			rectChildSaved.top = rectDlgNow.top+(int)((rectChildSaved.top-rectDlgSaved.top)*fRateScaleY);
			rectChildSaved.right = rectDlgNow.right+(int)((rectChildSaved.right-rectDlgSaved.right)* fRateScaleX);
			rectChildSaved.bottom = rectDlgNow.bottom+(int)((rectChildSaved.bottom-rectDlgSaved.bottom)* fRateScaleY);
			ScreenToClient(rectChildSaved);
			pWndChild->MoveWindow(rectChildSaved);
			pWndChild->SetFont(m_pCFont);
			pWndChild = pWndChild->GetNextWindow();		
		}
	}
	Invalidate();
}

//解码函数
BOOL CMessageTestDlg::Decode()
{
	unsigned char SCA[16];      /*short message service centre address*/
    unsigned char PA[16];       /*reply address*/
    unsigned char TP_PID;       /*protocol id,0 represent normal GSM type,point to point style*/
    unsigned char TP_DCS;       /*encode style,0x00,0x04 or 0x08*/
    unsigned char SCTS[16];     /*service time stamp*/
    char TP_UD_7bit[256];       /*initial data,ASCII code*/
	unsigned char TP_UD_8bit[256];   /*initial data,data message*/
    char TP_UD_UCS2[256]; /*initial data, Unicode character*/
    //char SMS;                   /*short message sequence*/
	unsigned char Rece_Buf[256];/*receive buffer*/
	unsigned char *buf_pointer; /*point to receive buffer*/
	
	/*for display*/
	char RNum[16];
	CString rnum;
	char RTime[16];
	CString time;
	char CenterNum[16];
	CString cnum;
	char RContent[256];
	CString content;
			
    if(UpdateData(TRUE)==TRUE)
	{
		String_Byte(m_nREncode.GetBuffer(m_nREncode.GetLength()),Rece_Buf,m_nREncode.GetLength()); /*get code with byte style*/
		m_nREncode.ReleaseBuffer();
		buf_pointer=Rece_Buf;
		
        Address_Convert(buf_pointer+2,SCA,(int)(*buf_pointer-1));
		Byte_String(SCA,CenterNum+1,(int)(*buf_pointer-1));
		CenterNum[0]='+';
		if(CenterNum[strlen(CenterNum)-1]=='F')
			CenterNum[strlen(CenterNum)-1]='\0';
		cnum.Format("%s",CenterNum); /*get message service center address*/
		cnum+="  ";
				
		buf_pointer+=Rece_Buf[0]+1;

        if(*buf_pointer & 0x80)
		{
            buf_pointer++;
            if(*buf_pointer & 1)
			{
				Address_Convert(buf_pointer+2,PA,(int)((*buf_pointer+1)/2));
				Byte_String(PA,RNum+1,(int)((*buf_pointer+1)/2));
				buf_pointer+=(*buf_pointer+1)/2+1;
			}
            else
			{
				Address_Convert(buf_pointer+2,PA,(int)(*buf_pointer/2));
				Byte_String(PA,RNum+1,(int)(*buf_pointer/2));
				buf_pointer+=*buf_pointer/2+1;
			}
			RNum[0]='+';
			if(RNum[strlen(RNum)-1]=='F')
				RNum[strlen(RNum)-1]='\0';                      			
		}
		rnum.Format("%s",RNum); /*get send address*/
		rnum+="  ";

        buf_pointer++;

        TP_PID=*buf_pointer;

        buf_pointer++;

        TP_DCS=*buf_pointer;

        buf_pointer++;

        Address_Convert(buf_pointer,SCTS,7);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -