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

📄 managemdmdlg.cpp

📁 发布AT指令操作Gms Modem 或手机发送短信MFC源代码
💻 CPP
字号:
// ManageMdmDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ManageMdm.h"
#include "ManageMdmDlg.h"
#include "setupdlg.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()

/////////////////////////////////////////////////////////////////////////////
// CManageMdmDlg dialog

CManageMdmDlg::CManageMdmDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CManageMdmDlg::IDD, pParent),m_nCommPort(1),m_bIsConnected(false)
{
	//{{AFX_DATA_INIT(CManageMdmDlg)
	m_nSimContent = 0;
	m_date = _T("");
	m_from = _T("");
	m_sMessage = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CManageMdmDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CManageMdmDlg)
	DDX_Control(pDX, IDC_PROGRESS1, m_progress);
	DDX_Control(pDX, IDC_EDIT_ATCOMMAND, m_ctlAtCmd);
	DDX_Control(pDX, IDC_LIST1, m_list);
	DDX_Text(pDX, IDC_EDIT_CONTENT, m_nSimContent);
	DDV_MinMaxInt(pDX, m_nSimContent, 1, 200);
	DDX_Text(pDX, IDC_EDIT_DATE, m_date);
	DDX_Text(pDX, IDC_EDIT_FROM, m_from);
	DDX_Text(pDX, IDC_EDIT_SM_MESSAGE, m_sMessage);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CManageMdmDlg, CDialog)
	//{{AFX_MSG_MAP(CManageMdmDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_CONNECT, OnButtonConnect)
	ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
	ON_BN_CLICKED(IDC_BUTTON_GET_CONTENT, OnButtonGetContent)
	ON_BN_CLICKED(IDC_BUTTON_READ_ALL_SM, OnButtonReadAllSm)
	ON_BN_CLICKED(ID_SEND_SM_BUTTON, OnSendSmButton)
	ON_NOTIFY(HDN_ITEMCHANGED, IDC_LIST1, OnItemchangedList1)
	ON_NOTIFY(NM_CLICK, IDC_LIST1, OnClickList1)
	ON_BN_CLICKED(IDC_BUTTON_DEL_SM, OnButtonDelSm)
	ON_BN_CLICKED(ID_ABOUT_BUTTON, OnAboutButton)
	ON_BN_CLICKED(ID_BUTTON_SETUP, OnButtonSetup)
	//}}AFX_MSG_MAP
	ON_MESSAGE(CGmsModem::WM_NEW_SM,OnNewSm)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CManageMdmDlg message handlers

BOOL CManageMdmDlg::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
	
	m_modem.SetNewSmEvent(CGmsModem::WM_NEW_SM,this->GetSafeHwnd());
	
	CreateList();
	
	RefreshList();

	RefreshButton();
	

	return TRUE;  // return TRUE  unless you set the focus to a control
}

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


void CManageMdmDlg::RefreshList()
{

	m_list.DeleteAllItems();

	vector<STRU_SMS>::iterator item;
	
	int i=0; 

	for(item = m_vecSm.begin(); item < m_vecSm.end(); item++)
	{
			
		CStringArray listItem;
		
		listItem.Add(CTool::IntToString(i+1));
		
		if( -1!=item->iIndex)
		{
			//Add no
			
			
			//add from
			listItem.Add( item ->cSender);
			
			//add date
			listItem.Add(item->cTimeStamp);
	
		}
		else
		{
	
			listItem.Add("None");
			listItem.Add("");
			
		}
		
		i++;
		
		m_list.AddItem(m_list.GetItemCount(), listItem, 0,FALSE);
		
	}

}

