如何在 MFC 编程中使用 GetDHtmlDocument()?
我正在尝试使用
HRESULTGetDHtmlDocument(IHTMLDocument2**pphtmlDoc);
HRESULT GetDHtmlDocument(IHTMLDocument2 **pphtmlDoc);
MFC 编程中的函数.
function in MFC programming.
基本上,我试图在给定不同配置(加载输入)的 HTML 视图对话框应用程序(C++ w/MFC)中呈现 GUI.
Basically, I am trying to render GUI in a HTML View Dialog application (C++ w/ MFC) given different configuration (loading input).
所以我将以下代码放入 OnInitDialog() 函数中.
So I put following code in a OnInitDialog() function.
BOOL CSampleProgramDlg::OnInitDialog()
{
CDHtmlDialog::OnInitDialog();
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
SetIcon(m_hIcon, TRUE);
SetIcon(m_hIcon, FALSE);
// My code starts from here....
HRESULT hr = S_OK;
IHTMLDocument2 *pphtmlDoc;
//MessageBox(_T("If I let this MessageBox to pop-up, the code works fine."));
hr = GetDHtmlDocument(&pphtmlDoc);
IHTMLElement *e;
hr = GetElement(_T("someElement"), &e);
if (SUCCEEDED(hr))
e->put_innerHTML(_T("<h1>someLoadingInputWillGoHereLater</h1>"));
//My code ends here.....
return TRUE;
}
正如我在上面的代码中注释掉的,如果我让 Messagebox 弹出 ID="someElement" 的元素将打印出 "someLoadingInputWillGoHereLater".
As I commented out in the above code, if I let the Messagebox to pop-up the element with ID="someElement" will print out "someLoadingInputWillGoHereLater".
但如果我注释掉消息框,GetDHtmlDocument() 会返回E_NOINTERFACE"HRESULT,这会使代码无法工作.
But if I comment out the Messagebox, GetDHtmlDocument() returns "E_NOINTERFACE" HRESULT, and it makes the code not working.
我只能猜测它可能是焦点"问题.不过,我无法弄清楚确切的原因.
I can only guess it maybe "focus" issue. I cannot figure out the exact cause, though.
所以我请求你的帮助.=(
So I ask for your help. =(
推荐答案
您对 GetDHtmlDocument()
和 GetElement()
的调用将返回 E_NOINTERFACE
.
Your calling to GetDHtmlDocument()
and GetElement()
will return E_NOINTERFACE
.
据我所知,当您在 CDHtmlDialog::OnInitDialog()
中执行时,不总是保证完全加载 html 文档.
According to my knowledge, you are not always guaranteed to have html document completely loaded when you're performing in CDHtmlDialog::OnInitDialog()
.
相反,您应该在 CSampleProgramDlg
中覆盖 CDHtmlDialog::OnDocumentComplete()
.这是一个回调函数,将在加载文档时调用.然后您就可以评估文档了.
Instead, you should override CDHtmlDialog::OnDocumentComplete()
in CSampleProgramDlg
. It's a callback function that'll be called when document is loaded. You can assess the document then.
void CSampleProgramDlg::OnDocumentComplete(
LPDISPATCH pDisp,
LPCTSTR szUrl
)
{
// You can get the document and elements here safely
}
您对 MessageBox()
的调用可能会以某种方式触发文档提前加载.虽然我不是 100% 确定.
Your calling to MessageBox()
may somehow trigger the document to be loaded in advance. Though I'm not 100% sure for it.
希望对您有所帮助.
相关文章