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.From开发者_如何学GoElement.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.
IHighlightRenderingServices
Trying to paint does not work with WebBrowser,I think the FormElement.Style that you already use is the way to go.
精彩评论