请问IHTMLElement 的引用空间是什么?
为什么我下面这段代码 会提示,IHTMLElement 缺少引用空间。
/// <summary>
/// 得到元素的位置
/// </summary>
/// <param name="elem">元素</param>
/// <returns></returns>
public static Rectangle GetElementRect(IHTMLElement body, IHTMLElement elem)
{
int x, y, w, h;
x = y = w = h = 0;
// 计算元素本身的位置
IHTMLElement2 elem2 = elem as IHTMLElement2;
IHTMLRect elemRect = elem2.getBoundingClientRect();
x = elemRect.left;
y = elemRect.top;
w = elemRect.right - elemRect.left;
h = elemRect.bottom - elemRect.top;
// TODO: 计算顶端htmlElem(docElem)的位置,一般不用计算,其位置应该为(0,0,xx,xx)
// 计算父亲iframes
if (body.document != elem.document)
{
List<IHTMLDOMNode> frames = new List<IHTMLDOMNode>();
_getEleParentFrames(body as IHTMLDOMNode, elem as IHTMLDOMNode, frames);
foreach (IHTMLDOMNode f in frames)
{
IHTMLElement2 frame2 = f as IHTMLElement2;
IHTMLRect frameRect = frame2.getBoundingClientRect();
x += frameRect.left;
y += frameRect.top;
}
}
Rectangle ret = new Rectangle();
ret.X = x;
ret.Y = y;
ret.Width = w;
ret.Height = h;
return ret;
}
------解决方案--------------------
在VS项目中添加Com对象引用:Microsoft Html Object Library
using mshtml;
http://longkm.blog.163.com/blog/static/116662640201203033841286/