CEF is related to MFC embedding

A brief introduction to xiadakeng,

1. To download appropriate compiled class library resources, do not download incomplete or problematic resources. You can search Baidu for the download address of the official website or famous outlets.

Of course, I also provided the relevant resources I downloaded,

The name is: cef_binary_90.6.7+g19ba721+chromium-90.0.4430.212_windows32.tar.bz2

The website is: https://download.csdn.net/download/newTTTTTT/19646972

A libcef.com is also available on the official website dll. After the PDB is decompressed, it's about 3G. It's useless. When debugging, there should be an error and the crash is the same. You can't see any information prompt. It is recommended not to download.

2 CMKAE, don't change the options indiscriminately, directly default and compile. Remember to use VS2019+CMAKE. Do not modify the MT compilation method in the config option of cmake. If this is adjusted to MD, cmake cannot be successfully implemented. You can only compile with MT and generate cel After SLN is opened with VS, compile it to generate the target file XX lib.. DLL or something.

3 VS create a new project -- > to use the static MFC class library, you need to add the necessary project setting options, including path, dll, lib, etc. If you can't fix this, you can't compile directly, or you can't recognize XXX, or you can't find XXX.

Remember to choose x86(win32) or x64 when CMAKE, which is consistent with the way your project needs to use.

3.1 you can unzip the CEF file package into the folder include and libcef_ Copy DLL to your project directory, and then include them in the include directory.

3.2 in the menu bar: item -- > XXX attribute -- > configuration option bar, select: all configurations -- > VC + + directory -- > General -- > include directory, and set the corresponding directory content: for example, add a D:19\mybro2\mybro2\include to include the include you copied.

3.3 project cel generated by compiling CEF Copy the relevant lib\dll files generated by SLN into the project. You can create two new folders, debuglib\releaselib, and store them in each folder:

Find the corresponding source location yourself, or in your compiled project cel The SLN output is under the debug/release folder, or under the CEF decompression folder. After this is done, add the path to your project:

3.4 select: debug -- > VC + + directory -- > General -- > Library Directory in the project -- > XXX attribute -- > configuration option bar, and enter your debuglib path; Select: release -- > VC + + directory -- > General -- > Library directory from the item -- > XXX attribute -- > configuration option bar, and enter your releaselib path.

3.5 select debug -- > C / C + + -- > preprocessor -- > preprocessor definition from the item -- > XXX attribute -- > configuration option bar. Here, you need to add one_ ITERATOR_DEBUG_LEVEL=0, because the lib generated by open source code is this, and an error will be reported if it is not added.

3.6 this is also a pit: select debug -- > C / C + + -- > code generation -- > runtime from the project -- > XXX attribute -- > configuration option bar. There is also a pit here. Select / MTd for debug and MT for release, which is consistent with the source lib. Otherwise, no matter what other types of options are selected, various errors will always appear.

3.7 this is a pit

In the item -- > XXX attribute -- > configuration option bar, select: all configuration -- > List tools -- > input and output -- > attach list file and add a my The contents of this file are as follows:

<?xml version="1.0" encoding="utf-8"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">  
  <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">  
    <application> 
      <!--The ID below indicates application support for Windows 8.1 -->  
      <supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}"/>  
      <!-- 10.0 -->  
      <supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}"/> 
    </application> 
  </compatibility> 
</assembly>

Without this file, it runs successfully, but the browser is white...

4 use routines to generate your own code

//Header file myclient h
#pragma once
#include "include/cef_client.h"


class CSimpleClient : public CefClient, public CefLifeSpanHandler
{
public:
    CSimpleClient();
    ~CSimpleClient();

    virtual CefRefPtr<CefLifeSpanHandler> GetLifeSpanHandler() OVERRIDE
    {
        return this;
    }

    // CefLifeSpanHandler methods:
    virtual void OnAfterCreated(CefRefPtr<CefBrowser> browser) OVERRIDE;

    CefRefPtr<CefBrowser> GetBrowser() { return m_cefBrowser; }

