www.pudn.com > src.zip > Client.cpp


// Client.cpp : Defines the class behaviors for the application. 
// 
 
#include "stdafx.h" 
#include "Client.h" 
 
#include "MainFrm.h" 
#include "LoginDlg.h" 
#include "registerdlg.h" 
#include "SetupServerDlg.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CClientApp 
 
BEGIN_MESSAGE_MAP(CClientApp, CWinApp) 
	//{{AFX_MSG_MAP(CClientApp) 
	ON_COMMAND(ID_APP_ABOUT, OnAppAbout) 
	ON_COMMAND(ID_SYS_SETUP, OnSysSetup) 
	ON_COMMAND(ID_SEND_FILE, OnSendFile) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CClientApp construction 
 
CClientApp::CClientApp() 
{ 
	// TODO: add construction code here, 
	// Place all significant initialization in InitInstance 
	m_uCurrentUserID=0; 
	m_bOnlineState=0; 
	m_nCurSysMsg=0; 
	m_nCurNotice=0;	 
	m_nCurOther=0; 
	m_nBitmapNum=0; 
	m_nNumberHeadBmp=0; 
	m_nTotalOnline=0; 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// The one and only CClientApp object 
 
CClientApp theApp; 
 
///////////////////////////////////////////////////////////////////////////// 
// CClientApp initialization 
 
BOOL CClientApp::InitInstance() 
{ 
	if (!AfxSocketInit()) 
	{ 
		AfxMessageBox(IDP_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 
 
	// Change the registry key under which our settings are stored. 
	// TODO: You should modify this string to be something appropriate 
	// such as the name of your company or organization. 
	SetRegistryKey(_T("My Program")); 
 
	if(!m_Socket.Create()) 
	{ 
		AfxMessageBox(IDS_CREATE_SOCKET_ERROR); 
		return FALSE; 
	} 
 
	if(!LoadFaceBmp()) 
	{ 
		AfxMessageBox(IDS_FACE_FILE_LOAD_FAILE); 
		return FALSE; 
	} 
	 
	CArrayAllUserID; 
	 
	int LoginType=ONLINE; 
	BOOL bApplyID=FALSE; 
 
	if(!LoadLocalAllUserID(AllUserID))	 
Regiser_begin: 
	{ 
		CRegisterDlg dlg; 
		m_Socket.SetOwner(&dlg); 
		 
		CStdioFile file1;	 
		if(file1.Open("Server.ini",CFile::modeRead)) 
		{		 
			CString str1,str2,str3,str4; 
			file1.ReadString(str1);	 
			file1.ReadString(str2);	 
			file1.Close(); 
			m_strServerIP=str1.Mid(3);			 
		    m_uServerPort=atoi(str2.GetBuffer(50)+5);			 
		} 
		else 
		{ 
			m_strServerIP=DEFAULT_SERVER_IP; 
			m_uServerPort=DEFAULT_SERVER_PORT;		 
		}		 
		 
		//m_strServerIP="192.168.0.208"; 
		//m_uServerPort=4000; 
		dlg.m_strServerIP=m_strServerIP; 
		dlg.m_uServerPort=m_uServerPort; 
		if(dlg.DoModal()==IDCANCEL) 
		{ 
			m_Socket.Close(); 
			return FALSE; 
		} 
		m_uCurrentUserID=dlg.m_uUserID; 
		m_strCurUserDir.Format("%d\\",m_uCurrentUserID); 
		m_strCurIDpwd=dlg.m_strPassword; 
		m_strServerIP=dlg.m_strServerIP; 
		m_uServerIP=dlg.m_uServerIP; 
		m_uServerPort=dlg.m_uServerPort; 
		SaveCurIDToFile(); 
		::CreateDirectory(m_strCurUserDir,NULL); 
		CFile f1(m_strCurUserDir+USERSETUP,CFile::modeCreate|CFile::modeWrite); 
		f1.Write((char*)&m_uServerIP,sizeof(DWORD)); 
		f1.Write((char*)&m_uServerPort,sizeof(DWORD)); 
		f1.Close(); 
		if(dlg.m_bApplyID) 
		{			 
			bApplyID=TRUE;			 
		} 
	} 
	else 
	{ 
		CLoginDlg dlg; 
		dlg.SetUserID(AllUserID); 
		m_Socket.SetOwner(&dlg); 
		if(dlg.DoModal()==IDCANCEL) 
		{ 
			m_Socket.Close(); 
			return FALSE; 
		} 
		if(dlg.lresult==1) 
			goto Regiser_begin; 
 
		if(dlg.m_LoginType)LoginType=ONHIDE; 
		else LoginType=ONLINE; 
		m_uCurrentUserID=dlg.uCurID; 
		m_strCurIDpwd=dlg.pwd; 
		m_strCurUserDir.Format("%d\\",m_uCurrentUserID); 
		m_uServerIP=dlg.uServerIP; 
		m_uServerPort=dlg.uPort; 
		LoadFriendInfo(); 
	} 
	 
	in_addr tIP; 
	tIP.S_un.S_addr=m_uServerIP; 
	m_strServerIP=inet_ntoa(tIP); 
	 
	CMainFrame* pFrame = new CMainFrame; 
	m_pMainWnd = pFrame; 
	pFrame->LoadFrame(IDR_MAINFRAME, 
		WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, NULL, 
		NULL); 
	 
	m_Socket.SetOwner(pFrame);	 
	pFrame->ShowWindow(SW_SHOW); 
	pFrame->UpdateWindow(); 
	 
	if(bApplyID) 
	{		 
		if(!m_bOnlineState) 
			pFrame->SetTimer(1,TimerSpanClient,NULL); 
		m_bOnlineState=1; 
		CString str=m_strCurUserDir;			 
		str=str.Left(str.GetLength()-1); 
		pFrame->m_TrayIcon.SetIcon(IDR_ONLINE,str); 
	} 
	else 
	{ 
		CData msg;	 
		msg.index=LoginType;		 
		msg.MyId=m_uCurrentUserID; 
		msg.tarIP=m_uServerIP; 
		msg.nPort=m_uServerPort;		 
		m_Socket.SendDataDirect(&msg);		 
	} 
	return TRUE; 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CClientApp message handlers 
 
BOOL CClientApp::LoadLocalAllUserID(CArray&aID) 
{ 
	CFile file; 
	if(!file.Open(ALLUSERIDFILE,CFile::modeRead)) 
		return FALSE; 
	int num=0,data; 
	file.Read(&num,sizeof(int)); 
	for(int i=0;iaFilename; 
	 
	char temp[50]; 
	for(int i=0;iAttach(bitmap);		 
		bit=(HBITMAP)::CopyImage(bitmap,IMAGE_BITMAP,16,16,LR_CREATEDIBSECTION); 
		m_paSmallBitmap[i]->Attach(bit); 
		 
		m_imaLarge.Add(m_paBitmap[i],crColorMask); 
		m_imaSmall.Add(m_paSmallBitmap[i],crColorMask);		 
		if(i%2==0) 
		{ 
			m_imaHead.Add(m_paBitmap[i],crColorMask); 
			m_imaSmallHead.Add(m_paSmallBitmap[i],crColorMask); 
		} 
	} 
 
	aFilename.RemoveAll(); 
 
	return TRUE; 
} 
 
BOOL CClientApp::LoadFriendInfo() 
{ 
	CFile file; 
	int bufoffset=UserFileReservedLength+MaxPasswordLength+3; 
	int countoffset=UserFileReservedLength+1; 
	if(!file.Open(m_strCurUserDir+USERFILE,CFile::modeRead)) 
	{ 
		return FALSE; 
	} 
	 
	WORD uCount=0; 
	file.Seek(countoffset,CFile::begin); 
	file.Read(&uCount,sizeof(WORD)); 
 
	file.Seek(bufoffset,CFile::begin); 
	for(int i=0;i