3.4.2 具体实现(8)
定义函数DispatchMessage(),用于发送数据,具体代码如下:
- //发送数据
- void CTelnetView::DispatchMessage(CString strText)
- {
- ASSERT(cSock);
- cSock->Send(strText, strText.GetLength());
- }
⑭定义函数GetLine(),用于获得一行数据,具体代码如下: - //获得一行数据
- BOOL CTelnetView::GetLine(unsigned char *bytes, int nBytes, int &ndx)
- {
- BOOL bLine = FALSE;
- while (bLine==FALSE && ndx<nBytes)
- {
- unsigned char ch = bytes[ndx];
-
- //原来设计的时候要去掉回车换行的,但是后来发现不能去掉
- switch(ch)
- {
- case '\r': //
- m_strLine += "\r"; //回车
- break;
- case '\n': //行结尾
- m_strLine += "\n";
- break;
- default: //其他数据
- m_strLine += ch;
- break;
- }
- ndx ++;
- if (ndx == nBytes)
- {
- bLine = TRUE;
- }
- }
- return bLine;
- }
⑮定义函数MessageReceived(),用于实现数据处理,具体代码如下:- //数据处理
- void CTelnetView::MessageReceived(LPCSTR pText)
- {
- BOOL bSkip = FALSE;
- int loop=0;
- CString tempStr = "0123456789;";
- CString tempStr2;
- int ColorVal;
- int tempY = 0;
-
- CDC *pDC = GetDC();
- OnPrepareDC(pDC);
- DrawCursor(pDC, FALSE);
-
- CRect clip;
- pDC->GetClipBox(clip);
-
- CMainFrame *frm = (CMainFrame*)GetTopLevelFrame();
- //设置颜色
- pDC->SetTextColor(cTextColor);
- pDC->SetBkColor(cBackgroundColor);
-
- pDC->SelectObject(GetStockObject(ANSI_FIXED_FONT));
- int length = strlen(pText);
- char text[2] = {0x00, 0x00};
- while(loop < length)
- {
- switch(pText[loop])
- {
-
- case 8: //删除
- CurrentXX--;
- if(CurrentXX < 0) CurrentXX = 0;
- loop++;
- break;
-
- case 9: //Tab键
- CurrentXX++; //
- loop++;
- break;
-
- case 13: //换行CR
- m_strline.Empty();
- CurrentXX = 0;
- loop++;
- break;
-
- case 27:
- loop++;
- //分析紧接着27的字符是否是91,如果不是91,则这两个字符都不作处理,直接跳出
- if (pText[loop]!=91)
- {
- loop++;
- break;
- }
- //如果是91,则接下来的数据则是系统相关数据
- else
- {
- loop++;
- while (tempStr.Find(pText[loop]) != -1)
- {
- tempStr2 += pText[loop];
- loop++;
- }
- if (pText[loop]=='m') //如果接下来的数据是m,则分析前面获得的字符串
- {
- //循环获得字符串中的值,其中字符串中的值都是以分号隔开的
- while (tempStr2 != "")
- {
- if (tempStr2.Find(";") != -1)
- {
- ColorVal = atoi(tempStr2.Mid(0,tempStr2.Find(";")));
- tempStr2tempStr2 = tempStr2.Mid(tempStr2.Find(";") + 1);