cef3+duilb 多次弹窗问题

更多精彩内容:http://www.codeye.top
问题描述:

使用cef3+duilib ,浏览器启动后,正常加载页面。等待几秒钟后,又会弹出一个空白页窗口。

解决方法:

经过调试发现,问题出现在cef初始化。具体代码如下:


void CWebClient::CreateBrowser(HWND hParentWnd, const RECT& rect)
{
	
	CEF_REQUIRE_UI_THREAD();
	CefMainArgs main_args(::GetModuleHandle(NULL));
	CefRefPtr spApp(new CWebApp); 
	int exit_code = CefExecuteProcess(main_args, spApp.get(),NULL);
	if (exit_code>0)
	{
		return;
	}

	/**  主要是这里出问题了
	// 进行初始化的相关操作
	CefSettings cSettings;  
	CefSettingsTraits::init( &cSettings);  
	cSettings.single_process = false;
	cSettings.ignore_certificate_errors = true;
	cSettings.log_severity = LOGSEVERITY_ERROR;
	cSettings.multi_threaded_message_loop = true;  // 使用duilib的消息循环进行处理消息
	cSettings.no_sandbox = true;
	cSettings.locale.str = L"zh-CN";
	CefInitialize(main_args, cSettings, spApp.get(), NULL);
   // 初始化相关操作结束
   **/

	CefWindowInfo info; 
	m_hParWnd = hParentWnd;
	info.SetAsChild( hParentWnd, rect);
	CefBrowserSettings browserSettings; 
	char* code = "UTF-8";
	cef_string_t encode={};
	cef_string_utf8_to_utf16(code,strlen(code),&encode);
	browserSettings.default_encoding=encode;
	browserSettings.background_color=CefColorSetARGB(0,0,0,1);
	CefRefPtr command_line = CefCommandLine::GetGlobalCommandLine();
	CefBrowserHost::CreateBrowser(info,this, m_strHomePage,browserSettings, NULL);
}

解决方法:

把初始化的相关内容放到main函数里,至于为什么?????请留言

修改如下


void CWebClient::InitCef( HINSTANCE hInst )
{
	CefMainArgs main_args(::GetModuleHandle(NULL));
	CefRefPtr spApp(new CWebApp); 
	int exit_code = CefExecuteProcess(main_args, spApp.get(),NULL);
	if (exit_code>0)
	{
		return;
	}
	CefSettings cSettings;  
	CefSettingsTraits::init( &cSettings);  
	cSettings.single_process = false;
	cSettings.ignore_certificate_errors = true;
	cSettings.log_severity = LOGSEVERITY_ERROR;
	cSettings.multi_threaded_message_loop = true; 
	cSettings.no_sandbox = true;
	cSettings.locale.str = L"zh-CN";
	CefInitialize(main_args, cSettings, spApp.get(), NULL);
}


// main 里的内容

int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{

	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);
	::CoInitialize(NULL);

	// 初始化cef ----------
	CWebClient::GetInstance()->InitCef(hInstance);
    // -----------------------

	CDuiFrameWnd *pFrame = CDuiFrameWnd::MainWnd();

	CPaintManagerUI::SetInstance(hInstance);
	CPaintManagerUI::SetCurrentPath(CPaintManagerUI::GetInstancePath());
	CPaintManagerUI::SetResourcePath(L"Skin");
	wstring strCmd = L"{\"title\":\"browser\",\"url\":\"http://127.0.0.1:9000/user/login\"}";
	//pFrame->SetParams(strCmd);
	pFrame->SetParams(wstring(strCmd));
	pFrame->Create(NULL, _T("BROWSER"), UI_WNDSTYLE_FRAME, WS_EX_WINDOWEDGE);
	pFrame->CenterWindow();
	pFrame->ShowWindow();
	CPaintManagerUI::MessageLoop();
	// Shut down CEF.
	
	return 0;
}

就这样吧,问题解决了就行。。如果不修改,在windows10 下偶尔是正常的,在win7下必现。

你可能感兴趣的:(duilib,cef3)