GShiEdit.cpp
资源名称:MyStock.rar [点击查看]
上传用户:wenjimin
上传日期:2014-08-12
资源大小:111k
文件大小:13k
源码类别:
金融证券系统
开发平台:
Visual C++
- // GShiEdit.cpp : implementation file
- //
- #include "stdafx.h"
- #include "MyStock.h"
- #include "GShiEdit.h"
- #include "InstFunDlg.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CGShiEdit dialog
- CGShiEdit::CGShiEdit(CWnd* pParent /*=NULL*/)
- : CDialog(CGShiEdit::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CGShiEdit)
- m_passw = FALSE;
- //}}AFX_DATA_INIT
- }
- void CGShiEdit::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CGShiEdit)
- DDX_Control(pDX, IDC_EDIT2, m_edit2);
- DDX_Control(pDX, IDC_EDIT1, m_edit1);
- DDX_Control(pDX, IDC_RICHEDIT1, m_RichEdit);
- DDX_Check(pDX, IDC_CHECK1, m_passw);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CGShiEdit, CDialog)
- //{{AFX_MSG_MAP(CGShiEdit)
- ON_WM_SIZE()
- ON_WM_CREATE()
- ON_EN_CHANGE(IDC_RICHEDIT1, OnChangeRichedit)
- ON_BN_CLICKED(IDC_BUTTON7, OnButton7)
- ON_BN_CLICKED(IDC_CHECK1, OnCheck1)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CGShiEdit message handlers
- static CString/*LPCTSTR*/ szKeywords = " Program Const Var Procedure Function "//程序、函数
- "DATE DAY HOUR MINUTE MONTH TIME WEEKDAY YEAR "//时间
- "ABS BETWEEN CROSS LONGCROSS MAX MIN MOD NOT RANGE REVERSE SGN "//算术函数
- "BACKSET BARSCOUNT BARSLAST BARSSINCE COUNT DMA EMA FILTER HHV HHVBARS LLV LLVBARS MA REF SMA SUM SUMBARS "//引用函数
- "IF ISDOWN ISEQUAL ISUP "//逻辑函数
- "ACOS ASIN ATAN CEILING COS EXP FLOOP INTPART LN LOG POW SIN SQRT TAN "//数学函数
- "AVEDEV DEVSQ FORCAST SLOPE STD STDP VAR VARP "//统计函数
- "COST PEAK PEAKBARS SAR SARTURN TROUGH TROUGHBARS WINNER ZIG "//指标函数
- "OPEN HIGH LOW CLOSE O H L C AMOUNT VOL "//行情函数
- "Integer Real Char Array Record Begin End While Repeat For Case Print "
- "Read If Until Then Else To DownTo Step Do "//语句
- "And Mod Div Not Or ";//关系运算符
- static LPCTSTR szConstants = " MAXINT ";
- BOOL CGShiEdit::OnInitDialog()
- {
- CDialog::OnInitDialog();
- m_passw=true;
- if(m_passw)
- {
- m_edit2.SetReadOnly();
- }
- else
- {
- m_edit2.SetReadOnly(FALSE);
- }
- PARAFORMAT pf;//这个结构包含RichEdit的段落格式信息
- pf.cbSize = sizeof(PARAFORMAT);
- pf.dwMask = PFM_TABSTOPS ;//可以有几个值,中间用||连接
- pf.cTabCount = MAX_TAB_STOPS; //
- for( int itab = 0 ; itab < pf.cTabCount ; itab++ )
- pf.rgxTabs[itab] = (itab + 1) * 1440/5 ;
- m_RichEdit.SetParaFormat( pf );//设置段落格式化属性
- CHARFORMAT cfDefault;//这个结构包含字符的格式化信息
- cfDefault.cbSize = sizeof(cfDefault);
- cfDefault.dwEffects = CFE_PROTECTED;
- cfDefault.dwMask = CFM_BOLD | CFM_FACE | CFM_SIZE | CFM_CHARSET | CFM_PROTECTED;
- cfDefault.yHeight = 220;
- cfDefault.bCharSet = /*0xEE*/ANSI_CHARSET;
- strcpy(cfDefault.szFaceName, _T("Courier New"));//设置字体的有关属性
- m_RichEdit.SetDefaultCharFormat(cfDefault);//设置字体的格式化属性
- //GetRichEditCtrl().SetEventMask(ENM_CHANGE | ENM_SELCHANGE | ENM_PROTECTED);//允许产生消息
- m_chComment = 1;
- m_chComment = _T('/');
- m_chComment2 =_T('/');
- m_strComment=_T("rem");
- m_bCaseSensitive = FALSE;
- m_bChangeCase = TRUE;
- //SetStringQuotes(_T("""));
- m_strStringQuotes = _T(""");//转义序列,表示双引号
- m_icKeyword.clrColor =RGB(0,0,255);
- m_icKeyword.bBold = FALSE;
- m_icNumber.clrColor = RGB(255,0,255);
- m_icNumber.bBold = FALSE;
- m_icComment.clrColor = RGB(0,128,0);
- m_icComment.bBold =FALSE ;
- m_icConstant.clrColor =RGB(0,0,0);
- m_icConstant.bBold =TRUE;
- m_icString.clrColor = RGB(255,128,255);
- m_icString.bBold =FALSE ;
- // SetKeywordColor(RGB(0,0,255), FALSE);
- // SetConstantColor(RGB(0,0,0), TRUE);
- // SetCommentColor(RGB(0,128,0), FALSE);
- // SetNumberColor(RGB(255,0,255), FALSE);
- // SetStringColor(RGB(255,128,255), FALSE);
- m_RichEdit.SetEventMask(ENM_CHANGE|ENM_SELCHANGE|ENM_PROTECTED);
- m_bInForcedChange = FALSE;
- //m_changeType = ctUndo;
- m_crOldSel.cpMin = m_crOldSel.cpMax = 0;
- m_strKeywords = m_strKeywords + szKeywords;
- int bbb=m_strKeywords.GetLength();
- m_strKeywordsLower = m_strKeywords;
- //if (!m_bCaseSensitive)
- m_strKeywordsLower.MakeLower();//变为小写
- int aaa=m_strKeywordsLower.GetLength();
- m_strConstants = m_strConstants + szConstants;
- m_strConstantsLower = m_strConstants;//m_strConstants和m_strConstantsLower都是字符串变量
- //if (!m_bCaseSensitive)//m_bCaseSensitive是布尔型变量
- m_strConstantsLower.MakeLower();//变为小写
- //m_RichEdit.SetWindowText(m_strKeywordsLower);
- return TRUE; // return TRUE unless you set the focus to a control
- // EXCEPTION: OCX Property Pages should return FALSE
- }
- void CGShiEdit::OnSize(UINT nType, int cx, int cy)
- {
- CDialog::OnSize(nType, cx, cy);
- /* if(m_isFirst==FALSE)
- {
- RECT rect;
- m_RichEdit.GetWindowRect(&rect);
- rect.bottom=cy;
- rect.right=cx;
- m_RichEdit.SetWindowPos(NULL,1,197,rect.right,rect.bottom,NULL);
- }
- m_isFirst=FALSE;*/
- CRect rect;
- if (m_RichEdit.GetSafeHwnd())
- {
- GetClientRect(&rect);
- rect.top += 195;
- rect.DeflateRect(0,0);
- m_RichEdit.MoveWindow(rect);
- }
- }
- int CGShiEdit::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CDialog::OnCreate(lpCreateStruct) == -1)
- return -1;
- m_isFirst=TRUE;
- return 0;
- }
- void CGShiEdit::OnChangeRichedit()
- {
- //if (m_bInForcedChange)
- // return;
- CHARRANGE crCurSel; //这个结构指定一个字串的范围
- m_RichEdit.GetSel(crCurSel);//得到控键中当前行中字符串的范围
- // switch(m_changeType)
- // {
- // case ctUndo: // old=(?,?) -> cur=(x,y)
- //MessageBox("Undo");
- FormatTextLines(crCurSel.cpMin, crCurSel.cpMax);//其实一直就运行这一句,其他case没有编//撤销
- //格式化当前行
- // break;
- // default:
- // FormatAll();
- // FormatTextRange(0, GetTextLength());//调用函数
- // break;
- // }
- //undo action does not call OnProtected, so make it default
- //m_changeType = ctUndo;
- }
- void CGShiEdit::FormatTextLines(int nLineStart, int nLineEnd)
- {
- long nStart = m_RichEdit.LineIndex(m_RichEdit.LineFromChar(nLineStart));
- long nEnd = m_RichEdit.LineIndex(m_RichEdit.LineFromChar(nLineEnd));
- nEnd += m_RichEdit.LineLength(nLineEnd);
- FormatTextRange(nStart, nEnd);//格式化当前行,看不出闪烁
- //FormatTextRange(0, GetTextLength());//格式化全部,会有更新时的闪烁
- //都是从头至尾对每个字符处理
- }
- void CGShiEdit::FormatTextRange(int nStart, int nEnd)
- {
- if (nStart >= nEnd)
- return;
- m_bInForcedChange = TRUE;
- CHARRANGE crOldSel;//这个结构指定一个Richedit中的字符串的范围
- m_RichEdit.GetSel(crOldSel);//返回范围
- m_RichEdit.LockWindowUpdate();//锁窗口
- m_RichEdit.HideSelection(TRUE, FALSE);//隐藏选择
- //参数1表示隐藏或显示
- //参数2表示这个改变是否是永久性的
- TCHAR *pBuffer = NULL;//定义一个字符缓冲区的指针
- try //异常捕获
- {
- m_RichEdit.SetSel(nStart, nEnd);//设置选择范围
- pBuffer = new TCHAR[nEnd - nStart + 1];//创建缓冲区
- long nLen = m_RichEdit.GetSelText(pBuffer);//拷贝一个副本到缓冲区,但不包括终止符
- //返回值是拷贝的大小--缓冲区的大小
- ASSERT(nLen <= nEnd - nStart);
- pBuffer[nLen] = 0;//填加终止符
- TCHAR *pStart, *pPtr;//定义指针
- pStart = pPtr = pBuffer;//都指向缓冲区的首地址
- TCHAR* pSymbolStart = NULL;//定义指针
- SymbolColor ic;//颜色结构变量
- while (*pPtr != 0)//从pBuffer的开始一直到尾
- {
- TCHAR ch = *pPtr;//因为pPtr开始是指向首地址的
- //所以ch第一次等于第一个字符
- if (ch == m_chComment && (m_chComment2 == 0 || pPtr[1] == m_chComment2))
- //如果ch='/'或者第二个字符='/'-------表示这一行是注释
- {
- pSymbolStart = pPtr;//保存开始地址
- do
- {
- ch = *(++pPtr);//赋值没有什么意义,主要是把指针移到最后(得到尾地址)
- }
- while (ch != 0 && ch != 'r');
- ic = m_icComment;
- }
- else if (IsStringQuote(ch))
- { // Process strings
- pSymbolStart = pPtr;
- TCHAR ch1 = ch;
- do
- {
- ch = *(++pPtr);
- }
- while (ch != 0 && ch != ch1 && ch != 'r');
- if (ch == ch1) pPtr++;
- ic = m_icString;
- }
- else if (_istdigit(ch))//_istdigit返回一个十进制的阿拉伯数字
- { // Process numbers
- pSymbolStart = pPtr;
- _tcstod(pSymbolStart, &pPtr);//_tcstod()把字符串转变为double型
- //这一句使pPtr增加了1,因为转换完成后,函数使pPtr自动指向下一位置
- //所以要明白_tcstod()函数
- ic = m_icNumber;
- }
- else if (_istalpha(ch) || ch == '_')//_istalpha()把文字字符转变为int型数字
- { // Process keywords
- pSymbolStart = pPtr;
- do
- {
- ch = *(++pPtr);
- }
- while (_istalnum(ch) || ch == '_');//_istalnum()把文字数字转变为数字
- *pPtr = 0;
- int cccj=IsKeyword("sum");
- int nPos = IsKeyword(pSymbolStart);
- if (nPos >= 0)
- {
- ChangeCase(nStart + pSymbolStart - pBuffer, nStart + pPtr - pBuffer,
- m_strKeywords.Mid(nPos+1, pPtr - pSymbolStart));
- if (_tcsicmp(m_strComment, pSymbolStart) == 0)//_tcsicmp()完成小写字母的比较
- {
- *pPtr = ch;
- *pSymbolStart = m_chComment;
- if (pSymbolStart[1] != 0 && m_chComment2 != 0)
- pSymbolStart[1] = m_chComment2;
- pPtr = pSymbolStart;
- pSymbolStart = NULL;
- continue;
- }
- ic = m_icKeyword;
- }
- else
- {
- nPos = IsConstant(pSymbolStart);
- if (nPos >= 0)
- {
- ChangeCase(nStart + pSymbolStart - pBuffer, nStart + pPtr - pBuffer,
- m_strConstants.Mid(nPos+1, pPtr - pSymbolStart));
- ic = m_icConstant;
- }
- else
- {
- pSymbolStart = NULL;
- }
- }
- *pPtr = ch;
- }
- else
- {
- pPtr++;
- }
- if (pSymbolStart != NULL)
- {
- ASSERT(pSymbolStart < pPtr);
- //SetFormatRange(nStart + pStart - pBuffer, nStart + pSymbolStart - pBuffer, FALSE, RGB(0,0,0));
- SetFormatRange(nStart + pSymbolStart - pBuffer, nStart + pPtr - pBuffer, ic.bBold, ic.clrColor);
- //SetFormatRange(nStart,nEnd, ic.bBold, ic.clrColor);
- pStart = pPtr;
- pSymbolStart = 0;
- }
- else if (*pPtr == 0)
- SetFormatRange(nStart + pStart - pBuffer, nStart + pPtr - pBuffer, FALSE, RGB(0,0,0));
- }//while完
- }//try完
- catch(...){}//例外处理
- delete [] pBuffer;
- m_RichEdit.SetSel(crOldSel);
- m_RichEdit.HideSelection(FALSE, FALSE);
- m_RichEdit.UnlockWindowUpdate();//开锁窗口
- m_bInForcedChange = FALSE;
- }
- int CGShiEdit::IsKeyword(LPCTSTR lpszSymbol)
- {
- CString strSymbol;
- strSymbol.Format(" %s ", lpszSymbol);
- //if (!m_bCaseSensitive) //如果不是光标位置
- strSymbol.MakeLower();//转换为小写
- return m_strKeywordsLower.Find(strSymbol,0);//如果返回-1表示没找到
- // return 0;
- }
- BOOL CGShiEdit::IsStringQuote(TCHAR ch)
- {
- //如果是字符引用
- return (m_strStringQuotes.Find(ch) >= 0);
- }
- void CGShiEdit::ChangeCase(int nStart, int nEnd, LPCTSTR lpsz)
- {
- ASSERT((nEnd - nStart) == (int)_tcslen(lpsz));
- if (!m_bCaseSensitive && m_bChangeCase)
- {
- m_RichEdit.SetSel(nStart, nEnd);//获得选择内容nStart是开始位置,nEnd是结束位置
- m_RichEdit.ReplaceSel(lpsz);//用lpsz替换获得的选择内容
- }
- }
- int CGShiEdit::IsConstant(LPCTSTR lpszSymbol)
- {
- CString strSymbol;
- strSymbol.Format(" %s ", lpszSymbol);
- if (!m_bCaseSensitive)
- strSymbol.MakeLower();
- return m_strConstantsLower.Find(strSymbol);
- }
- void CGShiEdit::SetFormatRange(int nStart, int nEnd, BOOL bBold, COLORREF clr)
- {
- //格式化一个区域,nStart--开始位置,nEnd--结尾位置,
- //bBoid--
- if (nStart >= nEnd)
- return;
- m_RichEdit.SetSel(nStart, nEnd);
- DWORD dwEffects =(bBold?CFE_BOLD:0);//三目运算,CFE_BOLD是粗体
- //这一句表示bBold是真,括号内得值就是CFF_BOLD,否则是0
- //也就是bBold是真,dwEffects就是粗体,否则不是粗体
- CHARFORMAT cfm;//字符格式的结构
- cfm.cbSize = sizeof(cfm);
- m_RichEdit.GetSelectionCharFormat(cfm);
- if ((cfm.dwMask & CFM_COLOR) && cfm.crTextColor == clr &&
- (cfm.dwMask & CFM_BOLD) && (cfm.dwEffects & CFE_BOLD) == dwEffects)
- return;
- cfm.dwEffects = dwEffects;//字体风格 如:粗体、斜体、下画线等
- cfm.crTextColor = clr;//设置字符颜色
- cfm.dwMask = CFM_BOLD | CFM_COLOR;//粗体、颜色属性有效
- m_RichEdit.SetSelectionCharFormat(cfm);//把字符属性给选择的字符
- }
- void CGShiEdit::OnButton7()
- {
- CHARRANGE cr;
- m_RichEdit.GetSel(cr);
- int kk=cr.cpMax;
- CString stra,str1,str2;
- m_RichEdit.GetWindowText(stra);
- int tol=strlen(stra);
- str1=stra.Left(kk);
- str2=stra.Right(tol-kk);
- CInstFunDlg dlg;
- if(dlg.DoModal()==IDOK)
- {
- CString str=dlg.str;//然后可以插入str
- str1=str1+str;
- int len=strlen(str1);
- cr.cpMax=len;
- cr.cpMin=len;
- str1=str1+str2;
- int msg=AfxMessageBox(str,/*MB_OKCANCEL*/MB_YESNO|MB_ICONINFORMATION);
- if(msg==IDYES) m_RichEdit.SetWindowText(str1);
- m_RichEdit.SetSel(cr);
- m_RichEdit.SetFocus();
- }
- }
- void CGShiEdit::OnCheck1()
- {
- m_passw=!m_passw;
- if(m_passw)
- {
- m_edit2.SetReadOnly();
- }
- else
- {
- m_edit2.SetReadOnly(FALSE);
- }
- }