class CAxWindow

来源:互联网 发布:柠檬绿茶淘宝店 编辑:程序博客网 时间:2024/06/06 04:07

typedef CAxWindowT<CWindow> CAxWindow;/////////////////////////////////////////////////////////////////////////////// CAxWindow - client side for an ActiveX host window#ifndef _ATL_NO_HOSTINGtemplate <class TBase /* = CWindow */>class CAxWindowT : public TBase{public:// ConstructorsCAxWindowT(HWND hWnd = NULL) : TBase(hWnd){AtlAxWinInit();}CAxWindowT< TBase >& operator=(HWND hWnd){m_hWnd = hWnd;return *this;}// Attributesstatic LPCTSTR GetWndClassName(){return _T(ATLAXWIN_CLASS);}// OperationsHWND Create(HWND hWndParent, _U_RECT rect = NULL, LPCTSTR szWindowName = NULL,DWORD dwStyle = 0, DWORD dwExStyle = 0,_U_MENUorID MenuOrID = 0U, LPVOID lpCreateParam = NULL){return CWindow::Create(GetWndClassName(), hWndParent, rect, szWindowName, dwStyle, dwExStyle, MenuOrID, lpCreateParam);}HRESULT CreateControl(LPCOLESTR lpszName, IStream* pStream = NULL, IUnknown** ppUnkContainer = NULL){return CreateControlEx(lpszName, pStream, ppUnkContainer);}HRESULT CreateControl(DWORD dwResID, IStream* pStream = NULL, IUnknown** ppUnkContainer = NULL){return CreateControlEx(dwResID, pStream, ppUnkContainer);}HRESULT CreateControlEx(LPCOLESTR lpszName, IStream* pStream = NULL, IUnknown** ppUnkContainer = NULL, IUnknown** ppUnkControl = NULL,REFIID iidSink = IID_NULL, IUnknown* punkSink = NULL){ATLASSERT(::IsWindow(m_hWnd));// We must have a valid window!// Get a pointer to the container object connected to this windowCComPtr<IAxWinHostWindow> spWinHost;HRESULT hr = QueryHost(&spWinHost);// If QueryHost failed, there is no host attached to this window// We assume that the user wants to create a new host and subclass the current windowif (FAILED(hr))return AtlAxCreateControlEx(lpszName, m_hWnd, pStream, ppUnkContainer, ppUnkControl, iidSink, punkSink);// Create the control requested by the callerCComPtr<IUnknown> pControl;if (SUCCEEDED(hr))hr = spWinHost->CreateControlEx(lpszName, m_hWnd, pStream, &pControl, iidSink, punkSink);// Send back the necessary interface pointersif (SUCCEEDED(hr)){if (ppUnkControl)*ppUnkControl = pControl.Detach();if (ppUnkContainer){hr = spWinHost.QueryInterface(ppUnkContainer);ATLASSERT(SUCCEEDED(hr)); // This should not fail!}}return hr;}HRESULT CreateControlEx(DWORD dwResID,  IStream* pStream = NULL, IUnknown** ppUnkContainer = NULL, IUnknown** ppUnkControl = NULL,REFIID iidSink = IID_NULL, IUnknown* punkSink = NULL){TCHAR szModule[MAX_PATH];DWORD dwFLen = GetModuleFileName(_AtlBaseModule.GetModuleInstance(), szModule, MAX_PATH);if( dwFLen == 0 )return AtlHresultFromLastError();else if( dwFLen == MAX_PATH )return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);CComBSTR bstrURL(OLESTR("res://"));HRESULT hr=bstrURL.Append(szModule);if(FAILED(hr)){return hr;}hr=bstrURL.Append(OLESTR("/"));if(FAILED(hr)){return hr;}TCHAR szResID[11];if (_stprintf_s(szResID, _countof(szResID), _T("%0d"), dwResID) == -1){return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);}hr=bstrURL.Append(szResID);if(FAILED(hr)){return hr;}ATLASSERT(::IsWindow(m_hWnd));return CreateControlEx(bstrURL, pStream, ppUnkContainer, ppUnkControl, iidSink, punkSink);}HRESULT AttachControl(IUnknown* pControl, IUnknown** ppUnkContainer){ATLASSERT(::IsWindow(m_hWnd));// We must have a valid window!// Get a pointer to the container object connected to this windowCComPtr<IAxWinHostWindow> spWinHost;HRESULT hr = QueryHost(&spWinHost);// If QueryHost failed, there is no host attached to this window// We assume that the user wants to create a new host and subclass the current windowif (FAILED(hr))return AtlAxAttachControl(pControl, m_hWnd, ppUnkContainer);// Attach the control specified by the callerif (SUCCEEDED(hr))hr = spWinHost->AttachControl(pControl, m_hWnd);// Get the IUnknown interface of the containerif (SUCCEEDED(hr) && ppUnkContainer){hr = spWinHost.QueryInterface(ppUnkContainer);ATLASSERT(SUCCEEDED(hr)); // This should not fail!}return hr;}HRESULT QueryHost(REFIID iid, void** ppUnk){ATLASSERT(ppUnk != NULL);if (ppUnk == NULL)return E_POINTER;HRESULT hr;*ppUnk = NULL;CComPtr<IUnknown> spUnk;hr = AtlAxGetHost(m_hWnd, &spUnk);if (SUCCEEDED(hr))hr = spUnk->QueryInterface(iid, ppUnk);return hr;}template <class Q>HRESULT QueryHost(Q** ppUnk){return QueryHost(__uuidof(Q), (void**)ppUnk);}HRESULT QueryControl(REFIID iid, void** ppUnk){ATLASSERT(ppUnk != NULL);if (ppUnk == NULL)return E_POINTER;HRESULT hr;*ppUnk = NULL;CComPtr<IUnknown> spUnk;hr = AtlAxGetControl(m_hWnd, &spUnk);if (SUCCEEDED(hr))hr = spUnk->QueryInterface(iid, ppUnk);return hr;}template <class Q>HRESULT QueryControl(Q** ppUnk){return QueryControl(__uuidof(Q), (void**)ppUnk);}HRESULT SetExternalDispatch(IDispatch* pDisp){HRESULT hr;CComPtr<IAxWinHostWindow> spHost;hr = QueryHost(__uuidof(IAxWinHostWindow), (void**)&spHost);if (SUCCEEDED(hr))hr = spHost->SetExternalDispatch(pDisp);return hr;}HRESULT SetExternalUIHandler(IDocHostUIHandlerDispatch* pUIHandler){HRESULT hr;CComPtr<IAxWinHostWindow> spHost;hr = QueryHost(__uuidof(IAxWinHostWindow), (void**)&spHost);if (SUCCEEDED(hr))hr = spHost->SetExternalUIHandler(pUIHandler);return hr;}};




原创粉丝点击