3

Basically, I need to load any HTML document in WebBrowser control and allow users to visually select one or more HTML DOM elements (in order to get their XPaths, but that's another story).

Hovering an element will highlight it with color A, moving mouse out will restore it's visual state.

Clicking an element will highlight it with color B, clicking again previously clicked element will restore it's visual state.

To summarize, it should behave like the FireBug Inspect Element feature, or like the Dapp Factory Select Content feature.


A naive approach would be to use HtmlElement.Style property like in the example below, but obviously I can't because the element could already have the border style set in the same manner in which case it should not be removed on MouseLeave:

void Document_MouseOver(object sender, HtmlElementEventArgs e)
{
    // what if e.FromElement.Style already contains "border: solid 1px Red;" ?
    e.FromElement.Style = "border: solid 1px Red; " + e.FromElement.Style;
    [...]
}

Possibly I could achieve this by setting/unsetting a custom CSS class (like one would do using JavaScript in the same case), but HtmlElement does not seem to expose such a property, and how would I inject CSS class definitions anyway?

Update: it is actually possible to set the CSS class with HtmlElement.SetAttribute("className"); to inject CSS classes, check for example C#: Best way to inject CSS into MSHTML instance?.

Any ideas on how to achieve this are welcome. Thank you.

Community
  • 1
  • 1
Maxim Gueivandov
  • 2,370
  • 1
  • 20
  • 33

2 Answers2

4

IHighlightRenderingServices

Sheng Jiang 蒋晟
  • 15,125
  • 2
  • 28
  • 46
  • 1
    Great hint. Found out this prehistoric era code sample which allowed me to underline hovered elements (however, IHTMLRenderStyle does not expose any border-related property...): http://groups.google.com/group/microsoft.public.inetsdk.programming.mshtml_hosting/msg/7fe8bfea7dcd107b – Maxim Gueivandov Jan 23 '11 at 03:00
1

Trying to paint does not work with WebBrowser,I think the FormElement.Style that you already use is the way to go.

Felice Pollano
  • 32,832
  • 9
  • 75
  • 115