void CManageMdmDlg::CreateList()
{
	m_list.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_HEADERDRAGDROP);

	CImageList img;
	img.Create(16, 16, ILC_COLOR8|ILC_MASK,2,2);
	
	img.Add(HICON(AfxGetApp()->LoadIcon(IDI_ICON_SM))); //group image collapse
	
	m_list.SetImageList(&img,LVSIL_SMALL);
	
	//realse image list
	img.Detach();
	
	//set head
	//Create Listview Head

	CRect listRect;
	m_list.GetWindowRect(&listRect);

	LIST_HEAD_ITEM headItem[]=
	{
		{IDS_ID,60},
		{IDS_FROM,150},
		{IDS_DATE,listRect.Width()-235},

			
	};
	
	for(int i=0; i<sizeof(headItem)/sizeof(LIST_HEAD_ITEM); i++)
	{	
		CString temp;
		temp.LoadString(headItem[i].nColumnStrId);
		m_list.InsertColumn(i,_T(temp),
			LVCFMT_LEFT,headItem[i].iHeadItemWidth);
	}
	
}


void CManageMdmDlg::RefreshAtCommand()
{
	//the max 1024 byte at command sets


	m_ctlAtCmd.SetWindowText(m_modem.GetLog());

	
	m_ctlAtCmd.SetScrollPos(SB_VERT,m_ctlAtCmd.GetScrollLimit(SB_VERT));

	UpdateData(FALSE);
	
	
}

void CManageMdmDlg::OnButtonConnect() 
{
	// TODO: Add your control notification handler code here
	try
	{
		m_modem.LinkModem(m_nCommPort);
		m_bIsConnected=true;
		RefreshButton();
	}
	catch (CExceptionEx *pEx)
	{
		pEx->Prompt("void CManageMdmDlg::OnButtonConnect()");
	}

	RefreshAtCommand();
}

void CManageMdmDlg::OnButton5() 
{
	// TODO: Add your control notification handler code here
	m_modem.Close();
	m_bIsConnected=false;
	RefreshButton();
}

void CManageMdmDlg::OnButtonGetContent() 
{
	// TODO: Add your control notification handler code here
	
	m_progress.SetRange(0,MAX_SM_SIZE);
	
	int i;

	try
	{	
		for(i=1; i<= MAX_SM_SIZE; i++)
		{
			m_progress.SetPos(i);
			
			m_modem.DelSm(i);
		}

	}
	catch (CExceptionEx *pEx)
	{
		m_progress.SetPos(MAX_SM_SIZE);

		m_nSimContent=i-1;

		UpdateData(FALSE);

		RefreshAtCommand();

		
	}
	
}

void CManageMdmDlg::OnButtonReadAllSm() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);

	m_vecSm.erase(m_vecSm.begin(),m_vecSm.end());
	
	m_progress.SetRange(0, m_nSimContent);

	for(int i=0 ;i<m_nSimContent; i++)
	{
		STRU_SMS newSm;
		
		try
		{
			newSm.iIndex=i;
			m_modem.ReadSm(&newSm,i+1);
		}
		catch (CExceptionEx *pEx)
		{
			newSm.iIndex=-1;
		}

		m_vecSm.push_back(newSm);
		
		m_progress.SetPos(i+1);
	}

	RefreshList();

	RefreshAtCommand();
}

void CManageMdmDlg::OnSendSmButton() 
{
	// TODO: Add your control notification handler code here
	CSendSmDlg dlg;
	dlg.DoModal();
}

CGmsModem &CManageMdmDlg::GetModem()
{
	return m_modem;
}

void CManageMdmDlg::OnItemchangedList1(NMHDR* pNMHDR, LRESULT* pResult) 
{
	HD_NOTIFY *phdn = (HD_NOTIFY *) pNMHDR;
	// TODO: Add your control notification handler code here
	
	*pResult = 0;
}

void CManageMdmDlg::OnClickList1(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	
	POSITION pos=m_list.GetFirstSelectedItemPosition();
	
	UpdateData(TRUE);
	
	if(pos!=NULL )
	{
		int iItem=m_list.GetNextSelectedItem(pos);
		
		vector<STRU_SMS>::iterator curSelItem= m_vecSm.begin() + iItem;
		
		if(-1 == curSelItem->iIndex)
		{
			m_sMessage="";
			m_from="";
			m_date="";
			UpdateData(FALSE);
			
			return;
		}
		
		m_sMessage=curSelItem->cMessage;
		
		m_from=curSelItem->cSender;
		
		m_date=curSelItem->cTimeStamp;
		
		UpdateData(FALSE);
	}
	
	*pResult = 0;
}


