chrome embedded framework: dialog based MFC browser

本文指导如何在MFC应用中集成CEF框架,实现浏览器功能,包括资源配置、初始化、消息处理及页面加载,以及如何在浏览器消息处理中加入用户自定义代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

google chrome 浏览器可以快速显示一些比较简单的界面, 对HTML5支持也很好。下面我提供如何在MFC中嵌入连览器的步骤。以及如何实现浏览器与MFC程序的交互。


1。下载cef (chrome embedded framework). 我使用的为:cef_binary_3.1750.1738_windows32

该包包含源程序,库,dll以及头文件。dll_wrapper需要重新编译。其中的resources以及下面的locales子目录是程序运行需要的资源,必须在配置时正确指定。

在系统环境变量里设置CEF_PATH c:\cef_binary_3.1750.1738_windows32\


2. 使用MSVC2010或者更高版本


3。新建一个dialog based MFC project. For example touchscr

4. add libcef.lib and libcef_dll_wrapper.lib into project (这两个lib都在下载包里可以找到)。

5.设置include路径包含 $CEF_PATH

6。建立client_app.h/cpp,client_handler.h/cpp,client_switches.h/cpp, stdafx.h/cpp

Client_app负责浏览器应用程序:生成浏览器以及消息处理(client_handler)

client_handler负责浏览器消息处理,这一部分是MFC和浏览器交互的地方。用户可以修改定制以适应自己程序需求。

读者可以参考cef包里面的cef_client程序。

7。这时候可以编译连接成功。注意使用MTD选项以及MFC as static library才能通过。

8。加入Client_app成员到MFC app.

public:
    BOOL m_bCEFInitialized;
    CefRefPtr<ClientApp> m_cefApp;

    CString m_szHomePage;
    virtual int ExitInstance();
    virtual BOOL PumpMessage();

InitInstance and PumpMessage 为overrided function

9.在InitInstance加入初始化client_app的代码:

    // initialize CEF.
    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;

    // setup settings
    CString szCEFCache;
    CString szPath;
    INT nLen = GetTempPath( 0, NULL ) + 1;
    GetTempPath( nLen, szPath.GetBuffer( nLen ));

    // save path
    szCEFCache.Format( _T("%scache\0\0"), szPath );
    

    CefSettings settings;
    //settings.no_sandbox = TRUE;
    //settings.multi_threaded_message_loop = FALSE;

    CefString(&settings.cache_path) = szCEFCache;
    //also need set the locale & resource path!
    CefString(&settings.resources_dir_path)="C:\\cef_binary_3.1750.1738_windows32\\Resources";
    CefString(&settings.locales_dir_path)="C:\\cef_binary_3.1750.1738_windows32\\Resources\\locales";


    void* sandbox_info = NULL;

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

重载函数:

int CtouchscrApp::ExitInstance()
{
    // TODO: Add your specialized code here and/or call the base class
    if( m_bCEFInitialized )
    {
        // closing stop work loop
        m_bCEFInitialized = FALSE;
        // release CEF app
        m_cefApp = NULL;
        // shutdown CEF
        CefShutdown();
    }
    return CWinApp::ExitInstance();
}


BOOL CtouchscrApp::PumpMessage()
{
    // TODO: Add your specialized code here and/or call the base class
    if( m_bCEFInitialized )
        CefDoMessageLoopWork();

    return CWinApp::PumpMessage();
}

10。在OnInitDialog里面加入启动浏览器代码:

    CRect rect;
    GetClientRect( rect );

    if( !theApp.m_cefApp->CreateBrowser(GetSafeHwnd(), rect, theApp.m_szHomePage ))
    {
        AfxMessageBox(_T("Failed to create CEF Browser Window. Applications is exiting"));
        return FALSE;
    }
    
    AfxGetApp()->PumpMessage();
浏览器即可自动载入指定的页面。


在浏览器消息处理里面加入用户代码,可以方便实现各种功能,比如按键处理,或者传送到远程服务器处理。

这个例子显示本地一个页面,实现页面按键响应用户特定的任务。






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值