// Map.cpp : Implementation of CMap
#include "stdafx.h"
#include "FunMap.h"
#include "Map.h"
#include <math.h>
/
// CMap
STDMETHODIMP CMap::get_MaxX(double *pVal)
{ //È¡µÃÊôÐÔ£¬X °ëÖáµÄ×î´óÖµ
*pVal = m_xMax;
return S_OK;
}
STDMETHODIMP CMap::put_MaxX(double newVal)
{ //ÉèÖÃÊôÐÔ£¬X °ëÖáµÄ×î´óÖµ
m_xMax = fabs( newVal );
FireViewChange();
return S_OK;
}
STDMETHODIMP CMap::get_MaxY(double *pVal)
{ //È¡µÃÊôÐÔ£¬Y °ëÖáµÄ×î´óÖµ
*pVal = m_yMax;
return S_OK;
}
STDMETHODIMP CMap::put_MaxY(double newVal)
{ //ÉèÖÃÊôÐÔ£¬Y °ëÖáµÄ×î´óÖµ
m_yMax = fabs( newVal );
FireViewChange();
return S_OK;
}
STDMETHODIMP CMap::get_Expression(BSTR *pVal)
{ //È¡µÃÊôÐÔ£¬º¯Êýʽ
*pVal = m_sExpression.Copy();
return S_OK;
}
STDMETHODIMP CMap::put_Expression(BSTR newVal)
{ //ÉèÖÃÊôÐÔ£¬º¯Êýʽ
m_sExpression = newVal;
if(Calc())
{
FireViewChange();
return S_OK;
}
else return E_INVALIDARG;
}
BOOL CMap::Calc()
{ //¸ù¾Ýº¯Êýʽ£¬¼ÆË㺯ÊýÇúÏßµÄ×ø±êµã
CComQIPtr< IActiveScript > pAS;
pAS.CoCreateInstance( L"VBScript" );
if( !pAS )
{
Fire_ScriptErr( CComBSTR( _T("¼ÓÔØ VBScript ÒýÇæʧ°Ü")) );
return FALSE;
}
CComQIPtr< IActiveScriptParse > pASP(pAS);
if( !pASP )
{
Fire_ScriptErr( CComBSTR( _T("²éѯ½âÎö½Ó¿Úʧ°Ü")) );
return FALSE;
}
pAS->SetScriptSite( this );
HRESULT hr = pASP->InitNew();
if( FAILED(hr) )
{
Fire_ScriptErr( CComBSTR( _T("½âÎö½Ó¿Ú³õʼ»¯Ê§°Ü")) );
pAS->Close(); return FALSE;
}
hr = pAS->AddNamedItem(L"MyObject", SCRIPTITEM_ISVISIBLE | SCRIPTITEM_ISSOURCE);
if( FAILED(hr) )
{
Fire_ScriptErr( CComBSTR( _T("Ìí¼Ó½Å±¾ÃüÃû´íÎó")) );
pAS->Close(); return FALSE;
}
::memset( m_xVal, 0, sizeof(double) * nPointCount );
::memset( m_yVal, 0, sizeof(double) * nPointCount );
USES_CONVERSION;
for(int i=0; i<nPointCount; i++)
{
TCHAR szScript[1000];
double x=i*(2*m_xMax)/nPointCount-m_xMax; //ÇóµÃ×Ô±äÁ¿µÄÖµ
char strX[20]; ::_gcvt(x,7,strX);
::wsprintf( szScript,
_T("i=%d/r/nx=%s/r/ny=%s/r/ncall Result(i,x,y)"),
i,A2CT(strX),OLE2CT(m_sExpression));
// ::MessageBox(NULL,szScript,_T("Éú³ÉµÄ½Å±¾³ÌÐò"),MB_OK);
/*****************************************************************
Éú³É VBScript µÄÑùʽΪ:
i = 0 'µ±Ç°ÕýÔÚ¼ÆËãµÄµãµÄÐòºÅ(0 ~ 199)
x = -4.0 '×Ô±äÁ¿µÄÖµ(¸ù¾ÝÐòºÅ¡¢´°¿Ú·¶Î§ºÍXÖá×ø±ê·¶Î§¼ÆËãµÃµ½)
y = sin(x) 'Óû§ÊäÈëµÄº¯Êýʽf(x)
call Result( i, x, y ) 'µ÷ÓÃ×ÔÉí¶ÔÏóµÄº¯Êý£¬±£´æ×ø±êµã
******************************************************************/
EXCEPINFO ei;
hr = pASP->ParseScriptText( CComBSTR( szScript ), L"MyObject",
NULL, NULL, 0, 0, 0, NULL, &ei);
if( FAILED( hr ) ) continue;
hr = pAS->SetScriptState( SCRIPTSTATE_CONNECTED );
if( FAILED( hr ) ) continue;
}
pAS->Close();
return TRUE;
}
// IActiveScriptSite
STDMETHODIMP CMap::GetLCID(/*[out]*/ LCID *plcid)
{ // È¡µÃµ±Ç°Ê¹ÓõÄÓïÑÔ»·¾³
return E_NOTIMPL; //ʹÓÃĬÈϵÄÓïÑÔ
}
STDMETHODIMP CMap::GetItemInfo(/*[in]*/ LPCOLESTR pstrName,
/*[in]*/ DWORD dwReturnMask,
/*[out]*/ IUnknown **ppunkItem,
/*[out]*/ ITypeInfo **ppTypeInfo)
{
if(ppTypeInfo)
{ //È¡µÃÀàÐÍÐÅÏ¢¿â£¬¹© VBScript ½øÐк¯Êý½âÎöÓÃ
*ppTypeInfo = NULL;
if(dwReturnMask & SCRIPTINFO_ITYPEINFO)
{
CComPtr< ITypeLib > sptLib;
::LoadTypeLib( L"FunMap.tlb", &sptLib ); //×°ÔØ×ÔÉíµÄÀàÐÍ¿â
if( sptLib )
sptLib->GetTypeInfo( 0, ppTypeInfo );
}
}
if(ppunkItem)
{ //È¡µÃ IUnknown Ö¸Õ룬¹© VBScript ½«À´µÄº¯Êýµ÷ÓÃ
*ppunkItem = NULL;
if(dwReturnMask & SCRIPTINFO_IUNKNOWN)
{
if ( 0 == _wcsicmp( L"MyObject", pstrName ) )
{
ControlQueryInterface( IID_IUnknown, (LPVOID *)ppunkItem );
}
}
}
return S_OK;
}
STDMETHODIMP CMap::GetDocVersionString(/*[out]*/ BSTR *pbstrVersionString)
{
return E_NOTIMPL;
}
STDMETHODIMP CMap::OnScriptTerminate(/*[in]*/ const VARIANT *pvarResult, /*[in]*/ const EXCEPINFO *pexcepinfo)
{
return S_OK;
}
STDMETHODIMP CMap::OnStateChange(/*[in]*/ SCRIPTSTATE ssScriptState)
{
return S_OK;
}
STDMETHODIMP CMap::OnScriptError(/*[in]*/ IActiveScriptError *pase)
{ // µ±Óû§ÊäÈëÁ˲»ÕýÈ·µÄº¯Êýʽ£¬»ò½Å±¾Ö´ÐеÄʱºò³ö´í
CComBSTR err,msg;
HRESULT hr = pase->GetSourceLineText( &err );
if( FAILED(hr) || !err.Length() )
msg = _T("Ö´ÐдíÎó");
else
{
msg = _T("Óï·¨´íÎó: ");
msg.AppendBSTR( err );
}
Fire_ScriptErr( msg );
return S_OK;
}
STDMETHODIMP CMap::OnEnterScript(void)
{
return S_OK;
}
STDMETHODIMP CMap::OnLeaveScript(void)
{
return S_OK;
}
STDMETHODIMP CMap::Result(long i, double x, double y)
{ // ¶¯Ì¬Éú³ÉµÄ½Å±¾µ÷Óøú¯Êý£¬ÓÃÀ´±£´æº¯ÊýÇúÏßµÄ×ø±ê
if( ( i >= 0 ) && ( i < nPointCount ) )
{
m_xVal[i] = x;
m_yVal[i] = y;
}
return S_OK;
}
STDMETHODIMP CMap::get_Pi(double *pVal)
{ // È¡µÃÊôÐÔ PI
*pVal = 3.1415926;
return S_OK;
}
STDMETHODIMP CMap::Log10(double dbVal, double *pdbVal)
{ // ¼ÆËã 10 Ϊµ×µÄ¶ÔÊý
*pdbVal = ::log10( dbVal );
return S_OK;
}