// WinFormsView.cpp : implementation file
//
#include "stdafx.h"
#include "[!output PROJECT_NAME].h"
#include "[!output PROJECT_NAME]Doc.h"
#include "WinFormsView.h"
#include "WFControlSite.h"
// CUserCtrlView
IMPLEMENT_DYNCREATE(CUserCtrlView, CView)
CUserCtrlView::CWFControlWrapper::CWFControlWrapper():pUnkControl(NULL)
{
}
CUserCtrlView::CWFControlWrapper::~CWFControlWrapper(void)
{
if (NULL != pUnkControl)
{
pUnkControl->Release();
pUnkControl = NULL ;
}
}
CUserCtrlView::CUserCtrlView()
{
m_bDotNetView = true;
m_strWinCtrlID = theApp.m_strCreateInfo;
m_pWinFormControl = NULL;
}
CUserCtrlView::~CUserCtrlView()
{
}
BEGIN_MESSAGE_MAP(CUserCtrlView, CView)
ON_WM_SIZE()
END_MESSAGE_MAP()
// CUserCtrlView drawing
void CUserCtrlView::OnDraw(CDC* pDC)
{
}
// CUserCtrlView diagnostics
#ifdef _DEBUG
void CUserCtrlView::AssertValid() const
{
CView::AssertValid();
}
void CUserCtrlView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
#endif //_DEBUG
BOOL CUserCtrlView::CreateControlSite( COleControlContainer* pContainer
, COleControlSite** ppSite
, UINT nID
, REFCLSID clsid )
{
if(m_bDotNetView)
{
ASSERT( ppSite != NULL );
*ppSite = new CWFControlSite(GetControlContainer());
return TRUE;
}
else
return CView::CreateControlSite(pContainer,ppSite,nID,clsid);
}
// CUserCtrlView message handlers
void CUserCtrlView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
if ( m_Control.GetManagedControl())
m_Control.SetWindowPos( NULL, 0, 0, cx, cy, SWP_NOZORDER|SWP_NOMOVE );
}
BOOL CUserCtrlView::PreTranslateMessage(MSG* pMsg)
{
if(m_bDotNetView)
{
ASSERT(pMsg != NULL);
ASSERT_VALID(this);
ASSERT(m_hWnd != NULL);
BOOL bRet = FALSE;
if(m_Control.pUnkControl != NULL)
{
CComQIPtr<IOleInPlaceActiveObject> spInPlace(m_Control.pUnkControl);
if (spInPlace)
{
bRet = (spInPlace->TranslateAccelerator(pMsg) == S_OK) ? TRUE : FALSE;
}
}
// allow tooltip messages to be filtered (skip CFormView)
if (CView::PreTranslateMessage(pMsg))
return TRUE;
// don't translate dialog messages when in Shift+F1 help mode
CFrameWnd* pFrameWnd = GetTopLevelFrame();
if (pFrameWnd != NULL && pFrameWnd->m_bHelpMode)
return FALSE;
// call all frame windows' PreTranslateMessage first
pFrameWnd = GetParentFrame(); // start with first parent frame
while (pFrameWnd != NULL)
{
// allow owner & frames to translate
if (pFrameWnd->PreTranslateMessage(pMsg))
return TRUE;
// try parent frames until there are no parent frames
pFrameWnd = pFrameWnd->GetParentFrame();
}
return bRet;
}
else
return CView::PreTranslateMessage(pMsg);
}
BOOL CUserCtrlView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Add your specialized code here and/or call the base class
cs.lpszClass = AfxRegisterWndClass(0);
cs.dwExStyle &= ~WS_EX_CLIENTEDGE;
cs.style|=WS_CLIPSIBLINGS;
return CView::PreCreateWindow(cs);
}
void CUserCtrlView::OnInitialUpdate()
{
if(!m_pWinFormControl&&m_strWinCtrlID!=_T(""))
{
try
{
CRect clientRect;
GetClientRect( &clientRect );
String* pString[] = new String*[1];
pString[0] = m_strWinCtrlID;
//get .net control from theApp.m_pManagedCnnObj:
m_pWinFormControl = theApp.m_pManagedCnnObj->GetType()->GetProperty(S"PrjItem")->GetValue(theApp.m_pManagedCnnObj,pString);
m_Control.pUnkControl = reinterpret_cast<IUnknown*>(System::Runtime::InteropServices::Marshal::GetIUnknownForObject(m_pWinFormControl).ToPointer());
CLSID clsid = { 0, 0, 0, { 0, 0, 0, 0, 0, 0, 0, 0 } };
m_Control.CreateControl(clsid, NULL, WS_VISIBLE|WS_CHILD, clientRect, this, 0);
m_Control.ModifyStyleEx( 0, WS_EX_CONTROLPARENT );
}
catch (Exception* exp)
{
CString strInfo = exp->Message;
AfxMessageBox(strInfo);
return;
}
}
CView::OnInitialUpdate();
}
IDispatch* CUserCtrlView::GetDispatch()
{
IDispatch* pDisp;
HRESULT hr = m_Control.pUnkControl->QueryInterface(IID_IDispatch, (void**) &pDisp);
return pDisp;
}