创建好工程后我们需要连接下面这两个静态库:
\cef_binary\Debug\Lib\libcef_dll_wrapper.lib
\cef_binary\lib\Debug\libcef.lib。
第2步:
创建全局变量:
CefRefPtr<CefApp> spApp;
在MFC程序实例的InitInstance()中添加以下初始化函数:
CefSettings cSettings; CefSettingsTraits::init( &cSettings); cSettings.multi_threaded_message_loop = true; CefString(&cSettings.cache_path) =CefString("缓存路径");//设置缓存路径 CefInitialize( cSettings, spApp);
第3步:
接下来来用类向导添加WM_CREATE消息,则会产生OnCreate函数。接着添加以下代理
// CWebView message handlers int CWebView::OnCreate( LPCREATESTRUCT lpCreateStruct ) { if ( CView::OnCreate(lpCreateStruct) == -1) return -1; CefRefPtr<CWebClient> client(new CWebClient()); m_cWebClient = client; CefWindowInfo info; info.SetAsChild( m_hWnd, CRect(0, 0, 800, 600)); CefBrowserSettings browserSettings; CefBrowser::CreateBrowser( info, static_cast<CefRefPtr<CefClient> >(client), "http://192.168.1.21:8080/dialysis/web/page/nav/content.jsp", browserSettings); return 0; }
调整大小:
void CWebView::OnSize( UINT nType, int cx, int cy ) { CView::OnSize(nType, cx, cy); if(m_cWebClient.get()) { CefRefPtr<CefBrowser> browser = m_cWebClient->GetBrowser(); if(browser) { CefWindowHandle hwnd = browser->GetWindowHandle(); RECT rect; this->GetClientRect(&rect); // ::SetWindowPos(hwnd, HWND_TOP, 0, 0, cx, cy, SWP_NOZORDER); ::MoveWindow( hwnd, 0, 0, cx, cy, true); } } }
建议再开一个定时器,定时去检查大小是否调整了,因为在实际应用中Onsize调用时brower不一定创建了。
第4步:
编译程序并把locales文件夹考到生成的程序目录中。