[Translation document This article is suitable for senior readers have read 2837 times]
Using MSHTML in C # Originally: Nikhil Dabas Translation: Abbey Original Source Code: IDOchostuiHandler (79KB) Abstract This article shows you how to use the AdochostuiHandler's MSHTML's Advanced interface. Overview This article will show you how to use the MSHTML's advanced support interface under .NET, especially in the IDOCHOSTUIHANDLER interface. This series of interfaces can help you freely adjust Microsoft's Web Browser Control (web browsing) user interface. You can display your personal custom context menu in Web Browser. And I will show you how to use the IDOCHOSTUIHANDLER interface instead of rewriting a same interface. Additional, I will also demonstrate how to receive an event emitted in the document element in the web page. The problem of the problem will put a web browser on an Form to achieve a complete browser feature, which is a very easy thing. But in your application, maybe you want to more fully control the interaction between users and web controls. For example, when your application uses a DHTML (dynamic HTML) user interface, IE's standard context menu is unable to match it. And an interface called IDOchostuiHandler can provide you with the ability to change all of these. Implementing the IDOCHOSTUIHANDAL interface Your application must implement an IDOCHOSTUIHANDLER interface, then use the icustomDoc :: setuiHandler () method to tell MSHTML you have implemented and want to use this interface. The IDOCHOSTUIHANDLER interface is included in the Internet Development SDK of the Platform SDK and is defined by MSHTMLHST.IDL. One way I have seen in the C # is the definition of their interfaces in the source code. And I am ready to use another method: Create a type library containing the desired interface to declare it, then create an Interop assembly through the TLBIMP tool. This way you don't need to define the parameters of the parameter. First, create an IDL file that contains the desired interface declaration, and of course you need to provide a UUID to the generated type library. The entire contents of the IDL file are as follows (MSHTMHSTEROP.IDL), and we only need some of the interfaces: [
UUID (47F05070-FD66-45CC-AD99-74260F94A16B)
]
Library MSHTMHSTIOP
{
Import "mshtmhst.idl";
Enum tagdochostuidblclk;
ENUM Tagdochostuiflag;
Enum tagdochostuitype;
Interface icustomDoc;
Interface IDochostShowui;
Interface IDochostuiHandler;
Interface idochostuihandler2;
Interface IhostDialoghelper;
}
In the above IDL file, I already include all the advanced support interfaces of the MSHTML and their enumeration types. The next step is to use this IDL file, generate the corresponding type library TLB file via the MIDL this Platform SDK tool. MIDL MSHTMHSTINTEROP.IDL / TLB BIN / MSHTMHSTITITEROP.TLB Next, we use the TLB file to generate a corresponding Interop assembly via the TLBIMP tool. Tlbimp bin / mshtmhstinterop.tlb /out:bin/mshtmhstinterop.dll
Now we can access these interfaces directly through a USING statement in our C # program. USING MSHTMHSTINTEROP; // Extract 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 install the idochostuihandler's personal implementation to MSHTML, we first need to get the ICUSTOMDOC interface, and then call the idochostuihandler interface as a parameter, call the setuihandler () method of the icustomDoc interface. // Election of HtmluiForm in htmlui.cs constructor
Public htmluiform ()
{
InitializationComponent ();
this.webbrowser.documentComplete =
New dwebbrowserevents2_documentcompleteeventhandler (this.WebBrowser_DocumentComplete);
Object flags = 0;
Object targetframe = string.empty;
Object poster = 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);
THISWEBBROWSER.NAVIGATE (@ "res: //htmlui.exe/sample1.htm",
REF FLAGS,
Ref targetframe,
Ref postdata,
Ref headers;
}
The above is all to do. Of course, FORM must implement all methods of the IDochostuiHandler interface. HTML file in the resource maybe you noticed, use a resource in my code: protocol. This is a simple way to pack HTML files and other support files into your EXE file. There are also a few advantages in this way: users cannot easily change your application interface; you don't have to pack other files into your installation package. What you have to do is only created a resource definition file: htmlui.rc sample1.htm HTML "Sample1.htm" it will be compiled into a res file, then you will join your RES file by using the / win32res compilation switch. The program is concentrated. Handling Document Event If your application has a DHTML-based user interface, you need to capture events emitted in the page to make it more perfect. When you run my demo program, you will see: When you click the button in the page, you will pop up a message dialog, which is triggered from the C # application, not the script in the page. The following is its code: // Extract from HTMlui.csprivate Void WebBBRowser_DocumentComplete (Object Sender,
AXSHDOCVW.DWEBBBROWSEREVENTS2_DOCUMENTCOMPLETEEVENT E)
{
// Get a document object
IHTMLDocument2 doc = (htmldocument2) this.WebBrowser.document;
// Get a reference to the button Reference
HTMLButtonElement Button = (HTMLButtonElement) Doc.all.Item ("Thebutton", NULL);
// Bind the event processor through an event interface
((Htmlbuttonelementevents2_event) Button .onclick =
New htmlbuttonelementevents2_onclickeventhandler (this.button_onclick);
}
Private bool button_onclick (htmleventobj e)
{
Messagebox.show ("Alert from the app: received thebutton.onclick!");
Return True;
}
Generating an application I have included an MAKE file in the source package so you can generate this demo of this article through the NMAKE command line tool in the command line mode. Note that TLBIMP MSHTML.TLB can do more time to complete.