本文介绍了如何扩展CAxHostWindow?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ATL用于COM ActiveX主机的标准类CAxHostWindow不支持接口IDocHostUIHandler2。如何扩展CAxHostWindow类为IDocHostUIHandler2支持?

Standard class CAxHostWindow that used by ATL for COM ActiveX hosting does not support interface IDocHostUIHandler2. How can i to extend CAxHostWindow class for IDocHostUIHandler2 supporting?

推荐答案

实际上,有一种方式自定义 CAxHostWindow ,而不修改它。例如,我想在客户端站点对象上实现 IOleCommandTarget 。棘手的部分是重写主机窗口的创建。以下说明如何完成:

Actually, there's a way to customize CAxHostWindow without modifying it. E.g, I wanted to implement IOleCommandTarget on the client site object. The tricky part was to override the creation of the host window. Here's how it can be done:

class ATL_NO_VTABLE CWebBrowserHost :
  public CAxHostWindow,
  public IOleCommandTarget
{
public:
  static CWndClassInfo& GetWndClassInfo()
  {
    static CWndClassInfo wc =
    {
      { sizeof(WNDCLASSEX), 0, StartWindowProc,
        0, 0, 0, 0, 0, (HBRUSH)(COLOR_WINDOW + 1), 0, NULL, 0 },
      NULL, NULL, IDC_ARROW, TRUE, 0, _T("")
    };
    return wc;
  }

  DECLARE_PROTECT_FINAL_CONSTRUCT()

  DECLARE_NO_REGISTRY()
  DECLARE_POLY_AGGREGATABLE(CWebBrowserHost)
  DECLARE_GET_CONTROLLING_UNKNOWN()

  BEGIN_COM_MAP(CWebBrowserHost)
    COM_INTERFACE_ENTRY(IDocHostUIHandler)
    COM_INTERFACE_ENTRY(IOleCommandTarget)
    COM_INTERFACE_ENTRY_CHAIN(CAxHostWindow)
  END_COM_MAP()

  HWND Create(
    _In_opt_ HWND hWndParent,
    _In_ _U_RECT rect = NULL,
    _In_opt_z_ LPCTSTR szWindowName = NULL,
    _In_ DWORD dwStyle = 0,
    _In_ DWORD dwExStyle = 0,
    _In_ _U_MENUorID MenuOrID = 0U,
    _In_opt_ LPVOID lpCreateParam = NULL)
  {
    ATOM atom = GetWndClassInfo().Register(&m_pfnSuperWindowProc);
    if (!atom)
      return NULL;

    // Allocate the thunk structure here, where we can fail gracefully.
    BOOL result = m_thunk.Init(NULL,NULL);
    if (result == FALSE)
    {
      SetLastError(ERROR_OUTOFMEMORY);
      return NULL;
    }

    _AtlWinModule.AddCreateWndData(&m_thunk.cd, this);

    dwStyle = GetWndStyle(dwStyle);
    dwExStyle = GetWndExStyle(dwExStyle);

    // set caption
    if (szWindowName == NULL)
      szWindowName = GetWndCaption();

    return CWindow::Create((LPCTSTR)atom, hWndParent, rect, szWindowName, dwStyle, dwExStyle, MenuOrID, lpCreateParam);
  }

  // IOleCommandTarget methods
  // ...

}

以下是使用方法:

CComPtr<CWebBrowserHost> m_webBrowserHost;

//...

// Create a child AX host window. 
CComObject<CWebBrowserHost>* pHost = NULL;
hr = CComObject<CWebBrowserHost>::CreateInstance(&pHost);
if (FAILED(hr))
  return 0;
m_webBrowserHost = pHost;

RECT rect;
GetClientRect(&rect);
m_webBrowserHost->Create(m_hWnd, rect, NULL, WS_CHILD | WS_VISIBLE);
if (m_webBrowserHost->m_hWnd == NULL)
  return 0;

// Create WebBrowser control
CComPtr<IUnknown> spControl;
hr = pHost->CreateControlEx(
  OLESTR("{8856F961-340A-11D0-A96B-00C04FD705A2}"), // WebBrowser Control CLSID
  m_webBrowserHost->m_hWnd, 
  NULL,
  &spControl, 
  DIID_DWebBrowserEvents2, 
  ((IUnknown*)(IDispEventImpl<1, CMainWindow, &DIID_DWebBrowserEvents2, &LIBID_SHDocVw, 0xFFFF, 0xFFFF>*)this)
);

if (FAILED(hr))
  return 0;

这篇关于如何扩展CAxHostWindow?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 02:40