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

📄 simpledemodlg.cpp

📁 小灵通短信开发DEMO
💻 CPP
字号:
// SimpleDemoDlg.cpp : implementation file
//

#include "stdafx.h"
#include ".\\export.h"
#include "SimpleDemo.h"
#include "SimpleDemoDlg.h"
#include "Shlwapi.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()

/////////////////////////////////////////////////////////////////////////////
// CSimpleDemoDlg dialog

CSimpleDemoDlg::CSimpleDemoDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSimpleDemoDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSimpleDemoDlg)
	m_strDataCoding = _T("15");
	m_strPriFlag = _T("1");
	m_strDestAddr = _T("020000000");
	m_bAutoRecv = FALSE;
	m_nTimes = 1;
	m_strSentFail = _T("");
	m_strSentSuc = _T("");
	m_strSM_ID = _T("");
	m_bNeedReport = FALSE;
	m_strRecvData = _T("");
	m_strSendData = _T("");
	m_nRecvCount = 0;
	m_strSourceAddr = _T("");
	m_ActiveStatus = _T("");
	m_strMsgType = _T("1");
	m_strFeeCode = _T("");
	m_strFeeType = _T("");
	m_strFeeTermID = _T("");
	m_strFixedFee = _T("");
	m_strServiceID = _T("");
	m_strValidTime = _T("");
	m_strAtTime = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_bInited = FALSE;	
}

void CSimpleDemoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSimpleDemoDlg)
	DDX_Text(pDX, IDC_EDIT_DataCoding, m_strDataCoding);
	DDX_Text(pDX, IDC_EDIT_PriFlag, m_strPriFlag);
	DDX_Text(pDX, IDC_EDIT_DestAddr, m_strDestAddr);
	DDX_Check(pDX, IDC_CHECK_AutoRecv, m_bAutoRecv);
	DDX_Text(pDX, IDC_EDIT_SendTimes, m_nTimes);
	DDV_MinMaxUInt(pDX, m_nTimes, 1, 32768);
	DDX_Text(pDX, IDC_EDIT_Sent_Fail, m_strSentFail);
	DDX_Text(pDX, IDC_EDIT_Sent_Suc, m_strSentSuc);
	DDX_Text(pDX, IDC_EDIT_SM_ID, m_strSM_ID);
	DDX_Check(pDX, IDC_CHECK_NeedReport, m_bNeedReport);
	DDX_Text(pDX, IDC_EDIT_RecvData, m_strRecvData);
	DDX_Text(pDX, IDC_EDIT_SendData, m_strSendData);
	DDX_Text(pDX, IDC_EDIT_Recv_Count, m_nRecvCount);
	DDX_Text(pDX, IDC_EDIT_SrcAddr, m_strSourceAddr);
	DDX_Text(pDX, IDC_EDIT_ActiveTest, m_ActiveStatus);
	DDX_Text(pDX, IDC_EDIT_MsgType, m_strMsgType);
	DDX_Text(pDX, IDC_EDIT_FeeCode, m_strFeeCode);
	DDX_Text(pDX, IDC_EDIT_FeeType, m_strFeeType);
	DDX_Text(pDX, IDC_EDIT_FeeTermID, m_strFeeTermID);
	DDX_Text(pDX, IDC_EDIT_FixedFee, m_strFixedFee);
	DDX_Text(pDX, IDC_EDIT_ServiceID, m_strServiceID);
	DDX_Text(pDX, IDC_EDIT_ValidTime, m_strValidTime);
	DDX_Text(pDX, IDC_EDIT_AtTime, m_strAtTime);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSimpleDemoDlg, CDialog)
	//{{AFX_MSG_MAP(CSimpleDemoDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_Init, OnInit)
	ON_BN_CLICKED(IDC_Close, OnClose)
	ON_BN_CLICKED(IDC_Send, OnSend)
	ON_EN_KILLFOCUS(IDC_SendData, OnKillfocusSendData)
	ON_BN_CLICKED(ID_OK, OnOk)
	ON_BN_CLICKED(IDC_CHECK_AutoRecv, OnCHECKAutoRecv)
	ON_BN_CLICKED(IDC_Receive, OnReceive)
	ON_BN_CLICKED(IDC_ActiveTest, OnActiveTest)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSimpleDemoDlg message handlers

