\

 

directory

Create the MFC project

The development environment

The preparatory work

Create an MFC project of type MDI or MDT

Copy the MFCPlus folder

Modify the base class of the App class

Modify the base class of the CMainFrame class (MDT projects do not do this step)

Modify CFirstMFCApp: : InitInstance ()

Modify CFirstMFCApp: : InitInstance () (2)

Modify CFirstMFCView: : the OnInitialUpdate ()

Document serialization support

Modify the Manifest configuration of the project

Method one:

Method 2:

Relocation of existing MFC projects

Compile your code

Sample

Technical support


SDI and dialog-based application architectures are not appropriate for the technical goals of ****WebRuntime****, so we will only consider MDI (multi-document) and MDT (multi-top-level window) architectures for MFC applications. This article is a basic guide to creating MFC Internet projects. Developers only need to follow the specified steps. The average developer should complete the steps of this article in about 3-5 minutes.

Create the MFC project

The development environment

Our recommended development environment is Visual Studio 2019.

The preparatory work

Download (click) the WebRuntime runtime support package. Once downloaded, unzip the binary package and go to the MFCPlus subfolder.

Create an MFC project of type MDI or MDT

For the sake of writing, we named the project FirstMFCApp, which is an MDI project of the MDI type:

(Create new MFC project)

The App class of the project is CFirstMFCApp:

(App class: CFirstMFCApp)

CFirstMFCView = CFirstMFCView

(Project View class: CFirstMFCView, which is a CFormView class)

Everything else is the default.

Copy the MFCPlus folder

Copy the ****MFCPlus**** folder to the newly generated project folder. This step replaces the PCH, H, and pch.cpp files of the new project, effectively adding them to the existing PCH, H, and pch.cpp files

#include "WebRuntimeApp.h"
Copy the code

(PCH,h add the previous line of code)

As well as

#include "WebRuntimeApp.cpp"
Copy the code

(PCH, CPP to add the previous line of code)

The copy is equivalent to adding a set of C++ source files and an mfcap.manifest file to the MFC project you create.

Modify the base class of the App class

Change the base class of CFirstMFCApp to CWebRuntimeApp:

class CFirstMFCApp : public CWebRuntimeApp
Copy the code

Modify the base class of the CMainFrame class (MDT projects do not do this step)

Change the base class of CMainFrame to:

class CMainFrame : public CWebMDIFrameWnd
Copy the code

Meanwhile, modify the following parts in the mainfrm. CPP file:

IMPLEMENT_DYNAMIC(CMainFrame, CWebMDIFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CWebMDIFrameWnd)
Copy the code

(Note the CWebMDIFrameWnd part of the macros above)

Modify CFirstMFCApp: : InitInstance ()

This step is required for both MDI and MDT projects. Add the following code at the beginning of this function:

BOOL CFirstMFCApp::InitInstance() { m_mapDOMObj[_T("main_panel")] = RUNTIME_CLASS(CFirstMFCView); if (! InitApp()) return false; // InitCommonControlsEx() is required on Windows XP if an applicationCopy the code

Note the following line:

    m_mapDOMObj[_T("main_panel")] = RUNTIME_CLASS(CFirstMFCView);
Copy the code

There is actually a “mapping” between a string and the CView class that can contain multiple similar rows, and this mapping sets the stage for future extensions of the Web DOM, that is, the “string name” in each mapping will be represented as a new Web page element type.

The code added here is required for both MDI and MDT project types. If you create a project is a type of MDT engineering, need to be in CFirstMFCApp: : InitInstance () function of body will be deleted the following code block:

// create main MDI Frame window CMainFrame* pMainFrame = new CMainFrame; if (! pMainFrame || ! pMainFrame->LoadFrame(IDR_MAINFRAME)) { delete pMainFrame; return FALSE; } m_pMainWnd = pMainFrame; // call DragAcceptFiles only if there's a suffix // In an MDI app, this should occur immediately after setting m_pMainWnd // Enable drag/drop open m_pMainWnd->DragAcceptFiles(); // Parse command line for standard shell commands, DDE, file open CCommandLineInfo cmdInfo; ParseCommandLine(cmdInfo); // Enable DDE Execute open EnableShellOpen(); RegisterShellFileTypes(TRUE); // Dispatch commands specified on the command line. Will return FALSE if // app was launched with /RegServer, /Register, /Unregserver or /Unregister. if (! ProcessShellCommand(cmdInfo)) return FALSE; // The main window has been initialized, so show and update it pMainFrame->ShowWindow(m_nCmdShow); pMainFrame->UpdateWindow();Copy the code

(In MDT project, the above code block is deleted directly)

Equivalent to execution

	m_pDocTemplate = pDocTemplate;
	AddDocTemplate(pDocTemplate);
Copy the code

The function returns “TRUE”.

Modify CFirstMFCApp: : InitInstance () (2)

If you are creating a MDI project, so need to CFirstMFCApp: : InitInstance () found in the following line of code:

    EnableTaskbarInteraction();
Copy the code

Replace this line with the following code block:

	EnableTaskbarInteraction();

	return TRUE;
}

