以下代码实现WebBrowser获取指定字符串的坐标并将WebBrowser的滚动条定位到该坐标
public void FindKeyWord(string keyWord) { WebBrowser wb = new WebBrowser(); foreach (HtmlElement item in wb.Document.All) {
if (item.InnerText != null) { if (ClearChar(item.InnerText) == keyWord) { Point point = GetPoint(item); wb.Document.Window.ScrollTo(point.X, point.Y);//滚动条至指定位置 break; } } } }
public Point GetPoint(HtmlElement el) { Point pos = new Point(el.OffsetRectangle.Left, el.OffsetRectangle.Top); //循环获取父级的坐标 HtmlElement tempEl = el.OffsetParent; while (tempEl != null) { pos.X += tempEl.OffsetRectangle.Left; pos.Y += tempEl.OffsetRectangle.Top; tempEl = tempEl.OffsetParent; } return pos; } public string ClearChar(string str) { str = str.Replace(" ", null); str = str.Replace(" ", null); str = str.Replace(" ", null); str = str.Replace(" ", null); return str; }