VC COLOR

  1. // DrawDoc.cpp : implementation of the CDrawDoc class    
  2. //    
  3.    
  4. #include "stdafx.h"    
  5. #include "Draw.h"    
  6.    
  7. #include "DrawDoc.h"    
  8. #include "MySocket.h"    
  9. #include "Line1.h"//p122    
  10.    
  11. #ifdef _DEBUG    
  12. #define new DEBUG_NEW    
  13. #undef THIS_FILE    
  14. static char THIS_FILE[] = __FILE__;   
  15. #endif    
  16.    
  17.   
  18. // CDrawDoc    
  19.    
  20. IMPLEMENT_DYNCREATE(CDrawDoc, CDocument)   
  21.    
  22. BEGIN_MESSAGE_MAP(CDrawDoc, CDocument)   
  23.     //{{AFX_MSG_MAP(CDrawDoc)    
  24.     ON_COMMAND(ID_LISTEN, OnListen)   
  25.     ON_COMMAND(ID_CONN, OnConn)   
  26.     ON_COMMAND(ID_BCLOSE, OnBclose)   
  27.     ON_COMMAND(ID_COLOR_BLACK, OnColorBlack)   
  28.     ON_COMMAND(ID_COLOR_BLUE, OnColorBlue)   
  29.     ON_COMMAND(ID_COLOR_GREEN, OnColorGreen)   
  30.     ON_COMMAND(ID_COLOR_RED, OnColorRed)   
  31.     ON_UPDATE_COMMAND_UI(ID_COLOR_BLACK, OnUpdateColorBlack)   
  32.     ON_UPDATE_COMMAND_UI(ID_COLOR_BLUE, OnUpdateColorBlue)   
  33.     ON_UPDATE_COMMAND_UI(ID_COLOR_GREEN, OnUpdateColorGreen)   
  34.     ON_UPDATE_COMMAND_UI(ID_COLOR_RED, OnUpdateColorRed)   
  35.     ON_COMMAND(ID_WIDTH_1, OnWidth1)   
  36.     ON_UPDATE_COMMAND_UI(ID_WIDTH_1, OnUpdateWidth1)   
  37.     ON_COMMAND(ID_WIDTH_2, OnWidth2)   
  38.     ON_UPDATE_COMMAND_UI(ID_WIDTH_2, OnUpdateWidth2)   
  39.     ON_COMMAND(ID_WIDTH_3, OnWidth3)   
  40.     ON_UPDATE_COMMAND_UI(ID_WIDTH_3, OnUpdateWidth3)   
  41.     ON_COMMAND(ID_WIDTH_4, OnWidth4)   
  42.     ON_UPDATE_COMMAND_UI(ID_WIDTH_4, OnUpdateWidth4)   
  43.     ON_COMMAND(ID_WIDTH_5, OnWidth5)   
  44.     ON_UPDATE_COMMAND_UI(ID_WIDTH_5, OnUpdateWidth5)   
  45.     //}}AFX_MSG_MAP    
  46. END_MESSAGE_MAP()   
  47.    
  48. //126    
  49. //设置画笔颜色    
  50. const COLORREF CDrawDoc::m_crColors[4]={   
  51.     RGB(0,0,0),   
  52.     RGB(255,0,0),   
  53.     RGB(0,255,0),   
  54.     RGB(0,0,255)   
  55. };   
  56. //设置画笔宽度    
  57. const int CDrawDoc::m_crWidths[5]={   
  58.     1,   
  59.     2,   
  60.     3,   
  61.     4,   
  62.     5   
  63. };   
  64.   
  65. // CDrawDoc construction/destruction    
  66.    
  67. CDrawDoc::CDrawDoc()   
  68. {   
  69.     // TODO: add one-time construction code here    
  70.     m_nColor=0;   
  71.     m_nWidth=0;   
  72.     BRED=BBLUE=BGREEN=FALSE;   
  73.     BBLACK=TRUE;   
  74.    
  75. }   
  76.    
  77. CDrawDoc::~CDrawDoc()   
  78. {   
  79. }   
  80. //p116    
  81. BOOL CDrawDoc::OnNewDocument()   
  82. {   
  83.     if (!CDocument::OnNewDocument())   
  84.         return FALSE;   
  85.    
  86.     // TODO: add reinitialization code here    
  87.     m_nColor = ID_COLOR_BLACK;   
  88.     //宽度    
  89.     m_nWidth = 1;   
  90.     // (SDI documents will reuse this document)    
  91.     m_strName = "127.0.0.1";   
  92.     m_iPort = 500;//监听对方的端口号    
  93.     m_sConnectSocket.SetParent(this);   
  94.     m_sListenSocket.SetParent(this);   
  95.    
  96.     return TRUE;   
  97. }   
  98.    
  99.    
  100.    
  101.   
  102. // CDrawDoc serialization    
  103.    
  104. void CDrawDoc::Serialize(CArchive& ar)//产生一个可存储的值的表示     
  105. {m_oaLines.Serialize(ar);   
  106. }   
  107.    
  108.   
  109. // CDrawDoc diagnostics    
  110.    
  111. #ifdef _DEBUG    
  112. void CDrawDoc::AssertValid() const   
  113. {   
  114.     CDocument::AssertValid();   
  115. }   
  116.    
  117. void CDrawDoc::Dump(CDumpContext& dc) const   
  118. {   
  119.     CDocument::Dump(dc);   
  120. }   
  121. #endif //_DEBUG    
  122.    
  123.   
  124. // CDrawDoc commands    
  125. //p117    
  126. void CDrawDoc::OnListen()    
  127. {   
  128.     // TODO: Add your command handler code here    
  129.     m_sConnectSocket.Create();//初始化连接的SOCKET    
  130.     m_sConnectSocket.Connect(m_strName,m_iPort);//连接对方    
  131.        
  132. }   
  133.    
  134. void CDrawDoc::OnConn()    
  135. {   
  136.     // TODO: Add your command handler code here    
  137.     m_sListenSocket.Create(m_iPort);//创建监听端口    
  138.     m_sListenSocket.Listen();//开始监听    
  139.    
  140.        
  141. }   
  142.    
  143. void CDrawDoc::OnBclose()    
  144. {   
  145.     // TODO: Add your command handler code here    
  146.     OnClose();//调用OnClose函数关闭连接    
  147.        
  148. }   
  149. //p118/*    
  150. void CDrawDoc::OnClose()   
  151. {   
  152.     m_sConnectSocket.Close();//关闭与对方的连接    
  153.     AfxMessageBox("连接已经被关闭!");   
  154.    
  155. }   
  156.    
  157. void CDrawDoc::OnAccept()   
  158. {   
  159.     m_sListenSocket.Accept(m_sConnectSocket);//接受对方的连接请求    
  160.    
  161. }   
  162.    
  163. void CDrawDoc::OnSend()   
  164. {   
  165.     MessageBox(NULL,"连接成功","消息",MB_OK);   
  166.     //连接成功后自动调用此函数    
  167.    
  168. }   
  169. //p120    
  170. CLine* CDrawDoc::AddLine(CPoint ptFrom, CPoint ptTo)   
  171. {   if(BBLACK)   
  172.     m_nColor=0;   
  173.     if(BRED)   
  174.     m_nColor=1;   
  175.     if(BGREEN)   
  176.     m_nColor=2;   
  177.     if(BBLUE)   
  178.     m_nColor=3;   
  179.     CLine* pLine = new CLine(ptFrom,ptTo,m_crColors[m_nColor],m_crWidths[m_nWidth]);//创建新的线条对象    
  180.     try   
  181.     {   
  182.        
  183.         m_oaLines.Add(pLine);//将线条添加到数组中    
  184.         SetModifiedFlag();   
  185.     }      
  186.     catch(CMemoryException*perr)//内存不足的处理    
  187.     {   
  188.         AfxMessageBox("Out of memory",MB_ICONSTOP|MB_OK);   
  189.         if(pLine)   
  190.         {   
  191.             delete pLine;   
  192.             pLine = NULL;   
  193.         }   
  194.         perr->Delete();   
  195.     }   
  196.     return pLine;   
  197. }   
  198. //    
  199. //p120    
  200.    
  201. int CDrawDoc::GetLineCoumt()   
  202. {   
  203.     return m_oaLines.GetSize();//返回线条数量    
  204. }   
  205. //    
  206. //p120    
  207. CLine* CDrawDoc::GetLine(int nIndex)   
  208. {   
  209.     return(CLine*) m_oaLines[nIndex];//返回一条特定的线条    
  210. }   
  211.    
  212. //p123    
  213. void CDrawDoc::OnReceive()   
  214. {   
  215.     char *pBuf = new char[1025];//用于存放接收到的信息的字符缓冲区    
  216.     int iBufSize = 1024;//缓冲区最大能接受的字节数    
  217.     int iRcvd;          //接收到的字节数    
  218.     CLine *pLine = new CLine;//新建线条类指针    
  219.    
  220.     iRcvd = m_sConnectSocket.Receive(pBuf,iBufSize);//调用receive函数接收数据    
  221.    
  222.     if(iRcvd == SOCKET_ERROR)   
  223.     {   
  224.         AfxMessageBox("接收失败");   
  225.     }   
  226.     else   
  227.     {   
  228.         pBuf[iRcvd] = NULL;   
  229.         memcpy((char*)pLine,pBuf,sizeof(CLine));//把接收到的字符串强制转换为线条类    
  230.         m_oaLines.Add(pLine);                   //在记录中加入此线条    
  231.         POSITION pos = this->GetFirstViewPosition();//获得视图的位置    
  232.         CView* pView = this->GetNextView(pos);   //获得视图的指针    
  233.         CDC* hDC = pView->GetDC();               //获得视图的设备环境    
  234.         pLine->Draw(hDC);                        //在视图上绘制基本线条    
  235.    
  236.    
  237.     }   
  238.    
  239. }   
  240.    
  241. UINT CDrawDoc::GetColor()   
  242. {   
  243.        
  244.     return  m_nColor;   
  245.            
  246. }   
  247. //127    
  248. void CDrawDoc::OnColorBlack()    
  249. {   
  250.     // TODO: Add your command handler code here    
  251.     m_nColor = ID_COLOR_BLACK - ID_COLOR_BLACK;    
  252.     m_nColor=0;   
  253.     BRED=BBLUE=BGREEN=FALSE;   
  254.     BBLACK=TRUE;   
  255.    
  256. }   
  257.    
  258. void CDrawDoc::OnColorBlue()    
  259. {   
  260.     // TODO: Add your command handler code here    
  261.     BRED=BBLACK=BGREEN=FALSE;   
  262.     BBLUE=TRUE;   
  263.     m_nColor = 3;   
  264. //  UpdateData(FALSE);    
  265. }   
  266.    
  267. void CDrawDoc::OnColorGreen()    
  268. {   
  269.     // TODO: Add your command handler code here    
  270.     BRED=BBLUE=BBLACK=FALSE;   
  271.     BGREEN=TRUE;   
  272.     m_nColor = 2;   
  273. }   
  274.    
  275. void CDrawDoc::OnColorRed()    
  276. {   
  277.     // TODO: Add your command handler code here    
  278.     BBLACK=BBLUE=BGREEN=FALSE;   
  279.     BRED=TRUE;   
  280.     m_nColor = 1;   
  281. }   
  282.    
  283. void CDrawDoc::OnUpdateColorBlack(CCmdUI* pCmdUI)    
  284. {   
  285.     // TODO: Add your command update UI handler code here    
  286.     pCmdUI->SetCheck(GetColor() == ID_COLOR_BLACK?1:0);   
  287. }   
  288.    
  289. void CDrawDoc::OnUpdateColorBlue(CCmdUI* pCmdUI)    
  290. {   
  291.     // TODO: Add your command update UI handler code here    
  292.     pCmdUI->SetCheck(GetColor() == ID_COLOR_BLUE?1:0);   
  293. }   
  294.    
  295. void CDrawDoc::OnUpdateColorGreen(CCmdUI* pCmdUI)    
  296. {   
  297.     // TODO: Add your command update UI handler code here    
  298.     pCmdUI->SetCheck(GetColor() == ID_COLOR_GREEN?1:0);   
  299. }   
  300.    
  301. void CDrawDoc::OnUpdateColorRed(CCmdUI* pCmdUI)    
  302. {   
  303.     // TODO: Add your command update UI handler code here    
  304.     pCmdUI->SetCheck(GetColor() == ID_COLOR_RED?1:0);   
  305. }   
  306.    
  307. void CDrawDoc::DeleteContents()    
  308. {   
  309.     // TODO: Add your specialized code here and/or call the base class    
  310.     int liCount=m_oaLines.GetSize();   
  311.     int liPos;   
  312.     if(liCount)   
  313.     {   
  314.         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);   
  315. }   
  316.    
  317. void CDrawDoc::OnWidth2()    
  318. {   
  319.     // TODO: Add your command handler code here    
  320.     m_nWidth = 2;//较细    
  321. }   
  322.    
  323. void CDrawDoc::OnUpdateWidth2(CCmdUI* pCmdUI)    
  324. {   
  325.     // TODO: Add your command update UI handler code here    
  326.     pCmdUI->SetCheck(GetWidth()==2?1:0);   
  327. }   
  328.    
  329. void CDrawDoc::OnWidth3()    
  330. {   
  331.     // TODO: Add your command handler code here    
  332.     m_nWidth = 3;//中    
  333. }   
  334.    
  335. void CDrawDoc::OnUpdateWidth3(CCmdUI* pCmdUI)    
  336. {   
  337.     // TODO: Add your command update UI handler code here    
  338.     pCmdUI->SetCheck(GetWidth()==3?1:0);   
  339. }   
  340.    
  341. void CDrawDoc::OnWidth4()    
  342. {   
  343.     // TODO: Add your command handler code here    
  344.     m_nWidth = 4;//较粗    
  345. }   
  346.    
  347. void CDrawDoc::OnUpdateWidth4(CCmdUI* pCmdUI)    
  348. {   
  349.     // TODO: Add your command update UI handler code here    
  350.     pCmdUI->SetCheck(GetWidth()==4?1:0);   
  351. }   
  352.    
  353. void CDrawDoc::OnWidth5()    
  354. {   
  355.     // TODO: Add your command handler code here    
  356.     m_nWidth = 5;//最粗    
  357.     MessageBox(NULL,"737373773","消息",MB_OK);   
  358.        
  359. }   
  360.    
  361. void CDrawDoc::OnUpdateWidth5(CCmdUI* pCmdUI)    
  362. {   
  363.     // TODO: Add your command update UI handler code here    
  364.     pCmdUI->SetCheck(GetWidth()==5?1:0);   
  365. }   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值