How to use GetDHtmlDocument() in MFC programming?

笑着哭i 提交于 2019-12-02 06:05:53

问题


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. =(


回答1:


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.



来源:https://stackoverflow.com/questions/5853607/how-to-use-getdhtmldocument-in-mfc-programming

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!