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

📄 sendview.cpp

📁 基于C/S模式
💻 CPP
字号:
// SendView.cpp : implementation file
//

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

#include "ChatClientDoc.h"//添加的代码
#include "SendView.h"

#include <stdlib.h>

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

/////////////////////////////////////////////////////////////////////////////
// CSendView

IMPLEMENT_DYNCREATE(CSendView, CEditView)

CSendView::CSendView()
{
	m_TimerID = 0;
}

CSendView::~CSendView()
{
}


BEGIN_MESSAGE_MAP(CSendView, CEditView)
	//{{AFX_MSG_MAP(CSendView)
	ON_WM_CHAR()
	ON_WM_TIMER()
	ON_COMMAND(ID_FILE_LEAVE, OnFileLeave)
	ON_UPDATE_COMMAND_UI(ID_FILE_LEAVE, OnUpdateFileLeave)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSendView drawing

void CSendView::OnDraw(CDC* pDC)
{
	CDocument* pDoc = GetDocument();
	// TODO: add draw code here
}

/////////////////////////////////////////////////////////////////////////////
// CSendView diagnostics

#ifdef _DEBUG
void CSendView::AssertValid() const
{
	CEditView::AssertValid();
}

void CSendView::Dump(CDumpContext& dc) const
{
	CEditView::Dump(dc);
}

CChatClientDoc* CSendView::GetDocument() // non-debug version is inline
{
	return STATIC_DOWNCAST(CChatClientDoc, m_pDocument);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CSendView message handlers

BOOL CSendView::DestroyWindow() 
{
	// TODO: Add your specialized code here and/or call the base class
	if (m_TimerID != 0)
		KillTimer(m_TimerID);
	
	return CEditView::DestroyWindow();
}

BOOL CSendView::PreCreateWindow(CREATESTRUCT& cs) 
{
	// TODO: Add your specialized code here and/or call the base class
		BOOL ret = CEditView::PreCreateWindow(cs);
	cs.style = AFX_WS_DEFAULT_VIEW | WS_VSCROLL | ES_AUTOHSCROLL |
		ES_AUTOVSCROLL | ES_MULTILINE | ES_NOHIDESEL;
	return ret;	
}

void CSendView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
    //当不是回车键时,通过父类的实现函数处理即可
	if ((nChar != VK_RETURN) || (nRepCnt!=1))
	{
		CEditView::OnChar(nChar, nRepCnt, nFlags);
		return;
	}
	//如果是回车键
	else
	{
		//得到文档类的指针,同时确保它是非空的
		CChatClientDoc* pDoc = GetDocument();
		ASSERT_VALID(pDoc);

		CString strText;
		//得到发送窗口里的要发送的字符串
 		GetEditCtrl().GetWindowText(strText);
        //调用SendMsg函数进行发送
		pDoc->SendMsg(strText);
        //字符串发送成功后将发送窗口的 内容清空
		strText=_T("");
		GetEditCtrl().SetWindowText(strText);
	}
	CEditView::OnChar(nChar, nRepCnt, nFlags);
}

void CSendView::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	CChatClientDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	if (pDoc->m_bAutoChat)
	{
		CString temp =TakeString();

		if (!temp.IsEmpty())
			pDoc->SendMsg(temp);
	}
	else
	{
		KillTimer(m_TimerID);
		m_TimerID = 0;
	}
	
	CEditView::OnTimer(nIDEvent);
}

void CSendView::OnFileLeave() 
{
	// TODO: Add your command handler code here
	//在这里通知文档类:
	//完成自动的从String资源里得到相应的字符串
	//然后发送出去
	CChatClientDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	if (!pDoc->m_bAutoChat)
	{
		pDoc->m_bAutoChat = TRUE;
		m_TimerID = SetTimer(1, 1000, NULL);
	}
	else
	{
		pDoc->m_bAutoChat = FALSE;
		KillTimer(m_TimerID);
	}

}

void CSendView::OnUpdateFileLeave(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	CChatClientDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	
	pCmdUI->SetCheck(pDoc->m_bAutoChat);
}

CString CSendView::TakeString()
{
	CString strResult;
	strResult.LoadString(IDS_LEAVE);
	return strResult;
}

⌨️ 快捷键说明

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