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

📄 chatclientview.cpp

📁 基于winsock的聊天程序
💻 CPP
字号:
// ChatClientView.cpp : implementation of the CChatClientView class
//

#include "stdafx.h"
#include "ChatClient.h"

#include "ChatClientDoc.h"
#include "CntrItem.h"
#include "ChatClientView.h"
#include "Login.h"
#include "mainFrm.h"

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

/////////////////////////////////////////////////////////////////////////////
// CChatClientView

IMPLEMENT_DYNCREATE(CChatClientView, CRichEditView)

BEGIN_MESSAGE_MAP(CChatClientView, CRichEditView)
	//{{AFX_MSG_MAP(CChatClientView)
	ON_WM_DESTROY()
	ON_COMMAND(ID_CONNECT, OnConnect)
	ON_COMMAND(ID_CLOSE, OnClose)
	ON_BN_CLICKED(IDC_FILTER, OnFilter)
	ON_BN_CLICKED(IDC_COLOR, OnColor)
	ON_BN_CLICKED(IDC_SEND, OnSend)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChatClientView construction/destruction

CChatClientView::CChatClientView()
{
	// TODO: add construction code here
	m_bConnect=FALSE;
	m_bClose=FALSE;
	
}

CChatClientView::~CChatClientView()
{
}

BOOL CChatClientView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
	return CRichEditView::PreCreateWindow(cs);
}

void CChatClientView::OnInitialUpdate()
{
	CRichEditView::OnInitialUpdate();


	cfm.cbSize=sizeof(cfm);
	cfm.bCharSet=GB2312_CHARSET;//对RichEditView初始文字格式
	cfm.crTextColor=RGB(0,0,0);
	cfm.dwMask=CFM_CHARSET | CFM_COLOR ;
	cfm.dwEffects=0;
	GetRichEditCtrl().SetDefaultCharFormat(cfm);
	// Set the printing margins (720 twips = 1/2 inch).
	SetMargins(CRect(720, 720, 720, 720));
}

void CChatClientView::OnDestroy()
{
	// Deactivate the item on destruction; this is important
	// when a splitter view is being used.
   CRichEditView::OnDestroy();
   COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
   if (pActiveItem != NULL && pActiveItem->GetActiveView() == this)
   {
      pActiveItem->Deactivate();
      ASSERT(GetDocument()->GetInPlaceActiveItem(this) == NULL);
   }
}


/////////////////////////////////////////////////////////////////////////////
// CChatClientView diagnostics

#ifdef _DEBUG
void CChatClientView::AssertValid() const
{
	CRichEditView::AssertValid();
}

void CChatClientView::Dump(CDumpContext& dc) const
{
	CRichEditView::Dump(dc);
}

CChatClientDoc* CChatClientView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CChatClientDoc)));
	return (CChatClientDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CChatClientView message handlers

void CChatClientView::OnConnect() 
{
	// TODO: Add your command handler code here
	CMainFrame* pFrame = (CMainFrame*)GetParent();
	(CMenu*)pFrame->GetMenu()->EnableMenuItem(ID_CONNECT,MF_GRAYED);
	CComboBox* pTo = (CComboBox*)pFrame->m_wndSend.GetDlgItem(IDC_TALKTO);
	pTo->SetWindowText("所有人");
	CLogin dlg;//取得服务器IP
	if(dlg.DoModal() != IDOK){
		return;
	}
	//连接服务器
	GetDocument()->ConnectSocket(dlg.m_strUsername, dlg.m_strAddress, dlg.m_Photo, dlg.m_nPort);
		UpdateData(TRUE);
	
}

void CChatClientView::OnClose() 
{
	// TODO: Add your command handler code here
	CMainFrame* pFrame = (CMainFrame*)GetParent();
	(CMenu*)pFrame->GetMenu()->EnableMenuItem(ID_CONNECT,MF_DISABLED);
	GetDocument()->Disconnect();//断开服务器
}


void CChatClientView::Message(LPCTSTR lpszMessage, COLORREF clr)
{
	//显示文字
	cfm.cbSize=sizeof(cfm);
	cfm.crTextColor=clr;
	cfm.dwMask=CFM_COLOR;
	CString strTemp = lpszMessage;
	
	int len = GetWindowTextLength();
	GetRichEditCtrl().SetSel(len,len);
	
	GetRichEditCtrl().SetSelectionCharFormat(cfm);
	GetRichEditCtrl().ReplaceSel(strTemp);
}
void CChatClientView::OnFilter() 
{
	// TODO: Add your control notification handler code here
	//设置或取消信息过滤
	CMainFrame* pFrame = (CMainFrame*)GetParent();
	CButton* pFilter = (CButton*)pFrame->m_wndSend.GetDlgItem(IDC_FILTER);

	GetDocument()->m_bFilter = 	pFilter->GetCheck();
}

void CChatClientView::OnColor() 
{
	// TODO: Add your control notification handler code here
	//设置字体颜色
	CColorDialog clr(GetDocument()->msg.m_color, 0, NULL);
	if(clr.DoModal()==IDOK)
	{
		if(clr.GetColor() == RGB(255,255,255))
			AfxMessageBox("把字体设为白色就看不见了啊!");
		else
			GetDocument()->msg.m_color = clr.GetColor();
	}	
}

void CChatClientView::OnSend() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	//取得用户将要发送的信息
	CMainFrame* pFrame = (CMainFrame*)GetParent();
	CComboBox* pTo = (CComboBox*)pFrame->m_wndSend.GetDlgItem(IDC_TALKTO);
	CComboBox* pType = (CComboBox*)pFrame->m_wndSend.GetDlgItem(IDC_FACE);
	CWnd* pText = pFrame->m_wndSend.GetDlgItem(IDC_TEXT);
	CButton* pSecret = (CButton*)pFrame->m_wndSend.GetDlgItem(IDC_SECRET);
	
	CMsg* msg = &(GetDocument()->msg);

	CString tmp;
	pTo->GetWindowText(tmp);
	if(tmp.IsEmpty())
	{
		AfxMessageBox("请选择聊天对象!");
		return;
	}

	msg->m_strTo = tmp;

	CString str;
	pType->GetWindowText(str);
	msg->m_nType = pType->FindString(0, str);

	tmp.Empty();
	pText->GetWindowText(tmp);
	msg->m_strText = tmp;
	if(!tmp.Compare(""))
		return;
	msg->m_bSecret = pSecret->GetCheck();

	pText->SetWindowText(_TEXT(""));
	GetDocument()->SendMsg();//发送信息
}

int CChatClientView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CRichEditView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	return 0;
}


⌨️ 快捷键说明

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