⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mfcappadapter.cpp

📁 As an alternate to the migration scenario, Microsoft offers some interoperation solutions (in an int
💻 CPP
字号:
// MfcAppAdapter.cpp
/*
 * Copyright (c) 2005 Alexey Shalnov
 * All Rights Reserved
 * 
 * http://home.arcor.de/alexeyshalnov/home/
*/

#include "stdafx.h"

#include "MfcAppAdapter.h"
#include "Hosting/IApplication.h"       

using namespace AS::MfcHost2;

////////////////////////////////////////////////////////////////

MfcAppAdapter::MfcAppAdapter()
{
}

MfcAppAdapter^ MfcAppAdapter::CreateInstance(IWndManager^ wndManager)
{
	ASSERT(wndManager);
	
	//we use only one instance
	if(Instance==nullptr)
	{
		Instance = gcnew MfcAppAdapter();
		if(!Instance->AttachApplication(wndManager))
		{
			delete Instance;
			Instance = nullptr;
		}
	}
	ASSERT(Instance!=nullptr);
	return Instance;
}

IFramework* MfcAppAdapter::GetFramework()
{
	return (IFramework*)MfcAppAdapter::m_pIFrameworkImpl;
}
				
// Command messages transmition
bool MfcAppAdapter::OnCmdMsg(MfcCommand commandID)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	
	int nID = (int)commandID;

	bool result = false;

	CMDIFrameWnd* pMainFrame = dynamic_cast <CMDIFrameWnd*>(AfxGetMainWnd());

	if(pMainFrame != NULL)
		result = pMainFrame->OnCmdMsg(nID, CN_COMMAND, NULL, NULL) != FALSE;
	
	return result;
}

// initialisation of Legacy MFC application
bool MfcAppAdapter::AttachApplication(IWndManager^ wndManager)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	
	bool retval = false;
	
	IApplication* pApp = dynamic_cast<IApplication*>(AfxGetApp());
	
	// We can host the application with IApplication interface only!!!
	ASSERT(pApp!=NULL);
	if(pApp!=NULL)
	{
		m_pWndManager = wndManager;
		
		HWND externframe = (HWND)(wndManager->GetExternFrame()).ToInt32();

		MfcAppAdapter::m_pIFrameworkImpl = new IFrameworkImpl(externframe);

		if (::IsWindow(externframe))
		{
			try 
			{
				if(pApp->AttachApplication(MfcAppAdapter::GetFramework())==TRUE)
					retval = true;
			}
			catch(...){}
		}
	}

	return retval;
}

// Close Legacy MFC application
MfcAppAdapter::~MfcAppAdapter()
{
	this->!MfcAppAdapter();
}
MfcAppAdapter::!MfcAppAdapter()
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	try 
	{
		IApplication* pApp = dynamic_cast<IApplication*>(AfxGetApp());
		if(pApp!=NULL)
			pApp->DetachApplication();
	} 
	finally
	{
		delete MfcAppAdapter::m_pIFrameworkImpl;
		MfcAppAdapter::m_pIFrameworkImpl = NULL;
		Instance=nullptr;
	}
}


/////////////////////////////////////////////////////////////////////////////
// IFramework
// Connect the Legacy MFC application with WinForms Framework
IFrameworkImpl::IFrameworkImpl(HWND ExternFrameHwnd)
{	
	m_ExternFrameHwnd = ExternFrameHwnd;
}
HWND IFrameworkImpl::GetExternFrame()	{return m_ExternFrameHwnd;};


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -