C # convenient methods and ideas for ActiveX to call JS Functions

Source: Internet
Author: User

Although AcitecX is an old and classic application technology, AcitecX is still so practical in today's ever-changing technological developments. Without it, you cannot find a more suitable technology to replace it.

This is not the case. Recently, in an application scenario of a company's product, I pulled it out and practiced it.

Next, the problem arises. Previously, JavaScript was used to call functions in ActiveX, and JavaScript was never called in ActiveX.

Then I searched for "c # activex call js" on google and found a call method.

This method can be obtained through the keyword search provided by me and will not be described in detail. The key steps are as follows:

1. Declare the IOleClientSite and IOleContainer interfaces.

2. IHTMLDocument and IHTMLWindow2 are obtained through a series of transformations.

3. Call JS functions.

 

This method is a steady and steady method, which is worth learning. After understanding this method, I have summarized the key point of IHTMLWindow2.

I am a lazy worm. After learning about the key points, I have simplified the methods and will share them with you here. Let's talk about the code.

Ideas:

1. Use JS to pass IHTMLWindow2 directly, saving a lot of declaration and conversion code. Directly focus on it.

2. The Calling syntax uses JS syntax to facilitate writing and understanding.

 

Code:

The js Code is as follows:

[Javascript]
<Script type = "text/javascript>
ActivexObj. SetFunc (window, "func"); // activexObj is an activex object, and "func" is the JS function name. The function name can be passed through parameters to make the code more flexible.
 
Function func (value ){
Alert (value );
}
</Script>
 

C # The Code is as follows:

Note: The project must reference Microsoft. mshtml.

[Csharp]
Using mshtml;
Public void SetFunc (object win, string func)
{
IHTMLWindow2 htmlWin = (win as IHTMLWindow2 );
If (htmlWin = null | string. IsNullOrEmpty (func ))
{
MessageBox. Show ("value assignment error. ");
}
Else
{
// The following is the call method. Because it is only an example, it is directly put in the SetFunc method. In actual development, you can put it in the appropriate places as needed.
// I provide two methods for calling: 1. reflection; 2. JS code syntax.
// You can use the appropriate method based on your knowledge. Both methods have the same effect.
 
 
// Method 1.
HtmlWin. GetType (). InvokeMember (func,
BindingFlags. Instance | BindingFlags. InvokeMethod | BindingFlags. Public,
Null, htmlWin, new object [] {"parameter "});
 
// Method 2.
String jsCode = string. Format ("{0} ('{1}')", func, "parameter ");
HtmlWin.exe cScript (jsCode, "jscript ");
 
 
}
}



From Sank Orange's column

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.