BOOL CSimpleDemoDlg::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
	  
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CSimpleDemoDlg::OnInit() 
{
	UpdateData();
	if( SMGP_API_SUCCESS == InitSMGPAPI(".\\smgpc.ini") )
	{
		m_bInited = TRUE;
		GetDlgItem(IDC_Init)->EnableWindow(FALSE);
		GetDlgItem(IDC_Send)->EnableWindow();
		GetDlgItem(IDC_Close)->EnableWindow();
		GetDlgItem(IDC_Receive)->EnableWindow();
		GetDlgItem(IDC_CHECK_AutoRecv)->EnableWindow();
		GetDlgItem(IDC_ActiveTest)->EnableWindow();
	}
}


void CSimpleDemoDlg::OnClose() 
{
	GetDlgItem(IDC_Init)->EnableWindow();
	GetDlgItem(IDC_Receive)->EnableWindow(FALSE);
	GetDlgItem(IDC_Close)->EnableWindow(FALSE);
	GetDlgItem(IDC_Send)->EnableWindow(FALSE);
	GetDlgItem(IDC_CHECK_AutoRecv)->EnableWindow(FALSE);
	GetDlgItem(IDC_ActiveTest)->EnableWindow(FALSE);
	m_bInited = FALSE;
	m_bAutoRecv = FALSE;
	UpdateData(FALSE);
	OnCHECKAutoRecv();

	SMGPDisconnect();

	SetWindowText("Demo");
}

void CSimpleDemoDlg::OnSend() 
{
	GetDlgItem(IDC_Send)->EnableWindow(FALSE);
	UpdateData(TRUE);
	m_nSent =0;
	m_nSentFail =0;
	m_nSentSuc =0;
	m_bEnoughSendThread = FALSE;
	m_strSentFail.Empty();
	m_strSentSuc.Empty();
	UpdateData(FALSE);
	
	for(UINT n=0; n<m_nTimes; n++)
	{
		m_MuSend.Lock();
		BOOL bEnough = m_bEnoughSendThread;
		m_MuSend.Unlock();

		if( bEnough == TRUE )
			break;

		DWORD id;
		CreateThread(NULL, 0, SendThread, this, 0, &id);
		UpdateData(FALSE);
		MessagePump();
	}

	TRACE("Send Thread: %d\n", n);

	while(1)
	{
		m_MuSend.Lock();
		UINT nSent = m_nSentFail + m_nSentSuc;
		m_MuSend.Unlock();
		if( nSent == m_nTimes )
			break;
		UpdateData(FALSE);
		MessagePump();
	}

	UpdateData(FALSE);
	GetDlgItem(IDC_Send)->EnableWindow(TRUE);
}

void CSimpleDemoDlg::OnKillfocusSendData() 
{
	UpdateData();
//	m_strSM_ID.Empty();
	UpdateData(FALSE);
}

void CSimpleDemoDlg::OnOk() 
{
}

void CSimpleDemoDlg::OnCancel() 
{
	if(m_bInited)
	{
		MessageBox("Plz CLOSE first.");
	
	}
	else
	{
//		ProxyExit();
		CDialog::OnCancel();
	}

}

void CSimpleDemoDlg::OnCHECKAutoRecv() 
{
	UpdateData();
	if( m_bAutoRecv )
	{
		DWORD id;
		CreateThread(NULL, 0, RecvThread, this, 0, &id);
		GetDlgItem(IDC_Receive)->EnableWindow(FALSE);
	}
	else
	{
		if( m_bInited )
			GetDlgItem(IDC_Receive)->EnableWindow();
	}
}

