准备工作:
把CEF3项目里的cefclient全部拷到自己的工程中去,并修改编译。
全局变量:
CefRefPtr<ClientApp> CefApp=NULL; HWND hMessageWnd;
重写MFC启动代码
extern "C" int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
//添加以下代码,开始
CefMainArgs main_args(hInstance);
CefApp = new ClientApp();
hMessageWnd = CreateMessageWindow(hInstance);
// Execute the secondary process, if any.
int exit_code = CefExecuteProcess(main_args, CefApp.get(), NULL);
if (exit_code >= 0)
return exit_code;
// Parse command line arguments. The passed in values are ignored on Windows.
// AppInitCommandLine(0, NULL);
CefSettings settings;
settings.multi_threaded_message_loop=true;
//settings
// Populate the settings based on command line arguments.
// AppGetSettings(settings);
// Initialize CEF.
if(!CefInitialize(main_args, settings, CefApp.get(), NULL))
{
ASSERT(0);
}
//结束
CWinThread *pThread = AfxGetThread();//获取主线程指针
CWinApp *pApp = AfxGetApp();
AfxWinInit();
....
pApp->InitApplication();
...
pThread->InitInstance();//初始化应用程序实例
...
nReturnCode = pThread->Run();//开始消息循环
CefShutdown();
}
WTL在_tWinMain中添加
//添加以下代码,开始
CefMainArgs main_args;//(hInstance);
CefApp = new ClientApp();
// Execute the secondary process, if any.
int exit_code = CefExecuteProcess(main_args, CefApp.get(), NULL);
if (exit_code >= 0)
return exit_code;
// Parse command line arguments. The passed in values are ignored on Windows.
// AppInitCommandLine(0, NULL);
CefSettings settings;
settings.multi_threaded_message_loop=true;
//settings
// Populate the settings based on command line arguments.
// AppGetSettings(settings);
// Initialize CEF.
if(!CefInitialize(main_args, settings, CefApp.get(), NULL))
{
ASSERT(0);
}
//结束
int nRet = Run(lpstrCmdLine, nCmdShow);
_Module.Term();
::CoUninitialize();
CefShutdown();
return nRet;
在_tWinMain 函数之前添加以下代码
LRESULT CALLBACK MessageWndProc(HWND hWnd, UINT message, WPARAM wParam,
LPARAM lParam);
HWND CreateMessageWindow(HINSTANCE hInstance) {
static const wchar_t kWndClass[] = L"ClientMessageWindow";
WNDCLASSEX wc = {0};
wc.cbSize = sizeof(wc);
wc.lpfnWndProc = MessageWndProc;
wc.hInstance = hInstance;
wc.lpszClassName = kWndClass;
RegisterClassEx(&wc);
return CreateWindow(kWndClass, 0, 0, 0, 0, 0, 0, HWND_MESSAGE, 0,
hInstance, 0);
}
LRESULT CALLBACK MessageWndProc(HWND hWnd, UINT message, WPARAM wParam,
LPARAM lParam) {
switch (message) {
case WM_COMMAND: {
int wmId = LOWORD(wParam);
switch (wmId) {
case 32500:
PostQuitMessage(0);
return 0;
}
}
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
在MFC或WTL中void CMainFrame::OnClose()添加以下代码
::DestroyWindow(hMessageWnd);