Click here to Skip to main content
15,892,809 members
Articles / Desktop Programming / ATL

Using Visual C++ .NET to build a program integrated with Microsoft Visual Basic for Application (VBA)

Rate me:
Please Sign up or sign in to vote.
3.07/5 (11 votes)
4 Dec 20022 min read 57.3K   766   20  
Create a MFC Program which integrates with Microsoft Visual Basic for Application.
// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "TangramAppDemo.h"

#include "MainFrm.h"

//////////////////$tangramapp$////////////////////////////
#include "Application.h"
#include "TangramAppDemoDoc.h"//Add here Document Class header
#include "TangramAppDemoView.h"
#include "TangramAppDemoDockingBarImpl.h" //Add here docking bar implementation header
#include "ChildFrm.h"
#include "AtlClass.h"
//////////////////$tangramapp end$////////////////////////////

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CMainFrame

IMPLEMENT_DYNAMIC(CMainFrame, CMDIFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
	ON_WM_CREATE()
//////////////////$tangramapp$////////////////////////////
	ON_WM_CLOSE()
	ON_WM_DESTROY()
	ON_WM_ACTIVATE()
	ON_WM_ENABLE()
	//Add here other Command & UI message process
	ON_WM_CONTEXTMENU()
	ON_COMMAND(ID_VIEW_SystemBar, OnSystemBar)
	ON_UPDATE_COMMAND_UI(ID_VIEW_SystemBar, OnUpdateViewSystembar)
	ON_COMMAND_RANGE(ID_VIEW_SystemBar+1000, ID_VIEW_SystemBar+1100, OnToolsViewUserControlbar)
	ON_UPDATE_COMMAND_UI_RANGE(ID_VIEW_SystemBar+1000, ID_VIEW_SystemBar+1100, OnUpdateToolsViewUserControlbar)
//////////////////$tangramapp end$////////////////////////////
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};


// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	m_nDockBarBase = 0;
	// TODO: add member initialization code here
	m_pDoc = NULL;
}

CMainFrame::~CMainFrame()
{
	for(int i = 0; i < m_DockingWndArray.GetSize(); i++)
	{
		CWnd* pDockingTangramBar = (CWnd*)((CTangramDockingObjImpl*)(m_DockingWndArray[i]));
		delete pDockingTangramBar;
	}
}


int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
//////////////////$tangramapp$////////////////////////////
	m_pObj = new CTangramManager(this,RUNTIME_CLASS(CChildFrame),RUNTIME_CLASS(CTangramAppDemoDockingBarImpl),_T("TangramAppServer.Documents"), NULL,RUNTIME_CLASS(CTangramAppDemoView),true,true);
	m_pObj->SetBitmap(LoadBitmap(theApp.m_hInstance,MAKEINTRESOURCE(IDR_BACKGROUND)));
	m_pObj->AddMfcObject(RUNTIME_CLASS(CTest));
	m_pObj -> m_pApp = &theApp;
	theApp.m_pMainWnd = this;

	//Create VBA Host:
	theApp.CreateHost(this);
//////////////////$tangramapp end$////////////////////////////
	
	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}

	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}
	// TODO: Delete these three lines if you don't want the toolbar to be dockable
	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
	EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndToolBar);
//////////////////$tangramapp$////////////////////////////
//Here is Create Main Project Document and "System Explorer" for all child document
	//------------------
	// Create System Bar:
	//------------------
	m_wndSystemBar.m_pDoc = new CTangramAppDemoDoc();
	m_wndSystemBar.m_pDoc->m_bMainDoc=true;
	m_wndSystemBar.m_pDoc->m_bHasDockState=false;
	CRuntimeClass* pClass = RUNTIME_CLASS(CSystemManagerFrameWnd);
	m_wndSystemBar.m_pFrameWnd = (CSystemManagerFrameWnd*)pClass->CreateObject();
	m_wndSystemBar.m_pFrameWnd->m_TangramFrameObj.m_pDoc = m_wndSystemBar.m_pDoc;
	
	if (!m_wndSystemBar.Create (_T("System Explorer"), this, CSize(150,100),
		TRUE, 
		ID_VIEW_SystemBar,
		WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | CBRS_LEFT| CBRS_FLOAT_MULTI))
	{
		TRACE0("Failed to create Solution Explorer bar\n");
		return FALSE;      // fail to create
	}

	m_wndSystemBar.EnableDocking(CBRS_ALIGN_ANY);
	theApp.UpdateRecentFilelist();

	DockControlBar(&m_wndSystemBar);

	if(m_pDoc->m_bHasDockState)
		SetDockState(m_pDoc->m_DockState);
