?? lxdrawdoc.cpp
字號:
// lxdrawDoc.cpp : implementation of the CLxdrawDoc class
//
#include "stdafx.h"
#include "lxdraw.h"
#include "lxdrawDoc.h"
#include "LxSocket.h"
#include "LxLine.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CLxdrawDoc
IMPLEMENT_DYNCREATE(CLxdrawDoc, CDocument)
BEGIN_MESSAGE_MAP(CLxdrawDoc, CDocument)
//{{AFX_MSG_MAP(CLxdrawDoc)
ON_COMMAND(ID_BCLOSE, OnBclose)
ON_COMMAND(ID_CONN, OnConn)
ON_COMMAND(ID_LISTEN, OnListen)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CLxdrawDoc construction/destruction
CLxdrawDoc::CLxdrawDoc()
{
// TODO: add one-time construction code here
}
CLxdrawDoc::~CLxdrawDoc()
{
}
BOOL CLxdrawDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// TODO: add reinitialization code here
//開始添加
m_strName="127.0.0.1";//監(jiān)聽方的IP地址,"127.0.0.1"是本機的意思,可以改為實際的IP地址。
m_iPort=500;//監(jiān)聽方的端口號
m_sConnectSocket.SetParent(this);
m_sListenSocket.SetParent(this);
//結束添加
// (SDI documents will reuse this document)
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CLxdrawDoc serialization
void CLxdrawDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
}
/////////////////////////////////////////////////////////////////////////////
// CLxdrawDoc diagnostics
#ifdef _DEBUG
void CLxdrawDoc::AssertValid() const
{
CDocument::AssertValid();
}
void CLxdrawDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CLxdrawDoc commands
void CLxdrawDoc::OnBclose()
{
// TODO: Add your command handler code here
OnClose();//調(diào)用OnClose函數(shù)關閉連接
}
void CLxdrawDoc::OnConn()
{
// TODO: Add your command handler code here
m_sConnectSocket.Create();//初始化連接的Socket,
m_sConnectSocket.Connect(m_strName,m_iPort);//連接對方
}
void CLxdrawDoc::OnListen()
{
// TODO: Add your command handler code here
m_sListenSocket.Create(m_iPort);//創(chuàng)建監(jiān)聽端口
m_sListenSocket.Listen();//開始監(jiān)聽
}
void CLxdrawDoc::OnClose()
{
m_sConnectSocket.Close();//關閉與對方的連接
AfxMessageBox("連接己被關閉!");
}
void CLxdrawDoc::OnAccept()
{
m_sListenSocket.Accept(m_sConnectSocket);//接受對方的連接請求
}
void CLxdrawDoc::OnSend()
{
MessageBox(NULL,"連接成功!","消息",MB_OK);//連接成功后自動調(diào)用此函數(shù)
}
CLxLine* CLxdrawDoc::AddLine(CPoint ptFrom, CPoint ptTo)
{
CLxLine *pLine=new CLxLine(ptFrom,ptTo);//創(chuàng)建新的線條對像
try
{
m_oaLines.Add(pLine);//將線條加到數(shù)組中
}
catch(CMemoryException* perr)//內(nèi)存不足的處理
{
AfxMessageBox("Out of memory",MB_ICONSTOP|MB_OK);
if(pLine)
{
delete pLine;
pLine=NULL;
}
perr->Delete();
}
return pLine;
}
int CLxdrawDoc::GetLineCount()
{
return m_oaLines.GetSize();//返回線條數(shù)量
}
CLxLine* CLxdrawDoc::GetLine(int nIndex)
{
return (CLxLine*) m_oaLines[nIndex];//返回一條特定的線條
}
void CLxdrawDoc::OnReceive()
{
char *pBuf=new char[1025];//用于存放接收到的信息的字符串,也可稱為緩沖區(qū)
int iBufSize=1024;//緩沖區(qū)最大能夠接收的字節(jié)數(shù)
int iRcvd; //接收到的字節(jié)數(shù)
CLxLine* pLine=new CLxLine; //新建線條類指針
iRcvd=m_sConnectSocket.Receive(pBuf,iBufSize);//調(diào)用Receive函數(shù)接收數(shù)據(jù)
if(iRcvd==SOCKET_ERROR)
{
AfxMessageBox("接收失敗");
}
else
{
pBuf[iRcvd]=NULL;
memcpy((char*)pLine,pBuf,sizeof(CLxLine)); //把接收到的字符串強制轉(zhuǎn)換為線條類
m_oaLines.Add(pLine); //在記錄中加入此線條
POSITION pos=this->GetFirstViewPosition();//獲得視圖的位置
CView* pView=this->GetNextView(pos); //獲得視圖的指針
CDC *hDC=pView->GetDC();//獲得視圖的設備環(huán)境
pLine->Draw(hDC); //在視圖上繪制本線條
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -