Home > Programming > Exposing SHDocVw

Exposing SHDocVw

A WebBrowserClass is just used for viewing pages embedded inside a .NET form and that’s about it, right?  The SHDocVw namespace can actually perform a ridiculous amount of neat features and override a lot of settings for something like an internet explorer add-on: getting the selected text of the browser, rewriting some of the content page (yes, you read that correctly; you can completely change the HTML of the page, including replacing text and/or images), setting the value of a selected field to something else, etc.

Want to set the user’s currently selected INPUT field to something?  Here’s a snippet for that:

try
{
mshtml.IHTMLDocument2 doc2 = (mshtml.IHTMLDocument2)Explorer.Document;

mshtml.IHTMLElement selobj = null;
mshtml.IHTMLTxtRange range = null;

if ((doc2 == null) || (doc2.activeElement == null))
return;

selobj = doc2.activeElement as mshtml.IHTMLElement;
if (selobj == null)
return;

selobj.setAttribute("value", "I just set your value.", 0);

return;
}
catch (Exception exc)
{
}

Want to handle some events, like the mouse selection changing?  Here’s a snippet for that:

m_iEvent.onselectionchange += new mshtml.HTMLDocumentEvents2_onselectionchangeEventHandler(m_iEvent_onselectionchange);

So far, I’ve been really impressed with the amount of flexibility (whether intended or not) is readily available in this class.  If anyone is interested, I can also demonstrate similar functionality using XUL/Javascript for FireFox add-ons.

  1. No comments yet.
  1. No trackbacks yet.