How to use GetDHtmlDocument() in MFC programming?(如何在 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.
希望对您有所帮助.
这篇关于如何在 MFC 编程中使用 GetDHtmlDocument()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 MFC 编程中使用 GetDHtmlDocument()?
基础教程推荐
- 从 std::cin 读取密码 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 使用从字符串中提取的参数调用函数 2022-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01