随笔 - 74, 文章 - 0, 评论 - 26, 引用 - 0
数据加载中……

c++ 和 jscript

#pragma once

#include <afxdisp.h>
#include <activscp.h>

class CCodeObject;
class CScriptSite;

class CScriptingSupportHelper
{
public:
    CScriptingSupportHelper();
    ~CScriptingSupportHelper();

    BOOL Create(CWnd* pWnd);
    BOOL RunScript(CString str);

    CCodeObject* GetCodeObject() const { return m_pCodeObject; }
    CScriptSite* GetScriptSite() const { return m_pScriptSite; }
    IActiveScript* GetActiveScript() const { return m_pActiveScript; }

private:
    CCodeObject* m_pCodeObject;
    CScriptSite* m_pScriptSite;

    IActiveScript* m_pActiveScript;  
    IActiveScriptParse* m_pActiveScriptParse;
};

class CCodeObject : public CCmdTarget
{
public:
    CCodeObject(CScriptingSupportHelper* pScripting, CWnd* pWnd);
    virtual ~CCodeObject();

    void Line(long, long, long, long);
    void Ellipse(long, long, long, long);
    void DrawText(LPCTSTR msg, long x, long y, long w, long h);

    void OnPaint();
    void OnMouseClick(long x, long y);

private:
    CWnd* m_pWnd;
    CScriptingSupportHelper* m_pScripting;
    BOOL GetDispatch(OLECHAR* name, COleDispatchDriver& disp, DISPID& dispid);

    enum
    {
        idLine = 1,
        idEllipse,
        idDrawText,
    };

    DECLARE_DISPATCH_MAP()
};

class CScriptSite : public IActiveScriptSite
{
public:
    CScriptSite(CScriptingSupportHelper* pScripting)  
    {
        m_pScripting = pScripting;
    };
   
    ~CScriptSite() 
    {
    };

    virtual ULONG STDMETHODCALLTYPE AddRef()
    {   
        return InterlockedIncrement(&m_nRefCount);
    }
   
    virtual ULONG STDMETHODCALLTYPE Release()
    {    
        return InterlockedDecrement(&m_nRefCount);
    };
   
    virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void** ppObj)
    {
        *ppObj = NULL;

        if ((iid == IID_IUnknown) || (iid == IID_IActiveScriptSite))
        {
            *ppObj= (IActiveScriptSite*)this;
            AddRef();
            return S_OK;
        }

        return E_NOINTERFACE;
    }

    virtual HRESULT STDMETHODCALLTYPE GetLCID(LCID __RPC_FAR *)
    {
        return E_NOTIMPL;
    }
   
    virtual HRESULT STDMETHODCALLTYPE GetItemInfo(LPCOLESTR, DWORD, IUnknown __RPC_FAR *__RPC_FAR * pObj, ITypeInfo __RPC_FAR *__RPC_FAR *)
    {
        ASSERT(m_pScripting);
        ASSERT(m_pScripting->GetCodeObject());

        *pObj = m_pScripting->GetCodeObject()->GetIDispatch(TRUE);
        return S_OK;
    }
       
    virtual HRESULT STDMETHODCALLTYPE GetDocVersionString(BSTR __RPC_FAR *)
    {
        return E_NOTIMPL;
    }
       
    virtual HRESULT STDMETHODCALLTYPE OnScriptTerminate(const VARIANT __RPC_FAR * ,const EXCEPINFO __RPC_FAR *)
    {
        return E_NOTIMPL;
    }

       
    virtual HRESULT STDMETHODCALLTYPE OnStateChange(SCRIPTSTATE)
    {
        return E_NOTIMPL;
    }
       
    virtual HRESULT STDMETHODCALLTYPE OnScriptError(IActiveScriptError __RPC_FAR * pScriptError)
    {
        return E_NOTIMPL;
    }
       
    virtual HRESULT STDMETHODCALLTYPE OnEnterScript()
    {
        return E_NOTIMPL;
    }
       
    virtual HRESULT STDMETHODCALLTYPE OnLeaveScript()
    {
        return E_NOTIMPL;
    }

private:
    long m_nRefCount;
    CScriptingSupportHelper* m_pScripting;
};

#include "StdAfx.h"
#include "ScriptingSupport.h"

CCodeObject::CCodeObject(CScriptingSupportHelper* pScripting, CWnd* pWnd)
    : m_pWnd(pWnd),
      m_pScripting(pScripting)

{
    EnableAutomation();
}

CCodeObject::~CCodeObject()
{
}

