?? e05doc.cpp
字號:
// E05Doc.cpp : implementation of the CE05Doc class
//
#include "stdafx.h"
#include "E05.h"
#include "SetupDlg.h"
#include "E05Doc.h"
#include "E05View.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CE05Doc
IMPLEMENT_DYNCREATE(CE05Doc, CDocument)
BEGIN_MESSAGE_MAP(CE05Doc, CDocument)
//{{AFX_MSG_MAP(CE05Doc)
ON_COMMAND(ID_FILE_CONNECT, OnFileConnect)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CE05Doc construction/destruction
CE05Doc::CE05Doc()
{
// TODO: add one-time construction code here
m_socket=NULL;
}
CE05Doc::~CE05Doc()
{
}
BOOL CE05Doc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
((CEditView*)m_viewList.GetHead())->SetWindowText(NULL);
// TODO: add reinitialization code here
// (SDI documents will reuse this document)
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CE05Doc serialization
void CE05Doc::Serialize(CArchive& ar)
{
// CEditView contains an edit control which handles all serialization
((CEditView*)m_viewList.GetHead())->SerializeRaw(ar);
}
/////////////////////////////////////////////////////////////////////////////
// CE05Doc diagnostics
#ifdef _DEBUG
void CE05Doc::AssertValid() const
{
CDocument::AssertValid();
}
void CE05Doc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CE05Doc commands
void CE05Doc::OnFileConnect()
{
// TODO: Add your command handler code here
CSetupDlg dlg;
if (dlg.DoModal()!=IDOK) return;
if (m_socket!=NULL)
{
m_socket->Close();
delete m_socket;
}
m_socket=new CChatSocket(this);
if (!m_socket->Create())
{
AfxMessageBox("創建Socket失敗!");
delete m_socket;
m_socket=NULL;
return;
}
if (!m_socket->Connect(dlg.m_address,dlg.m_port+BASE_CHANNEL))
{
AfxMessageBox("連接服務器失敗!");
delete m_socket;
m_socket=NULL;
return;
}
AfxMessageBox("成功連接到聊天服務器!");
}
void CE05Doc::ReceiveMessage()
{
ASSERT(m_socket);
char buffer[MAX_BUFFER_SIZE];
int len=m_socket->Receive(buffer,MAX_BUFFER_SIZE-1);
if (len<1)
{
AfxMessageBox("接收信息異常!");
return;
}
buffer[len]=0;
ShowMessage(buffer);
}
void CE05Doc::ShowMessage(LPSTR msg)
{
POSITION pos=GetFirstViewPosition();
while (pos!=NULL)
{
CEditView* view=(CEditView*)GetNextView(pos);
if (view->IsKindOf(RUNTIME_CLASS(CE05View))) continue;
CEdit& edit=view->GetEditCtrl();
char buffer[MAX_BUFFER_SIZE];
wsprintf(buffer,"%s\r\n",msg);
int len=edit.GetWindowTextLength();
edit.SetSel(len,len);
edit.ReplaceSel(buffer);
}
}
void CE05Doc::SendMessage(LPSTR msg)
{
if (m_socket==NULL) return;
if (!m_socket->Send(msg,strlen(msg)))
{
AfxMessageBox("發送消息失敗!");
return;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -