[Mmclibrary-cvs] mmclibrary/MMCFormsShim MMCFormsShimCtrl.cpp,1.2,1.3
Brought to you by:
imjimmurphy,
kachalkov
From: Lesley v. Z. <ex...@us...> - 2005-01-16 15:57:34
|
Update of /cvsroot/mmclibrary/mmclibrary/MMCFormsShim In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv962 Modified Files: MMCFormsShimCtrl.cpp Log Message: trying to understand and improve the formsshim code lots of comments added Index: MMCFormsShimCtrl.cpp =================================================================== RCS file: /cvsroot/mmclibrary/mmclibrary/MMCFormsShim/MMCFormsShimCtrl.cpp,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** MMCFormsShimCtrl.cpp 19 Oct 2004 16:46:38 -0000 1.2 --- MMCFormsShimCtrl.cpp 16 Jan 2005 15:57:21 -0000 1.3 *************** *** 7,24 **** // CMMCFormsShimCtrl HRESULT CMMCFormsShimCtrl::FinalConstruct() { ! Reset(); CComPtr<IUnknown> pAppDomainPunk; HRESULT hr = CorBindToRuntimeEx(NULL, NULL, STARTUP_LOADER_OPTIMIZATION_SINGLE_DOMAIN | STARTUP_CONCURRENT_GC, __uuidof(CorRuntimeHost), __uuidof(ICorRuntimeHost), (LPVOID*)&m_pHost); if(FAILED(hr)) return hr; hr = m_pHost->Start(); if(FAILED(hr)) return hr; hr = m_pHost->GetDefaultDomain(&pAppDomainPunk); if(FAILED(hr) || !pAppDomainPunk) return hr; hr = pAppDomainPunk->QueryInterface(__uuidof(mscorlib::_AppDomain), (LPVOID*)&m_pDefaultDomain); if(FAILED(hr) || !m_pDefaultDomain) --- 7,39 ---- // CMMCFormsShimCtrl + /**** + * this method is called immediately after constructing the CMMCFormsShimCtrl object + * We use it so that we can retrieve a status afterwards (return value) + ***/ HRESULT CMMCFormsShimCtrl::FinalConstruct() { ! // FIXME move to a constructor ! // Do stuff we should be doing in a constructor ! Reset(); + // Inititalize a COM interface pointer CComPtr<IUnknown> pAppDomainPunk; + + // make sure this can host the CLR HRESULT hr = CorBindToRuntimeEx(NULL, NULL, STARTUP_LOADER_OPTIMIZATION_SINGLE_DOMAIN | STARTUP_CONCURRENT_GC, __uuidof(CorRuntimeHost), __uuidof(ICorRuntimeHost), (LPVOID*)&m_pHost); if(FAILED(hr)) return hr; + + // I guess that if we can't start it we won't be able to do the next steps at all hr = m_pHost->Start(); if(FAILED(hr)) return hr; + + // get the default domain as a IUnknown if able hr = m_pHost->GetDefaultDomain(&pAppDomainPunk); if(FAILED(hr) || !pAppDomainPunk) return hr; + + // try to 'cast' the IUnkown to a Application Domain hr = pAppDomainPunk->QueryInterface(__uuidof(mscorlib::_AppDomain), (LPVOID*)&m_pDefaultDomain); if(FAILED(hr) || !m_pDefaultDomain) *************** *** 28,53 **** } ! ! LRESULT CMMCFormsShimCtrl::OnShowWindow(UINT, WPARAM, LPARAM, BOOL&) { CComPtr<IUnknown> pUnk; ! // Reset(); ! // FinalConstruct(); ! if (m_bstrAssemblyName.Length() && m_bstrClass.Length()) ! HRESULT hr = HostUserControl(m_bstrAssemblyName, m_bstrClass, &pUnk); return 0; } LRESULT CMMCFormsShimCtrl::OnSize(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& bHandled) { WORD wLength = LOWORD(lParam); WORD wHeight = HIWORD(lParam); ::MoveWindow(m_hWndForm, 0, 0, wLength, wHeight, TRUE); bHandled = FALSE; return 0; } ! void CMMCFormsShimCtrl::Reset() { --- 43,84 ---- } ! /**** ! * If we haven't already connected the hosted control, do it when the shim is shown. ! ***/ LRESULT CMMCFormsShimCtrl::OnShowWindow(UINT, WPARAM, LPARAM, BOOL&) { + // Inititalize a COM interface pointer CComPtr<IUnknown> pUnk; ! // Reset(); ! ! // FIXME MyVSUSerControlHostCtl is far cleaner ! // if the hosted control isn't connected, do it now ! if (m_bstrAssemblyName.Length() && m_bstrClass.Length()) ! HRESULT hr = HostUserControl(m_bstrAssemblyName, m_bstrClass, &pUnk); return 0; } + /**** + * When the shim is resized, resize the hosted user control. + ***/ LRESULT CMMCFormsShimCtrl::OnSize(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& bHandled) { + // retrieve the Length and Heigth from lParam WORD wLength = LOWORD(lParam); WORD wHeight = HIWORD(lParam); + + // move the window to the new location ::MoveWindow(m_hWndForm, 0, 0, wLength, wHeight, TRUE); + + // FIXME lying ? + // claim we didn't handle it (?) bHandled = FALSE; + return 0; } ! /**** ! * Reset certain variables back to constructing status ! ***/ void CMMCFormsShimCtrl::Reset() { *************** *** 58,115 **** } ! HRESULT CMMCFormsShimCtrl::HostUserControl(BSTR Assembly, BSTR Class, IUnknown **ppControlObject) ! { ! HRESULT hr = S_OK; ! if (m_hWndForm == NULL) ! { ! CComPtr<mscorlib::_Assembly> pAssembly; ! RECT rc; ! CComPtr<IWin32Window> pIWin32Window; ! hr = m_pDefaultDomain->Load_2(Assembly, &pAssembly); ! if(FAILED(hr) || (!pAssembly)) ! { ! hr = E_FAIL; Reset(); return hr; ! } ! hr = pAssembly->CreateInstance(Class, &m_varFormDisp); ! if((m_varFormDisp.vt != VT_DISPATCH) && (m_varFormDisp.vt != VT_UNKNOWN) || (!m_varFormDisp.punkVal)) ! { ! hr = E_FAIL; ! Reset(); ! return hr; ! } ! hr = m_varFormDisp.pdispVal->QueryInterface(IID_IUnknown, (LPVOID*)ppControlObject); ! hr = m_varFormDisp.pdispVal->QueryInterface(IID_IWin32Window, (LPVOID*)&pIWin32Window); ! if(FAILED(hr)) ! { ! Reset(); ! return hr; ! } ! ! long nWndForm = 0; ! hr = pIWin32Window->get_Handle(&nWndForm); ! if(FAILED(hr) || !nWndForm) ! { ! Reset(); ! return hr; ! } #pragma warning (push) #pragma warning (disable : 4312) ! m_hWndForm = (HWND)nWndForm; #pragma warning (pop) ! // parent the form window returned and move it into position ! ::SetParent(m_hWndForm, m_hWnd); ! ::GetWindowRect(m_hWnd, &rc); ! ::MoveWindow(m_hWndForm, 0, 0, rc.right-rc.left, rc.bottom-rc.top, TRUE); ! } return hr; --- 89,159 ---- } + /**** + * this the important function that we use in the mmclib to host a control on a formnode. + * first we try to create an instance of the CLASS type and test if we can do anything with it + * after that we parent the form window returned and move it into position + ***/ + HRESULT CMMCFormsShimCtrl::HostUserControl(BSTR Assembly, BSTR Class, IUnknown **ppControlObject) + { + HRESULT hr = S_OK; + // only proceed if our window handler is set + if (m_hWndForm == NULL) + { + CComPtr<mscorlib::_Assembly> pAssembly; + CComPtr<IWin32Window> pIWin32Window; ! // trying to lead a default domain (?) ! hr = m_pDefaultDomain->Load_2(Assembly, &pAssembly); ! if(FAILED(hr) || (!pAssembly)) ! { ! Reset(); ! return E_FAIL; ! } ! // try to create an Class instance and store it ! hr = pAssembly->CreateInstance(Class, &m_varFormDisp); ! ! // if the punkVal is either NULL or the VARIANT isn't a IUnknown Interface and doesn't implement IDispatch ! // it won't succeed and we return a failure status. ! if((m_varFormDisp.vt != VT_DISPATCH) && (m_varFormDisp.vt != VT_UNKNOWN) || (!m_varFormDisp.punkVal)) ! { ! Reset(); ! return E_FAIL; ! } ! // try to do some interface 'cast' magic ! hr = m_varFormDisp.pdispVal->QueryInterface(IID_IUnknown, (LPVOID*)ppControlObject); ! hr = m_varFormDisp.pdispVal->QueryInterface(IID_IWin32Window, (LPVOID*)&pIWin32Window); ! if(FAILED(hr)) ! { Reset(); return hr; ! } ! // try to get a windowhandle to the form ! long nWndForm = 0; ! hr = pIWin32Window->get_Handle(&nWndForm); ! if(FAILED(hr) || !nWndForm) ! { ! Reset(); ! return hr; ! } + // 'type cast' : conversion from 'long' to 'HWND' of greater size #pragma warning (push) #pragma warning (disable : 4312) ! m_hWndForm = (HWND)nWndForm; #pragma warning (pop) ! // parent the form window returned ! ::SetParent(m_hWndForm, m_hWnd); ! ! // move the window into position ! RECT rc; ! ::GetWindowRect(m_hWnd, &rc); ! ::MoveWindow(m_hWndForm, 0, 0, rc.right-rc.left, rc.bottom-rc.top, TRUE); ! } return hr; *************** *** 118,153 **** HRESULT CMMCFormsShimCtrl::RehostUserControl(IUnknown *pControlObject) { ! HRESULT hr = S_OK; ! if (m_hWndForm == NULL) ! { ! RECT rc; ! CComPtr<IWin32Window> pIWin32Window; ! hr = pControlObject->QueryInterface(IID_IWin32Window, (LPVOID*)&pIWin32Window); ! if(FAILED(hr)) ! { ! Reset(); ! return hr; ! } ! ! long nWndForm = 0; ! hr = pIWin32Window->get_Handle(&nWndForm); ! if(FAILED(hr) || !nWndForm) ! { ! Reset(); ! return hr; ! } #pragma warning (push) #pragma warning (disable : 4312) ! m_hWndForm = (HWND)nWndForm; #pragma warning (pop) ! // parent the form window returned and move it into position ! ::SetParent(m_hWndForm, m_hWnd); ! ::GetWindowRect(m_hWnd, &rc); ! ::MoveWindow(m_hWndForm, 0, 0, rc.right-rc.left, rc.bottom-rc.top, TRUE); ! } return hr; --- 162,197 ---- HRESULT CMMCFormsShimCtrl::RehostUserControl(IUnknown *pControlObject) { ! HRESULT hr = S_OK; ! if (m_hWndForm == NULL) ! { ! RECT rc; ! CComPtr<IWin32Window> pIWin32Window; ! hr = pControlObject->QueryInterface(IID_IWin32Window, (LPVOID*)&pIWin32Window); ! if(FAILED(hr)) ! { ! Reset(); ! return hr; ! } ! ! long nWndForm = 0; ! hr = pIWin32Window->get_Handle(&nWndForm); ! if(FAILED(hr) || !nWndForm) ! { ! Reset(); ! return hr; ! } #pragma warning (push) #pragma warning (disable : 4312) ! m_hWndForm = (HWND)nWndForm; #pragma warning (pop) ! // parent the form window returned and move it into position ! ::SetParent(m_hWndForm, m_hWnd); ! ::GetWindowRect(m_hWnd, &rc); ! ::MoveWindow(m_hWndForm, 0, 0, rc.right-rc.left, rc.bottom-rc.top, TRUE); ! } return hr; |