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

📄 chatdoc.cpp

📁 包含两个程序
💻 CPP
字号:
// chatdoc.cpp : implementation of the CChatDoc class
//
// This is a part of the Microsoft Foundation Classes C++ library.
// Copyright (C) 1992-1997 Microsoft Corporation
// All rights reserved.
//
// This source code is only intended as a supplement to the
// Microsoft Foundation Classes Reference and related
// electronic documentation provided with the library.
// See these sources for detailed information regarding the
// Microsoft Foundation Classes product.

#include "stdafx.h"
#include "chatter.h"

#include "chatsock.h"
#include "chatdoc.h"
#include "chatvw.h"
#include "sendvw.h"

#include "setupdlg.h"

#ifdef _WIN32
#ifndef _UNICODE
#include <strstrea.h>
#endif
#endif

#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CChatDoc

IMPLEMENT_DYNCREATE(CChatDoc, CDocument)

BEGIN_MESSAGE_MAP(CChatDoc, CDocument)
	//{{AFX_MSG_MAP(CChatDoc)
	ON_UPDATE_COMMAND_UI(ID_CONNECTIONS, OnUpdateConnections)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChatDoc construction/destruction

CChatDoc::CChatDoc()
{
	m_bAutoChat = FALSE;
	m_pSocket = NULL;
	m_pFile = NULL;
	m_pArchiveIn = NULL;
	m_pArchiveOut = NULL;
	clr=RGB(0,136,255);
	m_guolu=false;
	allclose=false;
	refuse=false;
	
}

CChatDoc::~CChatDoc()
{
}

BOOL CChatDoc::OnNewDocument()
{
allowshow=false;
	if (!CDocument::OnNewDocument())
		return FALSE;

#ifdef _WIN32
#ifndef _UNICODE
	if (AfxGetApp()->m_lpCmdLine[0] != '\0')
	{
		TCHAR strHandle[128];
		TCHAR strServer[128];
		int nChannel;

		istrstream(AfxGetApp()->m_lpCmdLine) >> strHandle >> strServer >> nChannel;
		return ConnectSocket(strHandle, strServer, nChannel);
	}
	else
#endif
#endif
	{
		CSetupDlg Dialog;

		Dialog.m_strHandle=m_strHandle;
		//Dialog.m_strServer=_T("");
     
		while(TRUE)
		{
			if (IDOK != Dialog.DoModal())
			{ SetTitle(m_strHandle);
				return FALSE;
			}	
			Dialog.m_strHandle.TrimLeft();
			Dialog.m_strHandle.TrimRight();

			if(Dialog.m_strHandle=="")
				{
				AfxMessageBox("名称不能为空!");
				return FALSE;
				}


	if (ConnectSocket(Dialog.m_strHandle, Dialog.m_strServer, 0))
			{ SetTitle(m_strHandle);
		return TRUE;
			}
			if (AfxMessageBox(IDS_CHANGEADDRESS,MB_YESNO) == IDNO)
			{SetTitle(m_strHandle);
				return FALSE;
			}
	
		}
	}
}

void CChatDoc::DeleteContents() 
{
	m_bAutoChat = FALSE;

	if ((m_pSocket != NULL) && (m_pFile != NULL) &&	(m_pArchiveOut != NULL))
	{
		
	

			msg.m_bClose=TRUE;
			msg.type="-2";
			msg.from=m_strHandle;
			msg.m_strText = "";
			msg.Serialize(*m_pArchiveOut);
			m_pArchiveOut->Flush();
	}	

	delete m_pArchiveOut;
	m_pArchiveOut = NULL;
	delete m_pArchiveIn;
	m_pArchiveIn = NULL;
	delete m_pFile;
	m_pFile = NULL;

	if (m_pSocket != NULL)
	{
		BYTE Buffer[50];
		m_pSocket->ShutDown();

		while(m_pSocket->Receive(Buffer,50) > 0);
	}

	delete m_pSocket;
	m_pSocket = NULL;

	for(POSITION pos=GetFirstViewPosition();pos!=NULL;)
	{
		CView* pView = GetNextView(pos);
		
		if (pView->IsKindOf(RUNTIME_CLASS(CChatView)))
		{
			CChatView* pChatView = (CChatView*)pView;
			pChatView->GetRichEditCtrl().SetWindowText(_T(""));
		}	
	}
	CDocument::DeleteContents();
}