//////////////////$tangramapp end$////////////////////////////

	return 0;
}
//////////////////$tangramapp$////////////////////////////
void CMainFrame::OnClose() 
{
	//-------------------------------------
	//Save all docking window status
	//-------------------------------------
	for(int i = 0; i < m_pDoc->m_DockingObjArray.GetSize(); i++)
	{
		CWnd* pDockingTangramBar = (CWnd*)(((CTangramDockingObjImpl*)(m_pDoc->m_DockingObjArray[i]))->m_pDockingWnd);
		m_DockingWndArray.InsertAt(i,pDockingTangramBar);
	}
	theApp.SaveFrameWorkInfo();
	//-------------------------------------

	//-------------------------------------
	//Close all opened document
	//-------------------------------------
	if(!m_pObj->CloseDocuments())return;

	//--------------------------------------
	//Unload ApcHost
	//--------------------------------------
	BOOL fTerminated = TRUE;
	theApp.ApcHost.WmClose(fTerminated);
	if (!fTerminated)
		return;

	//Remove VbaIDE ToolWindow
	theApp.RemoveVbaToolWindow();
	//-------------------------------------
	//Delete TangramManager Object
	//and destroy ApcHost Object
	//-------------------------------------
	delete m_pObj;
	theApp.ApcHost.Destroy();
	//////////////////////Begin Designer Support/////////////////////////////
	//Close ActiveX Designer
	theApp.OnCloseDesigner();
	//////////////////////End Designer Support/////////////////////////////
	CMDIFrameWnd::OnClose();
}

//---------------------------------------------------
//Change ApcHost status when mainframe status changed
//---------------------------------------------------
void CMainFrame::OnDestroy() 
{
	theApp.ApcHost.WmDestroy();
	CMDIFrameWnd::OnDestroy();
}

void CMainFrame::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized) 
{
	//theApp.ApcHost.WmActivate(nState);
	CMDIFrameWnd::OnActivate(nState, pWndOther, bMinimized);
	theApp.ApcHost.WmActivate(nState);
}

void CMainFrame::OnEnable(BOOL bEnable) 
{
	CMDIFrameWnd::OnEnable(bEnable);
	theApp.ApcHost.WmEnable(bEnable);			
}
//-----------------------------------------------------

//////////////////$tangramapp end$////////////////////////////


BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CMDIFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return TRUE;
}

//////////////////$tangramapp$////////////////////////////
//--------------------------------------------------
//Add here SystemBar Command Message Process
//--------------------------------------------------
void CMainFrame::OnSystemBar()
{
	// TODO: �ڴ���������������
	ShowControlBar(&m_wndSystemBar,
					!(m_wndSystemBar.IsVisible ()),
					TRUE);
	RecalcLayout ();
}

void CMainFrame::OnUpdateViewSystembar(CCmdUI *pCmdUI)
{
	// TODO: �ڴ������������û����洦��������
	pCmdUI->SetCheck (m_wndSystemBar.IsVisible ());
}
//-------------------------------------------------
//////////////////$tangramapp end$////////////////////////////

// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CMDIFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CMDIFrameWnd::Dump(dc);
}

#endif //_DEBUG


// CMainFrame message handlers

//---------------------------------------------------
//here is process mainframe control bar contextmenu
//---------------------------------------------------
void CMainFrame::OnContextMenu(CWnd* pWnd, CPoint point) 
{
	CMenu menu;
	menu.LoadMenu(IDR_POPUP_CONTROLBAR);
	CMenu* popup = menu.GetSubMenu(0);

	for(int i = 0; i < m_pDoc->m_DockingObjArray.GetSize(); i++)
	{
		CWnd* pDockingTangramBar = (CWnd*)(((CTangramDockingObjImpl*)(m_pDoc->m_DockingObjArray[i]))->m_pDockingWnd);
		CString strTitle;
		pDockingTangramBar->GetWindowText(strTitle);
		int nIndex = (((CTangramAppDemoDockingBarImpl*)(m_pDoc->m_DockingObjArray[i]))->m_nIndex);
		int indexCmd = ID_VIEW_SystemBar+nIndex+1000;
		popup->InsertMenu(nIndex, MF_BYPOSITION|MF_BYCOMMAND| MF_STRING,indexCmd , strTitle);
	}
	// display the popup menu
	popup->TrackPopupMenu(TPM_LEFTALIGN, point.x, point.y, this);
}

void CMainFrame::OnToolsViewUserControlbar (UINT uiId)
{
	int nIndex = uiId - ID_VIEW_SystemBar-1000;
	CControlBar* pDockingTangramBar = (CControlBar*)(((CTangramDockingObjImpl*)(m_pDoc->m_DockingObjArray[nIndex-1]))->m_pDockingWnd);
	ASSERT(pDockingTangramBar);
	
	ShowControlBar (pDockingTangramBar, !pDockingTangramBar->IsVisible (), true);
	RecalcLayout ();
}

void CMainFrame::OnUpdateToolsViewUserControlbar (CCmdUI* pCmdUI)
{
	int nIndex = pCmdUI->m_nID - ID_VIEW_SystemBar-1000;
	if(nIndex<0)return;
	CControlBar* pDockingTangramBar = (CControlBar*)(((CTangramDockingObjImpl*)(m_pDoc->m_DockingObjArray[nIndex-1]))->m_pDockingWnd);
	ASSERT(pDockingTangramBar);
	if (pDockingTangramBar == NULL)
	{
		pCmdUI->Enable (FALSE);
		return;
	}

	pCmdUI->SetCheck (pDockingTangramBar->IsVisible ());
}
//--------------------------------------------------------

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