DWORD CSimpleDemoDlg::SendThread(LPVOID lparam)
{
	CSimpleDemoDlg* pDlg = (CSimpleDemoDlg*)lparam;
	char strSM_ID[65];//短消息ID
	char strNum[245];

	while(pDlg->m_bInited)
	{
		pDlg->m_MuSend.Lock();
		if( pDlg->m_nSent == pDlg->m_nTimes)
		{
			pDlg->m_MuSend.Unlock();
			return 0;
		}
		sprintf(strNum, " %d", ++(pDlg->m_nSent) );
		pDlg->m_MuSend.Unlock();

		CString strSendString = pDlg->m_strSendData;// + strNum;
		int nCount = strSendString.GetLength();

		int nErrorCode;
		int nReturn = SMGPSendSingle( pDlg->m_bNeedReport,
			atoi((LPCSTR)pDlg->m_strPriFlag),
			(LPCSTR)pDlg->m_strServiceID,
			atoi( (LPCSTR)pDlg->m_strDataCoding),
			(LPCSTR)pDlg->m_strFeeType,
			(LPCSTR)pDlg->m_strFeeCode,
			(LPCSTR)pDlg->m_strFixedFee,
			(LPCSTR)pDlg->m_strValidTime,
			(LPCSTR)pDlg->m_strAtTime,
			(LPCSTR)pDlg->m_strFeeTermID,
			(LPCSTR)pDlg->m_strDestAddr,
			nCount,
			(LPCSTR)strSendString,
			strSM_ID,
			&nErrorCode,
			atoi(pDlg->m_strMsgType),
			(LPCSTR)pDlg->m_strSourceAddr);

		pDlg->m_MuSend.Lock();
		if( nReturn == SMGP_API_SUCCESS )
		{//成功
			pDlg->m_nSentSuc ++;
			pDlg->m_strSentSuc.Format("%d", pDlg->m_nSentSuc);

			pDlg->m_strSM_ID = strSM_ID;
		}
		else
		{
			pDlg->m_nSentFail ++;
			pDlg->m_strSentFail.Format("%d", pDlg->m_nSentFail);
			CString num;
			num.Format("%d",nErrorCode);
			pDlg->GetDlgItem(IDC_EDIT_ErrorCode)->SetWindowText( num );
		}

		pDlg->m_bEnoughSendThread = TRUE;
		pDlg->m_MuSend.Unlock();
	}
	return 0;
}

void CSimpleDemoDlg::MessagePump()
{
	MSG msg;
	if( GetMessage( &msg, NULL, 0, 0) )
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
}

DWORD CSimpleDemoDlg::RecvThread(LPVOID lparam)
{
	UINT nElapse = 1000;
	CSimpleDemoDlg* pDlg = (CSimpleDemoDlg*)lparam;
	{
		pDlg->m_nRecvCount = 0;
		CString strNum;
		strNum.Format("%d", pDlg->m_nRecvCount);
		pDlg->GetDlgItem(IDC_EDIT_Recv_Count)->
			SetWindowText(strNum);
	}

	while( pDlg->m_bAutoRecv )
	{
		DeliverResp Dlvr;
		if( SMGP_API_SUCCESS == SMGPDeliver( 5000, &Dlvr ) )
		{
			CString strContent( Dlvr.sMsgContent, Dlvr.NMsgLen);
			pDlg->m_nRecvCount ++;
		
			pDlg->
				GetDlgItem(IDC_EDIT_RecvData)->
				SetWindowText( strContent );
			
			pDlg->
				GetDlgItem(IDC_EDIT_Recv_DestAddr)->
				SetWindowText( Dlvr.sDestTermID );

			pDlg->
				GetDlgItem(IDC_EDIT_Recv_SrcAddr)->
				SetWindowText( Dlvr.SSrcTermID );

			CString strNum;
			strNum.Format("%d", pDlg->m_nRecvCount);
			pDlg->
				GetDlgItem(IDC_EDIT_Recv_Count)->
				SetWindowText(strNum);
		}
	}
	return 0;
}

void CSimpleDemoDlg::OnReceive() 
{
	GetDlgItem(IDC_Receive)->EnableWindow(FALSE);
	DeliverResp Dlvr;
	if( SMGP_API_SUCCESS == SMGPDeliver( 5000, &Dlvr ) )
	{
		CString strContent( Dlvr.sMsgContent, Dlvr.NMsgLen);
		m_nRecvCount ++;
		
		GetDlgItem(IDC_EDIT_RecvData)->
			SetWindowText( strContent );
		
		GetDlgItem(IDC_EDIT_Recv_DestAddr)->
			SetWindowText( Dlvr.sDestTermID );
		
		GetDlgItem(IDC_EDIT_Recv_SrcAddr)->
			SetWindowText( Dlvr.SSrcTermID );
		
		
		m_nRecvCount ++;
		UpdateData( FALSE );
	}
	GetDlgItem(IDC_Receive)->EnableWindow(TRUE);
}

void CSimpleDemoDlg::OnActiveTest() 
{
	GetDlgItem(IDC_ActiveTest)->EnableWindow(FALSE);
	if( SMGP_API_SUCCESS == CMPPActiveTest(3) )
		m_ActiveStatus = "Success";
	else
		m_ActiveStatus = "Fail";

	UpdateData( FALSE );	
	GetDlgItem(IDC_ActiveTest)->EnableWindow();
}

BOOL CSimpleDemoDlg::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext) 
{
	// TODO: Add your specialized code here and/or call the base class
	
	return CDialog::Create(IDD, pParentWnd);
}

⌨️ 快捷键说明

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