- // DrawDoc.cpp : implementation of the CDrawDoc class
- //
- #include "stdafx.h"
- #include "Draw.h"
- #include "DrawDoc.h"
- #include "MySocket.h"
- #include "Line1.h"//p122
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CDrawDoc
- IMPLEMENT_DYNCREATE(CDrawDoc, CDocument)
- BEGIN_MESSAGE_MAP(CDrawDoc, CDocument)
- //{{AFX_MSG_MAP(CDrawDoc)
- ON_COMMAND(ID_LISTEN, OnListen)
- ON_COMMAND(ID_CONN, OnConn)
- ON_COMMAND(ID_BCLOSE, OnBclose)
- ON_COMMAND(ID_COLOR_BLACK, OnColorBlack)
- ON_COMMAND(ID_COLOR_BLUE, OnColorBlue)
- ON_COMMAND(ID_COLOR_GREEN, OnColorGreen)
- ON_COMMAND(ID_COLOR_RED, OnColorRed)
- ON_UPDATE_COMMAND_UI(ID_COLOR_BLACK, OnUpdateColorBlack)
- ON_UPDATE_COMMAND_UI(ID_COLOR_BLUE, OnUpdateColorBlue)
- ON_UPDATE_COMMAND_UI(ID_COLOR_GREEN, OnUpdateColorGreen)
- ON_UPDATE_COMMAND_UI(ID_COLOR_RED, OnUpdateColorRed)
- ON_COMMAND(ID_WIDTH_1, OnWidth1)
- ON_UPDATE_COMMAND_UI(ID_WIDTH_1, OnUpdateWidth1)
- ON_COMMAND(ID_WIDTH_2, OnWidth2)
- ON_UPDATE_COMMAND_UI(ID_WIDTH_2, OnUpdateWidth2)
- ON_COMMAND(ID_WIDTH_3, OnWidth3)
- ON_UPDATE_COMMAND_UI(ID_WIDTH_3, OnUpdateWidth3)
- ON_COMMAND(ID_WIDTH_4, OnWidth4)
- ON_UPDATE_COMMAND_UI(ID_WIDTH_4, OnUpdateWidth4)
- ON_COMMAND(ID_WIDTH_5, OnWidth5)
- ON_UPDATE_COMMAND_UI(ID_WIDTH_5, OnUpdateWidth5)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- //126
- //设置画笔颜色
- const COLORREF CDrawDoc::m_crColors[4]={
- RGB(0,0,0),
- RGB(255,0,0),
- RGB(0,255,0),
- RGB(0,0,255)
- };
- //设置画笔宽度
- const int CDrawDoc::m_crWidths[5]={
- 1,
- 2,
- 3,
- 4,
- 5
- };
- /////////////////////////////////////////////////////////////////////////////
- // CDrawDoc construction/destruction
- CDrawDoc::CDrawDoc()
- {
- // TODO: add one-time construction code here
- m_nColor=0;
- m_nWidth=0;
- BRED=BBLUE=BGREEN=FALSE;
- BBLACK=TRUE;
- }
- CDrawDoc::~CDrawDoc()
- {
- }
- //p116
- BOOL CDrawDoc::OnNewDocument()
- {
- if (!CDocument::OnNewDocument())
- return FALSE;
- // TODO: add reinitialization code here
- m_nColor = ID_COLOR_BLACK;
- //宽度
- m_nWidth = 1;
- // (SDI documents will reuse this document)
- m_strName = "127.0.0.1";
- m_iPort = 500;//监听对方的端口号
- m_sConnectSocket.SetParent(this);
- m_sListenSocket.SetParent(this);
- return TRUE;
- }
- /////////////////////////////////////////////////////////////////////////////
- // CDrawDoc serialization
- void CDrawDoc::Serialize(CArchive& ar)//产生一个可存储的值的表示
- {m_oaLines.Serialize(ar);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CDrawDoc diagnostics
- #ifdef _DEBUG
- void CDrawDoc::AssertValid() const
- {
- CDocument::AssertValid();
- }
- void CDrawDoc::Dump(CDumpContext& dc) const
- {
- CDocument::Dump(dc);
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CDrawDoc commands
- //p117
- void CDrawDoc::OnListen()
- {
- // TODO: Add your command handler code here
- m_sConnectSocket.Create();//初始化连接的SOCKET
- m_sConnectSocket.Connect(m_strName,m_iPort);//连接对方
- }
- void CDrawDoc::OnConn()
- {
- // TODO: Add your command handler code here
- m_sListenSocket.Create(m_iPort);//创建监听端口
- m_sListenSocket.Listen();//开始监听
- }
- void CDrawDoc::OnBclose()
- {
- // TODO: Add your command handler code here
- OnClose();//调用OnClose函数关闭连接
- }
- //p118/*
- void CDrawDoc::OnClose()
- {
- m_sConnectSocket.Close();//关闭与对方的连接
- AfxMessageBox("连接已经被关闭!");
- }
- void CDrawDoc::OnAccept()
- {
- m_sListenSocket.Accept(m_sConnectSocket);//接受对方的连接请求
- }
- void CDrawDoc::OnSend()
- {
- MessageBox(NULL,"连接成功","消息",MB_OK);
- //连接成功后自动调用此函数
- }
- //p120
- CLine* CDrawDoc::AddLine(CPoint ptFrom, CPoint ptTo)
- { if(BBLACK)
- m_nColor=0;
- if(BRED)
- m_nColor=1;
- if(BGREEN)
- m_nColor=2;
- if(BBLUE)
- m_nColor=3;
- CLine* pLine = new CLine(ptFrom,ptTo,m_crColors[m_nColor],m_crWidths[m_nWidth]);//创建新的线条对象
- try
- {
- m_oaLines.Add(pLine);//将线条添加到数组中
- SetModifiedFlag();
- }
- catch(CMemoryException*perr)//内存不足的处理
- {
- AfxMessageBox("Out of memory",MB_ICONSTOP|MB_OK);
- if(pLine)
- {
- delete pLine;
- pLine = NULL;
- }
- perr->Delete();
- }
- return pLine;
- }
- //
- //p120
- int CDrawDoc::GetLineCoumt()
- {
- return m_oaLines.GetSize();//返回线条数量
- }
- //
- //p120
- CLine* CDrawDoc::GetLine(int nIndex)
- {
- return(CLine*) m_oaLines[nIndex];//返回一条特定的线条
- }
- //p123
- void CDrawDoc::OnReceive()
- {
- char *pBuf = new char[1025];//用于存放接收到的信息的字符缓冲区
- int iBufSize = 1024;//缓冲区最大能接受的字节数
- int iRcvd; //接收到的字节数
- CLine *pLine = new CLine;//新建线条类指针
- iRcvd = m_sConnectSocket.Receive(pBuf,iBufSize);//调用receive函数接收数据
- if(iRcvd == SOCKET_ERROR)
- {
- AfxMessageBox("接收失败");
- }
- else
- {
- pBuf[iRcvd] = NULL;
- memcpy((char*)pLine,pBuf,sizeof(CLine));//把接收到的字符串强制转换为线条类
- m_oaLines.Add(pLine); //在记录中加入此线条
- POSITION pos = this->GetFirstViewPosition();//获得视图的位置
- CView* pView = this->GetNextView(pos); //获得视图的指针
- CDC* hDC = pView->GetDC(); //获得视图的设备环境
- pLine->Draw(hDC); //在视图上绘制基本线条
- }
- }
- UINT CDrawDoc::GetColor()
- {
- return m_nColor;
- }
- //127
- void CDrawDoc::OnColorBlack()
- {
- // TODO: Add your command handler code here
- m_nColor = ID_COLOR_BLACK - ID_COLOR_BLACK;
- m_nColor=0;
- BRED=BBLUE=BGREEN=FALSE;
- BBLACK=TRUE;
- }
- void CDrawDoc::OnColorBlue()
- {
- // TODO: Add your command handler code here
- BRED=BBLACK=BGREEN=FALSE;
- BBLUE=TRUE;
- m_nColor = 3;
- // UpdateData(FALSE);
- }
- void CDrawDoc::OnColorGreen()
- {
- // TODO: Add your command handler code here
- BRED=BBLUE=BBLACK=FALSE;
- BGREEN=TRUE;
- m_nColor = 2;
- }
- void CDrawDoc::OnColorRed()
- {
- // TODO: Add your command handler code here
- BBLACK=BBLUE=BGREEN=FALSE;
- BRED=TRUE;
- m_nColor = 1;
- }
- void CDrawDoc::OnUpdateColorBlack(CCmdUI* pCmdUI)
- {
- // TODO: Add your command update UI handler code here
- pCmdUI->SetCheck(GetColor() == ID_COLOR_BLACK?1:0);
- }
- void CDrawDoc::OnUpdateColorBlue(CCmdUI* pCmdUI)
- {
- // TODO: Add your command update UI handler code here
- pCmdUI->SetCheck(GetColor() == ID_COLOR_BLUE?1:0);
- }
- void CDrawDoc::OnUpdateColorGreen(CCmdUI* pCmdUI)
- {
- // TODO: Add your command update UI handler code here
- pCmdUI->SetCheck(GetColor() == ID_COLOR_GREEN?1:0);
- }
- void CDrawDoc::OnUpdateColorRed(CCmdUI* pCmdUI)
- {
- // TODO: Add your command update UI handler code here
- pCmdUI->SetCheck(GetColor() == ID_COLOR_RED?1:0);
- }
- void CDrawDoc::DeleteContents()
- {
- // TODO: Add your specialized code here and/or call the base class
- int liCount=m_oaLines.GetSize();
- int liPos;
- if(liCount)
- {
- for(liPos=0;liPos<LICOUNT;LIPOS++) pCmdUI- here code handler UI update command your Add TODO: { pCmdUI) CDrawDoc::OnUpdateWidth1(CCmdUI* void } m_nWidth="1;//最细" CDrawDoc::OnWidth1() m_nWidth; return CDrawDoc::GetWidth() int CDocument::DeleteContents(); m_oaLines.RemoveAll(); m_oaLines[liPos]; delete>SetCheck(GetWidth()==1?1:0);
- }
- void CDrawDoc::OnWidth2()
- {
- // TODO: Add your command handler code here
- m_nWidth = 2;//较细
- }
- void CDrawDoc::OnUpdateWidth2(CCmdUI* pCmdUI)
- {
- // TODO: Add your command update UI handler code here
- pCmdUI->SetCheck(GetWidth()==2?1:0);
- }
- void CDrawDoc::OnWidth3()
- {
- // TODO: Add your command handler code here
- m_nWidth = 3;//中
- }
- void CDrawDoc::OnUpdateWidth3(CCmdUI* pCmdUI)
- {
- // TODO: Add your command update UI handler code here
- pCmdUI->SetCheck(GetWidth()==3?1:0);
- }
- void CDrawDoc::OnWidth4()
- {
- // TODO: Add your command handler code here
- m_nWidth = 4;//较粗
- }
- void CDrawDoc::OnUpdateWidth4(CCmdUI* pCmdUI)
- {
- // TODO: Add your command update UI handler code here
- pCmdUI->SetCheck(GetWidth()==4?1:0);
- }
- void CDrawDoc::OnWidth5()
- {
- // TODO: Add your command handler code here
- m_nWidth = 5;//最粗
- MessageBox(NULL,"737373773","消息",MB_OK);
- }
- void CDrawDoc::OnUpdateWidth5(CCmdUI* pCmdUI)
- {
- // TODO: Add your command update UI handler code here
- pCmdUI->SetCheck(GetWidth()==5?1:0);
- }