【发布时间】:2021-08-17 03:13:02
【问题描述】:
我正在做一个简单的QueryInterface() 来获取IHTMLElement2 接口,但它总是以E_NOINTERFACE 失败。
更新:我需要获取body 元素的IHTMLElement2 接口,因为它有一个focus() 方法,因此我可以将焦点设置到正文。用IHTMLElement接口是做不到的。
任何想法为什么会出现此错误(或如何联系body->focus())?
WebBrowser1->Navigate(L"c:\\test.htm");
while (WebBrowser1->Busy) Application->ProcessMessages();
DelphiInterface<IHTMLDocument2> diDoc = WebBrowser1->Document;
if (diDoc) {
DelphiInterface<IHTMLElement2> diBodyElement;
// all good until this point
if (SUCCEEDED(diDoc->QueryInterface(IID_IHTMLElement2, reinterpret_cast<void**>(&diBodyElement))) && diBodyElement) {
// Never reaches this part - always E_NOINTERFACE when querying for IID_IHTMLElement2
diBodyElement->focus();
}
}
【问题讨论】:
-
您正在尝试在
Document本身中查询IHTMLElement2,但文档不是元素,它是元素的容器。您要准确访问哪个元素?通常,Document应该首先查询IHTMLDocument2(你正在做的)或更高,然后从那里开始,例如搜索IHTMLDocument2.all,或使用IHTMLDocument3.getElementById()等。或者,如果你想要<html>元素本身,那么你真的在寻找IHTMLDocument3.documentElement而不是 -
@RemyLebeau 感谢您的回复 - 它有帮助。我现在已经在答案中找到了自己的解决方案。
标签: interface c++builder twebbrowser mshtml