我们一直在使用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对象进行测试。

我提供了一些评论,以帮助您一路走好。这已在带有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简化了尺寸获取。]

10-07 22:27