BEGIN_DISPATCH_MAP(CCodeObject, CCmdTarget)
DISP_FUNCTION_ID(CCodeObject, "Line", idLine, Line, VT_EMPTY, VTS_I4 VTS_I4 VTS_I4 VTS_I4)
DISP_FUNCTION_ID(CCodeObject, "Ellipse", idEllipse, Ellipse, VT_EMPTY, VTS_I4 VTS_I4 VTS_I4 VTS_I4)
DISP_FUNCTION_ID(CCodeObject, "DrawText", idDrawText, DrawText, VT_EMPTY, VTS_BSTR VTS_I4 VTS_I4 VTS_I4 VTS_I4)
END_DISPATCH_MAP()

void CCodeObject::Line(long x1, long y1, long x2, long y2)
{
    CWindowDC dc(m_pWnd);

    dc.MoveTo(x1, y1);
    dc.LineTo(x2, y2);
}

void CCodeObject::Ellipse(long x1, long y1, long x2, long y2)
{
    CWindowDC dc(m_pWnd);
    dc.Ellipse(x1, y1, x2, y2);
}

void CCodeObject::DrawText(LPCTSTR msg, long x, long y, long w, long h)
{
    CWindowDC dc(m_pWnd);
    CRect rect(x, y, x+w, y+h);

    dc.DrawText(msg, rect, 0);
}

void CCodeObject::OnPaint()
{
    COleDispatchDriver disp;
    DISPID dispid;
    if (GetDispatch(L"OnPaint", disp, dispid)) {
        disp.InvokeHelper(dispid, DISPATCH_METHOD, VT_EMPTY, 0, 0);
    }
}

BOOL CCodeObject::GetDispatch(OLECHAR* name, COleDispatchDriver& disp, DISPID& dispid)
{
    IDispatch* pScriptDispatch = 0;
    m_pScripting->GetActiveScript()->GetScriptDispatch(0, &pScriptDispatch);
    disp.AttachDispatch(pScriptDispatch);
    HRESULT hr = pScriptDispatch->GetIDsOfNames(IID_NULL, &name, 1, LOCALE_SYSTEM_DEFAULT, &dispid);
    return SUCCEEDED(hr);
}

void CCodeObject::OnMouseClick(long x, long y)
{
    COleDispatchDriver disp;
    DISPID dispid;
    if (GetDispatch(L"OnMouseClick", disp, dispid)) {
        disp.InvokeHelper(dispid, DISPATCH_METHOD, VT_EMPTY, 0, (const BYTE*)(VTS_I4 VTS_I4), x, y);
    }
}

CScriptingSupportHelper::CScriptingSupportHelper()
    : m_pCodeObject(0),
      m_pScriptSite(0),
      m_pActiveScript(0),
      m_pActiveScriptParse(0)
{
}

CScriptingSupportHelper::~CScriptingSupportHelper()
{
    if (m_pActiveScript)
    {
        m_pActiveScript->Close();
        m_pActiveScriptParse->Release();
        m_pActiveScript->Release();
    }

    delete m_pCodeObject; m_pCodeObject=0;
    delete m_pScriptSite; m_pScriptSite=0;
}

BOOL CScriptingSupportHelper::RunScript(CString strText)
{
    EXCEPINFO ei = {0};
    BSTR bstrText = strText.AllocSysString();
    m_pActiveScriptParse->ParseScriptText(bstrText, NULL, NULL, NULL, 0, 0, 0, NULL, &ei);
    SysFreeString(bstrText);

    m_pActiveScript->SetScriptState(SCRIPTSTATE_CONNECTED);

    return TRUE;
}

BOOL CScriptingSupportHelper::Create(CWnd* pWnd)
{
    m_pCodeObject = new CCodeObject(this, pWnd);
    m_pScriptSite = new CScriptSite(this);

    CLSID clsidJScript;
    CLSIDFromProgID(L"JScript", &clsidJScript);
    CoCreateInstance(clsidJScript, NULL, CLSCTX_INPROC_SERVER, IID_IActiveScript, (void **)&m_pActiveScript);
    m_pActiveScript->QueryInterface(IID_IActiveScriptParse, (void**)&m_pActiveScriptParse);
    m_pActiveScript->SetScriptSite(m_pScriptSite);
    m_pActiveScript->AddNamedItem(L"Code", SCRIPTITEM_ISVISIBLE | SCRIPTITEM_ISSOURCE | SCRIPTITEM_GLOBALMEMBERS);
    m_pActiveScriptParse->InitNew();


    return TRUE;
}




posted on 2007-11-20 09:19 井泉 阅读(653) 评论(0)  编辑 收藏 引用 所属分类: C++


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理