使用 C# 获取网页浏览器控件中 HTML 元素的绝对位置
我想知道是否可以使用 C# 获取我在 web 浏览器控件中加载的特定 HTML 元素的绝对位置。
我尝试了.Net 提供的几乎所有选项。 他们都没有给我正确的位置。他们都给我 0 作为 Y 坐标..该元素绝对不在 0 中..
有人有任何解决方案或想法来解决这个问题吗?
I was wondering if its possible to get the absolute position of specific HTML element I have loaded in webbrowser control with C#.
I tried almost all of the options that .Net provides..
none of them give me the correct position. all of them give me 0 for Y coordinate.. the element is definitely is not in 0..
does anybody have any solution or idea to solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这是我到目前为止得到的解决方案:
// 将网络浏览器的大小设置为与图像大小相同
int 宽度、高度;
宽度 = webBrowser1.Document.Images[0].ClientRectangle.Width;
height = webBrowser1.Document.Images[0].ClientRectangle.Height;
现在这段代码可以完美运行..但是计算偏移量时存在问题。我需要计算元素的 offsetparent,然后计算 offsetparent 的 offsetparent 等。我需要动态地执行此操作,而不是一一添加它。我不知道该怎么做。有什么想法吗?
编辑:
这是我的最后一个也是最终版本,它适用于任何 html 元素,它会找到我想要的任何元素的绝对位置..
然后使用该位置:
完成!
here is the solution I got so far:
// set the size of our web browser to be the same size as the image
int width, height;
width = webBrowser1.Document.Images[0].ClientRectangle.Width;
height = webBrowser1.Document.Images[0].ClientRectangle.Height;
now this code works perfectly.. but there is an issue with calculating the offsets. I need to calculate the offsetparent of the element then calculate the offsetparent of the offsetparent etc.. I need to do that dynamically not adding it one by one.. I don't know how to do that. any ideas?
EDIT:
here is my last and final version and it works with any html element it will find the absolute position of any element I want..
then use the position with:
done!
我喜欢以前的答案,但迭代父对象两次并不是很有效。请记住 - 您在这里使用 COM/ActiveX。这工作得更快:
然后
I like previous answers but iterating through parent objects twice is not very effective. Remember - you're working with COM/ActiveX here. This works much faster:
and then
谢谢,它就像一个魅力。我不得不将其重写为VB,只是想分享解决方案:
Thank you, it works like a charm. I had to rewrite it as VB, and just want to share the solution:
有一种直接获取坐标的方法。 IHTMLElement2 具有 getBoundingClientRect 方法,可以为您提供元素的坐标。
There is a direct way to get the coordinates. IHTMLElement2 has the method
getBoundingClientRect
that gives you the coordinates of the element.只是根据我的需要分享一个稍微不同的实现来获取绝对定位矩形:
Just sharing a slightly different implementation based on my needs to get the absolute positioning rectangle:
不知道为什么,但 offsetparent 并不总是返回(IE11 的情况,地图对象)
当未提供parentOffset时,我必须在循环中添加parentElement
并且您需要添加IE浏览器位置,菜单空间和边框...
Not sure why but offsetparent is not allways returned (case of IE11, map object)
I had to add parentElement in the loop when parentOffset is not provided
And you need to add IE browser position, the menu space and borders ...
对我来说最干净的方法如下:
HtmlElement elem = webBrowser.Document.GetElementById(idElement);
IHTMLRect 矩形 = ((IHTMLElement2) elem.DomElement).getBoundingClientRect();
// rect.top 和 rect.left 代表绝对坐标。
The cleanest way that works for me is the following:
HtmlElement elem = webBrowser.Document.GetElementById(idElement);
IHTMLRect rect = ((IHTMLElement2) elem.DomElement).getBoundingClientRect();
// rect.top and rect.left represent absolute coordinates.