如何使用GetDHtmlDocument()在MFC编程? [英] How to use GetDHtmlDocument() in MFC programming?
问题描述
我想尝试使用
HRESULT
GetDHtmlDocument(IHTMLDocument2
** pphtmlDoc);
在MFC编程中的功能。
基本上, HTML视图对话框应用程序(C + + W / MFC)给定不同的配置(加载输入)。
所以我把下面的代码放在OnInitDialog b
$ b
BOOL CSampleProgramDlg :: OnInitDialog()
{
CDHtmlDialog :: OnInitDialog();
ASSERT((IDM_ABOUTBOX& 0xFFF0)== IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX< 0xF000);
CMenu * pSysMenu = GetSystemMenu(FALSE);
如果(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);
//我的代码从这里开始....
HRESULT hr = S_OK;
IHTMLDocument2 * pphtmlDoc;
// MessageBox(_T(如果我让这个MessageBox弹出,代码工作正常。
hr = GetDHtmlDocument(& pphtmlDoc);
IHTMLElement * e;
hr = getElement(_T(someElement),& e);
if(SUCCEEDED(hr))
e-> put_innerHTML(_T(< h1> someLoadingInputWillGoHereLater< / h1>));
//我的代码在这里结束.....
return TRUE;
}
正如我在上面的代码中所说的,如果我让Messagebox弹出但是如果我注释掉Messagebox,GetDHtmlDocument()返回E_NOINTERFACEHRESULT,并且如果我注释掉了Messagebox,则返回E_NOINTERFACEHRESULT。它使代码不工作。
我只能猜测它可能是focus问题。我不能弄清楚确切的原因。
所以我请求你的帮助。 =
您致电 GetDHtmlDocument()
和 GetElement()
将返回 E_NOINTERFACE
。
您在 CDHtmlDialog :: OnInitDialog()
中执行时不始终保证完全加载html文档。
而应该覆盖 CSampleProgramDlg
中的 CDHtmlDialog :: OnDocumentComplete()
void CSampleProgramDlg :: OnDocumentComplete()函数将在文档加载时调用一个回调函数。
LPDISPATCH pDisp,
LPCTSTR szUrl
)
{
//您可以安全地获取文档和元素
}
您对 MessageBox()
的调用可能会以某种方式触发要加载的文档虽然我不是100%的肯定。
希望有帮助。
I am trying to use
HRESULT GetDHtmlDocument(IHTMLDocument2 **pphtmlDoc);
function in MFC programming.
Basically, I am trying to render GUI in a HTML View Dialog application (C++ w/ MFC) given different configuration (loading input).
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;
}
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".
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. =(
Your calling to GetDHtmlDocument()
and GetElement()
will return E_NOINTERFACE
.
According to my knowledge, you are not always guaranteed to have html document completely loaded when you're performing in CDHtmlDialog::OnInitDialog()
.
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
}
Your calling to MessageBox()
may somehow trigger the document to be loaded in advance. Though I'm not 100% sure for it.
Hope that helps.
这篇关于如何使用GetDHtmlDocument()在MFC编程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!