/////////////////////////////////////////////////////////////////////////////
// CChatDoc Operations

BOOL CChatDoc::ConnectSocket(LPCTSTR lpszHandle, LPCTSTR lpszAddress, UINT nPort)
{
	m_strHandle = lpszHandle;

	m_pSocket = new CChatSocket(this);

	if (!m_pSocket->Create())
	{
		delete m_pSocket;
		m_pSocket = NULL;
		AfxMessageBox(IDS_CREATEFAILED);
		return FALSE;
	}

	while (!m_pSocket->Connect(lpszAddress, nPort + 700))
	{
		if (AfxMessageBox(IDS_RETRYCONNECT,MB_YESNO) == IDNO)
		{
			delete m_pSocket;
			m_pSocket = NULL;
			return FALSE;
		}
	}

	m_pFile = new CSocketFile(m_pSocket);
	m_pArchiveIn = new CArchive(m_pFile,CArchive::load);
	m_pArchiveOut = new CArchive(m_pFile,CArchive::store);

CTime time;
time=CTime::GetCurrentTime();
stime=time.Format("%H:%M:%S");

	msg.type="-1";
	msg.m_bClose=FALSE;
	 msg.from=m_strHandle;
	 msg.to="所有人";
	 msg.sec="FALSE";
     msg.m_strText =stime ;
	 msg.clr="0,136,255"; 
	 SendMsg();

	return TRUE;
}

void CChatDoc::ProcessPendingRead() 
{
	do
	{
		ReceiveMsg();
		if (m_pSocket == NULL)
			return;
	}
	while(!m_pArchiveIn->IsBufferEmpty());
}

void CChatDoc::SendMsg()
{
	if (m_pArchiveOut != NULL)
	{
		
	//	msg.m_strText = m_strHandle + _T(": ") + strText;
     
		TRY
		{
			msg.Serialize(*m_pArchiveOut);
			m_pArchiveOut->Flush();
		}
		CATCH(CFileException, e)
		{
			m_bAutoChat = FALSE;
			m_pArchiveOut->Abort();
			delete m_pArchiveOut;
			m_pArchiveOut = NULL;
		
			CString strTemp;
			if (strTemp.LoadString(IDS_SERVERRESET))
				DisplayMsg(strTemp);
		}
		END_CATCH
	}
}

void CChatDoc::ReceiveMsg()
{
	
	TRY
	{
		msg.Serialize(*m_pArchiveIn);
	
		while(!msg.m_msgList.IsEmpty())
		{
			CString str1 = msg.m_msgList.RemoveHead();
			CString type1 =msg.typelist.RemoveHead();
			CString from1 =msg.fromlist.RemoveHead();

			CString to1=msg.tolist.RemoveHead();
			CString  sec1=msg.seclist.RemoveHead();
			CString  clr1=msg.clrlist.RemoveHead();  
			DisplayRecMsg(type1,from1,to1,sec1,str1,clr1);
		if(type1=="-1" || type1=="-2" || type1=="-7")
				  comset1(type1,from1);

		}


	}
	CATCH(CFileException, e)
	{
		m_bAutoChat = FALSE;
		msg.m_bClose = TRUE;
		m_pArchiveOut->Abort();

		CString strTemp;
		if (strTemp.LoadString(IDS_SERVERRESET))
			DisplayMsg(strTemp);
		if (strTemp.LoadString(IDS_CONNECTIONCLOSED))
			DisplayMsg(strTemp);
	}
	END_CATCH

		if(msg.m_bClose && refuse==true)
		{
             	delete m_pArchiveIn;
		m_pArchiveIn = NULL;
		delete m_pArchiveOut;
		m_pArchiveOut = NULL;
		delete m_pFile;
		m_pFile = NULL;
		delete m_pSocket;
		m_pSocket = NULL;
		return;
		}

	if (msg.m_bClose && allclose)
	{
		
		DisplayMsg("服务器已经关掉!");

		delete m_pArchiveIn;
		m_pArchiveIn = NULL;
		delete m_pArchiveOut;
		m_pArchiveOut = NULL;
		delete m_pFile;
		m_pFile = NULL;
		delete m_pSocket;
		m_pSocket = NULL;
		return;
	}
	if (msg.m_bClose && !allclose)
	{
		allowshow=true;
		DisplayMsg("已经有人使用这个名称,请换一个名称重新登录");
		allowshow=false;
        SetModifiedFlag(FALSE);
		delete m_pArchiveIn;
		m_pArchiveIn = NULL;
		delete m_pArchiveOut;
		m_pArchiveOut = NULL;
		delete m_pFile;
		m_pFile = NULL;
		delete m_pSocket;
		m_pSocket = NULL;
		return;
	}

}

