0

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

谁能告诉我是否可以创建这个 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

4

1 回答 1

3

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

我已经包含了一些评论,以帮助您一路走来。这是在带有 Internet Explorer 9 的 Windows 7 系统上使用 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;
}



[编辑1:删除不必要的调用IWebBrowser::put_RegisterAsBrowser]

[编辑 2:通过使用IHTMLElement::get_OffsetXXX而不是简化获取尺寸IHTMLElement::get_clientXXX]

于 2013-04-22T23:09:48.693 回答