Click here to Skip to main content
15,884,176 members
Articles / Desktop Programming / MFC

Develop MFC Doc/View Application Which Supports Any Number Document Template

Rate me:
Please Sign up or sign in to vote.
1.57/5 (3 votes)
2 Jun 20052 min read 36.5K   1.7K   26  
This article provides an introduction to TangramLittle, a C++ Framework for MFC and the .NET Framework. Knowledge in MFC and the .NET Framework is assumed.
// 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;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here



Comments and Discussions