void CChatDoc::DisplayMsg(LPCTSTR lpszText)
{

	for(POSITION pos=GetFirstViewPosition();pos!=NULL;)
	{
		CView* pView = GetNextView(pos);
		CChatView* pChatView = DYNAMIC_DOWNCAST(CChatView, pView);
		
		if (pChatView != NULL)
			pChatView->Message(lpszText,RGB(0,0,0));
	}
}

/////////////////////////////////////////////////////////////////////////////
// CChatDoc serialization

void CChatDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		for(POSITION pos=GetFirstViewPosition();pos!=NULL;)
		{
			CView* pView = GetNextView(pos);
			CChatView* pChatView = DYNAMIC_DOWNCAST(CChatView, pView);
		
			if (pChatView != NULL)
			{
				CRichEditCtrl& rich=pChatView->GetRichEditCtrl();
				CString str;
				rich.GetWindowText(str);
				ar.Write((LPVOID)(LPCTSTR)str,str.GetLength()*sizeof(TCHAR));
				//pChatView->SerializeRaw(ar);
			}		
		}
	}
}

/////////////////////////////////////////////////////////////////////////////
// CChatDoc diagnostics

#ifdef _DEBUG
void CChatDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CChatDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CChatDoc commands




void CChatDoc::DisplayRecMsg(CString type1,CString from1,CString to1,CString sec1,CString str1,CString clr1 )
{
	for(POSITION pos=GetFirstViewPosition();pos!=NULL;)
	{
		CView* pView = GetNextView(pos);
		CChatView* pChatView = DYNAMIC_DOWNCAST(CChatView, pView);
		
		if (pChatView != NULL)
		{
			
			if(type1=="-7")
				{
	pChatView->Message("系统:聊天室掌门人以一招'佛山无影脚',将",RGB(0,0,0));
	if(from1==m_strHandle)
	{
    refuse=true;
	pChatView->Message("你",RGB(0,0,255));
    }
	else
	pChatView->Message(from1,RGB(0,0,255));
   	pChatView->Message("踢出门外\r\n",RGB(0,0,0));
					
						
				}   
			if(type1=="-5")
				{
				  	 allclose=false;
					return;	
				}
			if(type1=="-3")
				 allclose=true;
			if(type1=="-1" )
			{	if(str1==stime) 
				{
				allowshow=true;
				SetModifiedFlag(TRUE);
				}
				pChatView->Message(from1,RGB(0,0,255));
					pChatView->Message("风尘仆仆地推门而入\r\n",RGB(255,0,0));
			}
			if(type1=="-2")
			{
					pChatView->Message(from1,RGB(0,0,255));
					pChatView->Message("静静地离开了,孤单的背影显得格外潇洒\r\n",RGB(255,0,0));
			}
			int type2=atoi(type1);
			if(type1!="-3" && type2>=0)
			{
    if(from1==m_strHandle || to1==m_strHandle || sec1=="FALSE" || to1 =="所有人")
              talk(type2,from1,to1,str1,clr1);			
			}	
					
		}
	}
}