BOOL CFirstMFCApp::CreateCosmosApp(CWebPageImpl* pWebPageImpl, CBrowserImpl* pBrowserImpl, CString strObjID, CString strAppData)
{
Copy the code

After replacement, CFirstMFCApp: : InitInstance () is divided into two functions, need to be in the class of CFirstMFCApp declare a “virtual function” :

class CFirstMFCApp : public CWebRuntimeApp { public: CMFCApp() noexcept; // Overrides public: virtual BOOL InitInstance(); virtual int ExitInstance(); // Implementation UINT m_nAppLook; BOOL m_bHiColorIcons; virtual void PreLoadState(); virtual void LoadCustomState(); virtual void SaveCustomState(); // For MDI applications, we need to override CWebRuntimeApp's virtual function:  virtual BOOL CreateCosmosApp ( CWebPageImpl* pWebPageImpl, CBrowserImpl* pBrowserImpl, CString strObjID, CString strAppData ); afx_msg void OnAppAbout(); DECLARE_MESSAGE_MAP() };Copy the code

(Note the added function CreateCosmosApp,

This function is provided by CWebRuntimeApp, overloaded here.)

Modify CFirstMFCView: : the OnInitialUpdate ()

Since CFirstMFCView is CFormView, its OnInitialUpdate() needs to be modified as follows:

void CMFCApplication1View::OnInitialUpdate() { CFormView::OnInitialUpdate(); // The following code applies only to CFormView derived classes: if (m_pDocument) {theapp.setFrameInfo (m_hWnd); }}Copy the code

Document serialization support

Override the serialization support function as follows:

// CFirstMFCAppDoc serialization

void CFirstMFCAppDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		ar << theApp.GetDocTemplateID(this);
		// TODO: add storing code here
	}
	else
	{
		ar >> theApp.m_strCreatingDOCID;
		// TODO: add loading code here
	}
}
Copy the code

Open the Resource panel and modify the String Table Resource whose ID is IDR_FirstMFCAppTYPE:

The modified string value is as follows: here “*.xxx” is the extension of the document supported by the MFC program (due to a Bug in the Visual Studio application wizard, the generated resource is faulty)

FirstMFCApp\nFirstMFCApp\nFirstMFCApp\nFirstMFCApp files\n.xxx\nFirstMFCApp.Document\nFirstMFCApp.Document
Copy the code

So far we have completed the MFC project to support WebRuntime code modifications.

Modify the Manifest configuration of the project

To support the full functionality of modern browsers, you need to modify the Manifest configuration of your project in the following two ways.

Method one:

The mfcapp. Manifest file is provided by ****MFCPlus**** and needs to be copied to the source file folder where the project is created:

(Modify the manifest configuration for Debug and Release compilation modes)

Method 2:

To import the project properties table file mfcapp.props, do as follows:

Step 1: Open the project’s property management toolbox as shown below:

Step 2: Select the target project from the property Management toolbox, right-click the mouse to open the shortcut menu, and select Add existing project properties:

Step 3: Select mfcap.props to complete the operation.

After completing the above steps, we are done supporting ****WebRuntime****.

Relocation of existing MFC projects

For existing MFC projects (MDI, MDT type projects, other types can contact us), because many MFC class objects are defined by designers themselves, there are some differences between the basic class link and our demonstration here. Developers can follow the above steps and make appropriate adjustments according to their own design ideas. Can be fully supported at ****WebRuntime****.

The migration steps are as follows (still applicable to newly created projects) :

  1. Replace the base class of App class with “CWinAppEx” to “CWebRuntimeApp”, and for MDI application type, at the project level, replace “CMDIFrameWndEx” with “CWebMDIFrameWnd”, as shown below:

  2. Modify the InitInstance() function of the target project App class by referring to the modification section for InitInstance() in this article;

  3. Copy the necessary code to the source folder of the target project as described in this article on MFCPlus;

  4. Treat the derived classes of CFormView as described in this article (override the OnInitialUpdate function);

  5. Handle document serialization as described in this article.

  6. Adjust the Manifest configuration of the target project as described in this article;

Of course, the above steps also apply to newly created MFC projects.

Compile your code

The project will be created to compile, note that we only support X64 compilation, that is, we only support 64-bit Windows applications. Copy the ****WebRuntime binary package **** to the compiled output directory, that is, the folder where the exe file resides. We’ll explain how to run a compiled program in more detail in another blog post, but this is just how to create and migrate such projects.

Sample

WebRuntime source code, including two examples ****, one is MFCApplication1 (MDI), the other is TheUniverse (MDT), developers can see the example source code, in the binary package contains the compiled version of the example and the relevant Web page, you can run directly.

Technical support

If you have any questions, please contact us on wechat:

\