spgamedlg.cpp

来自「SP短消息平台」· C++ 代码 · 共 775 行 · 第 1/2 页

CPP
775
字号
// SPGameDlg.cpp : implementation file
//

#include "stdafx.h"
#include "SPGame.h"
#include "SPGameDlg.h"



#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
int CSPGameDlg::m_nMsgNumber;


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
	//ON_MESSAGE(NETWORK_EVENT, OnNetWorkEvent)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSPGameDlg dialog

CSPGameDlg::CSPGameDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSPGameDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSPGameDlg)
	m_strEnterpriseCode = _T("");
	m_strGatewayPort = _T("");
	m_strNodeID = _T("");
	m_strPassword = _T("");
	m_strUserName = _T("");
	m_strGatewayIP = _T("");
	m_strLocalIP = _T("");
	m_strLocalPort = _T("");
	m_strConnectID = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CSPGameDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSPGameDlg)
	DDX_Control(pDX, IDC_SYSTEMSTATE, m_strSystemState);
	DDX_Text(pDX, IDC_ENTERPRISECODE, m_strEnterpriseCode);
	DDX_Text(pDX, IDC_GATEWAYPORT, m_strGatewayPort);
	DDX_Text(pDX, IDC_NODEID, m_strNodeID);
	DDX_Text(pDX, IDC_PASSWORD, m_strPassword);
	DDX_Text(pDX, IDC_USERNAME, m_strUserName);
	DDX_Text(pDX, IDC_GATEWAYADDR, m_strGatewayIP);
	DDX_Text(pDX, IDC_LOCALADDR, m_strLocalIP);
	DDX_Text(pDX, IDC_LOCALPORT, m_strLocalPort);
	DDX_Text(pDX, IDC_CONNECTID, m_strConnectID);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSPGameDlg, CDialog)
	//{{AFX_MSG_MAP(CSPGameDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_CONNECTGATEWAY, OnConnectGateway)
	ON_BN_CLICKED(IDC_DISCONNECTGATEWAY, OnDisconnectGateway)
	ON_BN_CLICKED(IDC_SENDMESSAGE, OnSendMessage)
//	ON_BN_CLICKED(IDC_SAVESET, OnSaveSet)
	ON_MESSAGE(NETWORK_EVENT, OnNetWorkEvent)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSPGameDlg message handlers

BOOL CSPGameDlg::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
	this->InitData ();
	this->UpdateData (FALSE);

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

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

//***************************************************************************************
		//函数实现代码
		//作者:张绍君
		//日期:2004/12/3
//***************************************************************************************
void CSPGameDlg::OnConnectGateway() 
{
	// TODO: Add your control notification handler code here
	BOOL bRet = InitNetwork ();
	if (bRet)
	{
		this->GetDlgItem (IDC_CONNECTGATEWAY)->EnableWindow(FALSE);
		this->GetDlgItem (IDC_DISCONNECTGATEWAY)->EnableWindow(TRUE);
		this->GetDlgItem (IDC_SENDMESSAGE)->EnableWindow(TRUE);
		this->SaveSet();

		CString strLogin;
		strLogin = "1testtest00000000";

		CString strTime;
		CTime t = CTime::GetCurrentTime ();
		int nMonth, nDay, nHour, nMinute, nSecond;
		nMonth  = t.GetMonth ();
		nDay    = t.GetDay ();
		nHour   = t.GetHour ();
		nMinute = t.GetMinute ();
		nSecond = t.GetSecond ();

		CString strTemp;

		if (nMonth/10 > 0)
			strTemp.Format ("%d", nMonth);
		else
			strTemp.Format ("0%d", nMonth);
		strTime += strTemp;

		if (nDay/10 > 0)
			strTemp.Format ("%d", nDay);
		else
			strTemp.Format ("0%d", nDay);
		strTime += strTemp;

		if (nHour/10 > 0)
			strTemp.Format ("%d", nHour);
		else
			strTemp.Format ("0%d", nHour);
		strTime += strTemp;

		if (nMinute/10 > 0)
			strTemp.Format ("%d", nMinute);
		else
			strTemp.Format ("0%d", nMinute);
		strTime += strTemp;

		if (nSecond/10 > 0)
			strTemp.Format ("%d", nSecond);
		else
			strTemp.Format ("0%d", nSecond);
		strTime += strTemp;

//		strTime.Format ("%d%d%d%d%d", t.GetMonth (), t.GetDay (), t.GetHour (), t.GetMinute (), t.GetSecond ());
		CString strPacket;
		strPacket = "1";
		strPacket += strLogin;

		CString strTTT;
		strTTT = "0x1";
		strTTT += strTime;
		strTTT += strPacket;


		int nLen = strTTT.GetLength ();

		CString str;
		str.Format ("%d%s", nLen, strTTT);

		
		MESSAGE_Head  head;
		Sgip_Bind	  bind;
		ZeroMemory (&bind, sizeof(Sgip_Bind));
		ZeroMemory (&head, sizeof(MESSAGE_Head));

		head.Command_ID = htons(SGIP_BIND);
		head.Message_Length = htons(sizeof (bind));
		head.Sequence1 = htons(1234);
		head.Sequence2 = htons(5678);
		head.Sequence3 = htons(9000);
	


		strcpy (bind.LoginName, "5454");
		strcpy (bind.LoginPassword, "W90020");
		bind.LoginType = htons(1);
		bind.Message_Head = head;

		this->SendDataGateway (bind);
	}


}

