//Explorer is Object of SHDocVw.WebBrowserClass
HTMLDocument htmlDoc = (HTMLDocument)this.Explorer.IWebBrowser_Document;
//inject Script
htmlDoc啷parentWindow.execScript("alert('hello world !!')", "javascript");
namespace MyToolBar
{
publicdelegatevoid DOMEvent(mshtml.IHTMLEventObj e);
public classDOMEventHandler
{
public DOMEvent Handler;
DispHTMLDocument Document;
public DOMEventHandler(DispHTMLDocument doc)
{
this.Document = doc;
}
[DispId(0)]
public voidCall()
{
Handler(Document.parentWindow.@event);
}
}
}
上面的类可以用作:
//Explorer is Object of SHDocVw.WebBrowserClass
HTMLDocument htmlDoc =(HTMLDocument)this.Explorer.IWebBrowser_Document;
DispHTMLDocument doc = (DispHTMLDocument ) htmlDoc
DOMEventHandler onmousedownhandler = newDOMEventHandler (doc);
onmousedownhandler.Handler += new DOMEvent(Mouse_Down);
doc.onmousedown = onmousedownhandler;
//mouse Down Can be like this
publicvoid Mouse_Down(mshtml.IHTMLEventObje)
{
MessageBox.Show(e.srcElement.tagName);
}
//Explorer is Object of SHDocVw.WebBrowserClass
HTMLDocument htmlDoc = (HTMLDocument)this.Explorer.IWebBrowser_Document;
//get all the images of document
IHTMLElementCollection imgs = htmlDoc.images;
foreach (HTMLImgClass imgTag in imgs)
{
MessageBox.Show(imgTag.src);
}
使用的getElementsByTagName
HTMLDocument htmlDoc = (HTMLDocument)this.Explorer.IWebBrowser_Document;
//get all the images of document
IHTMLElementCollection imgs = htmlDoc.getElementsByTagName("img");
foreach (HTMLImgClass imgTag in imgs)
{
MessageBox.Show(imgTag.src);
}
.
.
try
{
object w;
sp.QueryService(
ref guid,
ref riid,
out w );
Explorer = (WebBrowserClass)Marshal.CreateWrapperOfType(
w as IWebBrowser,
typeof(WebBrowserClass)
);
OnExplorerAttached(EventArgs.Empty);
// I added this
Explorer.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(ExplorerOnDocumentComplete);
[Guid("AE07141B-45D4-4a98-5F68-0333EA26E115")] //your GUID goes here
[BandObject("YourNewChildClass", BandObjectStyle.Horizontal | BandObjectStyle.ExplorerToolbar, HelpText = "YourNewChildClass")]
[ComVisible(true), ClassInterface(ClassInterfaceType.None)]
publicpartialclass YourNewChildClass: BandObject//Class from Pavel Zolnikov example
{
.
.
. //declaration and other functions e.g constructor etc
.
void YourNewChildClass_Load(object sender, EventArgs e) //load function of your class ..this is similar to Form Load event
{
try
{
this.Explorer.DownloadComplete += new DWebBrowserEvents2_DownloadCompleteEventHandler(Explorer_DownloadComplete);
}
catch (Exception ex)
{
//Something was wrong
MessageBox.Show(ex.Message);
}
} //end of Load function..
}//end of YourNewChildClass