void CManageMdmDlg::RefreshButton()
{
	if(m_bIsConnected)
	{
		GetDlgItem(IDC_BUTTON_CONNECT)->EnableWindow(FALSE);
		GetDlgItem(IDC_BUTTON_READ_ALL_SM)->EnableWindow(TRUE);
		GetDlgItem(IDC_BUTTON5)->EnableWindow(TRUE);
		GetDlgItem(ID_SEND_SM_BUTTON)->EnableWindow(TRUE);
		GetDlgItem(IDC_BUTTON_EXEC_CMD)->EnableWindow(TRUE);
		
		if(!m_list.GetFirstSelectedItemPosition())
		{
			GetDlgItem(IDC_BUTTON_DEL_SM)->EnableWindow(TRUE);
		}
		else
		{
			GetDlgItem(IDC_BUTTON_DEL_SM)->EnableWindow(FALSE);
		}

		GetDlgItem(IDC_BUTTON_GET_CONTENT)->EnableWindow(TRUE);
		
	}
	else
	{
		GetDlgItem(IDC_BUTTON_CONNECT)->EnableWindow(TRUE);
		GetDlgItem(IDC_BUTTON_READ_ALL_SM)->EnableWindow(FALSE);
		GetDlgItem(IDC_BUTTON5)->EnableWindow(FALSE);
		GetDlgItem(ID_SEND_SM_BUTTON)->EnableWindow(FALSE);
		GetDlgItem(IDC_BUTTON_EXEC_CMD)->EnableWindow(FALSE);
		GetDlgItem(IDC_BUTTON_DEL_SM)->EnableWindow(FALSE);
		GetDlgItem(IDC_BUTTON_GET_CONTENT)->EnableWindow(FALSE);
	}
}

void CManageMdmDlg::OnButtonDelSm() 
{
	// TODO: Add your control notification handler code here
	POSITION pos=m_list.GetFirstSelectedItemPosition();
	
	while(pos!=NULL)
	{
		int iItem=m_list.GetNextSelectedItem(pos);
		
		vector<STRU_SMS>::iterator curSelItem= m_vecSm.begin() + iItem;
		
		if(-1!= curSelItem->iIndex)
		{
			try
			{
				m_modem.DelSm(curSelItem ->iIndex);

				//already delete sm
				
			}
			catch (CExceptionEx *pEx)
			{
				
				//pEx->Prompt("void CManageMdmDlg::OnButtonDelSm()");		
			}

			curSelItem->iIndex=-1;
			
		}
		
	}

	RefreshList();
}

LRESULT CManageMdmDlg::OnNewSm(WPARAM wParam, LPARAM lParam)
{
	int index = lParam;
	
	ASSERT(index >0);

	

	STRU_SMS newSm;
	
	try
	{
		m_modem.ReadSm(&newSm,index);
	}
	catch (CExceptionEx *pEx)
	{
		newSm.iIndex=-1;
	}

	if(index!=-1)
	{
		CString info;
		
		info.Format("Receive a new Sm. It's index is %d. \n"
			"From:%s \n Date:%s \n Message:%s \n", index,newSm.cSender,
			newSm.cTimeStamp,newSm.cMessage);

		AfxMessageBox(info, MB_OK|MB_ICONINFORMATION);
	}
	
	RefreshAtCommand();
	
	return 0L;
}

void CManageMdmDlg::OnOK() 
{
	// TODO: Add extra validation here
	
	//CDialog::OnOK();
}

void CManageMdmDlg::OnAboutButton() 
{
	// TODO: Add your control notification handler code here
	CAboutDlg dlg;

	dlg.DoModal();
}

void CManageMdmDlg::OnButtonSetup() 
{
	// TODO: Add your control notification handler code here
	CSetupDlg dlg;
	
	dlg.m_nPort=m_nCommPort-1;

	if(IDOK==dlg.DoModal())
	{
		m_nCommPort =dlg.m_nPort+1;
	}

}

⌨️ 快捷键说明

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