继承DWebBrowserEvents2,IWebBrowser2控件窗口类如何得到Javascript事件,执行Javascript事件

iery 2003-07-14 04:27:04
继承DWebBrowserEvents2,IWebBrowser2控件窗口类如何得到Javascript事件,执行Javascript事件
...全文
600 10 打赏 收藏 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
histimes 2004-02-04
  • 打赏
  • 举报
回复
这是CodeGuru上的一片文章,对你有用:
Introduction
Sometimes, when we are using the IE Browser Control inside of a C++ application, we need to access the HTML elements. We can do it by using standard COM objects such as IWebBrowser2, IHTMLDocument2, and so forth. By doing this, we easily can implement features such as click button, click anchor, get input string, get HTML text, and so on. Unfortunately, Microsoft did not provide similar objects for JavaScript. In any case, it is possible to make a control for the JavaScript object inside an HTML page by using a traditional COM approach. This article describes the class CWebPage that allows you to do it and a technique to call a JavaScript function from C++ code.

How to Do This
As the result of using the presented class, it will be easy to call any JavaScript function from C++ code. To implement this feature, we should get a pointer to the IHTMLDocument2 interface. If we are using the CHtmlView class from MFC, we can get one by using member function CHtmlView::GetHtmlDocument(). In the case of using the IWebBrowser or IWebBrowser2 components, the function get_Document will bring us the desired interface. Here is an example:

CComPtr<IDispatch> spDisp = CHtmlView::GetHtmlDocument();
m_webPage.SetDocument(spDisp);
从IWebBrowser得到IHTMLDocument2

The rest of the things will be done by the CWebPage class. Here is an example of a JavaScript call without parameters.

m_webPage.CallJScript("Welcome");

The example of the JavaScript call with two parameters will look like this:

m_webPage.CallJScript("Miltiply","2.34","3.32");

The Class Implementation
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);
bool CallJScript(const CString strFunc,const CString strArg1);
bool CallJScript(const CString strFunc,const CString strArg1,
const CString strArg2);
bool CallJScript(const CString strFunc,const CString strArg1,
const CString strArg2,const CString strArg3);
bool CallJScript(const CString strFunc,const
CStringArray& paramArray);

protected

CComPtr<IHTMLDocument2> m_spDoc;

};

Calling Technique
The previously mentioned technique splits the following steps:

Getting a pointer to the IHTMLDocument2 interface.
Getting IDispatch for a JavaScript object in an HTML document.
Getting DISPID for a given name of a JavaScript function.
Putting parameters to the DISPPARAM structure.
Calling a JavaScript function by using the Invoke method of the IDispatch interface.
Here is an example of getting a IDispatch pointer to the Java Scripts objects:

bool CWebPage::GetJScript(CComPtr<IDispatch>& spDisp)
{
HRESULT hr = m_spDoc->get_Script(&spDisp);
ATLASSERT(SUCCEEDED(hr));
return SUCCEEDED(hr);
}

And here is the final function to call JavaScript:

CComVariant CWebPage::CallJScript(const CString strFunc,
const CStringArray&
paramArray)
{
//Getting IDispatch for Java Script objects
CComPtr<IDispatch> spScript;
if(!GetJScript(spScript))
{
ShowError("Cannot GetScript");
return false;
}
//Find dispid for given function in the object
CComBSTR bstrMember(strFunc);
DISPID dispid = NULL;
HRESULT hr = spScript->GetIDsOfNames(IID_NULL,&bstrMember,1,
LOCALE_SYSTEM_DEFAULT,&dispid);
if(FAILED(hr))
{
ShowError(GetSystemErrorMessage(hr));
return false;
}

const int arraySize = paramArray.GetSize();
//Putting parameters
DISPPARAMS dispparams;
memset(&dispparams, 0, sizeof dispparams);
dispparams.cArgs = arraySize;
dispparams.rgvarg = new VARIANT[dispparams.cArgs];
dispparams.cNamedArgs = 0;

for( int i = 0; i < arraySize; i++)
{
CComBSTR> bstr = paramArray.GetAt(arraySize - 1 - i);
// back reading
bstr.CopyTo(&dispparams.rgvarg[i].bstrVal);
dispparams.rgvarg[i].vt = VT_BSTR;
}
EXCEPINFO excepInfo;
memset(&excepInfo, 0, sizeof excepInfo);
CComVariant vaResult;
UINT nArgErr = (UINT)-1; // initialize to invalid arg
//Call JavaScript function
hr = spScript->Invoke(dispid,IID_NULL,0,
DISPATCH_METHOD,&dispparams,
&vaResult,&excepInfo,&nArgErr);
delete [] dispparams.rgvarg;
if(FAILED(hr))
{
ShowError(GetSystemErrorMessage(hr));
return false;
}
return vaResult;
}

