chatview.cpp

来自「一个简易的聊天室程序」· C++ 代码 · 共 179 行

CPP
179
字号
// ChatView.cpp : implementation file
//


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

//#include "ChatClientDoc.h"

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

/////////////////////////////////////////////////////////////////////////////
// CChatView

IMPLEMENT_DYNCREATE(CChatView, CFormView)

CChatView::CChatView()
	: CFormView(CChatView::IDD)
{
	//{{AFX_DATA_INIT(CChatView)
		// NOTE: the ClassWizard will add member initialization here
	bViewCreated = false;
	//}}AFX_DATA_INIT
}

CChatView::~CChatView()
{
}

void CChatView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CChatView)
//	DDX_Control(pDX, IDC_SEND_EDITBOX, m_SendEditBox);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CChatView, CFormView)
	//{{AFX_MSG_MAP(CChatView)
	ON_WM_SIZE()
	ON_WM_DESTROY()
 //	ON_EN_KILLFOCUS(IDC_SEND_EDITBOX, OnKillfocusSendEditbox)
    ON_CONTROL_RANGE(EN_KILLFOCUS, ID_CHAT_EDITBOX, ID_SEND_EDITBOX, OnEnKillFocus)

	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChatView diagnostics

#ifdef _DEBUG
void CChatView::AssertValid() const
{
	CFormView::AssertValid();
}

void CChatView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

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

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CChatView message handlers

void CChatView::OnSize(UINT nType, int cx, int cy) 
{
	CFormView::OnSize(nType, cx, cy);
	
	// TODO: Add your message handler code here

	if(bViewCreated)
	{
		m_ChatEditBox.MoveWindow(-1,-1 , cx+2 , cy - 50);
		m_SendEditBox.MoveWindow(0, cy - 50, cx + 2, 51);
	}
}

void CChatView::OnInitialUpdate() 
{
	CFormView::OnInitialUpdate();
	
	// TODO: Add your specialized code here and/or call the base class

	m_ChatEditBox.Create(WS_VISIBLE | 
		                 WS_CHILD | 
						 WS_BORDER | 
						 WS_VSCROLL |
						 ES_MULTILINE |
						 ES_AUTOVSCROLL |
						 ES_READONLY, 
						 CRect(0,0,0,0), 
						 this , 
						 ID_CHAT_EDITBOX);


	m_SendEditBox.Create(
		WS_BORDER|
		WS_CHILD|
		WS_VISIBLE|
		WS_VSCROLL |
		ES_MULTILINE|
		ES_AUTOVSCROLL,
		CRect(0,0,0,0), 
		this, 
		ID_SEND_EDITBOX);

	bViewCreated = true;

}

void CChatView::Message(LPCTSTR lpszMessage)
{
	CString strTemp = lpszMessage;
	strTemp += _T("\r\n");
	int len = GetWindowTextLength();
	m_ChatEditBox.SetSel(len,len);
	m_ChatEditBox.ReplaceSel(strTemp);

}

void CChatView::Serialize(CArchive& ar) 
{

}

void CChatView::OnDestroy() 
{
	CFormView::OnDestroy();
	
	// TODO: Add your message handler code here
	
	if(m_ChatEditBox.m_hWnd != NULL)
		m_ChatEditBox.DestroyWindow();

	if(m_SendEditBox.m_hWnd != NULL)
		m_SendEditBox.DestroyWindow();
}


void CChatView::OnKillfocusSendEditbox() 
{
	// TODO: Add your control notification handler code here


}

void CChatView::OnEnKillFocus(UINT nID)
{
//   RepaintIfEdit(GetDlgItem(nID));
	if(nID == ID_SEND_EDITBOX)
	{
		CChatClientDoc* pDoc = GetDocument();
		ASSERT_VALID(pDoc);

		CString strText;
		m_SendEditBox.GetWindowText(strText);

		pDoc->SendMsg(strText);

		strText=_T("");
		m_SendEditBox.SetWindowText(strText);
		m_SendEditBox.SetSel(9,9,FALSE);
	}
}

⌨️ 快捷键说明

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