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

📄 nclient.cpp

📁 《Visual C++经典编程大全》配套代码
💻 CPP
字号:
// NClient.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "NClient.h"
#include "DlgMain.h"
#include "DlgLogin.h"

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

/////////////////////////////////////////////////////////////////////////////
// CNClientApp

BEGIN_MESSAGE_MAP(CNClientApp, CWinApp)
	//{{AFX_MSG_MAP(CNClientApp)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG
	ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CNClientApp construction

CNClientApp::CNClientApp()
{	
	m_ServerIP = "";
	m_ServerPort = 0;
	m_socket = NULL;
	m_IsConnect = false;
	m_status = STA_NORMAL;
	m_strLastMsg = "";
	m_arrLastMsg = NULL;

	m_strUserCode = "";
	m_strUserName = "";
	m_strUserDesc = "";

	m_pMainDlg = NULL;	
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CNClientApp object

CNClientApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CNClientApp initialization

BOOL CNClientApp::InitInstance()
{
	AfxEnableControlContainer();

	if (!AfxSocketInit())
	{
		AfxMessageBox(CG_IDS_SOCKETS_INIT_FAILED);
		return FALSE;
	}
	// Standard initialization
	// If you are not using these features and wish to reduce the size
	//  of your final executable, you should remove from the following
	//  the specific initialization routines you do not need.

#ifdef _AFXDLL
	Enable3dControls();			// Call this when using MFC in a shared DLL
#else
	Enable3dControlsStatic();	// Call this when linking to MFC statically
#endif

	m_socket = new Cclient;
	if (!m_socket->Create())
	{
		delete m_socket;
		m_socket = NULL;
		AfxMessageBox("Create socket failed!");
		return FALSE;
	}

	LoadConfig();
	
	CDlgLogin pDlg;
	if(pDlg.DoModal()!=IDOK)
		return FALSE;
//	CNClientDlg dlg;
	CDlgMain dlg;
	m_pMainWnd = &dlg;
	m_pMainDlg = &dlg;
	int nResponse = dlg.DoModal();
	if (nResponse == IDOK)
	{
		// TODO: Place code here to handle when the dialog is
		//  dismissed with OK
	}
	else if (nResponse == IDCANCEL)
	{
		// TODO: Place code here to handle when the dialog is
		//  dismissed with Cancel
	}

	SaveConfig();

	// Since the dialog has been closed, return FALSE so that we exit the
	//  application, rather than start the application's message pump.
	return FALSE;
}

bool CNClientApp::ToConnect()
{
	if(m_IsConnect)
		return true;
	
	while(!m_socket->Connect(m_ServerIP,m_ServerPort))
	{
		if(AfxMessageBox("是否重新连接?",MB_YESNO)==IDNO)
		{
			return false;
		}
	}		
	
	m_IsConnect = true;

	return true;
}

bool CNClientApp::LoadConfig()
{
	char Vals[100];
	
	HKEY hk;
	DWORD rc = RegOpenKey(HKEY_LOCAL_MACHINE, "SOFTWARE\\MyAppClient\\",&hk);
	if(rc != ERROR_SUCCESS)	return false;
	
	DWORD lenIt = 100;
	DWORD dType;

	if(RegQueryValueEx(hk, "Server IP", NULL, &dType, (BYTE *)Vals, &lenIt) == ERROR_SUCCESS){
		m_ServerIP=(CString)Vals;
		if(strlen(m_ServerIP)<=0)	return false;
	}
	else
		return false ;

	lenIt = 100;
	if (RegQueryValueEx(hk, "Server Port", NULL, &dType, (BYTE *)&m_ServerPort, &lenIt) != ERROR_SUCCESS){
		m_ServerPort = 0;
		return false;
	}

	::RegCloseKey(hk);

	return true;
}

bool CNClientApp::SaveConfig()
{
	CString str = "";
	HKEY hKey;

    if (ERROR_SUCCESS != ::RegCreateKey(HKEY_LOCAL_MACHINE, "SOFTWARE\\MyAppClient\\", &hKey)) 
		return false;
	
	try{	

		RegSetValueEx(hKey, "Server IP",   0, REG_SZ, (LPBYTE)(LPCSTR)m_ServerIP, m_ServerIP.GetLength()+1);
		RegSetValueEx(hKey, "Server Port", NULL, REG_DWORD, (BYTE *)&m_ServerPort, sizeof(m_ServerPort));
		
		::RegCloseKey(hKey);
		return true;
	}
	catch(...){
		return false;	
	}	
	return true;
}

void CNClientApp::DeleteLastMsg()
{
	CArrayLastMsg *pItem = m_arrLastMsg;

	if(pItem == NULL) 
		return;

	while(pItem != NULL){
		m_arrLastMsg = pItem->pNext;
		delete pItem;
		pItem = m_arrLastMsg;
	}

}

void CNClientApp::AddLastMsg(CString nData)
{
	CArrayLastMsg *pItem = new CArrayLastMsg;

	pItem->strData = nData;
	pItem->pNext = m_arrLastMsg;
	m_arrLastMsg = pItem;

}

void CNClientApp::WriteData(CString strData)
{
	m_pMainDlg->WriteData(strData);
}

⌨️ 快捷键说明

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