Click here to Skip to main content
15,881,882 members
Articles / Desktop Programming / MFC

An MFC extension library to enable DLL plug-in technology for your application using MESSAGE_MAPs

Rate me:
Please Sign up or sign in to vote.
4.85/5 (47 votes)
8 Jun 2004CPOL17 min read 576.4K   8.6K   238  
A plug-in architecture which allows you to write plug-in DLLs for your application and extend/modify its functionality.
// (c) R.I.Allen 2002
// You may use this code in anyway that you feel, no guarantees or waranties are implied
// please keep all headers with any source used.
//
// PIScrollView.cpp : implementation file
//

#include "stdafx.h"
#include "PlugInApp.h"
#include "PIScrollView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CPIScrollView

IMPLEMENT_DYNCREATE(CPIScrollView, CScrollView)

CPIScrollView::CPIScrollView()
{
	m_pMaps = NULL;
	m_MapCount = 0;
}

CPIScrollView::~CPIScrollView()
{
	// release message map pointers
	for (int i = 0; i < m_MapCount; ++i)
	{
		delete m_pMaps[i];
		m_pMaps[i] = NULL;
	}
	delete []m_pMaps;
	m_pMaps = NULL;
	m_state.SetDestroyed();
}

void CPIScrollView::InitialisePlugIns()
{
	CPlugInApp *pApp = static_cast<CPlugInApp*>(AfxGetApp());
	// get our pointers to any plug in maps
	m_pMaps = pApp->GetMessageMaps(this, m_MapCount);
}

BEGIN_MESSAGE_MAP(CPIScrollView, CScrollView)
	//{{AFX_MSG_MAP(CPIScrollView)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPIScrollView drawing

void CPIScrollView::OnDraw(CDC* pDC)
{
	UNUSED_PARAMETER(pDC);
}

/////////////////////////////////////////////////////////////////////////////
// CPIScrollView diagnostics

#ifdef _DEBUG
void CPIScrollView::AssertValid() const
{
	CScrollView::AssertValid();
}

void CPIScrollView::Dump(CDumpContext& dc) const
{
	CScrollView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CPIScrollView message handlers
BOOL CPIScrollView::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo) 
{
	// check to see whether any of the plug-in DLL's
	// want to intercept this message and process it before the application class
	CPlugInApp *pApp = static_cast<CPlugInApp*>(AfxGetApp());
	CPIState	stateCopy(m_state);

	ASSERT(pApp);
	BOOL ret_pre = FALSE;
	BOOL ret_app = FALSE;
	BOOL ret_post = FALSE;
	m_bSuppressThisMessage = false;
	
	// allow DLL's to process this message before the exe
	ret_pre = pApp->ProcessCommandMessageMaps(stateCopy, true, &m_bSuppressThisMessage, m_pMaps, m_MapCount, nID, nCode, pExtra, pHandlerInfo);
	if (!m_bSuppressThisMessage)
		{
		// allow the exe to process the message is it hasn't been suppressed by a DLL
		ret_app = CScrollView::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
		}
	// allow DLL to process the message after the exe
	// note that DLLs get to do this whether the message has been supressed or not
	if (!stateCopy.IsDestroyed())
	{
		ret_post = pApp->ProcessCommandMessageMaps(stateCopy, false, &m_bSuppressThisMessage, m_pMaps, m_MapCount, nID, nCode, pExtra, pHandlerInfo);
	}

	if (ret_pre || ret_app || ret_post)
		return  TRUE;
	return FALSE;
}

BOOL CPIScrollView::OnWndMsg(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pResult)
{
	LRESULT	lResult;
	// special case for commands
	if (message == WM_COMMAND)
		{
		if (OnCommand(wParam, lParam))
			{
			lResult = 1;
			if (pResult != NULL)
				*pResult = lResult;
			return TRUE;
			}
		return FALSE;
		}

	// special case for notifies
	if (message == WM_NOTIFY)
		{
		NMHDR* pNMHDR = (NMHDR*)lParam;
		if (pNMHDR->hwndFrom != NULL && OnNotify(wParam, lParam, &lResult))
			{
			if (pResult != NULL)
				*pResult = lResult;
			return TRUE;
			}
		return FALSE;
		}

	// check to see whether any of the plug-in DLL's
	// want to intercept this message and process it before the application class
	CPlugInApp *pApp = static_cast<CPlugInApp*>(AfxGetApp());
	ASSERT(pApp);
	CPIState	stateCopy(m_state);
	BOOL ret_pre = FALSE;
	BOOL ret_app = FALSE;
	BOOL ret_post = FALSE;
	m_bSuppressThisMessage = false;
	// allow DLL's to process this message before the exe
	ret_pre = pApp->ProcessWindowMessageMaps(stateCopy, true, &m_bSuppressThisMessage, m_pMaps, m_MapCount, message, wParam, lParam, pResult);
	if (!m_bSuppressThisMessage)
		{
		// allow the exe to process the message is it hasn't been suppressed by a DLL
		ret_app = CScrollView::OnWndMsg(message, wParam, lParam, pResult);
		}
	if (!stateCopy.IsDestroyed())
	{
		// allow DLL to process the message after the exe
		// note that DLLs get to do this whether the message has been supressed or not
		ret_pre = pApp->ProcessWindowMessageMaps(stateCopy, false, &m_bSuppressThisMessage, m_pMaps, m_MapCount, message, wParam, lParam, pResult);
	}

	if (ret_pre || ret_app || ret_post)
		return  TRUE;
	return FALSE;
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Sirius Analytical Instruments
United Kingdom United Kingdom
A research and development programmer working for a pharmaceutical instrument company for the past 17 years.

I am one of those lucky people who enjoys his work and spends more time than he should either doing work or reseaching new stuff. I can also be found on playing DDO on the Cannith server (Send a tell to "Maetrim" who is my current main)

I am also a keep fit fanatic, doing cross country running and am seriously into [url]http://www.ryushinkan.co.uk/[/url] Karate at this time of my life, training from 4-6 times a week and recently achieved my 1st Dan after 6 years.

Comments and Discussions