如何在MFC编程中使用GetDHtmlDocument()
How to use GetDHtmlDocument() in MFC programming?
我正在尝试使用
HRESULTGetDHtmlDocument (IHTMLDocument2* * pphtmlDoc);
函数。
基本上,我试图呈现GUI在一个HTML视图对话框应用程序(c++ w/MFC)给定不同的配置(加载输入)。
所以我把下面的代码在OnInitDialog()函数。
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;
}
正如我在上面代码中注释的那样,如果我让消息框弹出ID="someElement"的元素将打印出" someeloadinginputwillgoherelater "。
但是如果我注释掉Messagebox, GetDHtmlDocument()返回"E_NOINTERFACE" HRESULT,并且它使代码不工作。
我只能猜测可能是"焦点"的问题。但是我不能找出确切的原因。
所以我请求你的帮助。= (
调用GetDHtmlDocument()
和GetElement()
将返回E_NOINTERFACE
。
据我所知,你是不是总是保证有html文档完全加载时,你在CDHtmlDialog::OnInitDialog()
执行。
CSampleProgramDlg
中重写CDHtmlDialog::OnDocumentComplete()
。它是一个回调函数会在文档被加载时被调用。你到时可以评估文件。
void CSampleProgramDlg::OnDocumentComplete(
LPDISPATCH pDisp,
LPCTSTR szUrl
)
{
// You can get the document and elements here safely
}
对MessageBox()
的调用可能会以某种方式触发文档提前加载。虽然我不是百分之百肯定。
希望对你有帮助。
相关文章:
- C++ MFC Libraries in Travis CI
- 从 bmp 文件数据创建 MFC CBitmap
- 如何在 MFC 中显示文件的所有行
- 目录删除 MFC 的问题
- MFC:如何设置CEdit框的焦点?
- MFC 中的抗锯齿
- 双击更改 mfc 中列表控件中的行的颜色
- C++MFC编辑框处理双击
- C++ MFC CObject 派生类摘要?
- 如何在 MFC 对话框中使用 OnDraw
- 在 1 个服务器 n 客户端套接字 C++ MFC 应用程序中更新数据的客户端
- MFC:你能在CDateTimeCtrl中改变自旋的加速度吗?
- MFC:我们如何轻松地从CTreeCtrl切换到CTreeView?
- __int64 CString 返回错误的值 - C++ MFC
- 如何在MFC中的静态文本控件上插入图标?
- 使用 MFC 的表/网格
- MFC 中的窗口消息管理:添加基类调用是否是强制性的?
- MFC/C++ ComboBox:禁用下拉列表关闭和打开(UI 冻结)的绘制
- C++ MFC 跟踪事件调用
- 如何在MFC编程中使用GetDHtmlDocument()