您的位置:首页 > 其它

MFC嵌入浏览器框架CEF

2016-05-23 19:51 357 查看
MFC嵌入浏览器框架CEF

flyfish

环境 win7,VC2013 update5

APP头文件

BOOL m_bCEFInitialized;//是否初始化
CefRefPtr<ClientApp> m_cefApp;


APP实现文件

APP构造函数

CXApp::CXApp()
{
m_bCEFInitialized = FALSE;
}


APP初始化函数

BOOL CXApp::InitInstance()
{

m_cefApp = new ClientApp();

// get arguments
CefMainArgs main_args(GetModuleHandle(NULL));

// Execute the secondary process, if any.
int exit_code = CefExecuteProcess(main_args, m_cefApp.get(), NULL);
if (exit_code >= 0)
return exit_code;

CefSettings settings;
settings.no_sandbox = TRUE;

//CefString(&settings.cache_path) = szCEFCache;

void* sandbox_info = NULL;
#if CEF_ENABLE_SANDBOX
// Manage the life span of the sandbox information object. This is necessary
// for sandbox support on Windows. See cef_sandbox_win.h for complete details.
CefScopedSandboxInfo scoped_sandbox;
sandbox_info = scoped_sandbox.sandbox_info();
#endif

//CEF Initiaized
m_bCEFInitialized = CefInitialize(main_args, settings, m_cefApp.get(), sandbox_info);
}


退出函数

int CXApp::ExitInstance()
{
// shutdown CEF
if (m_bCEFInitialized)
{
// closing stop work loop
m_bCEFInitialized = FALSE;
// release CEF app
m_cefApp = NULL;
// shutdown CEF
CefShutdown();
}

return CWinApp::ExitInstance();
}


重写函数

BOOL CXApp::PumpMessage()
{
// TODO:  在此添加专用代码和/或调用基类
if (m_bCEFInitialized)
CefDoMessageLoopWork();
return CPUMPKINPWinApp::PumpMessage();
}


对话框初始化函数

BOOL CXDlg::OnInitDialog()
{
CefWindowInfo window_info;
CRect rt;
GetWindowRect(&rt);
window_info.SetAsChild(this->GetSafeHwnd(), rt);
CefRefPtr<ClientHandler> handler(new ClientHandler());
m_cefHandler = handler;
CefBrowserSettings browser_settings;
std::string url;
url = "http://www.baidu.com/";
CefBrowserHost::CreateBrowser(window_info, handler.get(), url, browser_settings, NULL);
}

void CXDlg::OnSize(UINT nType, int cx, int cy)
{
CWnd* cefwindow = FindWindowEx(this->GetSafeHwnd(), NULL, L"CefBrowserWindow", NULL);
if (cefwindow)
{
cefwindow->MoveWindow(0, 0, cx, cy);
}
}


//加载其他网页时调用

CefBrowser* pBrowser = m_cefHandler->m_pBrowser;

CefRefPtr<CefFrame> pMainFram = pBrowser->GetMainFrame();
if (pMainFram)
{
std::string s = "www.baidu.com";
pMainFram->LoadURL(s.c_str());
}


下载地址
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: