Code/Resource
Windows Develop
Linux-Unix program
Internet-Socket-Network
Web Server
Browser Client
Ftp Server
Ftp Client
Browser Plugins
Proxy Server
Email Server
Email Client
WEB Mail
Firewall-Security
Telnet Server
Telnet Client
ICQ-IM-Chat
Search Engine
Sniffer Package capture
Remote Control
xml-soap-webservice
P2P
WEB(ASP,PHP,...)
TCP/IP Stack
SNMP
Grid Computing
SilverLight
DNS
Cluster Service
Network Security
Communication-Mobile
Game Program
Editor
Multimedia program
Graph program
Compiler program
Compress-Decompress algrithms
Crypt_Decrypt algrithms
Mathimatics-Numerical algorithms
MultiLanguage
Disk/Storage
Java Develop
assembly language
Applications
Other systems
Database system
Embeded-SCM Develop
FlashMX/Flex
source in ebook
Delphi VCL
OS Develop
MiddleWare
MPI
MacOS develop
LabView
ELanguage
Software/Tools
E-Books
Artical/Document
MyRichEditCtrl.cpp
Package: ASMcordEditCtrl.rar [view]
Upload User: qzjqfs
Upload Date: 2008-02-20
Package Size: 5k
Code Size: 11k
Category:
RichEdit
Development Platform:
Visual C++
- // MyRichEditCtrl.cpp : implementation file
- //
- #include "stdafx.h"
- #include "THComputer.h"
- #include "MyRichEditCtrl.h"
- #include "TranASMDlg.h"
- #include "MainFrm.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CMyRichEditCtrl
- CMyRichEditCtrl::CMyRichEditCtrl()
- {
- m_nVpos = 0;
- m_bEVpos = false;
- m_bAVpos = false;
- }
- CMyRichEditCtrl::~CMyRichEditCtrl()
- {
- }
- BEGIN_MESSAGE_MAP(CMyRichEditCtrl, CRichEditCtrl)
- //{{AFX_MSG_MAP(CMyRichEditCtrl)
- ON_WM_RBUTTONDOWN()
- ON_WM_PAINT()
- ON_WM_KEYUP()
- ON_WM_VSCROLL()
- ON_WM_MOUSEMOVE()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CMyRichEditCtrl message handlers
- BOOL CMyRichEditCtrl::PreTranslateMessage(MSG* pMsg)
- {
- pMessage = pMsg;
- if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_RETURN)
- {
- SetReturnBlank();
- return TRUE;
- /* if (pMsg->message == WM_VSCROLL && pMsg->wParam == )
- {
- }*/
- /* if (pMsg->wParam != VK_LEFT && pMsg->wParam != VK_RIGHT && pMsg->wParam != VK_UP &&pMsg->wParam != VK_DOWN)
- {
- HandleFont();
- }
- */
- }
- return CRichEditCtrl::PreTranslateMessage(pMsg);
- }
- void CMyRichEditCtrl::HandleFont()
- {
- CString strCur = GetCursorString();
- int nResult = SearchStr(strCur);
- int len = strCur.GetLength();
- //正确选中字符串;针对光标移动至字符串中间情况
- long nStartChar,nEndChar,start,end;
- GetSel(nStartChar,nEndChar);
- start = nStartChar;
- end = nEndChar;
- long nlast;
- SetSel(-1,-1);
- GetSel(nlast,nlast);
- SetSel(nStartChar,nEndChar);
- nStartChar = nEndChar-len;
- CString strTemp;
- SetSel(nStartChar,nEndChar);
- strTemp = GetSelText();
- while (strTemp != strCur && nEndChar-1 < nlast)
- {
- nStartChar++;
- nEndChar++;
- SetSel(nStartChar,nEndChar);
- strTemp = GetSelText();
- }
- SetCurColor(nResult,nStartChar,nEndChar);
- SetSel(start,end);
- }
- CString CMyRichEditCtrl::GetCursorString()
- {
- long start, end;
- GetSel(start, end);
- long startline = LineFromChar(start);
- long endlie = LineFromChar(end);
- if(startline == endlie && start != end) //用户选定了文本
- {
- return GetSelText();
- }
- else
- {
- int nlnstart, nlnend;
- long line = LineFromChar(end);
- int ntmp = end-LineIndex(line); //计算行内偏移
- CString strline;
- GetLineString(line, strline);
- int i=0, noffset=0;
- while(noffset!=ntmp) //得到光标所在行的字符串中的位置(字符串中中文为两个字符)
- {
- if(IsDBCSLeadByte(strline[i])) ++i;
- ++i; ++noffset;
- }
- ntmp = i;
- while(ntmp>0)
- {
- --ntmp;
- if(!isalnum(strline[ntmp]) && strline[ntmp]!='_')
- {
- if(ntmp>0)
- {
- if(IsDBCSLeadByte(strline[ntmp-1])) //检查是不是中文字符
- {
- --ntmp;
- }
- else
- break;
- }
- }
- }
- if(!(isalnum(strline[ntmp]) || IsDBCSLeadByte(strline[ntmp])) || strline[ntmp]=='_')
- nlnstart = ntmp+1;
- else
- nlnstart = ntmp;
- ntmp = i; //计算行内偏移
- int nstrlen = strline.GetLength();
- while(ntmp<nstrlen-1)
- {
- if(IsDBCSLeadByte(strline[ntmp])) //检查是不是中文字符
- ++ntmp;
- else
- {
- if(!isalnum(strline[ntmp]) && strline[ntmp]!='_')
- break;
- }
- ++ntmp;
- }
- nlnend = ntmp;
- CString str = strline.Mid(nlnstart, nlnend-nlnstart);
- return str;
- }
- }
- BOOL CMyRichEditCtrl::GetLineString(int nLine, CString &strLine)
- {
- strLine = _T("");
- int m_nLineCount = GetLineCount();
- if(nLine > m_nLineCount - 1 || nLine < 0)
- return TRUE;
- int nTemp = LineIndex(nLine);
- nTemp = LineLength(nTemp)*16+32; //缓冲区必须足够大,否则读取中文时可能会有乱码
- TCHAR *achLine = new TCHAR[nTemp];
- int nLen = GetLine(nLine, achLine, nTemp);
- //检查实际读出的字符数,一个中文应换算成两个字符
- int i = 0;
- nTemp = 0;
- while(nTemp != nLen)
- {
- if(IsDBCSLeadByte(achLine[i++]))
- i++;
- ++nTemp;
- }
- /////////////////////////////////////////////////
- BOOL bRealReturn;
- achLine[i] = 0;
- //检查是不是真的换行
- if(0xd == achLine[i-1])
- {
- achLine[--i] = 0;
- bRealReturn=TRUE;
- }
- else
- {
- achLine[i] = 0;
- bRealReturn=FALSE;
- }
- ////////////////////
- //尾部加一个空格,以送给语法解析器
- lstrcat(achLine, _T(" "));
- strLine = achLine;
- if(achLine)
- delete achLine;
- return bRealReturn;
- }
- int CMyRichEditCtrl::SearchStr(CString strCur)
- {
- CMainFrame *pMain = (CMainFrame *)FromHandle(m_hMainMyRichEdit);
- strCur.MakeUpper();
- //黑色的情况,不认识的
- int lenc = strCur.GetLength();
- if (lenc == 0)
- return 0;
- if (strCur == "MM" || strCur == "#II")
- return 0;
- //红色的情况,数字和‘H’
- bool flag = true;
- for (int j=0;j<lenc;j++)
- {
- int num = (int)strCur.GetAt(j);
- if (num>47 && num<58)
- ;
- else
- flag = false;
- }
- if (flag)
- {
- return 2;
- }
- CString strTemp;
- int num = atoi(strCur);
- strTemp.Format("%d",num);
- if (strCur == strTemp)
- return 2;
- //CString strTemp;
- if (strCur.GetAt(lenc-1) == 'H')//16进制
- {
- if (pMain->SixteenToTen(strCur.Left(lenc-1)) == pMain->SixteenToTen(strTemp))
- return 2;
- if (pMain->TenToSixteen(pMain->SixteenToTen(strCur.Left(lenc-1))) = strCur.Left(lenc-1))
- {
- return 2;
- }
- }
- if (strCur.GetAt(lenc-1) == 'B')//2进制
- {
- if (pMain->HexToBin(pMain->BinToHex(strCur.Left(lenc-1))) = strCur.Left(lenc-1))
- {
- flag = true;
- for (int j=0;j<lenc-1;j++)
- {
- int num = (int)strCur.GetAt(j);
- if (num>47 && num<50)
- ;
- else
- flag = false;
- }
- if (flag)
- {
- return 2;
- }
- }
- }
- //
- if (strCur == "R0" || strCur == "R1" || strCur == "R2" || strCur == "R3")
- return 1;
- for (int i=0;i<64;i++)
- {
- CString strZjfL,strZjfM,strZjfR;
- strZjfL = pMain->StuSeg[i].ZJfL;
- strZjfM = pMain->StuSeg[i].ZJfM;
- strZjfR = pMain->StuSeg[i].ZJfR;
- if (strCur == strZjfL || strCur == strZjfM || strCur == strZjfR)
- return 1;
- }
- return 0;
- }
- void CMyRichEditCtrl::SetCurColor(int n,int nStart, int nEnd)//指定区域,设置字体颜色
- {
- CHARFORMAT cf;
- ZeroMemory(&cf, sizeof(CHARFORMAT));
- cf.cbSize = sizeof(CHARFORMAT);
- cf.dwMask = CFM_COLOR;
- if (n == 0)
- cf.crTextColor = RGB(0,0,0);
- if (n == 1)
- cf.crTextColor = RGB(0,0,255);
- if (n == 2)
- cf.crTextColor = RGB(255,0,0);
- SetSel(nStart,nEnd); //设置处理区域
- SetSelectionCharFormat(cf);
- SetSel(nEnd,nEnd);
- cf.crTextColor = RGB(0,0,0);
- SetSelectionCharFormat(cf);
- }
- void CMyRichEditCtrl::SetReturnBlank()
- {
- long nstart,nEnd;
- GetSel(nstart,nEnd);
- int nIndex = LineFromChar(nstart);
- int len = LineLength(nIndex-1);
- char *buf = new char[len+1];
- GetLine(nIndex,buf);
- int num=0;
- while (buf[num] == ' ')
- {
- num++;
- }
- buf[num] = '';
- ReplaceSel("rn",true);
- if (num != len)
- {
- ReplaceSel(buf,true);
- }
- }
- BOOL CMyRichEditCtrl::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
- {
- return CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
- }
- void CMyRichEditCtrl::SetRangeFont(int nstart, int nend)
- {
- if (nstart>nend)
- return;
- for (int i=0;i<nend-nstart+1;i++)
- {
- SetSel(nstart+i,nstart+i);
- HandleFont();
- }
- }
- //确定是不是一行
- BOOL CMyRichEditCtrl::ConfirmLine(int nstart, int nend)
- {
- if (nstart>nend)
- return false;
- for (int i=0;i<nend-nstart+1;i++)
- {
- SetSel(nstart+i,nstart+i);
- CString str = GetCursorString();
- if (!str.IsEmpty())
- return true;
- }
- return false;
- }
- void CMyRichEditCtrl::OnRButtonDown(UINT nFlags, CPoint point)
- {
- CRichEditCtrl::OnRButtonDown(nFlags, point);
- }
- void CMyRichEditCtrl::OnPaint()
- {
- CRichEditCtrl::OnPaint();
- CBrush brushb(RGB(192,192,192));
- CDC* hdc;
- hdc = GetWindowDC();
- CRect rect;
- GetClientRect(&rect);
- hdc->FillRect(CRect(rect.left+5 ,rect.top+5 ,rect.left + 25,rect.Height ()+5),&brushb);//画底色
- brushb.DeleteObject();
- ReleaseDC(hdc);
- CTranASMDlg *pDlg = (CTranASMDlg *)FromHandle(m_hDlgTranASM);
- int nLine = pDlg->m_index;
- COLORREF col = pDlg->m_color;
- pDlg->DrawEllipse();//断点
- pDlg->DrawArrow(nLine,col);//箭头
- }
- void CMyRichEditCtrl::PreSubclassWindow()
- {
- CRichEditCtrl::PreSubclassWindow();
- SetRect(CRect(21,0,8000,8000));
- LimitText(20000);
- m_flag = false;
- // CRect rc(21,0,80,80);
- // SetMargins(&rc);
- }
- void CMyRichEditCtrl::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
- {
- CRichEditCtrl::OnKeyUp(nChar, nRepCnt, nFlags);
- CMainFrame *pMain = (CMainFrame *)FromHandle(m_hMainMyRichEdit);
- pMain->PviewTraFrom->m_DlgTranASMToSegDlg.m_CListBoxToSeg.SetFocus();
- HandleFont();
- pMain->PviewTraFrom->m_DlgTranASMDlg.m_CRichEditASM.SetFocus();
- SendMessage(WM_PAINT,0,0);
- m_flag = false;
- }
- void CMyRichEditCtrl::EnsureVisible(int nRow)
- {
- CRect rt;
- GetRect(&rt);
- int nLine = GetLineCount(); //行数
- int nHeight = 10; //行高
- int nVisalLine = rt.Height()/nHeight/2; //可视行数
- int nUp = GetFirstVisibleLine(); //最上面行
- int nDown = nUp + rt.Height()/nHeight/2 - 1;//最下面行
- int nMid = (nUp + nDown)/2; //中间行
- if (nRow == nUp)
- {
- SendMessage(WM_VSCROLL,SB_LINEUP,0);
- }
- if (nDown-nRow<2 && nDown == nLine-2) //当前位置到低端小于2行
- {
- SendMessage(WM_VSCROLL, SB_LINEDOWN, 0);
- }
- if (nRow < nUp) //在最上
- {
- while (nRow < nMid && nUp > 0)
- {
- SendMessage(WM_VSCROLL, SB_LINEUP, 0);
- nUp = GetFirstVisibleLine();
- nDown = nUp + rt.Height()/nHeight/2 - 1;
- nMid = (nUp + nDown)/2;
- }
- }
- if (nRow > nDown && nRow < nLine) //在最下
- {
- while (nRow > nMid && nDown < nLine-2)
- {
- SendMessage(WM_VSCROLL, SB_LINEDOWN, 0);
- nUp = GetFirstVisibleLine();
- nDown = nUp + rt.Height()/nHeight/2 - 1;
- nMid = (nUp + nDown)/2;
- }
- }
- while (nDown-nRow<2 && nDown<nLine-2) //往下滚
- {
- SendMessage(WM_VSCROLL, SB_LINEDOWN, 0);
- nUp = GetFirstVisibleLine();
- nDown = nUp + rt.Height()/nHeight/2 - 1;
- //nMid = (nUp + nDown)/2;
- }
- }
- /*
- #define SB_LINEUP 0
- #define SB_LINELEFT 0
- #define SB_LINEDOWN 1
- #define SB_LINERIGHT 1
- #define SB_PAGEUP 2
- #define SB_PAGELEFT 2
- #define SB_PAGEDOWN 3
- #define SB_PAGERIGHT 3
- #define SB_THUMBPOSITION 4
- #define SB_THUMBTRACK 5
- #define SB_TOP 6
- #define SB_LEFT 6
- #define SB_BOTTOM 7
- #define SB_RIGHT 7
- #define SB_ENDSCROLL 8
- */
- void CMyRichEditCtrl::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
- {
- if(nSBCode == SB_THUMBTRACK )
- {
- m_nVpos = nPos;
- m_bEVpos = true;
- m_bAVpos = true;
- }
- CRichEditCtrl::OnVScroll(nSBCode, nPos, pScrollBar);
- m_flag = true;
- }
- void CMyRichEditCtrl::OnMouseMove(UINT nFlags, CPoint point)
- {
- CRichEditCtrl::OnMouseMove(nFlags, point);
- }
- void CMyRichEditCtrl::DrawLineArrow(int index, COLORREF color)
- {
- /* if (index <0)
- return;
- UINT hScroll = GetScrollPos(SB_VERT);
- //高度24,44,64,84……
- int height = index*20;
- CDC *pDC;
- pDC = GetWindowDC();
- CBrush brushb(color);
- pDC->FillRect(CRect(12,12+height-hScroll,22,16+height-hScroll),&brushb);
- brushb.DeleteObject();
- CPen cpen,*pPen;
- cpen.CreatePen(PS_SOLID,3,color);
- pPen=pDC->SelectObject(&cpen);
- pDC->MoveTo(22,14+height-hScroll);
- pDC->LineTo(18,10+height-hScroll);
- pDC->MoveTo(22,14+height-hScroll);
- pDC->LineTo(18,18+height-hScroll);
- cpen.DeleteObject();*/
- }