如何在C/ c++代码中使用HTMLElement类

How to use HTMLElement class in C/C++ code

本文关键字:HTMLElement 代码 c++      更新时间:2023-10-16

我们一直使用IHTMLElement和IHTMLElement2脚本接口来访问web对象及其属性。现在我们的情况是,想要知道元素占用的客户端区域,减去边框和滚动条占用的任何区域。我遇到了HTMLDocument类,它有一个方法ClientRectangle()。它的文档听起来和我们正在看的非常相似。如果可能的话,我真的不知道如何访问这个方法。

谁能让知道是否有可能创建这个HTMLDocument类的实例并访问其方法?

链接到我正在谈论的类的MSDN文档。http://msdn.microsoft.com/en-us/library/system.windows.forms.htmlelement.clientrectangle.aspx?cs-save-lang=1& cs-lang = cpp # code-snippet-1

由于您的问题有点广泛,我提供的解决方案有点大,但完整和经过测试。如果你已经有一个指向有效的IHTMLDocument或IHTMLElement对象的指针,你可以很容易地检索到元素的位置和尺寸。获取尺寸的唯一要求是文档必须附加到IHTMLWindow/IWebBrowser对象。我已经包含了一个独立函数,用于创建IWebBrowser和IHTMLDocument对象进行测试。

我已经附上了一些评论来帮助你。在Windows 7系统和Internet Explorer 9上使用Visual Studio 2010进行了测试。这是我从示例中得到的结果集:

矩形= x=8 y=89宽=992高=31

=你好

内容

#include <comutil.h>    // _bstr_t
#include <mshtml.h>     // IHTMLDocument and IHTMLElement
#include <exdisp.h>     // IWebBrowser2
#include <atlbase.h>    // CComPtr
#include <string>
#include <iostream>
// Make sure we link in the support library!
#pragma comment(lib, "comsuppw.lib")
static const std::wstring
    exampleHtml(L"<body><html><br><br><p id="someid">hello</p></body>");
HRESULT CreateBrowserDocument(
    const std::wstring& html,
    CComPtr<IWebBrowser2>& returnBrowser,
    CComPtr<IHTMLDocument3>& returnDoc);

int main()
{
    ///////////////////////////////////////////////////////////////////////////
    // In order to get the position and dimension of an element we need to
    // have a browser object that owns the document we will work on. If you
    // create and use a IHTMLDocument object through CoCreateInstance it does
    // not have any rendering capabilities by default.
    ///////////////////////////////////////////////////////////////////////////
    HRESULT hr;
    hr = CoInitialize(NULL);
    if(SUCCEEDED(hr))
    {
        // Make sure these two items are scoped so CoUninitialize doesn't gump
        // us up.
        CComPtr<IWebBrowser2> browser;
        CComPtr<IHTMLDocument3> document;
        hr = CreateBrowserDocument(exampleHtml, browser, document);
        if(SUCCEEDED(hr))
        {
            CComPtr<IHTMLElement> element;
            ///////////////////////////////////////////////////////////////////
            // We grab the element by id to make the example easier. in some
            // cases you may need to iterate through all of the elements of the
            // document or parent element to find the one you want the
            // dimensions for.
            ///////////////////////////////////////////////////////////////////
            hr = document->getElementById(_bstr_t(L"someid"), &element);
            if(SUCCEEDED(hr) && element != NULL)
            {
                ///////////////////////////////////////////////////////////////
                // Now that we have the browser object, document object and the
                // element we want to get the dimensions for .... do it the
                // easy way.
                ///////////////////////////////////////////////////////////////
                _bstr_t contents;
                long left, top, width, height;
                // I skip the error checking here. Add it when you implement
                // your solution.
                hr = element->get_innerHTML(contents.GetAddress());
                hr = element->get_offsetLeft(&left);
                hr = element->get_offsetTop(&top);
                hr = element->get_offsetWidth(&width);
                hr = element->get_offsetHeight(&height);
                std::cout
                    << "Rect = "
                    << "x=" << left << " "
                    << "y=" << top << "  "
                    << "width=" << width << " "
                    << "height=" << height << std::endl
                    << "contents=" << contents << std::endl;
            }
        }
    }
    CoUninitialize();
    return 0;
}

