#include "stdafx.h" #include "icrEdit.h" #include "SystemFunctionList.h" #include "public.h" #include "icrEditDoc.h" #include "icrEditView.h" #include "Mainfrm.h" #include "SelectVariantDlg.h" #include "scriptproc.h" #include "scriptobject.h" #define unsigned int UWORD char * g_CurrentDir; extern BOOL ProcessScript(CString& strScript); DEFINE_GUID(CLSID_VBScript, 0xb54f3741, 0x5b07, 0x11cf, 0xa4, 0xb0, 0x0, 0xaa, 0x0, 0x4a, 0x55, 0xe8); // 错误输出 extern void HRVERIFY(HRESULT hr, char * msg); ///////////////////////////////////////////////////////////////////////////// // CIcrEditView IMPLEMENT_DYNCREATE(CIcrEditView, CSynEditView) BEGIN_MESSAGE_MAP(CIcrEditView, CSynEditView) //{{AFX_MSG_MAP(CIcrEditView) ON_WM_KEYDOWN() ON_WM_KEYUP() ON_WM_LBUTTONDOWN() ON_WM_LBUTTONUP() ON_WM_RBUTTONDOWN() ON_COMMAND(ID_FILE_PRINT_PREVIEW, OnFilePrintPreview) ON_COMMAND(ID_CHECK, OnCheck) ON_COMMAND(ID_SELANALOG, OnSelAnalog) ON_COMMAND(ID_FUNCTION, OnFunction) ON_COMMAND(ID_SELDIGITAL, OnSelDigital) //}}AFX_MSG_MAP // Standard printing commands ON_COMMAND(ID_FILE_PRINT, CSynEditView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_DIRECT, CSynEditView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_PREVIEW, CSynEditView::OnFilePrintPreview) END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CIcrEditView construction/destruction CIcrEditView::CIcrEditView() { // TODO: add construction code here } CIcrEditView::~CIcrEditView() { } BOOL CIcrEditView::PreCreateWindow(CREATESTRUCT& cs) { // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs return CSynEditView::PreCreateWindow(cs); } ///////////////////////////////////////////////////////////////////////////// // CIcrEditView drawing void CIcrEditView::OnDraw(CDC* pDC) { // TODO: add draw code for native data here } ///////////////////////////////////////////////////////////////////////////// // CIcrEditView printing BOOL CIcrEditView::OnPreparePrinting(CPrintInfo* pInfo) { // default preparation return DoPreparePrinting(pInfo); } void CIcrEditView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add extra initialization before printing } void CIcrEditView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add cleanup after printing } ///////////////////////////////////////////////////////////////////////////// // CIcrEditView diagnostics #ifdef _DEBUG #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CIcrEditView message handlers LRESULT CIcrEditView::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) { // TODO: Add your specialized code here and/or call the base class return CSynEditView::WindowProc(message, wParam, lParam); } void CIcrEditView::ShowCursorPosition() { long start, end; CRichEditCtrl& richctrl = GetRichEditCtrl(); richctrl.GetSel(start, end); long row = richctrl.LineFromChar(start); long column = start - richctrl.LineIndex(row); CString strShow; strShow.Format(_T("Ln:%d, Col:%d"), row+1, column+1); CMainFrame *mainfrm = (CMainFrame *)AfxGetMainWnd(); CStatusBarCtrl &statusCtrl = mainfrm->m_wndStatusBar.GetStatusBarCtrl(); statusCtrl.SetText(strShow, 1, 0); } CString CIcrEditView::GetCursorString() { long start, end; CRichEditCtrl& richctrl = GetRichEditCtrl(); richctrl.GetSel(start, end); long startline = richctrl.LineFromChar(start); long endlie = richctrl.LineFromChar(end); if(startline == endlie && start != end) //用户选定了文本 { return richctrl.GetSelText(); } else { int nlnstart, nlnend; long line = richctrl.LineFromChar(end); int ntmp = end-richctrl.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(ntmpGetStyle() & WS_CHILD) pWndPopupOwner = pWndPopupOwner->GetParent(); pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, ptCursor.x, ptCursor.y, pWndPopupOwner); CSynEditView::OnRButtonDown(nFlags, point); } void CIcrEditView::OnFilePrintPreview() { // TODO: Add your command handler code here CSynEditView::OnFilePrintPreview(); } /* 语法检查 */ void CIcrEditView::OnCheck() { // 预处理脚本 CString scrpt; GetRichEditCtrl().GetWindowText(scrpt); scrpt = scrpt + "\nShowResult(\"a\")"; ProcessScript(scrpt); MyActiveScriptSite g_iActiveScriptSite; IActiveScript *m_iActiveScript; IActiveScriptParse *m_iActiveScriptParse; CScriptObject m_ScriptObject; // 用引擎进行检查 g_iActiveScriptSite.m_pUnkScriptObject = m_ScriptObject.GetInterface(&IID_IUnknown); // Start inproc script engine, VBSCRIPT.DLL HRVERIFY(CoCreateInstance(CLSID_VBScript, NULL, CLSCTX_INPROC_SERVER, IID_IActiveScript, (void **)&m_iActiveScript), "CoCreateInstance() for CLSID_VBScript"); // Get engine's IActiveScriptParse interface. HRVERIFY(m_iActiveScript->QueryInterface(IID_IActiveScriptParse, (void **)&m_iActiveScriptParse), "QueryInterface() for IID_IActiveScriptParse"); // Give engine our IActiveScriptSite interface... HRVERIFY(m_iActiveScript->SetScriptSite(&g_iActiveScriptSite), "IActiveScript::SetScriptSite()"); // Give the engine a chance to initialize itself... HRVERIFY(m_iActiveScriptParse->InitNew(), "IActiveScriptParse::InitNew()"); // Add a root-level item to the engine's name space... HRVERIFY(m_iActiveScript->AddNamedItem(L"MyObject", SCRIPTITEM_ISVISIBLE | SCRIPTITEM_ISSOURCE), "IActiveScript::AddNamedItem()"); // Parse the code scriptlet... EXCEPINFO ei; BSTR pParseText = scrpt.AllocSysString(); m_iActiveScriptParse->ParseScriptText(pParseText, L"MyObject", NULL, NULL, 0, 0, 0L, NULL, &ei); /* 启动脚本执行 */ try { m_iActiveScript->SetScriptState(SCRIPTSTATE_CONNECTED); } catch(CException* e) { delete e; } } /* 选择系统变量 */ void CIcrEditView::OnSelAnalog() { CSelectVariantDlg dlg; dlg.m_nType = 0; if(dlg.DoModal()==IDOK) { CString var = dlg.m_strVar; HANDLE hGlobalMemory = GlobalAlloc(GHND, var.GetLength()+1); // 分配内存 LPBYTE lpGlobalMemory = (LPBYTE)GlobalLock(hGlobalMemory); // 锁定内存 memcpy(lpGlobalMemory,var,var.GetLength()+1); GlobalUnlock(hGlobalMemory); // 锁定内存块解锁 HWND hWnd = GetSafeHwnd(); // 获取安全窗口句柄 ::OpenClipboard(hWnd); // 打开剪贴板 ::EmptyClipboard(); // 清空剪贴板 ::SetClipboardData(CF_TEXT, hGlobalMemory); // 将内存中的数据放置到剪贴板 ::CloseClipboard(); // 关闭剪贴板 GetRichEditCtrl().Paste(); } } /* 选择系统函数 */ void CIcrEditView::OnFunction() { // TODO: Add your command handler code here CSystemFunctionList dlg; if(dlg.DoModal()==IDOK) { CString fun = dlg.m_strFunction; HANDLE hGlobalMemory = GlobalAlloc(GHND, fun.GetLength()+1); // 分配内存 LPBYTE lpGlobalMemory = (LPBYTE)GlobalLock(hGlobalMemory); // 锁定内存 memcpy(lpGlobalMemory,fun,fun.GetLength()+1); GlobalUnlock(hGlobalMemory); // 锁定内存块解锁 HWND hWnd = GetSafeHwnd(); // 获取安全窗口句柄 ::OpenClipboard(hWnd); // 打开剪贴板 ::EmptyClipboard(); // 清空剪贴板 ::SetClipboardData(CF_TEXT, hGlobalMemory); // 将内存中的数据放置到剪贴板 ::CloseClipboard(); // 关闭剪贴板 GetRichEditCtrl().Paste(); } } void CIcrEditView::OnSelDigital() { CSelectVariantDlg dlg; dlg.m_nType = 1; if(dlg.DoModal()==IDOK) { CString var = dlg.m_strVar; HANDLE hGlobalMemory = GlobalAlloc(GHND, var.GetLength()+1); // 分配内存 LPBYTE lpGlobalMemory = (LPBYTE)GlobalLock(hGlobalMemory); // 锁定内存 memcpy(lpGlobalMemory,var,var.GetLength()+1); GlobalUnlock(hGlobalMemory); // 锁定内存块解锁 HWND hWnd = GetSafeHwnd(); // 获取安全窗口句柄 ::OpenClipboard(hWnd); // 打开剪贴板 ::EmptyClipboard(); // 清空剪贴板 ::SetClipboardData(CF_TEXT, hGlobalMemory); // 将内存中的数据放置到剪贴板 ::CloseClipboard(); // 关闭剪贴板 GetRichEditCtrl().Paste(); } }