Notes About the Demo
To call a JavaScript function from the demo, you should select the function in the tree of the left window. After this, press the ! button on the menu bar.

Downloads
Download demo project - 34 Kb
Download source - 3 Kb

History
Date Posted: October 25, 2002


Comments:
grantchan 2003-09-04
  • 打赏
  • 举报
回复
cstring@163.com
cjwn 2003-09-01
  • 打赏
  • 举报
回复
我找到一个例子。搂主要的话留个email
我忘了在那个网站下的了。所以无法贴出地址。
atEleven 2003-08-30
  • 打赏
  • 举报
回复
我以前写过,用了一个BT 的方法实现.

javascript是在网页中的.事件发生的时候,就设置document.location到一个定义的字符串
当事件发生的时候,WebBrowser会企图Navigate到那个网址

在IWebBrowser2 的BeforeNavigate 事件中根据Navigate URL
判断是发生了哪些脚本事件并执行事件代码.同时Cancel掉该 Navigate 动作.
cjwn 2003-08-30
  • 打赏
  • 举报
回复
俺 找到了一个相关的例子。我发给你。
楼主email地址多少?
天限天空 2003-08-30
  • 打赏
  • 举报
回复
试一试 active script 技术

蒋晟 2003-08-22
  • 打赏
  • 举报
回复
IWebBrowser2是接口不是控件
可以得到元素事件的接口,但是Javascript本身没有事件
事件不能被执行,只能被触发。触发条件随元素和事件而异。
cjwn 2003-08-22
  • 打赏
  • 举报
回复
gz
grantchan 2003-08-21
  • 打赏
  • 举报
回复
up
kalakala 2003-07-24
  • 打赏
  • 举报
回复
up
内容概要:本文围绕【复现IEEE二区文献】基于状态空间表示与组件连接法的构网型逆变器统一小信号建模框架研究,提出了一种适用于多控制策略构网型逆变器的小信号建模方法。该方法结合状态空间建模理论与组件连接法(Component Connection Method),实现了对复杂逆变器系统动态特性的精确描述,尤其适用于分析系统在不同运行工况下的稳定性。通过Matlab代码实现,构建了包含控制环路、滤波器、锁相环等关键模块的完整小信号模型,并利用特征值分析与模态参与度评估系统稳定性,解决了传统建模方法在面对多时间尺度、强耦合非线性控制结构时建模困难、精度不足的问题。研究还探讨了关键控制参数对系统稳定边界的影响,为构网型逆变器的参数整定与优化设计提供了理论依据和技术支撑。; 适合人群:具备电力电子、自动控制理论基础,从事新能源发电、微电网、电力系统稳定性研究的研究生、科研人员及工程技术人员,尤其适合致力于高水平期刊论文复现与仿真实践的科研工作者。; 使用场景及目标:① 掌握构网型逆变器统一小信号建模的核心方法,复现IEEE二区高水平文献成果;② 理解状态空间法与组件连接法在复杂电力电子系统建模中的集成应用;③ 利用Matlab进行特征值分析与稳定性判据研究,支撑科研仿真与论文写作;④ 为多逆变器并联系统、微电网稳定性分析等课题提供建模工具与技术路线参考。; 阅读建议:建议读者结合Matlab代码与文中建模流程逐步调试,重点关注各子模块状态方程的推导与系统整体矩阵组装过程,深入理解控制参数对系统极点分布的影响规律,建议配合其他稳定性分析案例进行对比学习以加深理解。

3,248

社区成员

发帖
与我相关
我的任务
社区描述
ATL,Active Template Library活动(动态)模板库,是一种微软程序库,支持利用C++语言编写ASP代码以及其它ActiveX程序。
社区管理员
  • ATL/ActiveX/COM社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