// Here we create web browser and document objects. The additional browser
// object is required for layout management. I have taken a shortcut here and
// create an instance Internet Explorer instead. This allows the browser to
// create and initializes a HTMLDocument when we call IWebBrowser::Navigate.
HRESULT CreateBrowserDocument(
    const std::wstring& html,
    CComPtr<IWebBrowser2>& returnBrowser,
    CComPtr<IHTMLDocument3>& returnDoc)
{
    CComPtr<IHTMLDocument2> document;
    CComPtr<IWebBrowser2> browser;
    HRESULT hr;
    hr = CoCreateInstance(
        CLSID_InternetExplorer,
        NULL,
        CLSCTX_SERVER,
        IID_IWebBrowser2,
        reinterpret_cast<void**>(&browser));
    if(SUCCEEDED(hr))
    {
        // The browser does not contain a document by default. We can force
        // one though by navigating to the `about` page. This is fast and
        // does not require an internet connection.
        VARIANT empty;
        VariantInit(&empty);
        hr = browser->Navigate(
            _bstr_t(L"about:"), &empty, &empty, &empty, &empty);
        //  Wait for the load.
        if(SUCCEEDED(hr))
        {
            READYSTATE state;
            while(SUCCEEDED(hr = browser->get_ReadyState(&state)))
            {
                if(state == READYSTATE_COMPLETE) break;
            }
        }
        // The browser now has a document object. Grab it.
        if(SUCCEEDED(hr))
        {
            CComPtr<IDispatch> dispatch;
            hr = browser->get_Document(&dispatch);
            if(SUCCEEDED(hr) && dispatch != NULL)
            {
                hr = dispatch.QueryInterface<IHTMLDocument2>(&document);
            }
            else
            {
                hr = E_FAIL;
            }
        }
    }
    if(SUCCEEDED(hr))
    {
        // Since the about page is empty we can just write out our test HTML
        // directly to the document. Takes some effort since we need to
        // use a safe array to send it to the document.
        SAFEARRAY *pString = SafeArrayCreateVector(VT_VARIANT, 0, 1);
        if (pString != NULL)
        {
            VARIANT *param;
            hr = SafeArrayAccessData(pString, reinterpret_cast<void**>(&param));
            if(SUCCEEDED(hr))
            {
                const _bstr_t htmlString(SysAllocString(html.c_str()));
                param->vt = VT_BSTR;
                param->bstrVal = htmlString;
                hr = SafeArrayUnaccessData(pString);
                if(SUCCEEDED(hr))
                {
                    hr = document->write(pString);
                    document->close();
                }
            }
            SafeArrayDestroy(pString);
        }
        //  Set the return values
        if(SUCCEEDED(hr) && document != NULL && browser != NULL)
        {
            CComPtr<IHTMLDocument3> temp;
            hr = document.QueryInterface<IHTMLDocument3>(&temp);
            if(SUCCEEDED(hr) && temp != NULL)
            {
                document = temp;
            }
            else 
            {
                hr = E_FAIL;
            }
            CComPtr<IHTMLDocument3> tempDoc;
            if(SUCCEEDED(hr))
            {
                hr = document.QueryInterface<IHTMLDocument3>(&tempDoc);
            }
            if(SUCCEEDED(hr) && tempDoc != NULL)
            {
                returnDoc = tempDoc;
                returnBrowser = browser;
            }
        }
        else if(!FAILED(hr))
        {
            hr = E_FAIL;
        }
    }
    return hr;
}



[EDIT 1: remove不必要的call to IWebBrowser::put_RegisterAsBrowser]

[EDIT 2:用IHTMLElement::get_OffsetXXX代替IHTMLElement::get_clientXXX简化尺寸获取]