    //void CloseTheBrowser();

private:
    CefRefPtr<CefBrowser> m_cefBrowser;

    IMPLEMENT_REFCOUNTING(CSimpleClient);
    IMPLEMENT_LOCKING(CSimpleClient);
};

//Cpp file myclient cpp
#include "pch.h"
#include "CSimpleClient.h"


CSimpleClient::CSimpleClient() {};

CSimpleClient::~CSimpleClient()
{
}

void CSimpleClient::OnAfterCreated(CefRefPtr<CefBrowser> browser)
{
    m_cefBrowser = browser;
}

//void CSimpleClient::CloseTheBrowser()
//{
   // if (m_cefBrowser)
    //{
       // m_cefBrowser->GetHost()->CloseBrowser(1);
    //}
//}

After adding these two files, insert the initialization code in the dialog box

//Select one of the two initialization modes
BOOL Cos2App::CEF_Inits()
{
	CefMainArgs main_args(theApp.m_hInstance);
	CefRefPtr<CefApp> app;
	int exit_code = CefExecuteProcess(main_args, app.get(), NULL);
	if (exit_code >= 0)
	{
		TRACE1("Error: failed to init CEF with exit code %d!\n", exit_code);
		return FALSE;
	}

	CefSettings settings;
	CefSettingsTraits::init(&settings);
	settings.multi_threaded_message_loop = true;
	//settings.single_process = true;
	settings.no_sandbox = true;

	CefString(&settings.locale).FromWString(L"UTF-8");
	CefString(&settings.cache_path).FromString("./cachepath/");
	CefInitialize(main_args, settings, app.get(), NULL);
	return TRUE;
}

void CosApp::CEF_Inits2()
{
	CefSettings settings;
	CefSettingsTraits::init(&settings);
	settings.multi_threaded_message_loop = true;

	CefString(&settings.locale).FromWString(L"UTF-8");

	CefString(&settings.cache_path).FromString("./cachepath/");

	CefMainArgs mainArgs;
	CefRefPtr<CefApp> cefApp;
	CefInitialize(mainArgs, settings, cefApp, NULL);
}

//Then call it in the app, such as CEF_Inits();
//Add in OnInitDialog
    CRect rc;
	GetClientRect(rc);
	RECT rectnew = rc;
	rectnew.DeflateRect(10, 10, 10, 10);

	CefWindowInfo winInfo;
	winInfo.SetAsChild(m_hWnd, rectnew);

	CefRefPtr<CSimpleClient> client(new CSimpleClient());
	m_webClient = client;

	CefBrowserSettings browserSettings;
	CefBrowserHost::CreateBrowser(winInfo, client, _T("www.baidu.com"), browserSettings, NULL, NULL);

//Add in the dialog box OnSize()
    if (m_webClient)
	{
		CRect rc;
		GetClientRect(rc);
		rc.DeflateRect(10, 10, 10, 10);
		CefRefPtr<CefBrowser> pp = m_webClient->GetBrowser();
		HWND hw = pp->GetHost()->GetWindowHandle();
		::MoveWindow(hw, rc.left, rc.top, rc.Width(), rc.Height(), FALSE);
	}

5. Compile and generate the project. After success, copy these contents successively in your project debug/release directory and directly from cel Find it in the debug/release of SLN

Without these files, your program crashes as soon as it runs. After copying these files in turn, run your project and successfully see the browser~~~~

 

6. The last pit: no matter in release or debug mode, as long as you exit the program, it will crash directly. This can't be adjusted. I haven't been able to adjust it for a long time. I think I can directly generate a process and kill it before exiting....

 

Now I feel that the so-called open source portability and part-time jobs cannot be used ~ ~ ~ ~. The so-called portability took n more time to pile up. It's better not to transplant. For specific products, it's better to transfer funds for special purpose ~!

Keywords: C++ Web Development MFC

Added by chodges on Sun, 30 Jan 2022 01:15:18 +0200