void CSPGameDlg::OnDisconnectGateway() 
{
	// TODO: Add your control notification handler code here
	this->OnClose ();
	
}

void CSPGameDlg::OnSendMessage() 
{
	// TODO: Add your control notification handler code here
	m_SendConfig.DoModal();
}

void CSPGameDlg::OnOK() 
{
	// TODO: Add extra validation here
	this->FreeVariable ();
	CDialog::OnOK();
}

LRESULT CSPGameDlg::OnNetWorkEvent (WPARAM wParam, LPARAM lParam)
{
	//调用Winsock API函数,得到网络事件类型
    int iEvent = WSAGETSELECTEVENT(lParam);

    //调用Winsock API函数,得到发生此事件的客户端套接字
    SOCKET CurSock= (SOCKET)wParam;

    switch(iEvent)
    {
        case FD_CLOSE:       //客户端断开事件:
			OnClose ();
            break;
        case FD_READ:        //网络数据包到达事件
			OnRead ();
            break;
		case FD_CONNECT:
			OnConnect ();
        default: 
			 break;
     }

	return 1;
}

BOOL CSPGameDlg::SaveSet() 
{
	// TODO: Add your control notification handler code here

	UINT ret;
	ret = WritePrivateProfileString("CONFIGURE", "GatewayIP", m_strGatewayIP, ".\\Set.ini");
	if (ret < 0)
		return FALSE;

	ret = WritePrivateProfileString("CONFIGURE","GatewayPort", m_strGatewayPort, ".\\Set.ini");
	if (ret < 0)
		return FALSE;

	ret = WritePrivateProfileString("CONFIGURE","LocalIP", m_strLocalIP, ".\\Set.ini");
	if (ret < 0)
		return FALSE;

	ret = WritePrivateProfileString("CONFIGURE","LocalPort", m_strLocalPort, ".\\Set.ini");
	if (ret < 0)
		return FALSE;

	ret = WritePrivateProfileString("CONFIGURE","UserName", m_strUserName, ".\\Set.ini");
	if (ret < 0)
		return FALSE;

	ret = WritePrivateProfileString("CONFIGURE","UserPassword", m_strPassword, ".\\Set.ini");
	if (ret < 0)
		return FALSE;

	ret = WritePrivateProfileString("CONFIGURE","EnterpriseCode", m_strEnterpriseCode, ".\\Set.ini");
	if (ret < 0)
		return FALSE;

	ret = WritePrivateProfileString("CONFIGURE","NodeID", m_strNodeID, ".\\Set.ini");
	if (ret < 0)
		return FALSE;

	ret = WritePrivateProfileString("CONFIGURE","ConnectID", m_strConnectID, ".\\Set.ini");
	if (ret < 0)
		return FALSE;
	this->UpdateData (FALSE);

⌨️ 快捷键说明

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