JavaScript Calls from C++
最近要用到相关技术,先贴在这,有空再翻页。
本文转自:
http://www.codeguru.com/Cpp/I-N/ieprogram/article.php/c4399
http://www.codeguru.com/cpp/i-n/ieprogram/article.php/c4399/JavaScript-Calls-from-C.htm
http://www.codeproject.com/KB/COM/jscalls.aspx
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);
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 GetLastErr
相关文档:
基本上全部加了注释,一行一注....呵呵
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>数组排序</title>
<mce:style type="text/css"&g ......
文章作者:Slyar
先看两个代码,第一个是C程序,第二个是C++程序,除了头文件不同以外其他的都一样。
//demo.c
#include <stdio.h>
int foo();
int main()
{
int a;
foo(a);
return 0;
}
int foo(int a)
{
}//demo.cpp
#include <iostream>
int foo();
int main()
{
in ......
setitimer()为Linux的API,并非C语言的Standard Library,setitimer()有两个功能,一是指定一段时间后,才执行某个function,二是每间格一段时间就执行某个function,以下程序demo如何使用setitimer()。
/*
......
函数是程序的执行模块,是整个项目的细胞,如何组织好每一个函数的结构,如何提高程序的可读性和运行效率,是程序员们不断品味的主体。有时函数的设计能直接体现出程序员对C/C++的理解和感悟。
C/C++的函数类型不对外乎以下几种,有些描述可能不太贴切:
......