I need to find out the position of selected text in WebBrowser control. I can acquire IHTMLTxtRange through
IHTMLDocument2 htmlDocument = BookReaderWeb.Document as IHTMLDocument2;
IMarkupServices ms = (IMarkupServices) htmlDocument;
IHTMLSelectionObject selection = htmlDocument.selection;
if (selection == null) return;
IHTMLTxtRange range = selection.createRange() as IHTMLTxtRange;
but I have no idea how to obtain range position relative to full text (not html). thanks.
UPDATE
As Noseratio suggested, I implemented the following code:
IHTMLDocument2 htmlDocument = BookReaderWeb.Document as IHTMLDocument2;
var str = htmlDocument.body.outerText;
IMarkupServices ms = (IMarkupServices) htmlDocument;
IHTMLSelectionObject selection = htmlDocument.selection;
if (selection == null) return;
IHTMLTxtRange range = selection.createRange() as IHTMLTxtRange;
dynamic body = htmlDocument.body;
var bodyRange = body.createTextRange();
bodyRange.moveToElementText(body);
var bodyText = bodyRange.text;
var counter = 0;
while (bodyRange.compareEndPoints("StartToStart", range) != 0)
{
bodyRange.moveStart("character", 1);
++counter;
}
MessageBox.Show(str.Substring(counter, 20));
but it doesn't give correct result. The position is a few characters misplaced forward then it should be. It happens only on large html files, it works perfectly on smaller ones. Looks like dom api interprets some kind of tags as characters maybe..?