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.
精彩评论