分类: C/C++
2008-04-01 21:10:43
概览
问题的产生
实现 IDocHostUIHandler 接口[
uuid(47F05070-FD66-45cc-AD99-74260F94A16B)
]
library MsHtmHstInterop
{
import "MsHtmHst.idl";
enum tagDOCHOSTUIDBLCLK;
enum tagDOCHOSTUIFLAG;
enum tagDOCHOSTUITYPE;
interface ICustomDoc;
interface IDocHostShowUI;
interface IDocHostUIHandler;
interface IDocHostUIHandler2;
interface IHostDialogHelper;
};
在上述的这个 IDL 文件中,我已经包括了 MSHTML 所有的高级支持接口及其枚举类型。 midl MsHtmHstInterop.idl /tlb bin\MsHtmHstInterop.tlb接下来,我们再利用该 TLB 文件,通过 TLBImp 工具生成对应的 Interop 程序集。
tlbimp bin\MsHtmHstInterop.tlb /out:bin\MsHtmHstInterop.dll现在我们就可以在我们的 C# 程序里直接通过一个 using 语句使用这个程序集来访问这些接口了。
using MsHtmHstInterop; //节选自HtmlUI.cs
实现//节选自HtmlUI.cs中HtmlUIForm的constructor
public 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);
}
以上就是所有要做的了。当然地, form 必须实现 IDocHostUIHandler 接口的所有方法。
资源里的 HTML 文件 Sample1.htm HTML "Sample1.htm"之后它会被编译为一个 RES 文件,然后你再通过 /win32res 这个编译开关将该 RES 文件加入你的程序集中。
处理文档事件 //节选自HtmlUI.cs
private void WebBrowser_DocumentComplete(object sender,
AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent e)
{
// 获取文档对象
IHTMLDocument2 doc = (IHTMLDocument2)this.WebBrowser.Document;
// 获取按钮的一个引用reference
HTMLButtonElement button = (HTMLButtonElement)doc.all.item("theButton", null);
// 将事件处理器通过事件接口进行绑定
((HTMLButtonElementEvents2_Event)button).onclick +=
new HTMLButtonElementEvents2_onclickEventHandler(this.Button_onclick);
}
private bool Button_onclick(IHTMLEventObj e)
{
MessageBox.Show("Alert from the app: Received theButton.onclick!");
return true;
}
生成应用程序