Use the Advanced Support interface of mshtml in C #

Source: Internet
Author: User
Http://www.vckbase.com/document/viewdoc? Id = 1018

Use the Advanced Support interface of mshtml in C #

Original: Nikhil dabas

Translation: Abbey

Source: Using mshtml advanced hosting interfaces, codeproject

DownloadSource code: Idochostuihandler (79kb)

Summary
This article demonstrates how to use advanced mshtml interfaces such as idochostuihandler.

Overview
This article will show you how to use the advanced support interfaces of mshtml in. net, especially the idochostuihandler interface. This series of interfaces can help you adjust Microsoft's Web browser control user interface. You can display the customized context menu in the Web browser. I will show you how to use the idochostuihandler interface instead of rewriting the same interface. Additionally, I will demonstrate how to receive events from the document element on the web page.

Problem generation
It is very easy to place a web browser control on a form to implement complete browser functions. But in your applicationProgramYou may want to fully control the interaction between users and web control. For example, when your application uses a DHTML-based (Dynamic HTML) user interface, the standard context menu of IE is really not suitable for it. An interface called idochostuihandler can provide you with the ability to change all of these.

Implement the idochostuihandler Interface
Your application must implement an idochostuihandler interface, and then use the icustomdoc: setuihandler () method to tell mshtml that you have implemented and want to use this interface. The idochostuihandler interface is included in the Internet Development SDK of Platform SDK and defined by mshtmlhst. IDL.
One of the methods I have seen for introducing these interfaces in C # Is to manually write the definition of the interfaces in the source code. I want to use another method: Create a type library containing the required interfaces to declare it, and then create an InterOP Assembly through the tlbimp tool. In this way, you do not need to define the parameter sending process.
First, create an IDL file that contains the required interface declaration. Of course, you also need to provide a uuid for the generated Type Library. The entire content of this IDL file is as follows (mshtmhstinterop. IDL), and we only need some of the interfaces:

[UUID (47f05070-fd66-45cc-ad99-7417f94a16b)] library mshtmhstinterop {import "mshtmhst. IDL "; Enum handle; Enum tagdochostuiflag; Enum tagdochostuitype; interface icustomdoc; interface idochostshowui; interface idochostuihandler; interface idochostuihandler2; interface ihostdialoghelper ;};

In the above IDL file, I have included all the advanced support interfaces of mshtml and their enumeration types.
The next step is to use this IDL file to generate the corresponding Type Library TLB file through the midl Platform SDK tool.

 
Midl mshtmhstinterop. IDL/TLB bin \ mshtmhstinterop. TLB

Next, we use the TLB file to generate the corresponding InterOP assembly using the tlbimp tool.

 
Tlbimp bin \ mshtmhstinterop. TLB/out: bin \ mshtmhstinterop. dll

Now we can directly use this Assembly to access these interfaces through a using statement in our C # program.

 
Using mshtmhstinterop; // Excerpt from htmlui. CS

Implementation
In this demo, I will place a web browser control on the form, and implement the idochostuihandler interface by the Form class.
To connect the personal Implementation of idochostuihandler to mshtml, we first need to obtain the icustomdoc interface, and then call the personally implemented idochostuihandler interface as a parameter to call the setuihandler () method of the icustomdoc interface.

 
// Excerpt from htmlui. constructorpublic htmluiform () {initializecomponent (); this. webbrowser. documentcomplete + = new dwebbrowserevents2_documentcompleteeventhandler (this. webbrowser_documentcomplete); object flags = 0; object targetframe = string. empty; object postdata = string. empty; object headers = string. empty; this. webbrowser. navigate ("about: blank", ref flags, ref targetframe, ref postdata, ref headers); icustomdoc cdoc = (icustomdoc) This. webbrowser. document; cdoc. setuihandler (idochostuihandler) This); this. webbrowser. navigate (@ "res: // htmlui.exe/sample1.htm", ref flags, ref targetframe, ref postdata, ref headers );}

The above is all we have to do. Of course, form must implement all the methods of the idochostuihandler interface.

HTML files in resources
You may have noticed that a resource is used in my code: protocol. This is a simple method to package HTML files and their support files into your EXE files. This method has several advantages: you cannot easily change your application interface, and you do not have to package other files into your installation package. All you need to do is create a resource definition file: htmlui. RC

 
Sample1.htm html "sample1.htm"

It will be compiled into a res file, and then you can add the res file to your Assembly through the/win32res compilation switch.

Process Document events
If your application has a DHTML-based user interface, you need to capture the events sent by elements on the page to improve its functionality. When you run this demo program, you will see that when you click the button on the page, a message dialog box will pop up, which is triggered from the C # application, instead of scripts on the page. The following is the code:

// Excerpt from htmlui. csprivate void webbrowser_documentcomplete (Object sender, axshdocvw. dwebbrowserevents2_documentcompleteevent e) {// obtain the Document Object ihtmldocument2 Doc = (ihtmldocument2) This. webbrowser. document; // a reference to the get button referencehtmlbuttonelement button = (htmlbuttonelement) Doc. all. item ("thebutton", null); // bind the event processor through the event interface (htmlbuttonelementevents2_event) button ). onclick + = new htmlbuttonelementevents2_onclick Eventhandler (this. button_onclick);} private bool button_onclick (ihtmleventobj e) {MessageBox. Show ("alert from the app: received thebutton. onclick! "); Return true ;}

Generate an application
I have included a make file in the source code package, so that you can use the nmake command line tool to generate this demo program in this article. Note that the tlbimp mshtml. TLB step may take more time to complete.

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.