在完成了基本功能以后,接下来就可以实现输入经纬度在地图中显示地点的功能了。这里就要用到VC和JavaScript的混合编程了。
如何在VC中调用JavaScript的函数,以下两个分别是CWebPage类的.h和.cpp文件:
// WebPage.h: interface for the CWebPage class. // ////////////////////////////////////////////////////////////////////// #if !defined(AFX_WEBPAGE_H__AEBD50B8_EE66_40AB_8B92_C4EECB9BCD22__INCLUDED_) #define AFX_WEBPAGE_H__AEBD50B8_EE66_40AB_8B92_C4EECB9BCD22__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #include <atlbase.h> #include <Mshtml.h> class CWebPage { public: CWebPage(); virtual ~CWebPage(); bool SetDocument(IDispatch* pDisp); LPDISPATCH GetHtmlDocument() const; const CString GetLastError() const; bool GetJScript(CComPtr<IDispatch>& spDisp); bool GetJScripts(CComPtr<IHTMLElementCollection>& spColl); CString ScanJScript(CString& strAText, CStringArray& args); bool CallJScript(const CString strFunc,CComVariant* pVarResult = NULL); bool CallJScript(const CString strFunc,const CString strArg1,CComVariant* pVarResult = NULL); bool CallJScript(const CString strFunc,const CString strArg1,const CString strArg2,CComVariant* pVarResult = NULL); bool CallJScript(const CString strFunc,const CString strArg1,const CString strArg2,const CString strArg3,CComVariant* pVarResult = NULL); bool CallJScript(const CString strFunc,const CStringArray& paramArray,CComVariant* pVarResult = NULL); protected: void ShowError(LPCSTR lpszText); protected: CComPtr<IHTMLDocument2> m_spDoc; CString m_strError; }; inline void CWebPage::ShowError(LPCSTR lpszText) { m_strError = "JSCall Error:\n" + CString(lpszText); } inline const CString CWebPage::GetLastError() const { return m_strError; } inline LPDISPATCH CWebPage::GetHtmlDocument() const { return m_spDoc; } CString GetNextToken(CString& strSrc, const CString strDelim,bool bTrim = false, bool bFindOneOf = true); #endif // !defined(AFX_WEBPAGE_H__AEBD50B8_EE66_40AB_8B92_C4EECB9BCD22__INCLUDED_)