#include "StdAfx.h"
#include "MainFrame2.h"
#include "AppObject.h"
#include "ManagedObj.h"
#include "WinFormsView.h"
using namespace MainFrame2;
AppObject::AppObject(void)
{
m_pExternalApplication = NULL;
}
AppObject::~AppObject(void)
{
}
void AppObject::ActiveMainFrame(String* pAppImpl)
{
//if(!g_pDotNetExtImpl)return;
CString str = pAppImpl;
g_pDotNetExtImpl = (DotNetExtImpl*)_atoi64(str);
CWinApp* pApp = AfxGetApp();
ASSERT(pApp!=NULL);
InitialExtLib();
m_pMfcBkCtrl = new MfcBkCtrl();
set_PrjItem(S"bkctrl",m_pMfcBkCtrl);
m_pMainCtrl = new MainCtrl();
set_PrjItem(S"MainCtrl",m_pMainCtrl);
m_pTopCtrl = new TopCtrl();
set_PrjItem(S"bottomCtrl",m_pTopCtrl);
//��һ��MFC CView����Ϊ���ĵ��û���������
if(g_pDotNetExtImpl->m_strBackgroundType.CompareNoCase(_T("mfcview"))==0)
{
g_pDotNetExtImpl->m_pBkViewRuntimeClass = RUNTIME_CLASS(CUserCtrlView);
}
AfxSetResourceHandle(theApp.m_hInstance);
CMainFrame* pMainFrame = new CMainFrame;
theApp.m_pAppObj = this;
g_pDotNetExtImpl->m_pAppDisp = reinterpret_cast<IDispatch*>(System::Runtime::InteropServices::Marshal::GetIDispatchForObject(this).ToInt64());
try
{
InitInstance();
/**************************************************************************
��չӦ�ó��������AppObject����չ.NET�������ԣ�ͨ��һ����չ����AppObject
�������Ϊ������չ����չAppObject����������.NET����ʵ�֣���չAppObjectͨ��
����;����AppObject������н�����������ӦAppObject������¼�������Ϊ������
������չAppObject������VBA�����еĿɱ�����ͬ�������ļ������ṩ��ͬ����
չ�������ϵͳ�Ķ��ο������ƾ���ͨ���ṩ��չAppObjectʵ�ֵġ�
**************************************************************************/
//������չӦ�ó������
CString m_strExternalAppObjID = g_pDotNetExtImpl->m_strMainExAssemblyLibID;
/////////////////////Begin Create External Application Object//////////////////
if(m_strExternalAppObjID!=_T(""))
{
int nPos = -1;
nPos = m_strExternalAppObjID.Find(_T("#"));
String* strAssembly = m_strExternalAppObjID.Left(nPos);
String* strObjID = m_strExternalAppObjID.Mid(nPos+1);
Assembly* m_pDotNetAssembly = Assembly::Load(strAssembly);
Type* m_pDotNetExternalType = m_pDotNetAssembly->GetType(strObjID,true,true);
Object* m_pExternalAppObj = __try_cast< Object* >(Activator::CreateInstance(m_pDotNetExternalType));
//Your External Application Object must have a property - "Application" which hold a pointer
//to the Application object created above. By using the property, an external component can hook the
//application object's events, call its methods , modify its properties etc.
//In general the Application Object may reference the external application object(and vice versa).
//When application object and external application object establish a reference to eachother,
//they can interact on each other more effectively.
PropertyInfo* m_pPropertyInfo = m_pDotNetExternalType->GetProperty(S"Application");
m_pPropertyInfo->SetValue(m_pExternalAppObj,this,NULL);
m_pExternalApplication = m_pExternalAppObj;
g_pDotNetExtImpl->m_pExtAppDisp = reinterpret_cast<IDispatch*>(System::Runtime::InteropServices::Marshal::GetIDispatchForObject(m_pExternalAppObj).ToInt64());
}
/////////////////////End Create External Application Object//////////////////
}
catch (Exception* exp)
{
CString strInfo = exp->Message;
AfxMessageBox(strInfo);
return ;
}
if (!pMainFrame || !pMainFrame->LoadFrame(IDR_MAINFRAME))
return ;
g_pDotNetExtImpl->m_pApp->m_pMainWnd = pMainFrame;
//pApp->m_pMainWnd = pMainFrame;
// call DragAcceptFiles only if there's a suffix
// In an MDI app, this should occur immediately after setting m_pMainWnd
// Parse command line for standard shell commands, DDE, file open
//CCommandLineInfo cmdInfo;
//pApp->ParseCommandLine(cmdInfo);
// Dispatch commands specified on the command line. Will return FALSE if
// app was launched with /RegServer, /Register, /Unregserver or /Unregister.
//if (!pApp->ProcessShellCommand(cmdInfo))
//return ;
// The main window has been initialized, so show and update it
pMainFrame->ShowWindow(pApp->m_nCmdShow);
pMainFrame->UpdateWindow();
}
void MainFrame2::AppObject::set_PrjItem(String* strIndex, Object* value)
{
CString m_strDocIndex = theApp.StringToCStr(strIndex);
m_strDocIndex.MakeLower();
CManagedObj* m_pManagedObj = NULL;
if(!theApp.m_PrjItemDictionary.Lookup(LPCTSTR(m_strDocIndex),(CObject*&)m_pManagedObj))
{
m_pManagedObj = new CManagedObj();
m_pManagedObj -> m_pManagedObj = value;
theApp.m_PrjItemDictionary.SetAt(m_strDocIndex, m_pManagedObj);
}
}
Object* MainFrame2::AppObject::get_PrjItem(String* strIndex)
{
CString m_strDocIndex = theApp.StringToCStr(strIndex);
m_strDocIndex.MakeLower();
CManagedObj* m_pManagedObj = NULL;
if(theApp.m_PrjItemDictionary.Lookup(LPCTSTR(m_strDocIndex),(CObject*&)m_pManagedObj))
return m_pManagedObj->m_pManagedObj;
return NULL;
}
void MainFrame2::AppObject::Connect(Object* pCtrlObj, String* strObjName)
{
//insert object into object dictionary,thus other component can
//using this object through the property "PrjItem" of an application object:
set_PrjItem(strObjName, pCtrlObj);
//you can convert pCtrlObj to some proper object like this:
//somenamespace::someobj* pNewCtrlObj = __try_cast<somenamespace::someobj*>(pCtrlObj);
//if you want to hook event for this object,you can do like this:
//if(strObjName == S"somename")
//{
// pNewCtrlObj->add_SomeEvet(new System::EventHandler(this,OnSomeFunctionInThisObject));
//}
//if there exists an external application object, we pass pCtrlObj to this external application object,
//the provider of external application object can get pCtrlObj in the following manner:
if(m_pExternalApplication)
{
Type* t = m_pExternalApplication->GetType();
MethodInfo* mi = t->GetMethod(S"Connect");
Object* p[] = new Object*[2];
p[0] = pCtrlObj;
p[1] = strObjName;
mi->Invoke(m_pExternalApplication,p);
}
}