void CChatDoc::comset1(CString type1,CString from1)
{
for(POSITION pos=GetFirstViewPosition();pos!=NULL;)
	{
	
		CView* pView = GetNextView(pos);
		CSendView* pSendView = DYNAMIC_DOWNCAST(CSendView, pView);
		
		if (pSendView != NULL)
		{
			if(type1=="-1")
			{
				if(from1!=m_strHandle)
	pSendView->m_list.AddString(from1);
			}
				else
		   {
	int i=pSendView->m_list.FindStringExact (-1,from1);
		   if(i!=CB_ERR)
			   pSendView->m_list.DeleteString(i);
		   }
		   
			pSendView->m_list.SelectString(-1,pSendView->to);
			

		}
				
		}
		
}

void CChatDoc::talk(int type2, CString from1, CString to1, CString str1,CString clr1)
{	
	if(from1!=m_strHandle && to1!=m_strHandle && m_guolu)
       return;
	for(POSITION pos=GetFirstViewPosition();pos!=NULL;)
	{
		CView* pView = GetNextView(pos);
		CChatView* pChatView = DYNAMIC_DOWNCAST(CChatView, pView);
		
	if (pChatView != NULL)
	{

	   CString temp,to2,first,second,clrr,clrg,clrb;
	  
		int j1=clr1.Find(",");
		int j2=clr1.ReverseFind(','); 
          clrr=clr1.Left(j1);
		   clrg=clr1.Mid(j1+1,j2-j1-1);
		  clrb=clr1.Mid(j2+1);
		  COLORREF tclr=RGB(atoi(clrr),atoi(clrg),atoi(clrb));
           if(type2>32 || type2 <0) return;  
			temp.LoadString(IDS_TALK0+type2);	
					int i=temp.Find(",");
					if(i!=-1)
                    {
                        first=temp.Left(i);
                       if(i!=temp.GetLength()-1)
                       {
                        second=temp.Mid(i+1);
                        second+=":";
                       }
                       else
                       {
                           second=":";
                       }
	                pChatView->Message((LPCTSTR)from1,RGB(0,0,255));
					pChatView->Message((LPCTSTR)first,RGB(0,0,0));
					pChatView->Message((LPCTSTR)to1,RGB(0,0,255));
                    pChatView->Message((LPCTSTR)second,RGB(0,0,0));
                    pChatView->Message((LPCTSTR)str1,tclr);
                     pChatView->Message((LPCTSTR)"\r\n",tclr);
                    }
                    else
                    {
                        first=temp;
                        second=": ";
                   pChatView->Message(from1,RGB(0,0,255));
					pChatView->Message(first,RGB(0,0,0));
                    pChatView->Message(second,RGB(0,0,0));
                    pChatView->Message(str1,tclr);
                   pChatView->Message("\r\n",tclr);

                    }
    }
    }
    
 }
					
void CChatDoc::OnUpdateConnections(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable(TRUE);
for(POSITION pos=GetFirstViewPosition();pos!=NULL;)
	{
	
		CView* pView = GetNextView(pos);
		CSendView* pSendView = DYNAMIC_DOWNCAST(CSendView, pView);
		
		if (pSendView != NULL)
		{
		
	CString strFmt;
	if (strFmt.LoadString(IDS_CONNECTIONSFMT))
			{
		CString strTemp;
	//	wsprintf(strTemp.GetBuffer(50),strFmt,m_connectionList.GetCount());
	wsprintf(strTemp.GetBuffer(50),strFmt,pSendView->m_list.GetCount());
		strTemp.ReleaseBuffer();
		pCmdUI->SetText(strTemp);
			}
		}
	}
}				
					
					

⌨️ 快捷键说明

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