1

我将 WebKit 作为 COM 对象嵌入到纯 WinApi 应用程序中。我想通过获取 WM_WINDOWPOSCHANGED 来调整 WebKit 窗口的大小。

LRESULT CALLBACK CBrowserView::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    typedef LRESULT (CBrowserView::*PFNPROC)(WPARAM wParam, LPARAM lParam);
    PFNPROC pfnProc;
    switch (uMsg)
    {
        case WM_WINDOWPOSCHANGED:
            pfnProc = &CBrowserView::DoWindowPosChanged;
...


// CBrowserView::DoWindowPosChanged

LRESULT CBrowserView::DoWindowPosChanged(WPARAM wParam, LPARAM lParam)
{
    _ASSERT(m_hWnd);

    WINDOWPOS* pwp = (WINDOWPOS*)lParam;
    {
        if (pwp->cx != m_lClientWidth || pwp->cy != m_lClientHeight)
        {
            m_lClientWidth = pwp->cx;
            m_lClientHeight = pwp->cy;

            if (m_pIWebBrowser)
            {
                {
                    RECT rcClient;
                    rcClient.left = 0;
                    rcClient.top = 0;
                    rcClient.right = m_lClientWidth;
                    rcClient.bottom = m_lClientHeight;

                    HRESULT hr = m_pIWebBrowser->initWithFrame(rcClient, 0, 0);
                    if (FAILED(hr))
                        return E_FAIL;

                    hr = m_pIWebBrowser->mainFrame(&m_mainWebFrame);
                    if (FAILED(hr))
                        return E_FAIL;

                    BSTR urlBstr = SysAllocString(m_pszFile);;
                    WebKitToUrl(urlBstr, true);
                }
            }
        }
    }

    return 0;
}

但是这只工作一次,在 OnCreate 对话框中,小部件填充了这个客户区。在调整大小失败时,m_pIWebBrowser->initWithFrame(rcClient, 0, 0) 返回 E_FAIL。

还有另一种设置 m_pIWebBrowser 大小的方法吗?

4

1 回答 1

0

1.. 应该找到小部件 HWND。

...

hr = m_pIWebBrowser->QueryInterface(IID_IWebViewPrivate, reinterpret_cast<void**>(&webViewPrivate));

if (FAILED(hr))
    return E_FAIL;

hr = webViewPrivate->viewWindow(reinterpret_cast<OLE_HANDLE*>(&m_hudWnd));
if (FAILED(hr) || !m_hudWnd)
    return E_FAIL;

if (webViewPrivate)
{
    webViewPrivate->Release();
    webViewPrivate = NULL;
}

...

2.. 使用标准 WinApi 移动|调整窗口大小。

LRESULT CBrowserView::DoWindowPosChanged(WPARAM wParam, LPARAM lParam)
{
    _ASSERT(m_hWnd);

    WINDOWPOS* pwp = (WINDOWPOS*)lParam;
    {
        if (pwp->cx != m_lClientWidth || pwp->cy != m_lClientHeight)
        {
            m_lClientWidth = pwp->cx;
            m_lClientHeight = pwp->cy;

            if (m_pIWebBrowser && m_hudWnd)
            {
                RECT rcClient;
                rcClient.left = 0;
                rcClient.top = 0;
                rcClient.right = m_lClientWidth;
                rcClient.bottom = m_lClientHeight;

                ::MoveWindow(m_hudWnd, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom, TRUE);
            }
        }
    }
    return 0;
}
于 2013-03-06T06:12:47.823 回答