www.pudn.com > MyQQClient.zip > MainSocket.cpp


// MainSocket.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "MyQQClient.h" 
#include "MainSocket.h" 
#include "MyQQClientDlg.h" 
#include "ChatDialog.h" 
#include "LoginLoad.h" 
#include "ChatAction.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CMainSocket 
 
CMainSocket::CMainSocket() 
{ 
} 
 
CMainSocket::~CMainSocket() 
{ 
	CHATDLGINFO *pGlgInfo; 
	for (int i=0;i 0) 
	{ 
		pDlgChat = (CChatDialog *)m_ChatDlgs.GetAt(0); 
		m_ChatDlgs.RemoveAt(0); 
		pDlgChat->DestroyWindow(); 
	} 
	CChatAction * pChatAction; 
	while(m_ChatActions.GetSize() > 0) 
	{ 
		pChatAction = (CChatAction *)m_ChatActions.GetAt(i); 
		m_ChatActions.RemoveAt(0); 
		pChatAction->DestroyWindow(); 
	} 
 
	//m_ChatDlgs.RemoveAll(); 
} 
 
 
// Do not edit the following lines, which are needed by ClassWizard. 
#if 0 
BEGIN_MESSAGE_MAP(CMainSocket, CSocket) 
	//{{AFX_MSG_MAP(CMainSocket) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
#endif	// 0 
 
///////////////////////////////////////////////////////////////////////////// 
// CMainSocket member functions 
 
void CMainSocket::OnReceive(int nErrorCode)  
{ 
	CMyQQClientDlg * pWnd = static_cast (AfxGetMainWnd()); 
	Message msContent; 
	int iLen1 = sizeof(msContent);	int iLen2 = sizeof(Message); 
	memset(&msContent,0,sizeof(msContent)); 
	int iLen = Receive(&msContent,sizeof(msContent)); 
 
	pWnd->SendMessage(WM_RECEIVEMESSAGE,WPARAM(&msContent),0); 
	CSocket::OnReceive(nErrorCode); 
} 
 
void CMainSocket::SetUserName(CString sName) 
{ 
	m_UserName = sName; 
} 
 
CString CMainSocket::GetUserName() 
{ 
	return m_UserName; 
} 
 
void CMainSocket::SetImage(int image) 
{ 
	m_image = image; 
} 
 
int CMainSocket::GetImage() 
{ 
	return m_image; 
} 
 
void CMainSocket::OpenChatDlg(CString UserName) 
{ 
	CChatDialog *pDlgChat; 
	for(int i=0;im_ClientName == UserName) 
		{ 
			if (pDlgChat->IsIconic()) 
				pDlgChat->SendMessage(WM_SYSCOMMAND, SC_RESTORE, 0); 
			pDlgChat->SetForegroundWindow(); 
			return; 
		} 
	} 
	CHATDLGINFO *pInfo; 
	for (i=0;istrUserName == UserName) 
		{ 
			pDlgChat = new CChatDialog(); 
			pDlgChat->m_pClientSocket = this; 
			pDlgChat->m_UserName = UserName; 
			CWnd *pWnd = ((CMyQQClientDlg *)m_pMainDlg)->GetDesktopWindow(); 
			pDlgChat->Create(IDD_DIALOG_CHAT,pWnd); 
			 
			int nFrom = 0; 
			int nPos = 0; 
			CString strChat; 
			CString strName; 
			while(nFrom < pInfo->strChatMessage.GetLength()) 
			{ 
				nPos = pInfo->strChatMessage.Find("\r\n",nFrom); 
				if (nPos < 0) break; 
				strName = pInfo->strChatMessage.Mid(nFrom,nPos - nFrom - 5); 
				nFrom = nPos + 6; 
				nPos = pInfo->strChatMessage.Find("\r\n",nFrom); 
				if (nPos < 0) break; 
				strChat = pInfo->strChatMessage.Mid(nFrom,nPos - nFrom); 
				nFrom = nPos + 2; 
				pDlgChat->DisplayMessage(strName,strChat); 
			} 
			m_ChatDlgs.Add((CObject *)pDlgChat); 
			delete pInfo; 
			m_ChatInfo.RemoveAt(i); 
			pDlgChat->ShowWindow(SW_SHOW); 
			return; 
		} 
	} 
	pDlgChat = new CChatDialog(); 
 
	CWnd *pWnd = ((CMyQQClientDlg *)m_pMainDlg)->GetDesktopWindow(); 
	pDlgChat->Create(IDD_DIALOG_CHAT,pWnd); 
	 
	pDlgChat->m_pClientSocket = this; 
	pDlgChat->m_ClientName = UserName; 
	pDlgChat->m_UserName = m_UserName; 
 
	m_ChatDlgs.Add((CObject *)pDlgChat); 
 
	pDlgChat->ShowWindow(SW_SHOW);	 
} 
 
void CMainSocket::SendMessage(CString UserName, CString strMessage) 
{ 
 
	Message msObj; 
	msObj.iType = USERSESSION; 
	msObj.iSubType = SAYINPRIVATE; 
	CString strTemp = UserName; 
	int iLen = strTemp.GetLength(); 
	iLen > 20 ? 20 : iLen; 
	lstrcpy(msObj.strClientName,strTemp.GetBuffer(iLen)); 
 
	strTemp = m_UserName; 
	iLen = strTemp.GetLength(); 
	iLen > 20 ? 20 : iLen; 
	lstrcpy(msObj.strName,strTemp.GetBuffer(iLen)); 
 
	strTemp = strMessage; 
	iLen = strTemp.GetLength(); 
	iLen > 1024 ? 1024 : iLen; 
	lstrcpy(msObj.strContent,strTemp.GetBuffer(iLen)); 
	 
	msObj.iImage = m_image; 
	Send(&msObj,sizeof(msObj)); 
 
} 
 
void CMainSocket::ReceiveMessage(CString UserName, CString strMessage) 
{ 
	CChatDialog *pDlgChat; 
	for (int i=0;im_UserName == UserName) 
		{ 
			pDlgChat->DisplayMessage(UserName,strMessage); 
			pDlgChat->FlashWindow(TRUE); 
			return; 
		} 
	} 
	CHATDLGINFO *pInfo; 
	for (i=0;istrUserName == UserName) 
		{ 
			pInfo->strChatMessage += UserName + " ˵£º\r\n" +"    " + strMessage + "\r\n"; 
			return; 
		} 
	} 
	pInfo = new CHATDLGINFO; 
	pInfo->strUserName = UserName; 
 
	pInfo->strChatMessage = UserName + " ˵£º\r\n" +"    " + strMessage + "\r\n"; 
	m_ChatInfo.Add((CObject *)pInfo); 
} 
/* 
void CMainSocket::CloseChatDlg(CChatDialog *ChatDlg) 
{ 
	CChatDialog *pChatDlg; 
	int nSize = m_ChatDlgs.GetSize(); 
	for (int i=0;im_ClientName == strName) 
		{ 
			ChatDlg->DisplayMessage(strName,strMessage); 
			return; 
			break; 
			 
		} 
	} 
	CChatAction * ActionDlg; 
	ActionDlg = new CChatAction(); 
	ActionDlg->m_strClientName = strClientName; 
	ActionDlg->m_strName = strName; 
	ActionDlg->m_strConnect = strMessage; 
	ActionDlg->m_pMainSocket = this; 
	ActionDlg->Create(IDD_CHAT_ACTION); 
	ActionDlg->ShowWindow(SW_SHOW); 
 
}